signal_awaiter_utils.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*************************************************************************/
  2. /* signal_awaiter_utils.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "signal_awaiter_utils.h"
  31. #include "csharp_script.h"
  32. #include "mono_gd/gd_mono_cache.h"
  33. #include "mono_gd/gd_mono_class.h"
  34. #include "mono_gd/gd_mono_marshal.h"
  35. #include "mono_gd/gd_mono_utils.h"
  36. namespace SignalAwaiterUtils {
  37. Error connect_signal_awaiter(Object *p_source, const String &p_signal, Object *p_target, MonoObject *p_awaiter) {
  38. ERR_FAIL_NULL_V(p_source, ERR_INVALID_DATA);
  39. ERR_FAIL_NULL_V(p_target, ERR_INVALID_DATA);
  40. Ref<SignalAwaiterHandle> sa_con = memnew(SignalAwaiterHandle(p_awaiter));
  41. #ifdef DEBUG_ENABLED
  42. sa_con->set_connection_target(p_target);
  43. #endif
  44. Vector<Variant> binds;
  45. binds.push_back(sa_con);
  46. Error err = p_source->connect(p_signal, sa_con.ptr(),
  47. CSharpLanguage::get_singleton()->get_string_names()._signal_callback,
  48. binds, Object::CONNECT_ONESHOT);
  49. if (err != OK) {
  50. // Set it as completed to prevent it from calling the failure callback when released.
  51. // The awaiter will be aware of the failure by checking the returned error.
  52. sa_con->set_completed(true);
  53. }
  54. return err;
  55. }
  56. } // namespace SignalAwaiterUtils
  57. Variant SignalAwaiterHandle::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  58. #ifdef DEBUG_ENABLED
  59. ERR_FAIL_COND_V_MSG(conn_target_id && !ObjectDB::get_instance(conn_target_id), Variant(),
  60. "Resumed after await, but class instance is gone.");
  61. #endif
  62. if (p_argcount < 1) {
  63. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  64. r_error.argument = 1;
  65. return Variant();
  66. }
  67. Ref<SignalAwaiterHandle> self = *p_args[p_argcount - 1];
  68. if (self.is_null()) {
  69. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  70. r_error.argument = p_argcount - 1;
  71. r_error.expected = Variant::OBJECT;
  72. return Variant();
  73. }
  74. set_completed(true);
  75. int signal_argc = p_argcount - 1;
  76. MonoArray *signal_args = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(MonoObject), signal_argc);
  77. for (int i = 0; i < signal_argc; i++) {
  78. MonoObject *boxed = GDMonoMarshal::variant_to_mono_object(*p_args[i]);
  79. mono_array_setref(signal_args, i, boxed);
  80. }
  81. MonoException *exc = NULL;
  82. GD_MONO_BEGIN_RUNTIME_INVOKE;
  83. CACHED_METHOD_THUNK(SignalAwaiter, SignalCallback).invoke(get_target(), signal_args, &exc);
  84. GD_MONO_END_RUNTIME_INVOKE;
  85. if (exc) {
  86. GDMonoUtils::set_pending_exception(exc);
  87. ERR_FAIL_V(Variant());
  88. }
  89. return Variant();
  90. }
  91. void SignalAwaiterHandle::_bind_methods() {
  92. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &SignalAwaiterHandle::_signal_callback, MethodInfo("_signal_callback"));
  93. }
  94. SignalAwaiterHandle::SignalAwaiterHandle(MonoObject *p_managed) :
  95. MonoGCHandle(MonoGCHandle::new_strong_handle(p_managed), STRONG_HANDLE) {
  96. #ifdef DEBUG_ENABLED
  97. conn_target_id = 0;
  98. #endif
  99. }
  100. SignalAwaiterHandle::~SignalAwaiterHandle() {
  101. if (!completed) {
  102. MonoObject *awaiter = get_target();
  103. if (awaiter) {
  104. MonoException *exc = NULL;
  105. GD_MONO_BEGIN_RUNTIME_INVOKE;
  106. CACHED_METHOD_THUNK(SignalAwaiter, FailureCallback).invoke(awaiter, &exc);
  107. GD_MONO_END_RUNTIME_INVOKE;
  108. if (exc) {
  109. GDMonoUtils::set_pending_exception(exc);
  110. ERR_FAIL();
  111. }
  112. }
  113. }
  114. }