Truly independent web browser
LibJS: Replace a dictionary shape before its generation wraps around
Tim Ledbettera74c85aa4ec0
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rsindex ec19c48ed91..cbe48165b7b 100644--- a/Libraries/LibJS/Rust/src/runtime/object.rs+++ b/Libraries/LibJS/Rust/src/runtime/object.rs@@ -784,6 +784,13 @@ impl Object {self.shape.set(self.shape().create_dictionary_transition(vm));}+ fn dictionary_shape_to_change_in_place(&self, vm: &Vm) -> Gc<Shape> {+ if self.shape().dictionary_generation() == u32::MAX {+ self.invalidate_property_lookup_caches(vm);+ }+ self.shape()+ }+// 7.2 Testing and Comparison Operations, https://tc39.es/ecma262/#sec-testing-and-comparison-operations// 7.2.5 IsExtensible ( O ), https://tc39.es/ecma262/#sec-isextensible-o@@ -2868,7 +2875,8 @@ impl Object {if attributes != metadata.attributes {let shape = self.shape();if shape.is_dictionary() {- shape.set_property_attributes_without_transition(vm, property_key, attributes);+ self.dictionary_shape_to_change_in_place(vm)+ .set_property_attributes_without_transition(vm, property_key, attributes);} else {self.shape.set(shape.create_configure_transition(vm, property_key, attributes));@@ -2902,7 +2910,8 @@ impl Object {let shape = self.shape();if shape.is_dictionary() {- shape.add_property_without_transition(vm, property_key, attributes);+ self.dictionary_shape_to_change_in_place(vm)+ .add_property_without_transition(vm, property_key, attributes);} else {self.shape.set(shape.create_put_transition(vm, property_key, attributes));@@ -2938,7 +2947,8 @@ impl Object {let shape = self.shape();if shape.is_dictionary() {- shape.remove_property_without_transition(vm, property_key, metadata.offset);+ self.dictionary_shape_to_change_in_place(vm)+ .remove_property_without_transition(vm, property_key, metadata.offset);} else {self.shape.set(shape.create_delete_transition(vm, property_key));}diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rsindex a063008496a..4cfb61ff229 100644--- a/Libraries/LibJS/Rust/src/runtime/shape.rs+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs@@ -907,8 +907,9 @@ impl Shape {}fn increment_dictionary_generation(&self) {- self.dictionary_generation- .set(self.dictionary_generation.get().wrapping_add(1));+ let generation = self.dictionary_generation.get();+ assert!(generation < u32::MAX);+ self.dictionary_generation.set(generation + 1);}pub fn clone_for_prototype(&self, vm: &Vm) -> Gc<Shape> {diff --git a/Libraries/LibJS/Rust/src/utilities/test_js/test_js_functions.rs b/Libraries/LibJS/Rust/src/utilities/test_js/test_js_functions.rsindex 8cdd9ad74de..64bdf3c2a96 100644--- a/Libraries/LibJS/Rust/src/utilities/test_js/test_js_functions.rs+++ b/Libraries/LibJS/Rust/src/utilities/test_js/test_js_functions.rs@@ -44,6 +44,7 @@ use crate::utf16::Utf16View;pub const EXPOSED_GLOBAL_FUNCTIONS: &[(&str, RawNativeFunctionPointer)] = &[("canParseSource", raw_native!(can_parse_source)),("haveSameShape", raw_native!(have_same_shape)),+ ("setDictionaryGeneration", raw_native!(set_dictionary_generation)),("gc", raw_native!(collect_garbage)),("collectGarbageOnEveryAllocation",@@ -93,6 +94,15 @@ fn have_same_shape(vm: &Vm) -> ThrowCompletionOr<Value> {Ok(Value::from_bool(lhs.as_object().shape() == rhs.as_object().shape()))}+fn set_dictionary_generation(vm: &Vm) -> ThrowCompletionOr<Value> {+ let shape = vm.argument(0).to_object(vm)?.shape();+ if !shape.is_dictionary() {+ return vm.throw_completion(ErrorKind::TypeError, ErrorType::NotAnObjectOfType, &[&"dictionary"]);+ }+ shape.dictionary_generation.set(vm.argument(1).to_u32(vm)?);+ Ok(Value::UNDEFINED)+}+fn can_parse_source(vm: &Vm) -> ThrowCompletionOr<Value> {let realm = vm.current_realm().expect("canParseSource runs in a realm");let source = vm.argument(0).to_utf16_string(vm)?;diff --git a/Tests/LibJS/Runtime/dictionary-delete-caching.js b/Tests/LibJS/Runtime/dictionary-delete-caching.jsindex ae896724eb1..3360f17c104 100644--- a/Tests/LibJS/Runtime/dictionary-delete-caching.js+++ b/Tests/LibJS/Runtime/dictionary-delete-caching.js@@ -163,4 +163,23 @@ describe("dictionary mode objects remain cacheable after property deletion", ()expect(Object.getOwnPropertyDescriptor(obj, "prop24").value).toBe(24);expect(Object.getOwnPropertyDescriptor(obj, "prop26").value).toBe(26);});++ test("delete after the dictionary generation reaches its limit", () => {+ let obj = makeDictionaryObject();+ obj.last = "value";+ setDictionaryGeneration(obj, 1000);++ function readLast(object) {+ return object.last;+ }+ expect(readLast(obj)).toBe("value");++ setDictionaryGeneration(obj, 2 ** 32 - 1);+ delete obj.last;+ for (let i = 0; i < 999; i++) {+ Object.defineProperty(obj, "prop0", { enumerable: i % 2 === 1 });+ }+ obj.other = "other";+ expect(readLast(obj)).toBeUndefined();+ });});