base_object_glue.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*************************************************************************/
  2. /* base_object_glue.cpp */
  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. #include "base_object_glue.h"
  31. #ifdef MONO_GLUE_ENABLED
  32. #include "core/reference.h"
  33. #include "core/string_name.h"
  34. #include "../csharp_script.h"
  35. #include "../mono_gd/gd_mono_internals.h"
  36. #include "../mono_gd/gd_mono_utils.h"
  37. #include "../signal_awaiter_utils.h"
  38. Object *godot_icall_Object_Ctor(MonoObject *p_obj) {
  39. Object *instance = memnew(Object);
  40. GDMonoInternals::tie_managed_to_unmanaged(p_obj, instance);
  41. return instance;
  42. }
  43. void godot_icall_Object_Disposed(MonoObject *p_obj, Object *p_ptr) {
  44. #ifdef DEBUG_ENABLED
  45. CRASH_COND(p_ptr == NULL);
  46. #endif
  47. if (p_ptr->get_script_instance()) {
  48. CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(p_ptr->get_script_instance());
  49. if (cs_instance) {
  50. if (!cs_instance->is_destructing_script_instance()) {
  51. cs_instance->mono_object_disposed(p_obj);
  52. p_ptr->set_script_instance(NULL);
  53. }
  54. return;
  55. }
  56. }
  57. void *data = p_ptr->get_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index());
  58. if (data) {
  59. CSharpScriptBinding &script_binding = ((Map<Object *, CSharpScriptBinding>::Element *)data)->get();
  60. if (script_binding.inited) {
  61. Ref<MonoGCHandle> &gchandle = script_binding.gchandle;
  62. if (gchandle.is_valid()) {
  63. CSharpLanguage::release_script_gchandle(p_obj, gchandle);
  64. }
  65. }
  66. }
  67. }
  68. void godot_icall_Reference_Disposed(MonoObject *p_obj, Object *p_ptr, bool p_is_finalizer) {
  69. #ifdef DEBUG_ENABLED
  70. CRASH_COND(p_ptr == NULL);
  71. // This is only called with Reference derived classes
  72. CRASH_COND(!Object::cast_to<Reference>(p_ptr));
  73. #endif
  74. Reference *ref = static_cast<Reference *>(p_ptr);
  75. if (ref->get_script_instance()) {
  76. CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(ref->get_script_instance());
  77. if (cs_instance) {
  78. if (!cs_instance->is_destructing_script_instance()) {
  79. bool delete_owner;
  80. bool remove_script_instance;
  81. cs_instance->mono_object_disposed_baseref(p_obj, p_is_finalizer, delete_owner, remove_script_instance);
  82. if (delete_owner) {
  83. memdelete(ref);
  84. } else if (remove_script_instance) {
  85. ref->set_script_instance(NULL);
  86. }
  87. }
  88. return;
  89. }
  90. }
  91. // Unsafe refcount decrement. The managed instance also counts as a reference.
  92. // See: CSharpLanguage::alloc_instance_binding_data(Object *p_object)
  93. if (ref->unreference()) {
  94. memdelete(ref);
  95. } else {
  96. void *data = ref->get_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index());
  97. if (data) {
  98. CSharpScriptBinding &script_binding = ((Map<Object *, CSharpScriptBinding>::Element *)data)->get();
  99. if (script_binding.inited) {
  100. Ref<MonoGCHandle> &gchandle = script_binding.gchandle;
  101. if (gchandle.is_valid()) {
  102. CSharpLanguage::release_script_gchandle(p_obj, gchandle);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. MethodBind *godot_icall_Object_ClassDB_get_method(MonoString *p_type, MonoString *p_method) {
  109. StringName type(GDMonoMarshal::mono_string_to_godot(p_type));
  110. StringName method(GDMonoMarshal::mono_string_to_godot(p_method));
  111. return ClassDB::get_method(type, method);
  112. }
  113. MonoObject *godot_icall_Object_weakref(Object *p_obj) {
  114. if (!p_obj)
  115. return NULL;
  116. Ref<WeakRef> wref;
  117. Reference *ref = Object::cast_to<Reference>(p_obj);
  118. if (ref) {
  119. REF r = ref;
  120. if (!r.is_valid())
  121. return NULL;
  122. wref.instance();
  123. wref->set_ref(r);
  124. } else {
  125. wref.instance();
  126. wref->set_obj(p_obj);
  127. }
  128. return GDMonoUtils::unmanaged_get_managed(wref.ptr());
  129. }
  130. Error godot_icall_SignalAwaiter_connect(Object *p_source, MonoString *p_signal, Object *p_target, MonoObject *p_awaiter) {
  131. String signal = GDMonoMarshal::mono_string_to_godot(p_signal);
  132. return SignalAwaiterUtils::connect_signal_awaiter(p_source, signal, p_target, p_awaiter);
  133. }
  134. void godot_register_object_icalls() {
  135. mono_add_internal_call("Godot.Object::godot_icall_Object_Ctor", (void *)godot_icall_Object_Ctor);
  136. mono_add_internal_call("Godot.Object::godot_icall_Object_Disposed", (void *)godot_icall_Object_Disposed);
  137. mono_add_internal_call("Godot.Object::godot_icall_Reference_Disposed", (void *)godot_icall_Reference_Disposed);
  138. mono_add_internal_call("Godot.Object::godot_icall_Object_ClassDB_get_method", (void *)godot_icall_Object_ClassDB_get_method);
  139. mono_add_internal_call("Godot.Object::godot_icall_Object_weakref", (void *)godot_icall_Object_weakref);
  140. mono_add_internal_call("Godot.SignalAwaiter::godot_icall_SignalAwaiter_connect", (void *)godot_icall_SignalAwaiter_connect);
  141. }
  142. #endif // MONO_GLUE_ENABLED