diff --git a/modules/gdscript/gdscript_vm.cpp b/modules/gdscript/gdscript_vm.cpp index ddb0cf95029..ffa4a09b9d8 100644 --- a/modules/gdscript/gdscript_vm.cpp +++ b/modules/gdscript/gdscript_vm.cpp @@ -482,6 +482,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Variant retvalue; Variant *stack = nullptr; Variant **instruction_args = nullptr; + const Variant **&argptrs = const_cast(instruction_args); int defarg = 0; #ifdef DEBUG_ENABLED @@ -497,8 +498,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a if (p_state) { //use existing (supplied) state (awaited) - stack = (Variant *)p_state->stack.ptr(); - instruction_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check + stack = const_cast(reinterpret_cast(p_state->stack.ptr())); + instruction_args = const_cast(reinterpret_cast(&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size])); //ptr() to avoid bounds check line = p_state->line; ip = p_state->ip; alloca_size = p_state->stack.size(); @@ -526,8 +527,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a // Add 3 here for self, class, and nil. alloca_size = sizeof(Variant *) * 3 + sizeof(Variant *) * _instruction_args_size + sizeof(Variant) * _stack_size; - uint8_t *aptr = (uint8_t *)alloca(alloca_size); - stack = (Variant *)aptr; + uint8_t *aptr = reinterpret_cast(alloca(alloca_size)); + stack = reinterpret_cast(aptr); for (int i = 0; i < p_argcount; i++) { if (!argument_types[i].has_type) { @@ -573,7 +574,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } if (_instruction_args_size) { - instruction_args = (Variant **)&aptr[sizeof(Variant) * _stack_size]; + instruction_args = reinterpret_cast(&aptr[sizeof(Variant) * _stack_size]); } else { instruction_args = nullptr; } @@ -1333,7 +1334,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a if (Variant::can_convert_strict(src->get_type(), var_type)) { #endif // DEBUG_ENABLED Callable::CallError ce; - Variant::construct(var_type, *dst, const_cast(&src), 1, ce); + const Variant *args = src; + Variant::construct(var_type, *dst, &args, 1, ce); } else { #ifdef DEBUG_ENABLED err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) + @@ -1597,16 +1599,14 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Variant::Type t = Variant::Type(_code_ptr[ip + 2]); - Variant **argptrs = instruction_args; - GET_INSTRUCTION_ARG(dst, argc); Callable::CallError err; - Variant::construct(t, *dst, (const Variant **)argptrs, argc, err); + Variant::construct(t, *dst, argptrs, argc, err); #ifdef DEBUG_ENABLED if (err.error != Callable::CallError::CALL_OK) { - err_text = _get_call_error("'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs, *dst, err); + err_text = _get_call_error("'" + Variant::get_type_name(t) + "' constructor", argptrs, *dst, err); OPCODE_BREAK; } #endif @@ -1626,11 +1626,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(constructor_idx < 0 || constructor_idx >= _constructors_count); Variant::ValidatedConstructor constructor = _constructors_ptr[constructor_idx]; - Variant **argptrs = instruction_args; - GET_INSTRUCTION_ARG(dst, argc); - constructor(dst, (const Variant **)argptrs); + constructor(dst, argptrs); ip += 3; } @@ -1729,7 +1727,6 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a const StringName *methodname = &_global_names_ptr[methodname_idx]; GET_INSTRUCTION_ARG(base, argc); - Variant **argptrs = instruction_args; #ifdef DEBUG_ENABLED uint64_t call_time = 0; @@ -1746,7 +1743,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Callable::CallError err; if (call_ret) { GET_INSTRUCTION_ARG(ret, argc + 1); - base->callp(*methodname, (const Variant **)argptrs, argc, temp_ret, err); + base->callp(*methodname, argptrs, argc, temp_ret, err); *ret = temp_ret; #ifdef DEBUG_ENABLED if (ret->get_type() == Variant::NIL) { @@ -1776,7 +1773,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } #endif } else { - base->callp(*methodname, (const Variant **)argptrs, argc, temp_ret, err); + base->callp(*methodname, argptrs, argc, temp_ret, err); } #ifdef DEBUG_ENABLED @@ -1821,7 +1818,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } } } - err_text = _get_call_error("function '" + methodstr + (is_callable ? "" : "' in base '" + basestr) + "'", (const Variant **)argptrs, temp_ret, err); + err_text = _get_call_error("function '" + methodstr + (is_callable ? "" : "' in base '" + basestr) + "'", argptrs, temp_ret, err); OPCODE_BREAK; } #endif @@ -1858,7 +1855,6 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #else Object *base_obj = base->operator Object *(); #endif - Variant **argptrs = instruction_args; #ifdef DEBUG_ENABLED uint64_t call_time = 0; @@ -1871,10 +1867,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Callable::CallError err; if (call_ret) { GET_INSTRUCTION_ARG(ret, argc + 1); - temp_ret = method->call(base_obj, (const Variant **)argptrs, argc, err); + temp_ret = method->call(base_obj, argptrs, argc, err); *ret = temp_ret; } else { - temp_ret = method->call(base_obj, (const Variant **)argptrs, argc, err); + temp_ret = method->call(base_obj, argptrs, argc, err); } #ifdef DEBUG_ENABLED @@ -1907,7 +1903,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } } } - err_text = _get_call_error("function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs, temp_ret, err); + err_text = _get_call_error("function '" + methodstr + "' in base '" + basestr + "'", argptrs, temp_ret, err); OPCODE_BREAK; } #endif @@ -1933,8 +1929,6 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GET_INSTRUCTION_ARG(ret, argc); - const Variant **argptrs = const_cast(instruction_args); - Callable::CallError err; Variant::call_static(builtin_type, *methodname, argptrs, argc, *ret, err); @@ -1963,8 +1957,6 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GET_INSTRUCTION_ARG(ret, argc); - const Variant **argptrs = const_cast(instruction_args); - #ifdef DEBUG_ENABLED uint64_t call_time = 0; if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) { @@ -2004,8 +1996,6 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count); MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; - Variant **argptrs = instruction_args; - #ifdef DEBUG_ENABLED uint64_t call_time = 0; if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) { @@ -2014,7 +2004,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #endif GET_INSTRUCTION_ARG(ret, argc); - method->validated_call(nullptr, (const Variant **)argptrs, ret); + method->validated_call(nullptr, argptrs, ret); #ifdef DEBUG_ENABLED if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) { @@ -2040,7 +2030,6 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count); MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; - Variant **argptrs = instruction_args; #ifdef DEBUG_ENABLED uint64_t call_time = 0; if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) { @@ -2050,7 +2039,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GET_INSTRUCTION_ARG(ret, argc); VariantInternal::initialize(ret, Variant::NIL); - method->validated_call(nullptr, (const Variant **)argptrs, nullptr); + method->validated_call(nullptr, argptrs, nullptr); #ifdef DEBUG_ENABLED if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) { @@ -2092,8 +2081,6 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Object *base_obj = *VariantInternal::get_object(base); #endif - Variant **argptrs = instruction_args; - #ifdef DEBUG_ENABLED uint64_t call_time = 0; if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) { @@ -2102,7 +2089,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #endif GET_INSTRUCTION_ARG(ret, argc + 1); - method->validated_call(base_obj, (const Variant **)argptrs, ret); + method->validated_call(base_obj, argptrs, ret); #ifdef DEBUG_ENABLED if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) { @@ -2142,7 +2129,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #else Object *base_obj = *VariantInternal::get_object(base); #endif - Variant **argptrs = instruction_args; + #ifdef DEBUG_ENABLED uint64_t call_time = 0; if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) { @@ -2152,7 +2139,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GET_INSTRUCTION_ARG(ret, argc + 1); VariantInternal::initialize(ret, Variant::NIL); - method->validated_call(base_obj, (const Variant **)argptrs, nullptr); + method->validated_call(base_obj, argptrs, nullptr); #ifdef DEBUG_ENABLED if (GDScriptLanguage::get_singleton()->profiling && GDScriptLanguage::get_singleton()->profile_native_calls) { @@ -2180,10 +2167,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _builtin_methods_count); Variant::ValidatedBuiltInMethod method = _builtin_methods_ptr[_code_ptr[ip + 2]]; - Variant **argptrs = instruction_args; GET_INSTRUCTION_ARG(ret, argc + 1); - method(base, (const Variant **)argptrs, argc, ret); + method(base, argptrs, argc, ret); ip += 3; } @@ -2201,12 +2187,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _global_names_count); StringName function = _global_names_ptr[_code_ptr[ip + 2]]; - Variant **argptrs = instruction_args; - GET_INSTRUCTION_ARG(dst, argc); Callable::CallError err; - Variant::call_utility_function(function, dst, (const Variant **)argptrs, argc, err); + Variant::call_utility_function(function, dst, argptrs, argc, err); #ifdef DEBUG_ENABLED if (err.error != Callable::CallError::CALL_OK) { @@ -2215,7 +2199,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a // Call provided error string. err_text = vformat(R"*(Error calling utility function "%s()": %s)*", methodstr, *dst); } else { - err_text = _get_call_error(vformat(R"*(utility function "%s()")*", methodstr), (const Variant **)argptrs, *dst, err); + err_text = _get_call_error(vformat(R"*(utility function "%s()")*", methodstr), argptrs, *dst, err); } OPCODE_BREAK; } @@ -2236,11 +2220,9 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _utilities_count); Variant::ValidatedUtilityFunction function = _utilities_ptr[_code_ptr[ip + 2]]; - Variant **argptrs = instruction_args; - GET_INSTRUCTION_ARG(dst, argc); - function(dst, (const Variant **)argptrs, argc); + function(dst, argptrs, argc); ip += 3; } @@ -2258,12 +2240,10 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _gds_utilities_count); GDScriptUtilityFunctions::FunctionPtr function = _gds_utilities_ptr[_code_ptr[ip + 2]]; - Variant **argptrs = instruction_args; - GET_INSTRUCTION_ARG(dst, argc); Callable::CallError err; - function(dst, (const Variant **)argptrs, argc, err); + function(dst, argptrs, argc, err); #ifdef DEBUG_ENABLED if (err.error != Callable::CallError::CALL_OK) { @@ -2272,7 +2252,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a // Call provided error string. err_text = vformat(R"*(Error calling GDScript utility function "%s()": %s)*", methodstr, *dst); } else { - err_text = _get_call_error(vformat(R"*(GDScript utility function "%s()")*", methodstr), (const Variant **)argptrs, *dst, err); + err_text = _get_call_error(vformat(R"*(GDScript utility function "%s()")*", methodstr), argptrs, *dst, err); } OPCODE_BREAK; } @@ -2299,8 +2279,6 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a #endif const StringName *methodname = &_global_names_ptr[self_fun]; - Variant **argptrs = instruction_args; - GET_INSTRUCTION_ARG(dst, argc); const GDScript *gds = _script; @@ -2317,14 +2295,14 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Callable::CallError err; if (E) { - *dst = E->value->call(p_instance, (const Variant **)argptrs, argc, err); + *dst = E->value->call(p_instance, argptrs, argc, err); } else if (gds->native.ptr()) { if (*methodname != GDScriptLanguage::get_singleton()->strings._init) { MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname); if (!mb) { err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; } else { - *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err); + *dst = mb->call(p_instance->owner, argptrs, argc, err); } } else { err.error = Callable::CallError::CALL_OK; @@ -2339,7 +2317,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a if (err.error != Callable::CallError::CALL_OK) { String methodstr = *methodname; - err_text = _get_call_error("function '" + methodstr + "'", (const Variant **)argptrs, *dst, err); + err_text = _get_call_error("function '" + methodstr + "'", argptrs, *dst, err); OPCODE_BREAK; } @@ -2600,7 +2578,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a if (r->get_type() != ret_type) { if (Variant::can_convert_strict(r->get_type(), ret_type)) { Callable::CallError ce; - Variant::construct(ret_type, retvalue, const_cast(&r), 1, ce); + const Variant *args = r; + Variant::construct(ret_type, retvalue, &args, 1, ce); } else { #ifdef DEBUG_ENABLED err_text = vformat(R"(Trying to return value of type "%s" from a function whose return type is "%s".)",