gd_mono_utils.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "gd_mono_header.h"
  36. #ifdef JAVASCRIPT_ENABLED
  37. #include "gd_mono_wasm_m2n.h"
  38. #endif
  39. #include "core/object/class_db.h"
  40. #include "core/object/reference.h"
  41. #define UNHANDLED_EXCEPTION(m_exc) \
  42. if (unlikely(m_exc != nullptr)) { \
  43. GDMonoUtils::debug_unhandled_exception(m_exc); \
  44. GD_UNREACHABLE(); \
  45. } else \
  46. ((void)0)
  47. namespace GDMonoUtils {
  48. namespace Marshal {
  49. bool type_is_generic_array(MonoReflectionType *p_reftype);
  50. bool type_is_generic_dictionary(MonoReflectionType *p_reftype);
  51. bool type_is_system_generic_list(MonoReflectionType *p_reftype);
  52. bool type_is_system_generic_dictionary(MonoReflectionType *p_reftype);
  53. bool type_is_generic_ienumerable(MonoReflectionType *p_reftype);
  54. bool type_is_generic_icollection(MonoReflectionType *p_reftype);
  55. bool type_is_generic_idictionary(MonoReflectionType *p_reftype);
  56. void array_get_element_type(MonoReflectionType *p_array_reftype, MonoReflectionType **r_elem_reftype);
  57. void dictionary_get_key_value_types(MonoReflectionType *p_dict_reftype, MonoReflectionType **r_key_reftype, MonoReflectionType **r_value_reftype);
  58. GDMonoClass *make_generic_array_type(MonoReflectionType *p_elem_reftype);
  59. GDMonoClass *make_generic_dictionary_type(MonoReflectionType *p_key_reftype, MonoReflectionType *p_value_reftype);
  60. } // namespace Marshal
  61. _FORCE_INLINE_ void hash_combine(uint32_t &p_hash, const uint32_t &p_with_hash) {
  62. p_hash ^= p_with_hash + 0x9e3779b9 + (p_hash << 6) + (p_hash >> 2);
  63. }
  64. /**
  65. * If the object has a csharp script, returns the target of the gchandle stored in the script instance
  66. * Otherwise returns a newly constructed MonoObject* which is attached to the object
  67. * Returns nullptr on error
  68. */
  69. MonoObject *unmanaged_get_managed(Object *unmanaged);
  70. void set_main_thread(MonoThread *p_thread);
  71. MonoThread *attach_current_thread();
  72. void detach_current_thread();
  73. void detach_current_thread(MonoThread *p_mono_thread);
  74. MonoThread *get_current_thread();
  75. bool is_thread_attached();
  76. uint32_t new_strong_gchandle(MonoObject *p_object);
  77. uint32_t new_strong_gchandle_pinned(MonoObject *p_object);
  78. uint32_t new_weak_gchandle(MonoObject *p_object);
  79. void free_gchandle(uint32_t p_gchandle);
  80. void runtime_object_init(MonoObject *p_this_obj, GDMonoClass *p_class, MonoException **r_exc = nullptr);
  81. bool mono_delegate_equal(MonoDelegate *p_a, MonoDelegate *p_b);
  82. GDMonoClass *get_object_class(MonoObject *p_object);
  83. GDMonoClass *type_get_proxy_class(const StringName &p_type);
  84. GDMonoClass *get_class_native_base(GDMonoClass *p_class);
  85. MonoObject *create_managed_for_godot_object(GDMonoClass *p_class, const StringName &p_native, Object *p_object);
  86. MonoObject *create_managed_from(const StringName &p_from);
  87. MonoObject *create_managed_from(const NodePath &p_from);
  88. MonoObject *create_managed_from(const RID &p_from);
  89. MonoObject *create_managed_from(const Array &p_from, GDMonoClass *p_class);
  90. MonoObject *create_managed_from(const Dictionary &p_from, GDMonoClass *p_class);
  91. MonoDomain *create_domain(const String &p_friendly_name);
  92. String get_type_desc(MonoType *p_type);
  93. String get_type_desc(MonoReflectionType *p_reftype);
  94. String get_exception_name_and_message(MonoException *p_exc);
  95. void debug_print_unhandled_exception(MonoException *p_exc);
  96. void debug_send_unhandled_exception_error(MonoException *p_exc);
  97. void debug_unhandled_exception(MonoException *p_exc);
  98. void print_unhandled_exception(MonoException *p_exc);
  99. /**
  100. * Sets the exception as pending. The exception will be thrown when returning to managed code.
  101. * If no managed method is being invoked by the runtime, the exception will be treated as
  102. * an unhandled exception and the method will not return.
  103. */
  104. void set_pending_exception(MonoException *p_exc);
  105. extern thread_local int current_invoke_count;
  106. _FORCE_INLINE_ int get_runtime_invoke_count() {
  107. return current_invoke_count;
  108. }
  109. _FORCE_INLINE_ int &get_runtime_invoke_count_ref() {
  110. return current_invoke_count;
  111. }
  112. MonoObject *runtime_invoke(MonoMethod *p_method, void *p_obj, void **p_params, MonoException **r_exc);
  113. MonoString *object_to_string(MonoObject *p_obj, MonoException **r_exc);
  114. void property_set_value(MonoProperty *p_prop, void *p_obj, void **p_params, MonoException **r_exc);
  115. MonoObject *property_get_value(MonoProperty *p_prop, void *p_obj, void **p_params, MonoException **r_exc);
  116. uint64_t unbox_enum_value(MonoObject *p_boxed, MonoType *p_enum_basetype, bool &r_error);
  117. void dispose(MonoObject *p_mono_object, MonoException **r_exc);
  118. struct ScopeThreadAttach {
  119. ScopeThreadAttach();
  120. ~ScopeThreadAttach();
  121. private:
  122. MonoThread *mono_thread = nullptr;
  123. };
  124. StringName get_native_godot_class_name(GDMonoClass *p_class);
  125. template <typename... P>
  126. void add_internal_call(const char *p_name, void (*p_func)(P...)) {
  127. #ifdef JAVASCRIPT_ENABLED
  128. GDMonoWasmM2n::ICallTrampolines<P...>::add();
  129. #endif
  130. mono_add_internal_call(p_name, (void *)p_func);
  131. }
  132. template <typename R, typename... P>
  133. void add_internal_call(const char *p_name, R (*p_func)(P...)) {
  134. #ifdef JAVASCRIPT_ENABLED
  135. GDMonoWasmM2n::ICallTrampolinesR<R, P...>::add();
  136. #endif
  137. mono_add_internal_call(p_name, (void *)p_func);
  138. }
  139. } // namespace GDMonoUtils
  140. #define NATIVE_GDMONOCLASS_NAME(m_class) (GDMonoUtils::get_native_godot_class_name(m_class))
  141. #define GD_MONO_BEGIN_RUNTIME_INVOKE \
  142. int &_runtime_invoke_count_ref = GDMonoUtils::get_runtime_invoke_count_ref(); \
  143. _runtime_invoke_count_ref += 1; \
  144. ((void)0)
  145. #define GD_MONO_END_RUNTIME_INVOKE \
  146. _runtime_invoke_count_ref -= 1; \
  147. ((void)0)
  148. #define GD_MONO_SCOPE_THREAD_ATTACH \
  149. GDMonoUtils::ScopeThreadAttach __gdmono__scope__thread__attach__; \
  150. (void)__gdmono__scope__thread__attach__; \
  151. ((void)0)
  152. #ifdef DEBUG_ENABLED
  153. #define GD_MONO_ASSERT_THREAD_ATTACHED \
  154. CRASH_COND(!GDMonoUtils::is_thread_attached()); \
  155. ((void)0)
  156. #else
  157. #define GD_MONO_ASSERT_THREAD_ATTACHED ((void)0)
  158. #endif
  159. #endif // GD_MONOUTILS_H