gdscript_lambda_callable.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*************************************************************************/
  2. /* gdscript_lambda_callable.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 "gdscript_lambda_callable.h"
  31. #include "core/templates/hashfuncs.h"
  32. #include "gdscript.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. uint32_t GDScriptLambdaCallable::hash() const {
  42. return h;
  43. }
  44. String GDScriptLambdaCallable::get_as_text() const {
  45. if (function->get_name() != StringName()) {
  46. return function->get_name().operator String() + "(lambda)";
  47. }
  48. return "(anonymous lambda)";
  49. }
  50. CallableCustom::CompareEqualFunc GDScriptLambdaCallable::get_compare_equal_func() const {
  51. return compare_equal;
  52. }
  53. CallableCustom::CompareLessFunc GDScriptLambdaCallable::get_compare_less_func() const {
  54. return compare_less;
  55. }
  56. ObjectID GDScriptLambdaCallable::get_object() const {
  57. return script->get_instance_id();
  58. }
  59. void GDScriptLambdaCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  60. int captures_amount = captures.size();
  61. if (captures_amount > 0) {
  62. Vector<const Variant *> args;
  63. args.resize(p_argcount + captures_amount);
  64. for (int i = 0; i < captures_amount; i++) {
  65. args.write[i] = &captures[i];
  66. }
  67. for (int i = 0; i < p_argcount; i++) {
  68. args.write[i + captures_amount] = p_arguments[i];
  69. }
  70. r_return_value = function->call(nullptr, args.ptrw(), args.size(), r_call_error);
  71. r_call_error.argument -= captures_amount;
  72. } else {
  73. r_return_value = function->call(nullptr, p_arguments, p_argcount, r_call_error);
  74. }
  75. }
  76. GDScriptLambdaCallable::GDScriptLambdaCallable(Ref<GDScript> p_script, GDScriptFunction *p_function, const Vector<Variant> &p_captures) {
  77. script = p_script;
  78. function = p_function;
  79. captures = p_captures;
  80. h = (uint32_t)hash_djb2_one_64((uint64_t)this);
  81. }
  82. bool GDScriptLambdaSelfCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
  83. // Lambda callables are only compared by reference.
  84. return p_a == p_b;
  85. }
  86. bool GDScriptLambdaSelfCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
  87. // Lambda callables are only compared by reference.
  88. return p_a < p_b;
  89. }
  90. uint32_t GDScriptLambdaSelfCallable::hash() const {
  91. return h;
  92. }
  93. String GDScriptLambdaSelfCallable::get_as_text() const {
  94. if (function->get_name() != StringName()) {
  95. return function->get_name().operator String() + "(lambda)";
  96. }
  97. return "(anonymous lambda)";
  98. }
  99. CallableCustom::CompareEqualFunc GDScriptLambdaSelfCallable::get_compare_equal_func() const {
  100. return compare_equal;
  101. }
  102. CallableCustom::CompareLessFunc GDScriptLambdaSelfCallable::get_compare_less_func() const {
  103. return compare_less;
  104. }
  105. ObjectID GDScriptLambdaSelfCallable::get_object() const {
  106. return object->get_instance_id();
  107. }
  108. void GDScriptLambdaSelfCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  109. #ifdef DEBUG_ENABLED
  110. if (object->get_script_instance() == nullptr || object->get_script_instance()->get_language() != GDScriptLanguage::get_singleton()) {
  111. ERR_PRINT("Trying to call a lambda with an invalid instance.");
  112. r_call_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  113. return;
  114. }
  115. #endif
  116. int captures_amount = captures.size();
  117. if (captures_amount > 0) {
  118. Vector<const Variant *> args;
  119. args.resize(p_argcount + captures_amount);
  120. for (int i = 0; i < captures_amount; i++) {
  121. args.write[i] = &captures[i];
  122. }
  123. for (int i = 0; i < p_argcount; i++) {
  124. args.write[i + captures_amount] = p_arguments[i];
  125. }
  126. r_return_value = function->call(static_cast<GDScriptInstance *>(object->get_script_instance()), args.ptrw(), args.size(), r_call_error);
  127. r_call_error.argument -= captures_amount;
  128. } else {
  129. r_return_value = function->call(static_cast<GDScriptInstance *>(object->get_script_instance()), p_arguments, p_argcount, r_call_error);
  130. }
  131. }
  132. GDScriptLambdaSelfCallable::GDScriptLambdaSelfCallable(Ref<RefCounted> p_self, GDScriptFunction *p_function, const Vector<Variant> &p_captures) {
  133. reference = p_self;
  134. object = p_self.ptr();
  135. function = p_function;
  136. captures = p_captures;
  137. h = (uint32_t)hash_djb2_one_64((uint64_t)this);
  138. }
  139. GDScriptLambdaSelfCallable::GDScriptLambdaSelfCallable(Object *p_self, GDScriptFunction *p_function, const Vector<Variant> &p_captures) {
  140. object = p_self;
  141. function = p_function;
  142. captures = p_captures;
  143. h = (uint32_t)hash_djb2_one_64((uint64_t)this);
  144. }