Truly independent web browser
LibJS: Make absence cacheability a flag of the object method table
Andreas Klingc718c874e9dd
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs b/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rsindex cb9c85d2f78..d46021ede16 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/probes/property.rs@@ -285,10 +285,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {/// Branches to `miss` unless the object `object` has no other properties/// than those of its shape, for the names `GetMissingProperty` entries- /// can be for: only objects whose class keeps the default- /// `ObjectMethods::is_cacheable_for_property_absence` do. Plain objects,- /// ECMAScript functions, arrays and String objects are told apart- /// without loading the hook. Clobbers `class` and the scratch register.+ /// can be for: only objects whose class has+ /// `ObjectMethods::is_cacheable_for_property_absence` set do. Plain+ /// objects, ECMAScript functions, arrays and String objects are told+ /// apart without loading it. Clobbers `class` and the scratch register.pub(in crate::codegen) fn branch_unless_cacheable_for_property_absence(&mut self,object: Gpr,@@ -321,19 +321,16 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.masm.branch_test32(Condition::NonZero, scratch, u32::from(flags), cacheable);}- if layout.default_is_cacheable_for_property_absence == 0 || layout.class_object_methods == 0 {+ if layout.object_methods_is_cacheable_for_property_absence == 0 || layout.class_object_methods == 0 {self.masm.jump(miss);} else {self.masm.load64(shape, &Address::new(shape, layout.class_object_methods as i32));- self.masm- .move_imm64(scratch, layout.default_is_cacheable_for_property_absence);- self.masm.branch64_memory(- Condition::NotEqual,- &Address::new(shape, layout.object_methods_is_cacheable_for_property_absence as i32),+ self.masm.load8(scratch,- miss,+ &Address::new(shape, layout.object_methods_is_cacheable_for_property_absence as i32),);+ self.masm.branch_test32(Condition::Zero, scratch, 0xff, miss);}self.masm.bind(cacheable);}diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rsindex a7a92838061..cdd1b68892a 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs@@ -1437,7 +1437,6 @@ fn fake_object_layout() -> RuntimeLayout {class_object_methods: 8,object_methods_get_prototype_of: 0,object_methods_is_cacheable_for_property_absence: 0,- default_is_cacheable_for_property_absence: 0,// NB: Tests that look in the VM's keyed lookup cache set its entries.keyed_lookup_cache_entries: 0,keyed_store_cache_entries: 0,diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rsindex 8e9dfd14142..e2dd8a35557 100644--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs@@ -2088,11 +2088,10 @@ pub struct RuntimeLayout {/// `ObjectMethods::internal_get_prototype_of`, which is null for objects/// whose prototype is the prototype of their shape.pub object_methods_get_prototype_of: u32,- /// `ObjectMethods::is_cacheable_for_property_absence`, and the hook that- /// classes whose objects have no other properties than those of their- /// shape keep there, or 0 if JIT code does not check it.+ /// `ObjectMethods::is_cacheable_for_property_absence`, a bool that is+ /// true for classes whose objects have no other properties than those of+ /// their shape, or 0 if JIT code does not check it.pub object_methods_is_cacheable_for_property_absence: u32,- pub default_is_cacheable_for_property_absence: u64,/// The entries of the VM's keyed property lookup cache, which remembers/// lookups of names on shapes for keyed accesses whose own caches do not/// have them, or 0 if JIT code does not look in it. The entry for a namediff --git a/Libraries/LibJS/Rust/src/embedding/host/host_object.rs b/Libraries/LibJS/Rust/src/embedding/host/host_object.rsindex e26592c7dd3..635596000fd 100644--- a/Libraries/LibJS/Rust/src/embedding/host/host_object.rs+++ b/Libraries/LibJS/Rust/src/embedding/host/host_object.rs@@ -134,7 +134,7 @@ fn host_object_methods(table: &'static JSHostClass) -> ObjectMethods {methods.internal_own_property_keys = own_property_keys_through_hook;}if table.has_flag(JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE) {- methods.is_cacheable_for_property_absence = |_| false;+ methods.is_cacheable_for_property_absence = false;} else if table.has_flag(JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE) {methods.is_cacheable_for_absence_of = |_, _, property_key| !property_key.is_number();}diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rsindex db3aef21f8c..bdd4e269d02 100644--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs@@ -70,7 +70,7 @@ use crate::runtime::function_environment::{};use crate::runtime::generator_object::GeneratorObject;use crate::runtime::module_environment::ModuleEnvironment;-use crate::runtime::object::{ORDINARY_OBJECT_METHODS, ObjectMethods, PropertyKind};+use crate::runtime::object::{ObjectMethods, PropertyKind};use crate::runtime::string_object::StringObject;use crate::runtime::string_prototype::StringPrototype;@@ -375,8 +375,6 @@ fn runtime_layout() -> RuntimeLayout {ObjectMethods,is_cacheable_for_property_absence)),- default_is_cacheable_for_property_absence: ORDINARY_OBJECT_METHODS.is_cacheable_for_property_absence as usize- as u64,property_lookup_cache_polymorphic_tag: PROPERTY_LOOKUP_CACHE_POLYMORPHIC_DATA_TAG as u64,property_lookup_cache_polymorphic_entry_count: MAX_NUMBER_OF_SHAPES_TO_REMEMBER as u32,property_lookup_cache_entry_size: offset(size_of::<PropertyLookupCacheEntry>()),@@ -413,6 +411,9 @@ const _: () = assert!(== crate::layout::execution_context::ExecutionContext::NO_YIELD_CONTINUATION);+// NB: JIT code takes an offset of 0 to mean that there is no such field for it to read.+const _: () = assert!(offset_of!(ObjectMethods, is_cacheable_for_property_absence) != 0);+// NB: The compiler has its own copy of the bits of the interpreter's feedback.const _: () = {assert!(libjs_jit::snapshot::value_feedback::INT32 == crate::layout::feedback::value_feedback::INT32);diff --git a/Libraries/LibJS/Rust/src/runtime/module_namespace_object.rs b/Libraries/LibJS/Rust/src/runtime/module_namespace_object.rsindex 8b360f280cf..f02d1be8a3c 100644--- a/Libraries/LibJS/Rust/src/runtime/module_namespace_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/module_namespace_object.rs@@ -48,7 +48,7 @@ pub static MODULE_NAMESPACE_OBJECT_METHODS: ObjectMethods = ObjectMethods {internal_define_own_property: ModuleNamespaceObject::internal_define_own_property,internal_has_property: ModuleNamespaceObject::internal_has_property,internal_get: ModuleNamespaceObject::internal_get,- is_cacheable_for_property_absence: |_| false,+ is_cacheable_for_property_absence: false,internal_set: ModuleNamespaceObject::internal_set,internal_delete: ModuleNamespaceObject::internal_delete,internal_own_property_keys: ModuleNamespaceObject::internal_own_property_keys,diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rsindex f3fb439f2e4..5b14cb73f23 100644--- a/Libraries/LibJS/Rust/src/runtime/object.rs+++ b/Libraries/LibJS/Rust/src/runtime/object.rs@@ -239,7 +239,9 @@ pub struct ObjectMethods {pub name_for_call_stack: fn(&Object) -> Utf16String,/// The virtual methods of NativeFunction, which only native functions have.pub native_function: Option<&'static NativeFunctionMethods>,- pub is_cacheable_for_property_absence: fn(&Object) -> bool,+ /// Whether the absence of a property from the shape of an object of this class may be cached: false for classes+ /// whose objects have other properties than those of their shape. JIT code reads it (see RuntimeLayout).+ pub is_cacheable_for_property_absence: bool,/// Whether the absence of a property from objects of this object's shape may be cached, for objects that answer/// for a few names whatever their shape holds.pub is_cacheable_for_absence_of: fn(&Object, &Vm, &PropertyKey) -> bool,@@ -283,7 +285,7 @@ pub static ORDINARY_OBJECT_METHODS: ObjectMethods = ObjectMethods {function_realm: |_| None,name_for_call_stack: |_| unreachable!("FunctionObject::name_for_call_stack is pure virtual"),native_function: None,- is_cacheable_for_property_absence: |_| true,+ is_cacheable_for_property_absence: true,is_cacheable_for_absence_of: |object, _, _| object.is_cacheable_for_property_absence(),is_cacheable_for_inherited_property: |_| true,is_cacheable_across_realms: |_| true,@@ -2055,7 +2057,7 @@ impl Object {}pub fn is_cacheable_for_property_absence(&self) -> bool {- (self.methods().is_cacheable_for_property_absence)(self)+ self.methods().is_cacheable_for_property_absence}pub fn is_cacheable_for_absence_of(&self, vm: &Vm, property_key: &PropertyKey) -> bool {diff --git a/Libraries/LibJS/Rust/src/runtime/proxy_object.rs b/Libraries/LibJS/Rust/src/runtime/proxy_object.rsindex 6b6dcb290d5..6641eea2642 100644--- a/Libraries/LibJS/Rust/src/runtime/proxy_object.rs+++ b/Libraries/LibJS/Rust/src/runtime/proxy_object.rs@@ -99,7 +99,7 @@ pub static PROXY_OBJECT_METHODS: ObjectMethods = ObjectMethods {internal_define_own_property: ProxyObject::internal_define_own_property,internal_has_property: ProxyObject::internal_has_property,internal_get: ProxyObject::internal_get,- is_cacheable_for_property_absence: |_| false,+ is_cacheable_for_property_absence: false,internal_set: ProxyObject::internal_set,internal_delete: ProxyObject::internal_delete,internal_own_property_keys: ProxyObject::internal_own_property_keys,diff --git a/Tests/LibJS/Runtime/jit/missing-properties.js b/Tests/LibJS/Runtime/jit/missing-properties.jsindex 8388f312439..4c536e11502 100644--- a/Tests/LibJS/Runtime/jit/missing-properties.js+++ b/Tests/LibJS/Runtime/jit/missing-properties.js@@ -1,5 +1,6 @@// JIT code answers reads of properties that property caches remember as missing-// without calling into the runtime, for plain objects and for ECMAScript functions.+// without calling into the runtime, for objects that have no other properties than+// those of their shape.// These tests run every function many times, so that with a low JIT threshold they// are compiled.@@ -50,4 +51,35 @@ describe("missing properties in JIT code", () => {expect(readPrototype(lazy).constructor).toBe(lazy);}});++ test("objects of other classes", () => {+ const set = new Set();+ const iterator = [].values();+ for (let i = 0; i < iterations; ++i) {+ expect(readDefaultProps(set)).toBeUndefined();+ expect(readDefaultProps(iterator)).toBeUndefined();+ }+ Set.prototype.defaultProps = "inherited";+ try {+ expect(readDefaultProps(set)).toBe("inherited");+ expect(readDefaultProps(iterator)).toBeUndefined();+ } finally {+ delete Set.prototype.defaultProps;+ }+ expect(readDefaultProps(set)).toBeUndefined();+ });++ test("a proxy with the shape of an object that the property is missing from", () => {+ function read(value) {+ return value.defaultProps;+ }+ // This object and the proxy have the same shape: no properties, and+ // Object.prototype as a prototype that the empty shape was given later.+ const plain = Object.setPrototypeOf(Object.create(null), Object.prototype);+ const proxy = new Proxy({}, { get: (target, key) => `trap ${key}` });+ for (let i = 0; i < iterations; ++i) {+ expect(read(plain)).toBeUndefined();+ expect(read(proxy)).toBe("trap defaultProps");+ }+ });});