gdscript_lambda_callable.cpp 11 KB

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