gd_mono_utils.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**************************************************************************/
  2. /* gd_mono_utils.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_MONO_UTILS_H
  31. #define GD_MONO_UTILS_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. #ifdef JAVASCRIPT_ENABLED
  38. #include "gd_mono_wasm_m2n.h"
  39. #endif
  40. #include "core/object.h"
  41. #include "core/reference.h"
  42. #define UNHANDLED_EXCEPTION(m_exc) \
  43. if (unlikely(m_exc != NULL)) { \
  44. GDMonoUtils::debug_unhandled_exception(m_exc); \
  45. GD_UNREACHABLE(); \
  46. }
  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. bool type_has_flags_attribute(MonoReflectionType *p_reftype);
  57. void get_generic_type_definition(MonoReflectionType *p_reftype, MonoReflectionType **r_generic_reftype);
  58. void array_get_element_type(MonoReflectionType *p_array_reftype, MonoReflectionType **r_elem_reftype);
  59. void dictionary_get_key_value_types(MonoReflectionType *p_dict_reftype, MonoReflectionType **r_key_reftype, MonoReflectionType **r_value_reftype);
  60. GDMonoClass *make_generic_array_type(MonoReflectionType *p_elem_reftype);
  61. GDMonoClass *make_generic_dictionary_type(MonoReflectionType *p_key_reftype, MonoReflectionType *p_value_reftype);
  62. } // namespace Marshal
  63. _FORCE_INLINE_ void hash_combine(uint32_t &p_hash, const uint32_t &p_with_hash) {
  64. p_hash ^= p_with_hash + 0x9e3779b9 + (p_hash << 6) + (p_hash >> 2);
  65. }
  66. /**
  67. * If the object has a csharp script, returns the target of the gchandle stored in the script instance
  68. * Otherwise returns a newly constructed MonoObject* which is attached to the object
  69. * Returns NULL on error
  70. */
  71. MonoObject *unmanaged_get_managed(Object *unmanaged);
  72. void set_main_thread(MonoThread *p_thread);
  73. MonoThread *attach_current_thread();
  74. void detach_current_thread();
  75. void detach_current_thread(MonoThread *p_mono_thread);
  76. MonoThread *get_current_thread();
  77. bool is_thread_attached();
  78. _FORCE_INLINE_ bool is_main_thread() {
  79. return mono_domain_get() != NULL && mono_thread_get_main() == mono_thread_current();
  80. }
  81. void runtime_object_init(MonoObject *p_this_obj, GDMonoClass *p_class, MonoException **r_exc = NULL);
  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 NodePath &p_from);
  87. MonoObject *create_managed_from(const RID &p_from);
  88. MonoObject *create_managed_from(const Array &p_from, GDMonoClass *p_class);
  89. MonoObject *create_managed_from(const Dictionary &p_from, GDMonoClass *p_class);
  90. MonoDomain *create_domain(const String &p_friendly_name);
  91. String get_type_desc(MonoType *p_type);
  92. String get_type_desc(MonoReflectionType *p_reftype);
  93. String get_exception_name_and_message(MonoException *p_exc);
  94. void set_exception_message(MonoException *p_exc, String message);
  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;
  123. };
  124. template <typename... P>
  125. void add_internal_call(const char *p_name, void (*p_func)(P...)) {
  126. #ifdef JAVASCRIPT_ENABLED
  127. GDMonoWasmM2n::ICallTrampolines<P...>::add();
  128. #endif
  129. mono_add_internal_call(p_name, (void *)p_func);
  130. }
  131. template <typename R, typename... P>
  132. void add_internal_call(const char *p_name, R (*p_func)(P...)) {
  133. #ifdef JAVASCRIPT_ENABLED
  134. GDMonoWasmM2n::ICallTrampolinesR<R, P...>::add();
  135. #endif
  136. mono_add_internal_call(p_name, (void *)p_func);
  137. }
  138. } // namespace GDMonoUtils
  139. #define NATIVE_GDMONOCLASS_NAME(m_class) (GDMonoMarshal::mono_string_to_godot((MonoString *)m_class->get_field(BINDINGS_NATIVE_NAME_FIELD)->get_value(NULL)))
  140. #define GD_MONO_BEGIN_RUNTIME_INVOKE \
  141. int &_runtime_invoke_count_ref = GDMonoUtils::get_runtime_invoke_count_ref(); \
  142. _runtime_invoke_count_ref += 1;
  143. #define GD_MONO_END_RUNTIME_INVOKE \
  144. _runtime_invoke_count_ref -= 1;
  145. #define GD_MONO_SCOPE_THREAD_ATTACH \
  146. GDMonoUtils::ScopeThreadAttach __gdmono__scope__thread__attach__; \
  147. (void)__gdmono__scope__thread__attach__;
  148. #ifdef DEBUG_ENABLED
  149. #define GD_MONO_ASSERT_THREAD_ATTACHED \
  150. { CRASH_COND(!GDMonoUtils::is_thread_attached()); }
  151. #else
  152. #define GD_MONO_ASSERT_THREAD_ATTACHED
  153. #endif
  154. #endif // GD_MONO_UTILS_H