diff options
Diffstat (limited to 'modules/gdscript/gdscript_vm.cpp')
-rw-r--r-- | modules/gdscript/gdscript_vm.cpp | 897 |
1 files changed, 513 insertions, 384 deletions
diff --git a/modules/gdscript/gdscript_vm.cpp b/modules/gdscript/gdscript_vm.cpp index c73ba798aa..bebf34cbb3 100644 --- a/modules/gdscript/gdscript_vm.cpp +++ b/modules/gdscript/gdscript_vm.cpp @@ -1,32 +1,32 @@ -/*************************************************************************/ -/* gdscript_vm.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ +/**************************************************************************/ +/* gdscript_vm.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ #include "gdscript_function.h" @@ -35,39 +35,6 @@ #include "gdscript.h" #include "gdscript_lambda_callable.h" -Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_instance, Variant *p_stack, String &r_error) const { - int address = p_address & ADDR_MASK; - - //sequential table (jump table generated by compiler) - switch ((p_address & ADDR_TYPE_MASK) >> ADDR_BITS) { - case ADDR_TYPE_STACK: { -#ifdef DEBUG_ENABLED - ERR_FAIL_INDEX_V(address, _stack_size, nullptr); -#endif - return &p_stack[address]; - } break; - case ADDR_TYPE_CONSTANT: { -#ifdef DEBUG_ENABLED - ERR_FAIL_INDEX_V(address, _constant_count, nullptr); -#endif - return &_constants_ptr[address]; - } break; - case ADDR_TYPE_MEMBER: { -#ifdef DEBUG_ENABLED - if (unlikely(!p_instance)) { - r_error = "Cannot access member without instance."; - return nullptr; - } -#endif - //member indexing is O(1) - return &p_instance->members.write[address]; - } break; - } - - ERR_FAIL_V_MSG(nullptr, "Bad code! (unknown addressing mode)."); - return nullptr; -} - #ifdef DEBUG_ENABLED static String _get_script_name(const Ref<Script> p_script) { Ref<GDScript> gdscript = p_script; @@ -80,6 +47,16 @@ static String _get_script_name(const Ref<Script> p_script) { } } +static String _get_element_type(Variant::Type builtin_type, const StringName &native_type, const Ref<Script> &script_type) { + if (script_type.is_valid() && script_type->is_valid()) { + return _get_script_name(script_type); + } else if (native_type != StringName()) { + return native_type.operator String(); + } else { + return Variant::get_type_name(builtin_type); + } +} + static String _get_var_type(const Variant *p_var) { String basestr; @@ -108,15 +85,8 @@ static String _get_var_type(const Variant *p_var) { basestr = "Array"; const Array *p_array = VariantInternal::get_array(p_var); Variant::Type builtin_type = (Variant::Type)p_array->get_typed_builtin(); - StringName native_type = p_array->get_typed_class_name(); - Ref<Script> script_type = p_array->get_typed_script(); - - if (script_type.is_valid() && script_type->is_valid()) { - basestr += "[" + _get_script_name(script_type) + "]"; - } else if (native_type != StringName()) { - basestr += "[" + native_type.operator String() + "]"; - } else if (builtin_type != Variant::NIL) { - basestr += "[" + Variant::get_type_name(builtin_type) + "]"; + if (builtin_type != Variant::NIL) { + basestr += "[" + _get_element_type(builtin_type, p_array->get_typed_class_name(), p_array->get_typed_script()) + "]"; } } else { basestr = Variant::get_type_name(p_var->get_type()); @@ -134,10 +104,7 @@ Variant GDScriptFunction::_get_default_variant_for_data_type(const GDScriptDataT // Typed array. if (p_data_type.has_container_element_type()) { const GDScriptDataType &element_type = p_data_type.get_container_element_type(); - array.set_typed( - element_type.kind == GDScriptDataType::BUILTIN ? element_type.builtin_type : Variant::OBJECT, - element_type.native_type, - element_type.script_type); + array.set_typed(element_type.builtin_type, element_type.native_type, element_type.script_type); } return array; @@ -164,6 +131,8 @@ String GDScriptFunction::_get_call_error(const Callable::CallError &p_err, const #ifdef DEBUG_ENABLED if (p_err.expected == Variant::OBJECT && argptrs[errorarg]->get_type() == p_err.expected) { err_text = "Invalid type in " + p_where + ". The Object-derived class of argument " + itos(errorarg + 1) + " (" + _get_var_type(argptrs[errorarg]) + ") is not a subclass of the expected argument class."; + } else if (p_err.expected == Variant::ARRAY && argptrs[errorarg]->get_type() == p_err.expected) { + err_text = "Invalid type in " + p_where + ". The array of argument " + itos(errorarg + 1) + " (" + _get_var_type(argptrs[errorarg]) + ") does not have the same element type as the expected typed array argument."; } else #endif // DEBUG_ENABLED { @@ -232,8 +201,10 @@ void (*type_init_function_table[])(Variant *) = { static const void *switch_table_ops[] = { \ &&OPCODE_OPERATOR, \ &&OPCODE_OPERATOR_VALIDATED, \ - &&OPCODE_EXTENDS_TEST, \ - &&OPCODE_IS_BUILTIN, \ + &&OPCODE_TYPE_TEST_BUILTIN, \ + &&OPCODE_TYPE_TEST_ARRAY, \ + &&OPCODE_TYPE_TEST_NATIVE, \ + &&OPCODE_TYPE_TEST_SCRIPT, \ &&OPCODE_SET_KEYED, \ &&OPCODE_SET_KEYED_VALIDATED, \ &&OPCODE_SET_INDEXED_VALIDATED, \ @@ -413,14 +384,19 @@ void (*type_init_function_table[])(Variant *) = { #define OPCODE(m_op) \ m_op: -#define OPCODE_WHILE(m_test) \ - OPSWHILE: +#define OPCODE_WHILE(m_test) #define OPCODES_END \ OPSEXIT: #define OPCODES_OUT \ OPSOUT: -#define DISPATCH_OPCODE goto OPSWHILE #define OPCODE_SWITCH(m_test) goto *switch_table_ops[m_test]; +#ifdef DEBUG_ENABLED +#define DISPATCH_OPCODE \ + last_opcode = _code_ptr[ip]; \ + goto *switch_table_ops[last_opcode] +#else +#define DISPATCH_OPCODE goto *switch_table_ops[_code_ptr[ip]] +#endif #define OPCODE_BREAK goto OPSEXIT #define OPCODE_OUT goto OPSOUT #else @@ -473,6 +449,9 @@ void (*type_init_function_table[])(Variant *) = { #define OP_GET_BASIS get_basis #define OP_GET_RID get_rid +#define METHOD_CALL_ON_NULL_VALUE_ERROR(method_pointer) "Cannot call method '" + (method_pointer)->get_name() + "' on a null value." +#define METHOD_CALL_ON_FREED_INSTANCE_ERROR(method_pointer) "Cannot call method '" + (method_pointer)->get_name() + "' on a previously freed instance." + Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state) { OPCODES_TABLE; @@ -482,6 +461,33 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a r_err.error = Callable::CallError::CALL_OK; + static thread_local int call_depth = 0; + if (unlikely(++call_depth > MAX_CALL_DEPTH)) { + call_depth--; +#ifdef DEBUG_ENABLED + String err_file; + if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && !p_instance->script->path.is_empty()) { + err_file = p_instance->script->path; + } else if (_script) { + err_file = _script->path; + } + if (err_file.is_empty()) { + err_file = "<built-in>"; + } + String err_func = name; + if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && !p_instance->script->name.is_empty()) { + err_func = p_instance->script->name + "." + err_func; + } + int err_line = _initial_line; + const char *err_text = "Stack overflow. Check for infinite recursion in your script."; + if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) { + // Debugger break did not happen. + _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text, false, ERR_HANDLER_SCRIPT); + } +#endif + return _get_default_variant_for_data_type(return_type); + } + Variant retvalue; Variant *stack = nullptr; Variant **instruction_args = nullptr; @@ -516,10 +522,12 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a r_err.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; r_err.argument = _argument_count; + call_depth--; return _get_default_variant_for_data_type(return_type); } else if (p_argcount < _argument_count - _default_arg_count) { r_err.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_err.argument = _argument_count - _default_arg_count; + call_depth--; return _get_default_variant_for_data_type(return_type); } else { defarg = _argument_count - p_argcount; @@ -546,7 +554,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a if (!argument_types[i].is_type(*p_args[i], true)) { r_err.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_err.argument = i; - r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT; + r_err.expected = argument_types[i].builtin_type; + call_depth--; return _get_default_variant_for_data_type(return_type); } if (argument_types[i].kind == GDScriptDataType::BUILTIN) { @@ -607,21 +616,52 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #define CHECK_SPACE(m_space) \ GD_ERR_BREAK((ip + m_space) > _code_size) -#define GET_VARIANT_PTR(m_v, m_code_ofs) \ - Variant *m_v; \ - m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, stack, err_text); \ - if (unlikely(!m_v)) \ - OPCODE_BREAK; +#define GET_VARIANT_PTR(m_v, m_code_ofs) \ + Variant *m_v; \ + { \ + int address = _code_ptr[ip + 1 + (m_code_ofs)]; \ + int address_type = (address & ADDR_TYPE_MASK) >> ADDR_BITS; \ + if (unlikely(address_type < 0 || address_type >= ADDR_TYPE_MAX)) { \ + err_text = "Bad address type."; \ + OPCODE_BREAK; \ + } \ + int address_index = address & ADDR_MASK; \ + if (unlikely(address_index < 0 || address_index >= variant_address_limits[address_type])) { \ + if (address_type == ADDR_TYPE_MEMBER && !p_instance) { \ + err_text = "Cannot access member without instance."; \ + } else { \ + err_text = "Bad address index."; \ + } \ + OPCODE_BREAK; \ + } \ + m_v = &variant_addresses[address_type][address_index]; \ + if (unlikely(!m_v)) \ + OPCODE_BREAK; \ + } #else #define GD_ERR_BREAK(m_cond) #define CHECK_SPACE(m_space) -#define GET_VARIANT_PTR(m_v, m_code_ofs) \ - Variant *m_v; \ - m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, stack, err_text); + +#define GET_VARIANT_PTR(m_v, m_code_ofs) \ + Variant *m_v; \ + { \ + int address = _code_ptr[ip + 1 + (m_code_ofs)]; \ + m_v = &variant_addresses[(address & ADDR_TYPE_MASK) >> ADDR_BITS][address & ADDR_MASK]; \ + if (unlikely(!m_v)) \ + OPCODE_BREAK; \ + } #endif +#define LOAD_INSTRUCTION_ARGS \ + int instr_arg_count = _code_ptr[ip + 1]; \ + for (int i = 0; i < instr_arg_count; i++) { \ + GET_VARIANT_PTR(v, i + 1); \ + instruction_args[i] = v; \ + } \ + ip += 1; // Offset to skip instruction argcount. + #define GET_INSTRUCTION_ARG(m_v, m_idx) \ Variant *m_v = instruction_args[m_idx] @@ -639,21 +679,20 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a bool exit_ok = false; bool awaited = false; #endif +#ifdef DEBUG_ENABLED + int variant_address_limits[ADDR_TYPE_MAX] = { _stack_size, _constant_count, p_instance ? p_instance->members.size() : 0, script->static_variables.size() }; +#endif + + Variant *variant_addresses[ADDR_TYPE_MAX] = { stack, _constants_ptr, p_instance ? p_instance->members.ptrw() : nullptr, script->static_variables.ptrw() }; #ifdef DEBUG_ENABLED OPCODE_WHILE(ip < _code_size) { - int last_opcode = _code_ptr[ip] & INSTR_MASK; + int last_opcode = _code_ptr[ip]; #else OPCODE_WHILE(true) { #endif - // Load arguments for the instruction before each instruction. - int instr_arg_count = ((_code_ptr[ip]) & INSTR_ARGS_MASK) >> INSTR_BITS; - for (int i = 0; i < instr_arg_count; i++) { - GET_VARIANT_PTR(v, i + 1); - instruction_args[i] = v; - } - OPCODE_SWITCH(_code_ptr[ip] & INSTR_MASK) { + OPCODE_SWITCH(_code_ptr[ip]) { OPCODE(OPCODE_OPERATOR) { CHECK_SPACE(5); @@ -661,9 +700,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Variant::Operator op = (Variant::Operator)_code_ptr[ip + 4]; GD_ERR_BREAK(op >= Variant::OP_MAX); - GET_INSTRUCTION_ARG(a, 0); - GET_INSTRUCTION_ARG(b, 1); - GET_INSTRUCTION_ARG(dst, 2); + GET_VARIANT_PTR(a, 0); + GET_VARIANT_PTR(b, 1); + GET_VARIANT_PTR(dst, 2); #ifdef DEBUG_ENABLED @@ -696,9 +735,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(operator_idx < 0 || operator_idx >= _operator_funcs_count); Variant::ValidatedOperatorEvaluator operator_func = _operator_funcs_ptr[operator_idx]; - GET_INSTRUCTION_ARG(a, 0); - GET_INSTRUCTION_ARG(b, 1); - GET_INSTRUCTION_ARG(dst, 2); + GET_VARIANT_PTR(a, 0); + GET_VARIANT_PTR(b, 1); + GET_VARIANT_PTR(dst, 2); operator_func(a, b, dst); @@ -706,91 +745,95 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } DISPATCH_OPCODE; - OPCODE(OPCODE_EXTENDS_TEST) { + OPCODE(OPCODE_TYPE_TEST_BUILTIN) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(a, 0); - GET_INSTRUCTION_ARG(b, 1); - GET_INSTRUCTION_ARG(dst, 2); - -#ifdef DEBUG_ENABLED - if (b->get_type() != Variant::OBJECT || b->operator Object *() == nullptr) { - err_text = "Right operand of 'is' is not a class."; - OPCODE_BREAK; - } -#endif + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(value, 1); - bool extends_ok = false; - if (a->get_type() == Variant::OBJECT && a->operator Object *() != nullptr) { -#ifdef DEBUG_ENABLED - bool was_freed; - Object *obj_A = a->get_validated_object_with_check(was_freed); + Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 3]; + GD_ERR_BREAK(builtin_type < 0 || builtin_type >= Variant::VARIANT_MAX); - if (was_freed) { - err_text = "Left operand of 'is' is a previously freed instance."; - OPCODE_BREAK; - } + *dst = value->get_type() == builtin_type; + ip += 4; + } + DISPATCH_OPCODE; - Object *obj_B = b->get_validated_object_with_check(was_freed); + OPCODE(OPCODE_TYPE_TEST_ARRAY) { + CHECK_SPACE(6); - if (was_freed) { - err_text = "Right operand of 'is' is a previously freed instance."; - OPCODE_BREAK; - } -#else + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(value, 1); - Object *obj_A = *a; - Object *obj_B = *b; -#endif // DEBUG_ENABLED + GET_VARIANT_PTR(script_type, 2); + Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 4]; + int native_type_idx = _code_ptr[ip + 5]; + GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count); + const StringName native_type = _global_names_ptr[native_type_idx]; - GDScript *scr_B = Object::cast_to<GDScript>(obj_B); + bool result = false; + if (value->get_type() == Variant::ARRAY) { + Array *array = VariantInternal::get_array(value); + result = array->get_typed_builtin() == ((uint32_t)builtin_type) && array->get_typed_class_name() == native_type && array->get_typed_script() == *script_type; + } - if (scr_B) { - //if B is a script, the only valid condition is that A has an instance which inherits from the script - //in other situation, this should return false. + *dst = result; + ip += 6; + } + DISPATCH_OPCODE; - if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) { - GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr()); - //bool found=false; - while (cmp) { - if (cmp == scr_B) { - //inherits from script, all ok - extends_ok = true; - break; - } + OPCODE(OPCODE_TYPE_TEST_NATIVE) { + CHECK_SPACE(4); - cmp = cmp->_base; - } - } + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(value, 1); - } else { - GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B); + int native_type_idx = _code_ptr[ip + 3]; + GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count); + const StringName native_type = _global_names_ptr[native_type_idx]; -#ifdef DEBUG_ENABLED - if (!nc) { - err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "')."; - OPCODE_BREAK; - } -#endif - extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name()); - } + bool was_freed = false; + Object *object = value->get_validated_object_with_check(was_freed); + if (was_freed) { + err_text = "Left operand of 'is' is a previously freed instance."; + OPCODE_BREAK; } - *dst = extends_ok; + *dst = object && ClassDB::is_parent_class(object->get_class_name(), native_type); ip += 4; } DISPATCH_OPCODE; - OPCODE(OPCODE_IS_BUILTIN) { + OPCODE(OPCODE_TYPE_TEST_SCRIPT) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(value, 0); - GET_INSTRUCTION_ARG(dst, 1); - Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3]; + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(value, 1); - GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX); + GET_VARIANT_PTR(type, 2); + Script *script_type = Object::cast_to<Script>(type->operator Object *()); + GD_ERR_BREAK(!script_type); + + bool was_freed = false; + Object *object = value->get_validated_object_with_check(was_freed); + if (was_freed) { + err_text = "Left operand of 'is' is a previously freed instance."; + OPCODE_BREAK; + } + + bool result = false; + if (object && object->get_script_instance()) { + Script *script_ptr = object->get_script_instance()->get_script().ptr(); + while (script_ptr) { + if (script_ptr == script_type) { + result = true; + break; + } + script_ptr = script_ptr->get_base_script().ptr(); + } + } - *dst = value->get_type() == var_type; + *dst = result; ip += 4; } DISPATCH_OPCODE; @@ -798,22 +841,31 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_SET_KEYED) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(dst, 0); - GET_INSTRUCTION_ARG(index, 1); - GET_INSTRUCTION_ARG(value, 2); + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(index, 1); + GET_VARIANT_PTR(value, 2); bool valid; dst->set(*index, *value, &valid); #ifdef DEBUG_ENABLED if (!valid) { + Object *obj = dst->get_validated_object(); String v = index->operator String(); - if (!v.is_empty()) { - v = "'" + v + "'"; + bool read_only_property = false; + if (obj) { + read_only_property = ClassDB::has_property(obj->get_class_name(), v) && (ClassDB::get_property_setter(obj->get_class_name(), v) == StringName()); + } + if (read_only_property) { + err_text = vformat(R"(Cannot set value into property "%s" (on base "%s") because it is read-only.)", v, _get_var_type(dst)); } else { - v = "of type '" + _get_var_type(index) + "'"; + if (!v.is_empty()) { + v = "'" + v + "'"; + } else { + v = "of type '" + _get_var_type(index) + "'"; + } + err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'"; } - err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'"; OPCODE_BREAK; } #endif @@ -824,9 +876,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_SET_KEYED_VALIDATED) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(dst, 0); - GET_INSTRUCTION_ARG(index, 1); - GET_INSTRUCTION_ARG(value, 2); + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(index, 1); + GET_VARIANT_PTR(value, 2); int index_setter = _code_ptr[ip + 4]; GD_ERR_BREAK(index_setter < 0 || index_setter >= _keyed_setters_count); @@ -854,9 +906,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_SET_INDEXED_VALIDATED) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(dst, 0); - GET_INSTRUCTION_ARG(index, 1); - GET_INSTRUCTION_ARG(value, 2); + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(index, 1); + GET_VARIANT_PTR(value, 2); int index_setter = _code_ptr[ip + 4]; GD_ERR_BREAK(index_setter < 0 || index_setter >= _indexed_setters_count); @@ -886,9 +938,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_GET_KEYED) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(src, 0); - GET_INSTRUCTION_ARG(index, 1); - GET_INSTRUCTION_ARG(dst, 2); + GET_VARIANT_PTR(src, 0); + GET_VARIANT_PTR(index, 1); + GET_VARIANT_PTR(dst, 2); bool valid; #ifdef DEBUG_ENABLED @@ -918,9 +970,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_GET_KEYED_VALIDATED) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(src, 0); - GET_INSTRUCTION_ARG(key, 1); - GET_INSTRUCTION_ARG(dst, 2); + GET_VARIANT_PTR(src, 0); + GET_VARIANT_PTR(key, 1); + GET_VARIANT_PTR(dst, 2); int index_getter = _code_ptr[ip + 4]; GD_ERR_BREAK(index_getter < 0 || index_getter >= _keyed_getters_count); @@ -954,9 +1006,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_GET_INDEXED_VALIDATED) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(src, 0); - GET_INSTRUCTION_ARG(index, 1); - GET_INSTRUCTION_ARG(dst, 2); + GET_VARIANT_PTR(src, 0); + GET_VARIANT_PTR(index, 1); + GET_VARIANT_PTR(dst, 2); int index_getter = _code_ptr[ip + 4]; GD_ERR_BREAK(index_getter < 0 || index_getter >= _indexed_getters_count); @@ -986,8 +1038,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_SET_NAMED) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(dst, 0); - GET_INSTRUCTION_ARG(value, 1); + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(value, 1); int indexname = _code_ptr[ip + 3]; @@ -999,8 +1051,16 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #ifdef DEBUG_ENABLED if (!valid) { - String err_type; - err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'."; + Object *obj = dst->get_validated_object(); + bool read_only_property = false; + if (obj) { + read_only_property = ClassDB::has_property(obj->get_class_name(), *index) && (ClassDB::get_property_setter(obj->get_class_name(), *index) == StringName()); + } + if (read_only_property) { + err_text = vformat(R"(Cannot set value into property "%s" (on base "%s") because it is read-only.)", String(*index), _get_var_type(dst)); + } else { + err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'."; + } OPCODE_BREAK; } #endif @@ -1011,8 +1071,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_SET_NAMED_VALIDATED) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(dst, 0); - GET_INSTRUCTION_ARG(value, 1); + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(value, 1); int index_setter = _code_ptr[ip + 3]; GD_ERR_BREAK(index_setter < 0 || index_setter >= _setters_count); @@ -1026,8 +1086,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_GET_NAMED) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(src, 0); - GET_INSTRUCTION_ARG(dst, 1); + GET_VARIANT_PTR(src, 0); + GET_VARIANT_PTR(dst, 1); int indexname = _code_ptr[ip + 3]; @@ -1056,8 +1116,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_GET_NAMED_VALIDATED) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(src, 0); - GET_INSTRUCTION_ARG(dst, 1); + GET_VARIANT_PTR(src, 0); + GET_VARIANT_PTR(dst, 1); int index_getter = _code_ptr[ip + 3]; GD_ERR_BREAK(index_getter < 0 || index_getter >= _getters_count); @@ -1070,7 +1130,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_SET_MEMBER) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(src, 0); + GET_VARIANT_PTR(src, 0); int indexname = _code_ptr[ip + 2]; GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count); const StringName *index = &_global_names_ptr[indexname]; @@ -1094,7 +1154,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_GET_MEMBER) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(dst, 0); + GET_VARIANT_PTR(dst, 0); int indexname = _code_ptr[ip + 2]; GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count); const StringName *index = &_global_names_ptr[indexname]; @@ -1113,8 +1173,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ASSIGN) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(dst, 0); - GET_INSTRUCTION_ARG(src, 1); + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(src, 1); *dst = *src; @@ -1124,7 +1184,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ASSIGN_TRUE) { CHECK_SPACE(2); - GET_INSTRUCTION_ARG(dst, 0); + GET_VARIANT_PTR(dst, 0); *dst = true; @@ -1134,7 +1194,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ASSIGN_FALSE) { CHECK_SPACE(2); - GET_INSTRUCTION_ARG(dst, 0); + GET_VARIANT_PTR(dst, 0); *dst = false; @@ -1144,8 +1204,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(dst, 0); - GET_INSTRUCTION_ARG(src, 1); + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(src, 1); Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3]; GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX); @@ -1172,37 +1232,47 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_ASSIGN_TYPED_ARRAY) { - CHECK_SPACE(3); - GET_INSTRUCTION_ARG(dst, 0); - GET_INSTRUCTION_ARG(src, 1); + CHECK_SPACE(6); + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(src, 1); - Array *dst_arr = VariantInternal::get_array(dst); + GET_VARIANT_PTR(script_type, 2); + Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 4]; + int native_type_idx = _code_ptr[ip + 5]; + GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count); + const StringName native_type = _global_names_ptr[native_type_idx]; if (src->get_type() != Variant::ARRAY) { #ifdef DEBUG_ENABLED - err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) + - "' to a variable of type '" + +"'."; -#endif + err_text = vformat(R"(Trying to assign a value of type "%s" to a variable of type "Array[%s]".)", + _get_var_type(src), _get_element_type(builtin_type, native_type, *script_type)); +#endif // DEBUG_ENABLED OPCODE_BREAK; } - if (!dst_arr->typed_assign(*src)) { + + Array *array = VariantInternal::get_array(src); + + if (array->get_typed_builtin() != ((uint32_t)builtin_type) || array->get_typed_class_name() != native_type || array->get_typed_script() != *script_type) { #ifdef DEBUG_ENABLED - err_text = "Trying to assign a typed array with an array of different type.'"; -#endif + err_text = vformat(R"(Trying to assign an array of type "%s" to a variable of type "Array[%s]".)", + _get_var_type(src), _get_element_type(builtin_type, native_type, *script_type)); +#endif // DEBUG_ENABLED OPCODE_BREAK; } - ip += 3; + *dst = *src; + + ip += 6; } DISPATCH_OPCODE; OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(dst, 0); - GET_INSTRUCTION_ARG(src, 1); + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(src, 1); #ifdef DEBUG_ENABLED - GET_INSTRUCTION_ARG(type, 2); + GET_VARIANT_PTR(type, 2); GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *()); GD_ERR_BREAK(!nc); if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) { @@ -1210,12 +1280,20 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a "' to a variable of type '" + nc->get_name() + "'."; OPCODE_BREAK; } - Object *src_obj = src->operator Object *(); - if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) { - err_text = "Trying to assign value of type '" + src_obj->get_class_name() + - "' to a variable of type '" + nc->get_name() + "'."; - OPCODE_BREAK; + if (src->get_type() == Variant::OBJECT) { + bool was_freed = false; + Object *src_obj = src->get_validated_object_with_check(was_freed); + if (!src_obj && was_freed) { + err_text = "Trying to assign invalid previously freed instance."; + OPCODE_BREAK; + } + + if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) { + err_text = "Trying to assign value of type '" + src_obj->get_class_name() + + "' to a variable of type '" + nc->get_name() + "'."; + OPCODE_BREAK; + } } #endif // DEBUG_ENABLED *dst = *src; @@ -1226,11 +1304,11 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(dst, 0); - GET_INSTRUCTION_ARG(src, 1); + GET_VARIANT_PTR(dst, 0); + GET_VARIANT_PTR(src, 1); #ifdef DEBUG_ENABLED - GET_INSTRUCTION_ARG(type, 2); + GET_VARIANT_PTR(type, 2); Script *base_type = Object::cast_to<Script>(type->operator Object *()); GD_ERR_BREAK(!base_type); @@ -1240,29 +1318,38 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE_BREAK; } - if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) { - ScriptInstance *scr_inst = src->operator Object *()->get_script_instance(); - if (!scr_inst) { - err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() + - "' to a variable of type '" + base_type->get_path().get_file() + "'."; + if (src->get_type() == Variant::OBJECT) { + bool was_freed = false; + Object *val_obj = src->get_validated_object_with_check(was_freed); + if (!val_obj && was_freed) { + err_text = "Trying to assign invalid previously freed instance."; OPCODE_BREAK; } - Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr(); - bool valid = false; + if (val_obj) { // src is not null + ScriptInstance *scr_inst = val_obj->get_script_instance(); + if (!scr_inst) { + err_text = "Trying to assign value of type '" + val_obj->get_class_name() + + "' to a variable of type '" + base_type->get_path().get_file() + "'."; + OPCODE_BREAK; + } - while (src_type) { - if (src_type == base_type) { - valid = true; - break; + Script *src_type = scr_inst->get_script().ptr(); + bool valid = false; + + while (src_type) { + if (src_type == base_type) { + valid = true; + break; + } + src_type = src_type->get_base_script().ptr(); } - src_type = src_type->get_base_script().ptr(); - } - if (!valid) { - err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() + - "' to a variable of type '" + base_type->get_path().get_file() + "'."; - OPCODE_BREAK; + if (!valid) { + err_text = "Trying to assign value of type '" + val_obj->get_script_instance()->get_script()->get_path().get_file() + + "' to a variable of type '" + base_type->get_path().get_file() + "'."; + OPCODE_BREAK; + } } } #endif // DEBUG_ENABLED @@ -1275,8 +1362,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_CAST_TO_BUILTIN) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(src, 0); - GET_INSTRUCTION_ARG(dst, 1); + GET_VARIANT_PTR(src, 0); + GET_VARIANT_PTR(dst, 1); Variant::Type to_type = (Variant::Type)_code_ptr[ip + 3]; GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX); @@ -1304,9 +1391,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_CAST_TO_NATIVE) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(src, 0); - GET_INSTRUCTION_ARG(dst, 1); - GET_INSTRUCTION_ARG(to_type, 2); + GET_VARIANT_PTR(src, 0); + GET_VARIANT_PTR(dst, 1); + GET_VARIANT_PTR(to_type, 2); GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *()); GD_ERR_BREAK(!nc); @@ -1335,9 +1422,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_CAST_TO_SCRIPT) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(src, 0); - GET_INSTRUCTION_ARG(dst, 1); - GET_INSTRUCTION_ARG(to_type, 2); + GET_VARIANT_PTR(src, 0); + GET_VARIANT_PTR(dst, 1); + GET_VARIANT_PTR(to_type, 2); Script *base_type = Object::cast_to<Script>(to_type->operator Object *()); @@ -1383,6 +1470,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CONSTRUCT) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(2 + instr_arg_count); ip += instr_arg_count; @@ -1390,6 +1478,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a int argc = _code_ptr[ip + 1]; Variant::Type t = Variant::Type(_code_ptr[ip + 2]); + Variant **argptrs = instruction_args; GET_INSTRUCTION_ARG(dst, argc); @@ -1409,8 +1498,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CONSTRUCT_VALIDATED) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(2 + instr_arg_count); - ip += instr_arg_count; int argc = _code_ptr[ip + 1]; @@ -1430,6 +1519,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CONSTRUCT_ARRAY) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(1 + instr_arg_count); ip += instr_arg_count; @@ -1451,6 +1541,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CONSTRUCT_TYPED_ARRAY) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(3 + instr_arg_count); ip += instr_arg_count; @@ -1463,9 +1554,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a const StringName native_type = _global_names_ptr[native_type_idx]; Array array; - array.set_typed(builtin_type, native_type, *script_type); array.resize(argc); - for (int i = 0; i < argc; i++) { array[i] = *(instruction_args[i]); } @@ -1473,13 +1562,14 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GET_INSTRUCTION_ARG(dst, argc); *dst = Variant(); // Clear potential previous typed array. - *dst = array; + *dst = Array(array, builtin_type, native_type, *script_type); ip += 4; } DISPATCH_OPCODE; OPCODE(OPCODE_CONSTRUCT_DICTIONARY) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(2 + instr_arg_count); ip += instr_arg_count; @@ -1504,11 +1594,12 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_CALL_ASYNC) OPCODE(OPCODE_CALL_RETURN) OPCODE(OPCODE_CALL) { - CHECK_SPACE(3 + instr_arg_count); - bool call_ret = (_code_ptr[ip] & INSTR_MASK) != OPCODE_CALL; + bool call_ret = (_code_ptr[ip]) != OPCODE_CALL; #ifdef DEBUG_ENABLED - bool call_async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC; + bool call_async = (_code_ptr[ip]) == OPCODE_CALL_ASYNC; #endif + LOAD_INSTRUCTION_ARGS + CHECK_SPACE(3 + instr_arg_count); ip += instr_arg_count; @@ -1533,17 +1624,33 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Callable::CallError err; if (call_ret) { GET_INSTRUCTION_ARG(ret, argc + 1); +#ifdef DEBUG_ENABLED + Variant::Type base_type = base->get_type(); + Object *base_obj = base->get_validated_object(); + StringName base_class = base_obj ? base_obj->get_class_name() : StringName(); +#endif base->callp(*methodname, (const Variant **)argptrs, argc, *ret, err); #ifdef DEBUG_ENABLED + if (ret->get_type() == Variant::NIL) { + if (base_type == Variant::OBJECT) { + if (base_obj) { + MethodBind *method = ClassDB::get_method(base_class, *methodname); + if (*methodname == CoreStringNames::get_singleton()->_free || (method && !method->has_return())) { + err_text = R"(Trying to get a return value of a method that returns "void")"; + OPCODE_BREAK; + } + } + } else if (Variant::has_builtin_method(base_type, *methodname) && !Variant::has_builtin_method_return_value(base_type, *methodname)) { + err_text = R"(Trying to get a return value of a method that returns "void")"; + OPCODE_BREAK; + } + } + if (!call_async && ret->get_type() == Variant::OBJECT) { // Check if getting a function state without await. bool was_freed = false; Object *obj = ret->get_validated_object_with_check(was_freed); - if (was_freed) { - err_text = "Got a freed object as a result of the call."; - OPCODE_BREAK; - } if (obj && obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) { err_text = R"(Trying to call an async function without "await".)"; OPCODE_BREAK; @@ -1603,8 +1710,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_CALL_METHOD_BIND) OPCODE(OPCODE_CALL_METHOD_BIND_RET) { + bool call_ret = (_code_ptr[ip]) == OPCODE_CALL_METHOD_BIND_RET; + LOAD_INSTRUCTION_ARGS CHECK_SPACE(3 + instr_arg_count); - bool call_ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_METHOD_BIND_RET; ip += instr_arg_count; @@ -1619,10 +1727,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a bool freed = false; Object *base_obj = base->get_validated_object_with_check(freed); if (freed) { - err_text = "Trying to call a function on a previously freed instance."; + err_text = METHOD_CALL_ON_FREED_INSTANCE_ERROR(method); OPCODE_BREAK; } else if (!base_obj) { - err_text = "Trying to call a function on a null value."; + err_text = METHOD_CALL_ON_NULL_VALUE_ERROR(method); OPCODE_BREAK; } #else @@ -1682,6 +1790,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CALL_BUILTIN_STATIC) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(4 + instr_arg_count); ip += instr_arg_count; @@ -1727,6 +1836,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CALL_NATIVE_STATIC) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(3 + instr_arg_count); ip += instr_arg_count; @@ -1770,6 +1880,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #ifdef DEBUG_ENABLED #define OPCODE_CALL_PTR(m_type) \ OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \ + LOAD_INSTRUCTION_ARGS \ CHECK_SPACE(3 + instr_arg_count); \ ip += instr_arg_count; \ int argc = _code_ptr[ip + 1]; \ @@ -1780,10 +1891,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a bool freed = false; \ Object *base_obj = base->get_validated_object_with_check(freed); \ if (freed) { \ - err_text = "Trying to call a function on a previously freed instance."; \ + err_text = METHOD_CALL_ON_FREED_INSTANCE_ERROR(method); \ OPCODE_BREAK; \ } else if (!base_obj) { \ - err_text = "Trying to call a function on a null value."; \ + err_text = METHOD_CALL_ON_NULL_VALUE_ERROR(method); \ OPCODE_BREAK; \ } \ const void **argptrs = call_args_ptr; \ @@ -1808,6 +1919,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #else #define OPCODE_CALL_PTR(m_type) \ OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \ + LOAD_INSTRUCTION_ARGS \ CHECK_SPACE(3 + instr_arg_count); \ ip += instr_arg_count; \ int argc = _code_ptr[ip + 1]; \ @@ -1865,6 +1977,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE_CALL_PTR(PACKED_VECTOR3_ARRAY); OPCODE_CALL_PTR(PACKED_COLOR_ARRAY); OPCODE(OPCODE_CALL_PTRCALL_OBJECT) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(3 + instr_arg_count); ip += instr_arg_count; @@ -1880,10 +1993,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a bool freed = false; Object *base_obj = base->get_validated_object_with_check(freed); if (freed) { - err_text = "Trying to call a function on a previously freed instance."; + err_text = METHOD_CALL_ON_FREED_INSTANCE_ERROR(method); OPCODE_BREAK; } else if (!base_obj) { - err_text = "Trying to call a function on a null value."; + err_text = METHOD_CALL_ON_NULL_VALUE_ERROR(method); OPCODE_BREAK; } #else @@ -1908,7 +2021,13 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a VariantInternal::initialize(ret, Variant::OBJECT); Object **ret_opaque = VariantInternal::get_object(ret); method->ptrcall(base_obj, argptrs, ret_opaque); - VariantInternal::update_object_id(ret); + if (method->is_return_type_raw_object_ptr()) { + // The Variant has to participate in the ref count since the method returns a raw Object *. + VariantInternal::object_assign(ret, *ret_opaque); + } else { + // The method, in case it returns something, returns an already encapsulated object. + VariantInternal::update_object_id(ret); + } #ifdef DEBUG_ENABLED if (GDScriptLanguage::get_singleton()->profiling) { @@ -1919,6 +2038,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } DISPATCH_OPCODE; OPCODE(OPCODE_CALL_PTRCALL_NO_RETURN) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(3 + instr_arg_count); ip += instr_arg_count; @@ -1934,10 +2054,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a bool freed = false; Object *base_obj = base->get_validated_object_with_check(freed); if (freed) { - err_text = "Trying to call a function on a previously freed instance."; + err_text = METHOD_CALL_ON_FREED_INSTANCE_ERROR(method); OPCODE_BREAK; } else if (!base_obj) { - err_text = "Trying to call a function on a null value."; + err_text = METHOD_CALL_ON_NULL_VALUE_ERROR(method); OPCODE_BREAK; } #else @@ -1971,6 +2091,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CALL_BUILTIN_TYPE_VALIDATED) { + LOAD_INSTRUCTION_ARGS + CHECK_SPACE(3 + instr_arg_count); ip += instr_arg_count; @@ -2005,6 +2127,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CALL_UTILITY) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(3 + instr_arg_count); ip += instr_arg_count; @@ -2039,6 +2162,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CALL_UTILITY_VALIDATED) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(3 + instr_arg_count); ip += instr_arg_count; @@ -2060,6 +2184,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CALL_GDSCRIPT_UTILITY) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(3 + instr_arg_count); ip += instr_arg_count; @@ -2095,6 +2220,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CALL_SELF_BASE) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(3 + instr_arg_count); ip += instr_arg_count; @@ -2164,7 +2290,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a CHECK_SPACE(2); // Do the one-shot connect. - GET_INSTRUCTION_ARG(argobj, 0); + GET_VARIANT_PTR(argobj, 0); Signal sig; bool is_signal = true; @@ -2191,7 +2317,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a if (result.get_type() != Variant::SIGNAL) { // Not async, return immediately using the target from OPCODE_AWAIT_RESUME. - GET_VARIANT_PTR(target, 3); + GET_VARIANT_PTR(target, 2); *target = result; ip += 4; // Skip OPCODE_AWAIT_RESUME and its data. is_signal = false; @@ -2227,7 +2353,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } #ifdef DEBUG_ENABLED gdfs->state.function_name = name; - gdfs->state.script_path = _script->get_path(); + gdfs->state.script_path = _script->get_script_path(); #endif gdfs->state.defarg = defarg; gdfs->function = this; @@ -2257,13 +2383,14 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE_BREAK; } #endif - GET_INSTRUCTION_ARG(result, 0); + GET_VARIANT_PTR(result, 0); *result = p_state->result; ip += 2; } DISPATCH_OPCODE; OPCODE(OPCODE_CREATE_LAMBDA) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(2 + instr_arg_count); ip += instr_arg_count; @@ -2292,6 +2419,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a DISPATCH_OPCODE; OPCODE(OPCODE_CREATE_SELF_LAMBDA) { + LOAD_INSTRUCTION_ARGS CHECK_SPACE(2 + instr_arg_count); GD_ERR_BREAK(p_instance == nullptr); @@ -2338,7 +2466,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_JUMP_IF) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(test, 0); + GET_VARIANT_PTR(test, 0); bool result = test->booleanize(); @@ -2355,7 +2483,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_JUMP_IF_NOT) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(test, 0); + GET_VARIANT_PTR(test, 0); bool result = test->booleanize(); @@ -2378,7 +2506,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_JUMP_IF_SHARED) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(val, 0); + GET_VARIANT_PTR(val, 0); if (val->is_shared()) { int to = _code_ptr[ip + 2]; @@ -2392,7 +2520,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_RETURN) { CHECK_SPACE(2); - GET_INSTRUCTION_ARG(r, 0); + GET_VARIANT_PTR(r, 0); retvalue = *r; #ifdef DEBUG_ENABLED exit_ok = true; @@ -2402,7 +2530,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_RETURN_TYPED_BUILTIN) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(r, 0); + GET_VARIANT_PTR(r, 0); Variant::Type ret_type = (Variant::Type)_code_ptr[ip + 2]; GD_ERR_BREAK(ret_type < 0 || ret_type >= Variant::VARIANT_MAX); @@ -2433,9 +2561,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_RETURN_TYPED_ARRAY) { CHECK_SPACE(5); - GET_INSTRUCTION_ARG(r, 0); + GET_VARIANT_PTR(r, 0); - GET_INSTRUCTION_ARG(script_type, 1); + GET_VARIANT_PTR(script_type, 1); Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 3]; int native_type_idx = _code_ptr[ip + 4]; GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count); @@ -2443,30 +2571,25 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a if (r->get_type() != Variant::ARRAY) { #ifdef DEBUG_ENABLED - err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "Array[%s]".)", - Variant::get_type_name(r->get_type()), Variant::get_type_name(builtin_type)); -#endif + err_text = vformat(R"(Trying to return a value of type "%s" where expected return type is "Array[%s]".)", + _get_var_type(r), _get_element_type(builtin_type, native_type, *script_type)); +#endif // DEBUG_ENABLED OPCODE_BREAK; } - Array array; - array.set_typed(builtin_type, native_type, *script_type); + Array *array = VariantInternal::get_array(r); + if (array->get_typed_builtin() != ((uint32_t)builtin_type) || array->get_typed_class_name() != native_type || array->get_typed_script() != *script_type) { #ifdef DEBUG_ENABLED - bool valid = array.typed_assign(*VariantInternal::get_array(r)); -#else - array.typed_assign(*VariantInternal::get_array(r)); + err_text = vformat(R"(Trying to return an array of type "%s" where expected return type is "Array[%s]".)", + _get_var_type(r), _get_element_type(builtin_type, native_type, *script_type)); #endif // DEBUG_ENABLED - - // Assign the return value anyway since we want it to be the valid type. - retvalue = array; - -#ifdef DEBUG_ENABLED - if (!valid) { - err_text = "Trying to return a typed array with an array of different type.'"; OPCODE_BREAK; } + retvalue = *array; + +#ifdef DEBUG_ENABLED exit_ok = true; #endif // DEBUG_ENABLED OPCODE_BREAK; @@ -2474,9 +2597,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_RETURN_TYPED_NATIVE) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(r, 0); + GET_VARIANT_PTR(r, 0); - GET_INSTRUCTION_ARG(type, 1); + GET_VARIANT_PTR(type, 1); GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *()); GD_ERR_BREAK(!nc); @@ -2514,9 +2637,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_RETURN_TYPED_SCRIPT) { CHECK_SPACE(3); - GET_INSTRUCTION_ARG(r, 0); + GET_VARIANT_PTR(r, 0); - GET_INSTRUCTION_ARG(type, 1); + GET_VARIANT_PTR(type, 1); Script *base_type = Object::cast_to<Script>(type->operator Object *()); GD_ERR_BREAK(!base_type); @@ -2580,8 +2703,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_BEGIN) { CHECK_SPACE(8); // Space for this and a regular iterate. - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); bool valid; if (!container->iter_init(*counter, valid)) { @@ -2595,7 +2718,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *iterator = container->iter_get(*counter, valid); #ifdef DEBUG_ENABLED @@ -2612,8 +2735,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_BEGIN_INT) { CHECK_SPACE(8); // Check space for iterate instruction too. - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); int64_t size = *VariantInternal::get_int(container); @@ -2621,7 +2744,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a *VariantInternal::get_int(counter) = 0; if (size > 0) { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); VariantInternal::initialize(iterator, Variant::INT); *VariantInternal::get_int(iterator) = 0; @@ -2639,8 +2762,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_BEGIN_FLOAT) { CHECK_SPACE(8); // Check space for iterate instruction too. - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); double size = *VariantInternal::get_float(container); @@ -2648,7 +2771,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a *VariantInternal::get_float(counter) = 0.0; if (size > 0) { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); VariantInternal::initialize(iterator, Variant::FLOAT); *VariantInternal::get_float(iterator) = 0; @@ -2666,8 +2789,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2) { CHECK_SPACE(8); // Check space for iterate instruction too. - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); Vector2 *bounds = VariantInternal::get_vector2(container); @@ -2675,7 +2798,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a *VariantInternal::get_float(counter) = bounds->x; if (bounds->x < bounds->y) { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); VariantInternal::initialize(iterator, Variant::FLOAT); *VariantInternal::get_float(iterator) = bounds->x; @@ -2693,8 +2816,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2I) { CHECK_SPACE(8); // Check space for iterate instruction too. - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); Vector2i *bounds = VariantInternal::get_vector2i(container); @@ -2702,7 +2825,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a *VariantInternal::get_int(counter) = bounds->x; if (bounds->x < bounds->y) { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); VariantInternal::initialize(iterator, Variant::INT); *VariantInternal::get_int(iterator) = bounds->x; @@ -2720,8 +2843,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3) { CHECK_SPACE(8); // Check space for iterate instruction too. - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); Vector3 *bounds = VariantInternal::get_vector3(container); double from = bounds->x; @@ -2734,7 +2857,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0); if (do_continue) { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); VariantInternal::initialize(iterator, Variant::FLOAT); *VariantInternal::get_float(iterator) = from; @@ -2752,8 +2875,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3I) { CHECK_SPACE(8); // Check space for iterate instruction too. - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); Vector3i *bounds = VariantInternal::get_vector3i(container); int64_t from = bounds->x; @@ -2766,7 +2889,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0); if (do_continue) { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); VariantInternal::initialize(iterator, Variant::INT); *VariantInternal::get_int(iterator) = from; @@ -2784,8 +2907,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_BEGIN_STRING) { CHECK_SPACE(8); // Check space for iterate instruction too. - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); String *str = VariantInternal::get_string(container); @@ -2793,7 +2916,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a *VariantInternal::get_int(counter) = 0; if (!str->is_empty()) { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); VariantInternal::initialize(iterator, Variant::STRING); *VariantInternal::get_string(iterator) = str->substr(0, 1); @@ -2811,14 +2934,14 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_BEGIN_DICTIONARY) { CHECK_SPACE(8); // Check space for iterate instruction too. - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); Dictionary *dict = VariantInternal::get_dictionary(container); const Variant *next = dict->next(nullptr); if (!dict->is_empty()) { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *counter = *next; *iterator = *next; @@ -2836,8 +2959,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_BEGIN_ARRAY) { CHECK_SPACE(8); // Check space for iterate instruction too. - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); Array *array = VariantInternal::get_array(container); @@ -2845,7 +2968,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a *VariantInternal::get_int(counter) = 0; if (!array->is_empty()) { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *iterator = array->get(0); // Skip regular iterate. @@ -2862,13 +2985,13 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #define OPCODE_ITERATE_BEGIN_PACKED_ARRAY(m_var_type, m_elem_type, m_get_func, m_var_ret_type, m_ret_type, m_ret_get_func) \ OPCODE(OPCODE_ITERATE_BEGIN_PACKED_##m_var_type##_ARRAY) { \ CHECK_SPACE(8); \ - GET_INSTRUCTION_ARG(counter, 0); \ - GET_INSTRUCTION_ARG(container, 1); \ + GET_VARIANT_PTR(counter, 0); \ + GET_VARIANT_PTR(container, 1); \ Vector<m_elem_type> *array = VariantInternal::m_get_func(container); \ VariantInternal::initialize(counter, Variant::INT); \ *VariantInternal::get_int(counter) = 0; \ if (!array->is_empty()) { \ - GET_INSTRUCTION_ARG(iterator, 2); \ + GET_VARIANT_PTR(iterator, 2); \ VariantInternal::initialize(iterator, Variant::m_var_ret_type); \ m_ret_type *it = VariantInternal::m_ret_get_func(iterator); \ *it = array->get(0); \ @@ -2894,8 +3017,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_BEGIN_OBJECT) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); #ifdef DEBUG_ENABLED bool freed = false; @@ -2933,7 +3056,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *iterator = obj->callp(CoreStringNames::get_singleton()->_iter_get, (const Variant **)args, 1, ce); #ifdef DEBUG_ENABLED if (ce.error != Callable::CallError::CALL_OK) { @@ -2950,8 +3073,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); bool valid; if (!container->iter_next(*counter, valid)) { @@ -2965,7 +3088,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *iterator = container->iter_get(*counter, valid); #ifdef DEBUG_ENABLED @@ -2982,8 +3105,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_INT) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); int64_t size = *VariantInternal::get_int(container); int64_t *count = VariantInternal::get_int(counter); @@ -2995,7 +3118,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *VariantInternal::get_int(iterator) = *count; ip += 5; // Loop again. @@ -3006,8 +3129,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_FLOAT) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); double size = *VariantInternal::get_float(container); double *count = VariantInternal::get_float(counter); @@ -3019,7 +3142,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *VariantInternal::get_float(iterator) = *count; ip += 5; // Loop again. @@ -3030,8 +3153,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_VECTOR2) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); const Vector2 *bounds = VariantInternal::get_vector2((const Variant *)container); double *count = VariantInternal::get_float(counter); @@ -3043,7 +3166,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *VariantInternal::get_float(iterator) = *count; ip += 5; // Loop again. @@ -3054,8 +3177,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_VECTOR2I) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); const Vector2i *bounds = VariantInternal::get_vector2i((const Variant *)container); int64_t *count = VariantInternal::get_int(counter); @@ -3067,7 +3190,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *VariantInternal::get_int(iterator) = *count; ip += 5; // Loop again. @@ -3078,8 +3201,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_VECTOR3) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); const Vector3 *bounds = VariantInternal::get_vector3((const Variant *)container); double *count = VariantInternal::get_float(counter); @@ -3091,7 +3214,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *VariantInternal::get_float(iterator) = *count; ip += 5; // Loop again. @@ -3102,8 +3225,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_VECTOR3I) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); const Vector3i *bounds = VariantInternal::get_vector3i((const Variant *)container); int64_t *count = VariantInternal::get_int(counter); @@ -3115,7 +3238,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *VariantInternal::get_int(iterator) = *count; ip += 5; // Loop again. @@ -3126,8 +3249,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_STRING) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); const String *str = VariantInternal::get_string((const Variant *)container); int64_t *idx = VariantInternal::get_int(counter); @@ -3138,7 +3261,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *VariantInternal::get_string(iterator) = str->substr(*idx, 1); ip += 5; // Loop again. @@ -3149,8 +3272,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_DICTIONARY) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); const Dictionary *dict = VariantInternal::get_dictionary((const Variant *)container); const Variant *next = dict->next(counter); @@ -3160,7 +3283,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *counter = *next; *iterator = *next; @@ -3172,8 +3295,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_ARRAY) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); const Array *array = VariantInternal::get_array((const Variant *)container); int64_t *idx = VariantInternal::get_int(counter); @@ -3184,7 +3307,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *iterator = array->get(*idx); ip += 5; // Loop again. @@ -3195,8 +3318,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #define OPCODE_ITERATE_PACKED_ARRAY(m_var_type, m_elem_type, m_get_func, m_ret_get_func) \ OPCODE(OPCODE_ITERATE_PACKED_##m_var_type##_ARRAY) { \ CHECK_SPACE(4); \ - GET_INSTRUCTION_ARG(counter, 0); \ - GET_INSTRUCTION_ARG(container, 1); \ + GET_VARIANT_PTR(counter, 0); \ + GET_VARIANT_PTR(container, 1); \ const Vector<m_elem_type> *array = VariantInternal::m_get_func((const Variant *)container); \ int64_t *idx = VariantInternal::get_int(counter); \ (*idx)++; \ @@ -3205,7 +3328,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto<0 || jumpto> _code_size); \ ip = jumpto; \ } else { \ - GET_INSTRUCTION_ARG(iterator, 2); \ + GET_VARIANT_PTR(iterator, 2); \ *VariantInternal::m_ret_get_func(iterator) = array->get(*idx); \ ip += 5; \ } \ @@ -3225,8 +3348,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE(OPCODE_ITERATE_OBJECT) { CHECK_SPACE(4); - GET_INSTRUCTION_ARG(counter, 0); - GET_INSTRUCTION_ARG(container, 1); + GET_VARIANT_PTR(counter, 0); + GET_VARIANT_PTR(container, 1); #ifdef DEBUG_ENABLED bool freed = false; @@ -3264,7 +3387,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size); ip = jumpto; } else { - GET_INSTRUCTION_ARG(iterator, 2); + GET_VARIANT_PTR(iterator, 2); *iterator = obj->callp(CoreStringNames::get_singleton()->_iter_get, (const Variant **)args, 1, ce); #ifdef DEBUG_ENABLED if (ce.error != Callable::CallError::CALL_OK) { @@ -3283,7 +3406,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a int global_idx = _code_ptr[ip + 2]; GD_ERR_BREAK(global_idx < 0 || global_idx >= GDScriptLanguage::get_singleton()->get_global_array_size()); - GET_INSTRUCTION_ARG(dst, 0); + GET_VARIANT_PTR(dst, 0); *dst = GDScriptLanguage::get_singleton()->get_global_array()[global_idx]; ip += 3; @@ -3297,7 +3420,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a const StringName *globalname = &_global_names_ptr[globalname_idx]; GD_ERR_BREAK(!GDScriptLanguage::get_singleton()->get_named_globals_map().has(*globalname)); - GET_INSTRUCTION_ARG(dst, 0); + GET_VARIANT_PTR(dst, 0); *dst = GDScriptLanguage::get_singleton()->get_named_globals_map()[*globalname]; ip += 3; @@ -3307,7 +3430,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #define OPCODE_TYPE_ADJUST(m_v_type, m_c_type) \ OPCODE(OPCODE_TYPE_ADJUST_##m_v_type) { \ CHECK_SPACE(2); \ - GET_INSTRUCTION_ARG(arg, 0); \ + GET_VARIANT_PTR(arg, 0); \ VariantTypeAdjust<m_c_type>::adjust(arg); \ ip += 2; \ } \ @@ -3355,14 +3478,17 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a CHECK_SPACE(3); #ifdef DEBUG_ENABLED - GET_INSTRUCTION_ARG(test, 0); + GET_VARIANT_PTR(test, 0); bool result = test->booleanize(); if (!result) { String message_str; if (_code_ptr[ip + 2] != 0) { - GET_INSTRUCTION_ARG(message, 1); - message_str = *message; + GET_VARIANT_PTR(message, 1); + Variant message_var = *message; + if (message->get_type() != Variant::NIL) { + message_str = message_var; + } } if (message_str.is_empty()) { err_text = "Assertion failed."; @@ -3442,8 +3568,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a //error // function, file, line, error, explanation String err_file; - if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && !p_instance->script->path.is_empty()) { - err_file = p_instance->script->path; + bool instance_valid_with_script = p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid(); + if (instance_valid_with_script && !get_script()->path.is_empty()) { + err_file = get_script()->path; } else if (script) { err_file = script->path; } @@ -3451,7 +3578,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a err_file = "<built-in>"; } String err_func = name; - if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && !p_instance->script->name.is_empty()) { + if (instance_valid_with_script && !p_instance->script->name.is_empty()) { err_func = p_instance->script->name + "." + err_func; } int err_line = line; @@ -3505,5 +3632,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a stack[i].~Variant(); } + call_depth--; + return retvalue; } |