gd_mono_internals.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*************************************************************************/
  2. /* gd_mono_internals.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "gd_mono_internals.h"
  31. #include "../csharp_script.h"
  32. #include "../mono_gc_handle.h"
  33. #include "../utils/macros.h"
  34. #include "../utils/thread_local.h"
  35. #include "gd_mono_class.h"
  36. #include "gd_mono_marshal.h"
  37. #include "gd_mono_utils.h"
  38. #include <mono/metadata/exception.h>
  39. namespace GDMonoInternals {
  40. void tie_managed_to_unmanaged(MonoObject *managed, Object *unmanaged) {
  41. // This method should not fail
  42. CRASH_COND(!unmanaged);
  43. // All mono objects created from the managed world (e.g.: 'new Player()')
  44. // need to have a CSharpScript in order for their methods to be callable from the unmanaged side
  45. Reference *ref = Object::cast_to<Reference>(unmanaged);
  46. GDMonoClass *klass = GDMonoUtils::get_object_class(managed);
  47. CRASH_COND(!klass);
  48. GDMonoClass *native = GDMonoUtils::get_class_native_base(klass);
  49. CRASH_COND(native == NULL);
  50. if (native == klass) {
  51. // If it's just a wrapper Godot class and not a custom inheriting class, then attach a
  52. // script binding instead. One of the advantages of this is that if a script is attached
  53. // later and it's not a C# script, then the managed object won't have to be disposed.
  54. // Another reason for doing this is that this instance could outlive CSharpLanguage, which would
  55. // be problematic when using a script. See: https://github.com/godotengine/godot/issues/25621
  56. CSharpScriptBinding script_binding;
  57. script_binding.inited = true;
  58. script_binding.type_name = NATIVE_GDMONOCLASS_NAME(klass);
  59. script_binding.wrapper_class = klass;
  60. script_binding.gchandle = ref ? MonoGCHandle::create_weak(managed) : MonoGCHandle::create_strong(managed);
  61. script_binding.owner = unmanaged;
  62. if (ref) {
  63. // Unsafe refcount increment. The managed instance also counts as a reference.
  64. // This way if the unmanaged world has no references to our owner
  65. // but the managed instance is alive, the refcount will be 1 instead of 0.
  66. // See: godot_icall_Reference_Dtor(MonoObject *p_obj, Object *p_ptr)
  67. // May not me referenced yet, so we must use init_ref() instead of reference()
  68. if (ref->init_ref()) {
  69. CSharpLanguage::get_singleton()->post_unsafe_reference(ref);
  70. }
  71. }
  72. // The object was just created, no script instance binding should have been attached
  73. CRASH_COND(unmanaged->has_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index()));
  74. void *data = (void *)CSharpLanguage::get_singleton()->insert_script_binding(unmanaged, script_binding);
  75. // Should be thread safe because the object was just created and nothing else should be referencing it
  76. unmanaged->set_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index(), data);
  77. return;
  78. }
  79. Ref<MonoGCHandle> gchandle = ref ? MonoGCHandle::create_weak(managed) : MonoGCHandle::create_strong(managed);
  80. Ref<CSharpScript> script = CSharpScript::create_for_managed_type(klass, native);
  81. CRASH_COND(script.is_null());
  82. ScriptInstance *si = CSharpInstance::create_for_managed_type(unmanaged, script.ptr(), gchandle);
  83. unmanaged->set_script_and_instance(script.get_ref_ptr(), si);
  84. }
  85. void unhandled_exception(MonoException *p_exc) {
  86. mono_unhandled_exception((MonoObject *)p_exc); // prints the exception as well
  87. if (GDMono::get_singleton()->get_unhandled_exception_policy() == GDMono::POLICY_TERMINATE_APP) {
  88. // Too bad 'mono_invoke_unhandled_exception_hook' is not exposed to embedders
  89. GDMono::unhandled_exception_hook((MonoObject *)p_exc, NULL);
  90. GD_UNREACHABLE();
  91. } else {
  92. #ifdef DEBUG_ENABLED
  93. GDMonoUtils::debug_send_unhandled_exception_error((MonoException *)p_exc);
  94. if (ScriptDebugger::get_singleton())
  95. ScriptDebugger::get_singleton()->idle_poll();
  96. #endif
  97. }
  98. }
  99. } // namespace GDMonoInternals