From 4c14051b3f3355e490fc9c2dbd3e08269b6d90fe Mon Sep 17 00:00:00 2001 From: George Marques Date: Thu, 14 Oct 2021 11:09:47 -0300 Subject: [PATCH] Zero Dictionary and Array variants when changing type with reset So they don't reference to the old values anymore and instead refer to a new value. --- core/variant/variant_internal.h | 4 +-- .../runtime/features/arrays_arent_shared.gd | 32 +++++++++++++++++++ .../runtime/features/arrays_arent_shared.out | 6 ++++ .../features/dictionaries_arent_shared.gd | 19 +++++++++++ .../features/dictionaries_arent_shared.out | 3 ++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 modules/gdscript/tests/scripts/runtime/features/arrays_arent_shared.gd create mode 100644 modules/gdscript/tests/scripts/runtime/features/arrays_arent_shared.out create mode 100644 modules/gdscript/tests/scripts/runtime/features/dictionaries_arent_shared.gd create mode 100644 modules/gdscript/tests/scripts/runtime/features/dictionaries_arent_shared.out diff --git a/core/variant/variant_internal.h b/core/variant/variant_internal.h index 40c8a1bfde0..37383ff2ecd 100644 --- a/core/variant/variant_internal.h +++ b/core/variant/variant_internal.h @@ -1301,12 +1301,12 @@ struct VariantZeroAssigner { template <> struct VariantZeroAssigner { - static _FORCE_INLINE_ void zero(Variant *v) {} + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_dictionary(v) = Dictionary(); } }; template <> struct VariantZeroAssigner { - static _FORCE_INLINE_ void zero(Variant *v) {} + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_array(v) = Array(); } }; template <> diff --git a/modules/gdscript/tests/scripts/runtime/features/arrays_arent_shared.gd b/modules/gdscript/tests/scripts/runtime/features/arrays_arent_shared.gd new file mode 100644 index 00000000000..18174eae674 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/arrays_arent_shared.gd @@ -0,0 +1,32 @@ +# https://github.com/godotengine/godot/issues/48121 + +func test(): + var x := [] + var y := [] + x.push_back(y) + print("TEST ARRAY ADD TO SELF: " + str(len(y))) + x.clear() + + x = Array() + y = Array() + x.push_back(y) + print("TEST ARRAY ADD TO SELF: " + str(len(y))) + x.clear() + + x = Array().duplicate() + y = Array().duplicate() + x.push_back(y) + print("TEST ARRAY ADD TO SELF: " + str(len(y))) + x.clear() + + x = [].duplicate() + y = [].duplicate() + x.push_back(y) + print("TEST ARRAY ADD TO SELF: " + str(len(y))) + x.clear() + + x = Array() + y = Array() + x.push_back(y) + print("TEST ARRAY ADD TO SELF: " + str(len(y))) + x.clear() diff --git a/modules/gdscript/tests/scripts/runtime/features/arrays_arent_shared.out b/modules/gdscript/tests/scripts/runtime/features/arrays_arent_shared.out new file mode 100644 index 00000000000..f6b7d3cc399 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/arrays_arent_shared.out @@ -0,0 +1,6 @@ +GDTEST_OK +TEST ARRAY ADD TO SELF: 0 +TEST ARRAY ADD TO SELF: 0 +TEST ARRAY ADD TO SELF: 0 +TEST ARRAY ADD TO SELF: 0 +TEST ARRAY ADD TO SELF: 0 diff --git a/modules/gdscript/tests/scripts/runtime/features/dictionaries_arent_shared.gd b/modules/gdscript/tests/scripts/runtime/features/dictionaries_arent_shared.gd new file mode 100644 index 00000000000..d5a5f8de64a --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/dictionaries_arent_shared.gd @@ -0,0 +1,19 @@ +# https://github.com/godotengine/godot/issues/48121 + +func test(): + var x := Dictionary() + var y := Dictionary() + y[0]=1 + y[1]=1 + y[2]=1 + print("TEST OTHER DICTIONARY: " + str(len(x))) + x.clear() + + x = Dictionary().duplicate() + y = Dictionary().duplicate() + y[0]=1 + y[1]=1 + y[2]=1 + print("TEST OTHER DICTIONARY: " + str(len(x))) + x.clear() + return diff --git a/modules/gdscript/tests/scripts/runtime/features/dictionaries_arent_shared.out b/modules/gdscript/tests/scripts/runtime/features/dictionaries_arent_shared.out new file mode 100644 index 00000000000..0bf49f5934b --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/dictionaries_arent_shared.out @@ -0,0 +1,3 @@ +GDTEST_OK +TEST OTHER DICTIONARY: 0 +TEST OTHER DICTIONARY: 0