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