base_object_glue.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_db.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. Ref<MonoGCHandle> &gchandle = ((Map<Object *, CSharpScriptBinding>::Element *)data)->get().gchandle;
  60. if (gchandle.is_valid()) {
  61. CSharpLanguage::release_script_gchandle(p_obj, gchandle);
  62. }
  63. }
  64. }
  65. void godot_icall_Reference_Disposed(MonoObject *p_obj, Object *p_ptr, bool p_is_finalizer) {
  66. #ifdef DEBUG_ENABLED
  67. CRASH_COND(p_ptr == NULL);
  68. // This is only called with Reference derived classes
  69. CRASH_COND(!Object::cast_to<Reference>(p_ptr));
  70. #endif
  71. Reference *ref = static_cast<Reference *>(p_ptr);
  72. if (ref->get_script_instance()) {
  73. CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(ref->get_script_instance());
  74. if (cs_instance) {
  75. if (!cs_instance->is_destructing_script_instance()) {
  76. bool r_owner_deleted;
  77. cs_instance->mono_object_disposed_baseref(p_obj, p_is_finalizer, r_owner_deleted);
  78. if (!r_owner_deleted && !p_is_finalizer) {
  79. // If the native instance is still alive and Dispose() was called
  80. // (instead of the finalizer), then we remove the script instance.
  81. ref->set_script_instance(NULL);
  82. }
  83. }
  84. return;
  85. }
  86. }
  87. // Unsafe refcount decrement. The managed instance also counts as a reference.
  88. // See: CSharpLanguage::alloc_instance_binding_data(Object *p_object)
  89. if (ref->unreference()) {
  90. memdelete(ref);
  91. } else {
  92. void *data = ref->get_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index());
  93. if (data) {
  94. Ref<MonoGCHandle> &gchandle = ((Map<Object *, CSharpScriptBinding>::Element *)data)->get().gchandle;
  95. if (gchandle.is_valid()) {
  96. CSharpLanguage::release_script_gchandle(p_obj, gchandle);
  97. }
  98. }
  99. }
  100. }
  101. MethodBind *godot_icall_Object_ClassDB_get_method(MonoString *p_type, MonoString *p_method) {
  102. StringName type(GDMonoMarshal::mono_string_to_godot(p_type));
  103. StringName method(GDMonoMarshal::mono_string_to_godot(p_method));
  104. return ClassDB::get_method(type, method);
  105. }
  106. MonoObject *godot_icall_Object_weakref(Object *p_obj) {
  107. if (!p_obj)
  108. return NULL;
  109. Ref<WeakRef> wref;
  110. Reference *ref = Object::cast_to<Reference>(p_obj);
  111. if (ref) {
  112. REF r = ref;
  113. if (!r.is_valid())
  114. return NULL;
  115. wref.instance();
  116. wref->set_ref(r);
  117. } else {
  118. wref.instance();
  119. wref->set_obj(p_obj);
  120. }
  121. return GDMonoUtils::create_managed_for_godot_object(CACHED_CLASS(WeakRef), Reference::get_class_static(), Object::cast_to<Object>(wref.ptr()));
  122. }
  123. Error godot_icall_SignalAwaiter_connect(Object *p_source, MonoString *p_signal, Object *p_target, MonoObject *p_awaiter) {
  124. String signal = GDMonoMarshal::mono_string_to_godot(p_signal);
  125. return SignalAwaiterUtils::connect_signal_awaiter(p_source, signal, p_target, p_awaiter);
  126. }
  127. void godot_register_object_icalls() {
  128. mono_add_internal_call("Godot.Object::godot_icall_Object_Ctor", (void *)godot_icall_Object_Ctor);
  129. mono_add_internal_call("Godot.Object::godot_icall_Object_Disposed", (void *)godot_icall_Object_Disposed);
  130. mono_add_internal_call("Godot.Object::godot_icall_Reference_Disposed", (void *)godot_icall_Reference_Disposed);
  131. mono_add_internal_call("Godot.Object::godot_icall_Object_ClassDB_get_method", (void *)godot_icall_Object_ClassDB_get_method);
  132. mono_add_internal_call("Godot.Object::godot_icall_Object_weakref", (void *)godot_icall_Object_weakref);
  133. mono_add_internal_call("Godot.SignalAwaiter::godot_icall_SignalAwaiter_connect", (void *)godot_icall_SignalAwaiter_connect);
  134. }
  135. #endif // MONO_GLUE_ENABLED