Godot Engine – Multi-platform 2D and 3D game engine
[GDScript] Fix crash when changing variable type in hot reload
VewDevf901a781dea2
diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cppindex c430ff767bc..f8b3f09283e 100644--- a/modules/gdscript/gdscript.cpp+++ b/modules/gdscript/gdscript.cpp@@ -33,6 +33,7 @@#include "gdscript_analyzer.h"#include "gdscript_cache.h"#include "gdscript_compiler.h"+#include "gdscript_helper.h"#include "gdscript_linter.h"#include "gdscript_parser.h"#include "gdscript_tokenizer_buffer.h"@@ -2062,9 +2063,19 @@ void GDScriptInstance::reload_members() {// Transfer the old members into their new position.for (KeyValue<StringName, GDScript::MemberInfo> &E : script->member_indices) {const GDScript::MemberInfo *old = script->old_member_indices.getptr(E.key);+ bool member_value_assigned = false;+if (old != nullptr) {- Variant value = members[old->index];- new_members[E.value.index] = value;+ const Variant value = members[old->index];+ const bool is_same_type = E.value.data_type.is_type(value, false);+ if (is_same_type) {+ member_value_assigned = true;+ new_members[E.value.index] = value;+ }+ }++ if (!member_value_assigned) {+ new_members[E.value.index] = GDScriptHelper::construct_empty_value_for_data_type(E.value.data_type);}}members = std::move(new_members);diff --git a/modules/gdscript/gdscript_function.h b/modules/gdscript/gdscript_function.hindex e5c6f4933bd..4864112d7af 100644--- a/modules/gdscript/gdscript_function.h+++ b/modules/gdscript/gdscript_function.h@@ -457,8 +457,6 @@ private:String _get_callable_call_error(const String &p_where, const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Variant &p_ret, const Callable::CallError &p_err) const;#endif- Variant _get_default_variant_for_data_type(const GDScriptDataType &p_data_type);-public:static constexpr int MAX_CALL_DEPTH = 2048; // Limit to try to avoid crash because of a stack overflow.diff --git a/modules/gdscript/gdscript_helper.cpp b/modules/gdscript/gdscript_helper.cppnew file mode 100644index 00000000000..3a83541d049--- /dev/null+++ b/modules/gdscript/gdscript_helper.cpp@@ -0,0 +1,72 @@+/**************************************************************************/+/* gdscript_helper.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_helper.h"++#include "core/variant/variant.h"++namespace GDScriptHelper {++Variant construct_empty_value_for_data_type(const GDScriptDataType &p_data_type) {+ if (p_data_type.kind == GDScriptDataType::BUILTIN) {+ if (p_data_type.builtin_type == Variant::ARRAY) {+ Array array;+ // Typed array.+ if (p_data_type.has_container_element_type(0)) {+ const GDScriptDataType &element_type = p_data_type.get_container_element_type(0);+ array.set_typed(element_type.builtin_type, element_type.native_type, element_type.script_type);+ }++ return array;+ } else if (p_data_type.builtin_type == Variant::DICTIONARY) {+ Dictionary dict;+ // Typed dictionary.+ if (p_data_type.has_container_element_types()) {+ const GDScriptDataType &key_type = p_data_type.get_container_element_type_or_variant(0);+ const GDScriptDataType &value_type = p_data_type.get_container_element_type_or_variant(1);+ dict.set_typed(key_type.builtin_type, key_type.native_type, key_type.script_type, value_type.builtin_type, value_type.native_type, value_type.script_type);+ }++ return dict;+ } else {+ Callable::CallError ce;+ Variant variant;+ Variant::construct(p_data_type.builtin_type, variant, nullptr, 0, ce);++ ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, Variant());++ return variant;+ }+ }++ return Variant();+}++} //namespace GDScriptHelperdiff --git a/modules/gdscript/gdscript_helper.h b/modules/gdscript/gdscript_helper.hnew file mode 100644index 00000000000..0f8257c733b--- /dev/null+++ b/modules/gdscript/gdscript_helper.h@@ -0,0 +1,41 @@+/**************************************************************************/+/* gdscript_helper.h */+/**************************************************************************/+/* 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. */+/**************************************************************************/++#pragma once++#include "gdscript_function.h"++#include "core/variant/variant.h"++namespace GDScriptHelper {++Variant construct_empty_value_for_data_type(const GDScriptDataType &p_data_type);++};diff --git a/modules/gdscript/gdscript_vm.cpp b/modules/gdscript/gdscript_vm.cppindex 0699d9c07cb..da78d9a56c5 100644--- a/modules/gdscript/gdscript_vm.cpp+++ b/modules/gdscript/gdscript_vm.cpp@@ -30,6 +30,7 @@#include "gdscript.h"#include "gdscript_function.h"+#include "gdscript_helper.h"#include "gdscript_lambda_callable.h"#include "core/object/class_db.h"@@ -117,41 +118,6 @@ void GDScriptFunction::_profile_native_call(uint64_t p_t_taken, const String &p_#endif // DEBUG_ENABLED-Variant GDScriptFunction::_get_default_variant_for_data_type(const GDScriptDataType &p_data_type) {- if (p_data_type.kind == GDScriptDataType::BUILTIN) {- if (p_data_type.builtin_type == Variant::ARRAY) {- Array array;- // Typed array.- if (p_data_type.has_container_element_type(0)) {- const GDScriptDataType &element_type = p_data_type.get_container_element_type(0);- array.set_typed(element_type.builtin_type, element_type.native_type, element_type.script_type);- }-- return array;- } else if (p_data_type.builtin_type == Variant::DICTIONARY) {- Dictionary dict;- // Typed dictionary.- if (p_data_type.has_container_element_types()) {- const GDScriptDataType &key_type = p_data_type.get_container_element_type_or_variant(0);- const GDScriptDataType &value_type = p_data_type.get_container_element_type_or_variant(1);- dict.set_typed(key_type.builtin_type, key_type.native_type, key_type.script_type, value_type.builtin_type, value_type.native_type, value_type.script_type);- }-- return dict;- } else {- Callable::CallError ce;- Variant variant;- Variant::construct(p_data_type.builtin_type, variant, nullptr, 0, ce);-- ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, Variant());-- return variant;- }- }-- return Variant();-}-#ifdef DEBUG_ENABLEDString GDScriptFunction::_get_call_error(const String &p_where, const Variant **p_argptrs, int p_argcount, const Variant &p_ret, const Callable::CallError &p_err) const {switch (p_err.error) {@@ -502,7 +468,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_aOPCODES_TABLE;if (!_code_ptr) {- return _get_default_variant_for_data_type(return_type);+ return GDScriptHelper::construct_empty_value_for_data_type(return_type);}r_err.error = Callable::CallError::CALL_OK;@@ -524,7 +490,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a_err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text, false, ERR_HANDLER_SCRIPT);GDScriptLanguage::get_singleton()->debug_break(err_text, false);#endif- return _get_default_variant_for_data_type(return_type);+ return GDScriptHelper::construct_empty_value_for_data_type(return_type);}Variant retvalue;@@ -560,13 +526,13 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_ar_err.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;r_err.expected = _argument_count;call_depth--;- return _get_default_variant_for_data_type(return_type);+ return GDScriptHelper::construct_empty_value_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.expected = _argument_count - _default_arg_count;call_depth--;- return _get_default_variant_for_data_type(return_type);+ return GDScriptHelper::construct_empty_value_for_data_type(return_type);} else {defarg = _argument_count - p_argcount;}@@ -594,7 +560,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_ar_err.argument = i;r_err.expected = argument_types[i].builtin_type;call_depth--;- return _get_default_variant_for_data_type(return_type);+ return GDScriptHelper::construct_empty_value_for_data_type(return_type);}if (argument_types[i].kind == GDScriptDataType::BUILTIN) {if (argument_types[i].builtin_type == Variant::DICTIONARY && argument_types[i].has_container_element_types()) {@@ -614,7 +580,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_ar_err.argument = i;r_err.expected = argument_types[i].builtin_type;call_depth--;- return _get_default_variant_for_data_type(return_type);+ return GDScriptHelper::construct_empty_value_for_data_type(return_type);}memnew_placement(&stack[i + FIXED_ADDRESSES_MAX], Variant(variant));}@@ -4010,7 +3976,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_aGDScriptLanguage::get_singleton()->debug_break(err_text, false);// Get a default return type in case of failure- retvalue = _get_default_variant_for_data_type(return_type);+ retvalue = GDScriptHelper::construct_empty_value_for_data_type(return_type);#endifOPCODE_OUT;