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