Skip to content

Truly independent web browser

LibJS: Cache missing properties of String objects and arrays

Tim Ledbetterfd07f06fb6c1
diff --git a/Libraries/LibJS/Rust/src/runtime/array.rs b/Libraries/LibJS/Rust/src/runtime/array.rs
index 96670983151..228f4a2ef27 100644
--- a/Libraries/LibJS/Rust/src/runtime/array.rs
+++ b/Libraries/LibJS/Rust/src/runtime/array.rs
@@ -48,7 +48,7 @@ pub const ARRAY_IS_PROXY_TARGET_OFFSET: usize = core::mem::offset_of!(Array, is_
pub static ARRAY_OBJECT_METHODS: ObjectMethods = ObjectMethods {
internal_get_own_property: Array::internal_get_own_property,
- is_cacheable_for_property_absence: |_| false,
+ is_cacheable_for_absence_of: |_, vm, property_key| !property_key.is_number() && *property_key != vm.names.length,
internal_set: Array::internal_set,
internal_define_own_property: Array::internal_define_own_property,
internal_has_property: Array::internal_has_property,
@@ -85,12 +85,16 @@ impl Array {
/// An array with `prototype`, for `class`, which is Array or a class that extends it.
pub fn new_with_class(vm: &Vm, class: &'static Class, realm: Gc<Realm>, prototype: Gc<Object>) -> Self {
- let premade_shape = realm
- .intrinsics()
- .new_array_shape_if_created()
- .filter(|shape| shape.prototype() == Some(prototype));
+ let premade_shape = realm.intrinsics().new_array_shape_if_created();
let base = match premade_shape {
- Some(shape) => Object::new_with_shape(class, shape, MayInterfereWithIndexedPropertyAccess::No),
+ Some(shape) if shape.prototype() == Some(prototype) => {
+ Object::new_with_shape(class, shape, MayInterfereWithIndexedPropertyAccess::No)
+ }
+ Some(shape) => Object::new_with_shape(
+ class,
+ shape.create_prototype_transition(vm, Some(prototype)),
+ MayInterfereWithIndexedPropertyAccess::No,
+ ),
None => Object::new_with_prototype(vm, class, prototype, MayInterfereWithIndexedPropertyAccess::No),
};
let array = Self {
diff --git a/Libraries/LibJS/Rust/src/runtime/string_object.rs b/Libraries/LibJS/Rust/src/runtime/string_object.rs
index 47452817fed..66456b9453b 100644
--- a/Libraries/LibJS/Rust/src/runtime/string_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/string_object.rs
@@ -36,7 +36,7 @@ pub struct StringObject {
pub static STRING_OBJECT_METHODS: ObjectMethods = ObjectMethods {
initialize: StringObject::initialize,
internal_get_own_property: StringObject::internal_get_own_property,
- is_cacheable_for_property_absence: |_| false,
+ is_cacheable_for_absence_of: |_, _, property_key| !property_key.is_number(),
internal_define_own_property: StringObject::internal_define_own_property,
internal_own_property_keys: StringObject::internal_own_property_keys,
eligible_for_own_property_enumeration_fast_path: |_| false,
diff --git a/Tests/LibJS/Runtime/inline-cache-edge-cases.js b/Tests/LibJS/Runtime/inline-cache-edge-cases.js
index 0b704a4366b..f764263691f 100644
--- a/Tests/LibJS/Runtime/inline-cache-edge-cases.js
+++ b/Tests/LibJS/Runtime/inline-cache-edge-cases.js
@@ -178,3 +178,39 @@ describe("Cached inherited setter", () => {
expect(receiver.x).toBe(2);
});
});
+
+describe("Cached missing property", () => {
+ function expectToBeFoundOnceAdded(read, receiver, holder) {
+ expect(read(receiver)).toBeUndefined();
+ expect(read(receiver)).toBeUndefined();
+ holder.defaultProps = "added";
+ try {
+ expect(read(receiver)).toBe("added");
+ } finally {
+ delete holder.defaultProps;
+ }
+ expect(read(receiver)).toBeUndefined();
+ }
+
+ test("of a string is found once a prototype has it", () => {
+ const read = value => value.defaultProps;
+ expectToBeFoundOnceAdded(read, "path", String.prototype);
+ expectToBeFoundOnceAdded(read, "path", Object.prototype);
+ });
+
+ test("of a String object is found once it or a prototype has it", () => {
+ const read = value => value.defaultProps;
+ const string = new String("path");
+ expectToBeFoundOnceAdded(read, string, string);
+ expectToBeFoundOnceAdded(read, string, String.prototype);
+ expectToBeFoundOnceAdded(read, string, Object.prototype);
+ });
+
+ test("of an array is found once it or a prototype has it", () => {
+ const read = value => value.defaultProps;
+ const array = [1, 2];
+ expectToBeFoundOnceAdded(read, array, array);
+ expectToBeFoundOnceAdded(read, array, Array.prototype);
+ expectToBeFoundOnceAdded(read, array, Object.prototype);
+ });
+});