gdscript_lambda_callable.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**************************************************************************/
  2. /* gdscript_lambda_callable.cpp */
  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. #include "gdscript_lambda_callable.h"
  31. #include "gdscript.h"
  32. #include "core/templates/hashfuncs.h"
  33. bool GDScriptLambdaCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
  34. // Lambda callables are only compared by reference.
  35. return p_a == p_b;
  36. }
  37. bool GDScriptLambdaCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
  38. // Lambda callables are only compared by reference.
  39. return p_a < p_b;
  40. }
  41. bool GDScriptLambdaCallable::is_valid() const {
  42. return CallableCustom::is_valid() && function != nullptr;
  43. }
  44. uint32_t GDScriptLambdaCallable::hash() const {
  45. return h;
  46. }
  47. String GDScriptLambdaCallable::get_as_text() const {
  48. if (function == nullptr) {
  49. return "<invalid lambda>";
  50. }
  51. if (function->get_name() != StringName()) {
  52. return function->get_name().operator String() + "(lambda)";
  53. }
  54. return "(anonymous lambda)";
  55. }
  56. CallableCustom::CompareEqualFunc GDScriptLambdaCallable::get_compare_equal_func() const {
  57. return compare_equal;
  58. }
  59. CallableCustom::CompareLessFunc GDScriptLambdaCallable::get_compare_less_func() const {
  60. return compare_less;
  61. }
  62. ObjectID GDScriptLambdaCallable::get_object() const {
  63. return script->get_instance_id();
  64. }
  65. StringName GDScriptLambdaCallable::get_method() const {
  66. return function->get_name();
  67. }
  68. void GDScriptLambdaCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  69. int captures_amount = captures.size();
  70. if (function == nullptr) {
  71. r_return_value = Variant();
  72. r_call_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  73. return;
  74. }
  75. if (captures_amount > 0) {
  76. Vector<const Variant *> args;
  77. args.resize(p_argcount + captures_amount);
  78. for (int i = 0; i < captures_amount; i++) {
  79. args.write[i] = &captures[i];
  80. if (captures[i].get_type() == Variant::OBJECT) {
  81. bool was_freed = false;
  82. captures[i].get_validated_object_with_check(was_freed);
  83. if (was_freed) {
  84. ERR_PRINT(vformat(R"(Lambda capture at index %d was freed. Passed "null" instead.)", i));
  85. static Variant nil;
  86. args.write[i] = &nil;
  87. }
  88. }
  89. }
  90. for (int i = 0; i < p_argcount; i++) {
  91. args.write[i + captures_amount] = p_arguments[i];
  92. }
  93. r_return_value = function->call(nullptr, args.ptrw(), args.size(), r_call_error);
  94. switch (r_call_error.error) {
  95. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  96. r_call_error.argument -= captures_amount;
  97. #ifdef DEBUG_ENABLED
  98. if (r_call_error.argument < 0) {
  99. ERR_PRINT(vformat("GDScript bug (please report): Invalid value of lambda capture at index %d.", captures_amount + r_call_error.argument));
  100. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; // TODO: Add a more suitable error code.
  101. r_call_error.argument = 0;
  102. r_call_error.expected = 0;
  103. }
  104. #endif
  105. break;
  106. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  107. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  108. r_call_error.expected -= captures_amount;
  109. #ifdef DEBUG_ENABLED
  110. if (r_call_error.expected < 0) {
  111. ERR_PRINT("GDScript bug (please report): Invalid lambda captures count.");
  112. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; // TODO: Add a more suitable error code.
  113. r_call_error.argument = 0;
  114. r_call_error.expected = 0;
  115. }
  116. #endif
  117. break;
  118. default:
  119. break;
  120. }
  121. } else {
  122. r_return_value = function->call(nullptr, p_arguments, p_argcount, r_call_error);
  123. }
  124. }
  125. GDScriptLambdaCallable::GDScriptLambdaCallable(Ref<GDScript> p_script, GDScriptFunction *p_function, const Vector<Variant> &p_captures) :
  126. function(p_function) {
  127. ERR_FAIL_NULL(p_script.ptr());
  128. ERR_FAIL_NULL(p_function);
  129. script = p_script;
  130. captures = p_captures;
  131. h = (uint32_t)hash_murmur3_one_64((uint64_t)this);
  132. }
  133. bool GDScriptLambdaSelfCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
  134. // Lambda callables are only compared by reference.
  135. return p_a == p_b;
  136. }
  137. bool GDScriptLambdaSelfCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
  138. // Lambda callables are only compared by reference.
  139. return p_a < p_b;
  140. }
  141. bool GDScriptLambdaSelfCallable::is_valid() const {
  142. return CallableCustom::is_valid() && function != nullptr;
  143. }
  144. uint32_t GDScriptLambdaSelfCallable::hash() const {
  145. return h;
  146. }
  147. String GDScriptLambdaSelfCallable::get_as_text() const {
  148. if (function == nullptr) {
  149. return "<invalid lambda>";
  150. }
  151. if (function->get_name() != StringName()) {
  152. return function->get_name().operator String() + "(lambda)";
  153. }
  154. return "(anonymous lambda)";
  155. }
  156. CallableCustom::CompareEqualFunc GDScriptLambdaSelfCallable::get_compare_equal_func() const {
  157. return compare_equal;
  158. }
  159. CallableCustom::CompareLessFunc GDScriptLambdaSelfCallable::get_compare_less_func() const {
  160. return compare_less;
  161. }
  162. ObjectID GDScriptLambdaSelfCallable::get_object() const {
  163. return object->get_instance_id();
  164. }
  165. void GDScriptLambdaSelfCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  166. #ifdef DEBUG_ENABLED
  167. if (object->get_script_instance() == nullptr || object->get_script_instance()->get_language() != GDScriptLanguage::get_singleton()) {
  168. ERR_PRINT("Trying to call a lambda with an invalid instance.");
  169. r_call_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  170. return;
  171. }
  172. #endif
  173. int captures_amount = captures.size();
  174. if (function == nullptr) {
  175. r_return_value = Variant();
  176. r_call_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  177. return;
  178. }
  179. if (captures_amount > 0) {
  180. Vector<const Variant *> args;
  181. args.resize(p_argcount + captures_amount);
  182. for (int i = 0; i < captures_amount; i++) {
  183. args.write[i] = &captures[i];
  184. if (captures[i].get_type() == Variant::OBJECT) {
  185. bool was_freed = false;
  186. captures[i].get_validated_object_with_check(was_freed);
  187. if (was_freed) {
  188. ERR_PRINT(vformat(R"(Lambda capture at index %d was freed. Passed "null" instead.)", i));
  189. static Variant nil;
  190. args.write[i] = &nil;
  191. }
  192. }
  193. }
  194. for (int i = 0; i < p_argcount; i++) {
  195. args.write[i + captures_amount] = p_arguments[i];
  196. }
  197. r_return_value = function->call(static_cast<GDScriptInstance *>(object->get_script_instance()), args.ptrw(), args.size(), r_call_error);
  198. switch (r_call_error.error) {
  199. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  200. r_call_error.argument -= captures_amount;
  201. #ifdef DEBUG_ENABLED
  202. if (r_call_error.argument < 0) {
  203. ERR_PRINT(vformat("GDScript bug (please report): Invalid value of lambda capture at index %d.", captures_amount + r_call_error.argument));
  204. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; // TODO: Add a more suitable error code.
  205. r_call_error.argument = 0;
  206. r_call_error.expected = 0;
  207. }
  208. #endif
  209. break;
  210. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  211. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  212. r_call_error.expected -= captures_amount;
  213. #ifdef DEBUG_ENABLED
  214. if (r_call_error.expected < 0) {
  215. ERR_PRINT("GDScript bug (please report): Invalid lambda captures count.");
  216. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; // TODO: Add a more suitable error code.
  217. r_call_error.argument = 0;
  218. r_call_error.expected = 0;
  219. }
  220. #endif
  221. break;
  222. default:
  223. break;
  224. }
  225. } else {
  226. r_return_value = function->call(static_cast<GDScriptInstance *>(object->get_script_instance()), p_arguments, p_argcount, r_call_error);
  227. }
  228. }
  229. GDScriptLambdaSelfCallable::GDScriptLambdaSelfCallable(Ref<RefCounted> p_self, GDScriptFunction *p_function, const Vector<Variant> &p_captures) :
  230. function(p_function) {
  231. ERR_FAIL_NULL(p_self.ptr());
  232. ERR_FAIL_NULL(p_function);
  233. reference = p_self;
  234. object = p_self.ptr();
  235. captures = p_captures;
  236. h = (uint32_t)hash_murmur3_one_64((uint64_t)this);
  237. }
  238. GDScriptLambdaSelfCallable::GDScriptLambdaSelfCallable(Object *p_self, GDScriptFunction *p_function, const Vector<Variant> &p_captures) :
  239. function(p_function) {
  240. ERR_FAIL_NULL(p_self);
  241. ERR_FAIL_NULL(p_function);
  242. object = p_self;
  243. captures = p_captures;
  244. h = (uint32_t)hash_murmur3_one_64((uint64_t)this);
  245. }