Skip to content

Truly independent web browser

LibJS: Don’t cache the absence of an indexed property on a shape

sideshowbarkerf458b1817f5e
diff --git a/Libraries/LibJS/Rust/src/bytecode/property_access.rs b/Libraries/LibJS/Rust/src/bytecode/property_access.rs
index ee4b73d5e5a..a4ba5398770 100644
--- a/Libraries/LibJS/Rust/src/bytecode/property_access.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/property_access.rs
@@ -138,7 +138,9 @@ pub fn get_by_value_with_keyed_cache(
let shape_key = ShapeKey::of(shape);
let mut cacheable_metadata = CacheableGetPropertyMetadata {
- property_absence_is_cacheable: base_object.is_cacheable_for_absence_of(vm, property_key),
+ // NB: Indexed properties live outside the shape, so a shape can't vouch for the absence of one.
+ property_absence_is_cacheable: !property_key.is_number()
+ && base_object.is_cacheable_for_absence_of(vm, property_key),
..Default::default()
};
let value = base_object.internal_get(
@@ -394,7 +396,9 @@ pub fn get_with_property_lookup_cache(
let shape_key = ShapeKey::of(shape);
let mut cacheable_metadata = CacheableGetPropertyMetadata {
- property_absence_is_cacheable: base_obj.is_cacheable_for_absence_of(vm, property_name),
+ // NB: Indexed properties live outside the shape, so a shape can't vouch for the absence of one.
+ property_absence_is_cacheable: !property_name.is_number()
+ && base_obj.is_cacheable_for_absence_of(vm, property_name),
..Default::default()
};
let value = base_obj.internal_get(
diff --git a/Tests/LibJS/Runtime/destructuring-index-key-after-a-miss.js b/Tests/LibJS/Runtime/destructuring-index-key-after-a-miss.js
new file mode 100644
index 00000000000..ef2132fd44d
--- /dev/null
+++ b/Tests/LibJS/Runtime/destructuring-index-key-after-a-miss.js
@@ -0,0 +1,73 @@
+// A miss on one object must not answer for an indexed property of the next object with the same shape, since indexed
+// properties live outside the shape.
+test("object pattern with a numeric key", () => {
+ function second(o) {
+ const { 1: y } = o;
+ return y;
+ }
+ expect(second({ a: 1 })).toBeUndefined();
+ expect(second({ a: 2, 1: "y" })).toBe("y");
+});
+
+test("object pattern with a string key that is an array index", () => {
+ function second(o) {
+ // prettier-ignore
+ const { "1": y } = o;
+ return y;
+ }
+ expect(second({ a: 1 })).toBeUndefined();
+ expect(second({ a: 2, 1: "y" })).toBe("y");
+});
+
+test("destructuring assignment with a numeric key", () => {
+ function second(o) {
+ let y;
+ ({ 1: y } = o);
+ return y;
+ }
+ expect(second({ a: 1 })).toBeUndefined();
+ expect(second({ a: 2, 1: "y" })).toBe("y");
+});
+
+test("a hit after a miss", () => {
+ function second(o) {
+ const { 1: y } = o;
+ return y;
+ }
+ expect(second({ a: 2, 1: "y" })).toBe("y");
+ expect(second({ a: 1 })).toBeUndefined();
+ expect(second({ a: 2, 1: "y" })).toBe("y");
+});
+
+test("an indexed property added to the prototype after a miss", () => {
+ function second(o) {
+ const { 1: y } = o;
+ return y;
+ }
+ const prototype = {};
+ const object = Object.create(prototype);
+ expect(second(object)).toBeUndefined();
+ prototype[1] = "y";
+ expect(second(object)).toBe("y");
+});
+
+test("function objects", () => {
+ function second(o) {
+ const { 1: y } = o;
+ return y;
+ }
+ function f() {}
+ function g() {}
+ Object.setPrototypeOf(f, null);
+ Object.setPrototypeOf(g, null);
+ g[1] = "y";
+ expect(second(f)).toBeUndefined();
+ expect(second(g)).toBe("y");
+});
+
+test("setting an index through a deep prototype chain", () => {
+ let object = {};
+ for (let i = 0; i < 100_000; ++i) object = Object.create(object);
+ object[1] = "y";
+ expect(object[1]).toBe("y");
+});