gd_mono_utils.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*************************************************************************/
  2. /* gd_mono_utils.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GD_MONOUTILS_H
  31. #define GD_MONOUTILS_H
  32. #include <mono/metadata/threads.h>
  33. #include "../mono_gc_handle.h"
  34. #include "../utils/macros.h"
  35. #include "../utils/thread_local.h"
  36. #include "gd_mono_header.h"
  37. #include "core/object.h"
  38. #include "core/reference.h"
  39. #define UNLIKELY_UNHANDLED_EXCEPTION(m_exc) \
  40. if (unlikely(m_exc != NULL)) { \
  41. GDMonoUtils::debug_unhandled_exception(m_exc); \
  42. _UNREACHABLE_(); \
  43. }
  44. namespace GDMonoUtils {
  45. typedef void (*GodotObject_Dispose)(MonoObject *, MonoObject **);
  46. typedef Array *(*Array_GetPtr)(MonoObject *, MonoObject **);
  47. typedef Dictionary *(*Dictionary_GetPtr)(MonoObject *, MonoObject **);
  48. typedef MonoObject *(*SignalAwaiter_SignalCallback)(MonoObject *, MonoArray *, MonoObject **);
  49. typedef MonoObject *(*SignalAwaiter_FailureCallback)(MonoObject *, MonoObject **);
  50. typedef MonoObject *(*GodotTaskScheduler_Activate)(MonoObject *, MonoObject **);
  51. typedef MonoArray *(*StackTrace_GetFrames)(MonoObject *, MonoObject **);
  52. typedef MonoBoolean (*IsArrayGenericType)(MonoObject *, MonoObject **);
  53. typedef MonoBoolean (*IsDictionaryGenericType)(MonoObject *, MonoObject **);
  54. typedef MonoBoolean (*IsArrayGenericType)(MonoObject *, MonoObject **);
  55. typedef MonoBoolean (*IsDictionaryGenericType)(MonoObject *, MonoObject **);
  56. typedef void (*DebugUtils_StackFrameInfo)(MonoObject *, MonoString **, int *, MonoString **, MonoObject **);
  57. struct MonoCache {
  58. // -----------------------------------------------
  59. // corlib classes
  60. // Let's use the no-namespace format for these too
  61. GDMonoClass *class_MonoObject;
  62. GDMonoClass *class_bool;
  63. GDMonoClass *class_int8_t;
  64. GDMonoClass *class_int16_t;
  65. GDMonoClass *class_int32_t;
  66. GDMonoClass *class_int64_t;
  67. GDMonoClass *class_uint8_t;
  68. GDMonoClass *class_uint16_t;
  69. GDMonoClass *class_uint32_t;
  70. GDMonoClass *class_uint64_t;
  71. GDMonoClass *class_float;
  72. GDMonoClass *class_double;
  73. GDMonoClass *class_String;
  74. GDMonoClass *class_IntPtr;
  75. #ifdef DEBUG_ENABLED
  76. GDMonoClass *class_System_Diagnostics_StackTrace;
  77. StackTrace_GetFrames methodthunk_System_Diagnostics_StackTrace_GetFrames;
  78. GDMonoMethod *method_System_Diagnostics_StackTrace_ctor_bool;
  79. GDMonoMethod *method_System_Diagnostics_StackTrace_ctor_Exception_bool;
  80. #endif
  81. GDMonoClass *class_KeyNotFoundException;
  82. MonoClass *rawclass_Dictionary;
  83. // -----------------------------------------------
  84. GDMonoClass *class_Vector2;
  85. GDMonoClass *class_Rect2;
  86. GDMonoClass *class_Transform2D;
  87. GDMonoClass *class_Vector3;
  88. GDMonoClass *class_Basis;
  89. GDMonoClass *class_Quat;
  90. GDMonoClass *class_Transform;
  91. GDMonoClass *class_AABB;
  92. GDMonoClass *class_Color;
  93. GDMonoClass *class_Plane;
  94. GDMonoClass *class_NodePath;
  95. GDMonoClass *class_RID;
  96. GDMonoClass *class_GodotObject;
  97. GDMonoClass *class_GodotReference;
  98. GDMonoClass *class_Node;
  99. GDMonoClass *class_Control;
  100. GDMonoClass *class_Spatial;
  101. GDMonoClass *class_WeakRef;
  102. GDMonoClass *class_Array;
  103. GDMonoClass *class_Dictionary;
  104. GDMonoClass *class_MarshalUtils;
  105. #ifdef DEBUG_ENABLED
  106. GDMonoClass *class_DebuggingUtils;
  107. DebugUtils_StackFrameInfo methodthunk_DebuggingUtils_GetStackFrameInfo;
  108. #endif
  109. GDMonoClass *class_ExportAttribute;
  110. GDMonoField *field_ExportAttribute_hint;
  111. GDMonoField *field_ExportAttribute_hintString;
  112. GDMonoClass *class_SignalAttribute;
  113. GDMonoClass *class_ToolAttribute;
  114. GDMonoClass *class_RemoteAttribute;
  115. GDMonoClass *class_SyncAttribute;
  116. GDMonoClass *class_RemoteSyncAttribute;
  117. GDMonoClass *class_MasterSyncAttribute;
  118. GDMonoClass *class_PuppetSyncAttribute;
  119. GDMonoClass *class_MasterAttribute;
  120. GDMonoClass *class_PuppetAttribute;
  121. GDMonoClass *class_SlaveAttribute;
  122. GDMonoClass *class_GodotMethodAttribute;
  123. GDMonoField *field_GodotMethodAttribute_methodName;
  124. GDMonoField *field_GodotObject_ptr;
  125. GDMonoField *field_NodePath_ptr;
  126. GDMonoField *field_Image_ptr;
  127. GDMonoField *field_RID_ptr;
  128. GodotObject_Dispose methodthunk_GodotObject_Dispose;
  129. Array_GetPtr methodthunk_Array_GetPtr;
  130. Dictionary_GetPtr methodthunk_Dictionary_GetPtr;
  131. IsArrayGenericType methodthunk_MarshalUtils_IsArrayGenericType;
  132. IsDictionaryGenericType methodthunk_MarshalUtils_IsDictionaryGenericType;
  133. SignalAwaiter_SignalCallback methodthunk_SignalAwaiter_SignalCallback;
  134. SignalAwaiter_FailureCallback methodthunk_SignalAwaiter_FailureCallback;
  135. GodotTaskScheduler_Activate methodthunk_GodotTaskScheduler_Activate;
  136. Ref<MonoGCHandle> task_scheduler_handle;
  137. bool corlib_cache_updated;
  138. bool godot_api_cache_updated;
  139. void clear_members();
  140. void cleanup();
  141. MonoCache() {
  142. corlib_cache_updated = false;
  143. godot_api_cache_updated = false;
  144. clear_members();
  145. }
  146. };
  147. extern MonoCache mono_cache;
  148. void update_corlib_cache();
  149. void update_godot_api_cache();
  150. void clear_cache();
  151. _FORCE_INLINE_ void hash_combine(uint32_t &p_hash, const uint32_t &p_with_hash) {
  152. p_hash ^= p_with_hash + 0x9e3779b9 + (p_hash << 6) + (p_hash >> 2);
  153. }
  154. /**
  155. * If the object has a csharp script, returns the target of the gchandle stored in the script instance
  156. * Otherwise returns a newly constructed MonoObject* which is attached to the object
  157. * Returns NULL on error
  158. */
  159. MonoObject *unmanaged_get_managed(Object *unmanaged);
  160. void set_main_thread(MonoThread *p_thread);
  161. void attach_current_thread();
  162. void detach_current_thread();
  163. MonoThread *get_current_thread();
  164. _FORCE_INLINE_ bool is_main_thread() {
  165. return mono_domain_get() != NULL && mono_thread_get_main() == mono_thread_current();
  166. }
  167. void runtime_object_init(MonoObject *p_this_obj);
  168. GDMonoClass *get_object_class(MonoObject *p_object);
  169. GDMonoClass *type_get_proxy_class(const StringName &p_type);
  170. GDMonoClass *get_class_native_base(GDMonoClass *p_class);
  171. MonoObject *create_managed_for_godot_object(GDMonoClass *p_class, const StringName &p_native, Object *p_object);
  172. MonoObject *create_managed_from(const NodePath &p_from);
  173. MonoObject *create_managed_from(const RID &p_from);
  174. MonoObject *create_managed_from(const Array &p_from, GDMonoClass *p_class);
  175. MonoObject *create_managed_from(const Dictionary &p_from, GDMonoClass *p_class);
  176. MonoDomain *create_domain(const String &p_friendly_name);
  177. String get_exception_name_and_message(MonoException *p_exc);
  178. void set_exception_message(MonoException *p_exc, String message);
  179. void debug_print_unhandled_exception(MonoException *p_exc);
  180. void debug_send_unhandled_exception_error(MonoException *p_exc);
  181. _NO_RETURN_ void debug_unhandled_exception(MonoException *p_exc);
  182. void print_unhandled_exception(MonoException *p_exc);
  183. /**
  184. * Sets the exception as pending. The exception will be thrown when returning to managed code.
  185. * If no managed method is being invoked by the runtime, the exception will be treated as
  186. * an unhandled exception and the method will not return.
  187. */
  188. void set_pending_exception(MonoException *p_exc);
  189. extern _THREAD_LOCAL_(int) current_invoke_count;
  190. _FORCE_INLINE_ int get_runtime_invoke_count() {
  191. return current_invoke_count;
  192. }
  193. _FORCE_INLINE_ int &get_runtime_invoke_count_ref() {
  194. return current_invoke_count;
  195. }
  196. MonoObject *runtime_invoke(MonoMethod *p_method, void *p_obj, void **p_params, MonoException **r_exc);
  197. MonoObject *runtime_invoke_array(MonoMethod *p_method, void *p_obj, MonoArray *p_params, MonoException **r_exc);
  198. MonoString *object_to_string(MonoObject *p_obj, MonoException **r_exc);
  199. void property_set_value(MonoProperty *p_prop, void *p_obj, void **p_params, MonoException **r_exc);
  200. MonoObject *property_get_value(MonoProperty *p_prop, void *p_obj, void **p_params, MonoException **r_exc);
  201. uint64_t unbox_enum_value(MonoObject *p_boxed, MonoType *p_enum_basetype, bool &r_error);
  202. void dispose(MonoObject *p_mono_object, MonoException **r_exc);
  203. } // namespace GDMonoUtils
  204. #define NATIVE_GDMONOCLASS_NAME(m_class) (GDMonoMarshal::mono_string_to_godot((MonoString *)m_class->get_field(BINDINGS_NATIVE_NAME_FIELD)->get_value(NULL)))
  205. #define CACHED_CLASS(m_class) (GDMonoUtils::mono_cache.class_##m_class)
  206. #define CACHED_CLASS_RAW(m_class) (GDMonoUtils::mono_cache.class_##m_class->get_mono_ptr())
  207. #define CACHED_RAW_MONO_CLASS(m_class) (GDMonoUtils::mono_cache.rawclass_##m_class)
  208. #define CACHED_FIELD(m_class, m_field) (GDMonoUtils::mono_cache.field_##m_class##_##m_field)
  209. #define CACHED_METHOD(m_class, m_method) (GDMonoUtils::mono_cache.method_##m_class##_##m_method)
  210. #define CACHED_METHOD_THUNK(m_class, m_method) (GDMonoUtils::mono_cache.methodthunk_##m_class##_##m_method)
  211. #ifdef REAL_T_IS_DOUBLE
  212. #define REAL_T_MONOCLASS CACHED_CLASS_RAW(double)
  213. #else
  214. #define REAL_T_MONOCLASS CACHED_CLASS_RAW(float)
  215. #endif
  216. #define GD_MONO_BEGIN_RUNTIME_INVOKE \
  217. int &_runtime_invoke_count_ref = GDMonoUtils::get_runtime_invoke_count_ref(); \
  218. _runtime_invoke_count_ref += 1;
  219. #define GD_MONO_END_RUNTIME_INVOKE \
  220. _runtime_invoke_count_ref -= 1;
  221. inline void invoke_method_thunk(void (*p_method_thunk)()) {
  222. GD_MONO_BEGIN_RUNTIME_INVOKE;
  223. p_method_thunk();
  224. GD_MONO_END_RUNTIME_INVOKE;
  225. }
  226. template <class R>
  227. R invoke_method_thunk(R (*p_method_thunk)()) {
  228. GD_MONO_BEGIN_RUNTIME_INVOKE;
  229. R r = p_method_thunk();
  230. GD_MONO_END_RUNTIME_INVOKE;
  231. return r;
  232. }
  233. template <class P1>
  234. void invoke_method_thunk(void (*p_method_thunk)(P1), P1 p_arg1) {
  235. GD_MONO_BEGIN_RUNTIME_INVOKE;
  236. p_method_thunk(p_arg1);
  237. GD_MONO_END_RUNTIME_INVOKE;
  238. }
  239. template <class R, class P1>
  240. R invoke_method_thunk(R (*p_method_thunk)(P1), P1 p_arg1) {
  241. GD_MONO_BEGIN_RUNTIME_INVOKE;
  242. R r = p_method_thunk(p_arg1);
  243. GD_MONO_END_RUNTIME_INVOKE;
  244. return r;
  245. }
  246. template <class P1, class P2>
  247. void invoke_method_thunk(void (*p_method_thunk)(P1, P2), P1 p_arg1, P2 p_arg2) {
  248. GD_MONO_BEGIN_RUNTIME_INVOKE;
  249. p_method_thunk(p_arg1, p_arg2);
  250. GD_MONO_END_RUNTIME_INVOKE;
  251. }
  252. template <class R, class P1, class P2>
  253. R invoke_method_thunk(R (*p_method_thunk)(P1, P2), P1 p_arg1, P2 p_arg2) {
  254. GD_MONO_BEGIN_RUNTIME_INVOKE;
  255. R r = p_method_thunk(p_arg1, p_arg2);
  256. GD_MONO_END_RUNTIME_INVOKE;
  257. return r;
  258. }
  259. template <class P1, class P2, class P3>
  260. void invoke_method_thunk(void (*p_method_thunk)(P1, P2, P3), P1 p_arg1, P2 p_arg2, P3 p_arg3) {
  261. GD_MONO_BEGIN_RUNTIME_INVOKE;
  262. p_method_thunk(p_arg1, p_arg2, p_arg3);
  263. GD_MONO_END_RUNTIME_INVOKE;
  264. }
  265. template <class R, class P1, class P2, class P3>
  266. R invoke_method_thunk(R (*p_method_thunk)(P1, P2, P3), P1 p_arg1, P2 p_arg2, P3 p_arg3) {
  267. GD_MONO_BEGIN_RUNTIME_INVOKE;
  268. R r = p_method_thunk(p_arg1, p_arg2, p_arg3);
  269. GD_MONO_END_RUNTIME_INVOKE;
  270. return r;
  271. }
  272. template <class P1, class P2, class P3, class P4>
  273. void invoke_method_thunk(void (*p_method_thunk)(P1, P2, P3, P4), P1 p_arg1, P2 p_arg2, P3 p_arg3, P4 p_arg4) {
  274. GD_MONO_BEGIN_RUNTIME_INVOKE;
  275. p_method_thunk(p_arg1, p_arg2, p_arg3, p_arg4);
  276. GD_MONO_END_RUNTIME_INVOKE;
  277. }
  278. template <class R, class P1, class P2, class P3, class P4>
  279. R invoke_method_thunk(R (*p_method_thunk)(P1, P2, P3, P4), P1 p_arg1, P2 p_arg2, P3 p_arg3, P4 p_arg4) {
  280. GD_MONO_BEGIN_RUNTIME_INVOKE;
  281. R r = p_method_thunk(p_arg1, p_arg2, p_arg3, p_arg4);
  282. GD_MONO_END_RUNTIME_INVOKE;
  283. return r;
  284. }
  285. template <class P1, class P2, class P3, class P4, class P5>
  286. void invoke_method_thunk(void (*p_method_thunk)(P1, P2, P3, P4, P5), P1 p_arg1, P2 p_arg2, P3 p_arg3, P4 p_arg4, P5 p_arg5) {
  287. GD_MONO_BEGIN_RUNTIME_INVOKE;
  288. p_method_thunk(p_arg1, p_arg2, p_arg3, p_arg4, p_arg5);
  289. GD_MONO_END_RUNTIME_INVOKE;
  290. }
  291. template <class R, class P1, class P2, class P3, class P4, class P5>
  292. R invoke_method_thunk(R (*p_method_thunk)(P1, P2, P3, P4, P5), P1 p_arg1, P2 p_arg2, P3 p_arg3, P4 p_arg4, P5 p_arg5) {
  293. GD_MONO_BEGIN_RUNTIME_INVOKE;
  294. R r = p_method_thunk(p_arg1, p_arg2, p_arg3, p_arg4, p_arg5);
  295. GD_MONO_END_RUNTIME_INVOKE;
  296. return r;
  297. }
  298. #endif // GD_MONOUTILS_H