gdscript_lambda_callable.cpp 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. }