You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
Merge pull request #74730 from MarioLiebisch/fix-74726
Fix read-only dictionaries adding missing keys
This commit is contained in:
@@ -83,9 +83,16 @@ Variant &Dictionary::operator[](const Variant &p_key) {
|
|||||||
if (unlikely(_p->read_only)) {
|
if (unlikely(_p->read_only)) {
|
||||||
if (p_key.get_type() == Variant::STRING_NAME) {
|
if (p_key.get_type() == Variant::STRING_NAME) {
|
||||||
const StringName *sn = VariantInternal::get_string_name(&p_key);
|
const StringName *sn = VariantInternal::get_string_name(&p_key);
|
||||||
*_p->read_only = _p->variant_map[sn->operator String()];
|
const String &key = sn->operator String();
|
||||||
} else {
|
if (likely(_p->variant_map.has(key))) {
|
||||||
|
*_p->read_only = _p->variant_map[key];
|
||||||
|
} else {
|
||||||
|
*_p->read_only = Variant();
|
||||||
|
}
|
||||||
|
} else if (likely(_p->variant_map.has(p_key))) {
|
||||||
*_p->read_only = _p->variant_map[p_key];
|
*_p->read_only = _p->variant_map[p_key];
|
||||||
|
} else {
|
||||||
|
*_p->read_only = Variant();
|
||||||
}
|
}
|
||||||
|
|
||||||
return *_p->read_only;
|
return *_p->read_only;
|
||||||
|
|||||||
@@ -88,6 +88,12 @@ TEST_CASE("[Dictionary] Assignment using bracket notation ([])") {
|
|||||||
CHECK(int(map[0]) == 400);
|
CHECK(int(map[0]) == 400);
|
||||||
// Check that assigning 0 doesn't overwrite the value for `false`.
|
// Check that assigning 0 doesn't overwrite the value for `false`.
|
||||||
CHECK(int(map[false]) == 128);
|
CHECK(int(map[false]) == 128);
|
||||||
|
|
||||||
|
// Ensure read-only maps aren't modified by non-existing keys.
|
||||||
|
const auto length = map.size();
|
||||||
|
map.make_read_only();
|
||||||
|
CHECK(int(map["This key does not exist"].get_type()) == Variant::NIL);
|
||||||
|
CHECK(map.size() == length);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("[Dictionary] get_key_lists()") {
|
TEST_CASE("[Dictionary] get_key_lists()") {
|
||||||
|
|||||||
Reference in New Issue
Block a user