Pārlūkot izejas kodu

LibJS: Make Object::internal_get() reveal the used property offset

This function now takes an optional out parameter for callers who would
like to what kind of property we ended up getting.

This will be used to implement inline caching for property lookups.

Also, to prepare for adding more forms of caching, the out parameter
is a struct CacheablePropertyMetadata rather than just an offset. :^)
Andreas Kling 2 gadi atpakaļ
vecāks
revīzija
52cd671163

+ 1 - 1
Userland/Applications/Spreadsheet/JSIntegration.cpp

@@ -109,7 +109,7 @@ JS::ThrowCompletionOr<bool> SheetGlobalObject::internal_has_property(JS::Propert
     return Object::internal_has_property(name);
 }
 
-JS::ThrowCompletionOr<JS::Value> SheetGlobalObject::internal_get(const JS::PropertyKey& property_name, JS::Value receiver) const
+JS::ThrowCompletionOr<JS::Value> SheetGlobalObject::internal_get(const JS::PropertyKey& property_name, JS::Value receiver, JS::CacheablePropertyMetadata*) const
 {
     if (property_name.is_string()) {
         if (property_name.as_string() == "value") {

+ 1 - 1
Userland/Applications/Spreadsheet/JSIntegration.h

@@ -28,7 +28,7 @@ public:
     virtual ~SheetGlobalObject() override = default;
 
     virtual JS::ThrowCompletionOr<bool> internal_has_property(JS::PropertyKey const& name) const override;
-    virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver) const override;
+    virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver, JS::CacheablePropertyMetadata*) const override;
     virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver) override;
 
     JS_DECLARE_NATIVE_FUNCTION(get_real_cell_contents);

+ 2 - 2
Userland/Libraries/LibJS/Runtime/ArgumentsObject.cpp

@@ -33,7 +33,7 @@ void ArgumentsObject::visit_edges(Cell::Visitor& visitor)
 }
 
 // 10.4.4.3 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-get-p-receiver
-ThrowCompletionOr<Value> ArgumentsObject::internal_get(PropertyKey const& property_key, Value receiver) const
+ThrowCompletionOr<Value> ArgumentsObject::internal_get(PropertyKey const& property_key, Value receiver, CacheablePropertyMetadata* cacheable_metadata) const
 {
     // 1. Let map be args.[[ParameterMap]].
     auto& map = *m_parameter_map;
@@ -44,7 +44,7 @@ ThrowCompletionOr<Value> ArgumentsObject::internal_get(PropertyKey const& proper
     // 3. If isMapped is false, then
     if (!is_mapped) {
         // a. Return ? OrdinaryGet(args, P, Receiver).
-        return Object::internal_get(property_key, receiver);
+        return Object::internal_get(property_key, receiver, cacheable_metadata);
     }
 
     // FIXME: a. Assert: map contains a formal parameter mapping for P.

+ 1 - 1
Userland/Libraries/LibJS/Runtime/ArgumentsObject.h

@@ -23,7 +23,7 @@ public:
 
     virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyKey const&) const override;
     virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyKey const&, PropertyDescriptor const&) override;
-    virtual ThrowCompletionOr<Value> internal_get(PropertyKey const&, Value receiver) const override;
+    virtual ThrowCompletionOr<Value> internal_get(PropertyKey const&, Value receiver, CacheablePropertyMetadata*) const override;
     virtual ThrowCompletionOr<bool> internal_set(PropertyKey const&, Value value, Value receiver) override;
     virtual ThrowCompletionOr<bool> internal_delete(PropertyKey const&) override;
 

+ 2 - 0
Userland/Libraries/LibJS/Runtime/IndexedProperties.h

@@ -15,6 +15,8 @@ namespace JS {
 struct ValueAndAttributes {
     Value value;
     PropertyAttributes attributes { default_attributes };
+
+    Optional<u32> property_offset {};
 };
 
 class IndexedProperties;

+ 2 - 2
Userland/Libraries/LibJS/Runtime/ModuleNamespaceObject.cpp

@@ -137,14 +137,14 @@ ThrowCompletionOr<bool> ModuleNamespaceObject::internal_has_property(PropertyKey
 }
 
 // 10.4.6.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-module-namespace-exotic-objects-get-p-receiver
-ThrowCompletionOr<Value> ModuleNamespaceObject::internal_get(PropertyKey const& property_key, Value receiver) const
+ThrowCompletionOr<Value> ModuleNamespaceObject::internal_get(PropertyKey const& property_key, Value receiver, CacheablePropertyMetadata* cacheable_metadata) const
 {
     auto& vm = this->vm();
 
     // 1. If Type(P) is Symbol, then
     if (property_key.is_symbol()) {
         // a. Return ! OrdinaryGet(O, P, Receiver).
-        return MUST(Object::internal_get(property_key, receiver));
+        return MUST(Object::internal_get(property_key, receiver, cacheable_metadata));
     }
 
     // 2. Let exports be O.[[Exports]].

+ 1 - 1
Userland/Libraries/LibJS/Runtime/ModuleNamespaceObject.h

@@ -25,7 +25,7 @@ public:
     virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyKey const&) const override;
     virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyKey const&, PropertyDescriptor const&) override;
     virtual ThrowCompletionOr<bool> internal_has_property(PropertyKey const&) const override;
-    virtual ThrowCompletionOr<Value> internal_get(PropertyKey const&, Value receiver) const override;
+    virtual ThrowCompletionOr<Value> internal_get(PropertyKey const&, Value receiver, CacheablePropertyMetadata* = nullptr) const override;
     virtual ThrowCompletionOr<bool> internal_set(PropertyKey const&, Value value, Value receiver) override;
     virtual ThrowCompletionOr<bool> internal_delete(PropertyKey const&) override;
     virtual ThrowCompletionOr<MarkedVector<Value>> internal_own_property_keys() const override;

+ 18 - 5
Userland/Libraries/LibJS/Runtime/Object.cpp

@@ -764,7 +764,7 @@ ThrowCompletionOr<Optional<PropertyDescriptor>> Object::internal_get_own_propert
     PropertyDescriptor descriptor;
 
     // 3. Let X be O's own property whose key is P.
-    auto [value, attributes] = *maybe_storage_entry;
+    auto [value, attributes, property_offset] = *maybe_storage_entry;
 
     // 4. If X is a data property, then
     if (!value.is_accessor()) {
@@ -791,6 +791,9 @@ ThrowCompletionOr<Optional<PropertyDescriptor>> Object::internal_get_own_propert
     // 7. Set D.[[Configurable]] to the value of X's [[Configurable]] attribute.
     descriptor.configurable = attributes.is_configurable();
 
+    // Non-standard: Add the property offset to the descriptor. This is used to populate CacheablePropertyMetadata.
+    descriptor.property_offset = property_offset;
+
     // 8. Return D.
     return descriptor;
 }
@@ -836,7 +839,7 @@ ThrowCompletionOr<bool> Object::internal_has_property(PropertyKey const& propert
 }
 
 // 10.1.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-get-p-receiver
-ThrowCompletionOr<Value> Object::internal_get(PropertyKey const& property_key, Value receiver) const
+ThrowCompletionOr<Value> Object::internal_get(PropertyKey const& property_key, Value receiver, CacheablePropertyMetadata* cacheable_metadata) const
 {
     VERIFY(!receiver.is_empty());
     VERIFY(property_key.is_valid());
@@ -860,8 +863,16 @@ ThrowCompletionOr<Value> Object::internal_get(PropertyKey const& property_key, V
     }
 
     // 3. If IsDataDescriptor(desc) is true, return desc.[[Value]].
-    if (descriptor->is_data_descriptor())
+    if (descriptor->is_data_descriptor()) {
+        // Non-standard: If the caller has requested cacheable metadata and the property is an own property, fill it in.
+        if (cacheable_metadata && descriptor->property_offset.has_value()) {
+            *cacheable_metadata = CacheablePropertyMetadata {
+                .type = CacheablePropertyMetadata::Type::OwnProperty,
+                .property_offset = descriptor->property_offset.value(),
+            };
+        }
         return *descriptor->value;
+    }
 
     // 4. Assert: IsAccessorDescriptor(desc) is true.
     VERIFY(descriptor->is_accessor_descriptor());
@@ -1075,6 +1086,7 @@ Optional<ValueAndAttributes> Object::storage_get(PropertyKey const& property_key
 
     Value value;
     PropertyAttributes attributes;
+    Optional<u32> property_offset;
 
     if (property_key.is_number()) {
         auto value_and_attributes = m_indexed_properties.get(property_key.as_number());
@@ -1094,9 +1106,10 @@ Optional<ValueAndAttributes> Object::storage_get(PropertyKey const& property_key
 
         value = m_storage[metadata->offset];
         attributes = metadata->attributes;
+        property_offset = metadata->offset;
     }
 
-    return ValueAndAttributes { .value = value, .attributes = attributes };
+    return ValueAndAttributes { .value = value, .attributes = attributes, .property_offset = property_offset };
 }
 
 bool Object::storage_has(PropertyKey const& property_key) const
@@ -1111,7 +1124,7 @@ void Object::storage_set(PropertyKey const& property_key, ValueAndAttributes con
 {
     VERIFY(property_key.is_valid());
 
-    auto [value, attributes] = value_and_attributes;
+    auto [value, attributes, _] = value_and_attributes;
 
     if (property_key.is_number()) {
         auto index = property_key.as_number();

+ 12 - 1
Userland/Libraries/LibJS/Runtime/Object.h

@@ -39,6 +39,17 @@ struct PrivateElement {
     Handle<Value> value;
 };
 
+// Non-standard: This is information optionally returned by object property access functions.
+//               It can be used to implement inline caches for property lookup.
+struct CacheablePropertyMetadata {
+    enum class Type {
+        NotCacheable,
+        OwnProperty,
+    };
+    Type type { Type::NotCacheable };
+    Optional<u32> property_offset;
+};
+
 class Object : public Cell {
     JS_CELL(Object, Cell);
 
@@ -118,7 +129,7 @@ public:
     virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyKey const&) const;
     virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyKey const&, PropertyDescriptor const&);
     virtual ThrowCompletionOr<bool> internal_has_property(PropertyKey const&) const;
-    virtual ThrowCompletionOr<Value> internal_get(PropertyKey const&, Value receiver) const;
+    virtual ThrowCompletionOr<Value> internal_get(PropertyKey const&, Value receiver, CacheablePropertyMetadata* = nullptr) const;
     virtual ThrowCompletionOr<bool> internal_set(PropertyKey const&, Value value, Value receiver);
     virtual ThrowCompletionOr<bool> internal_delete(PropertyKey const&);
     virtual ThrowCompletionOr<MarkedVector<Value>> internal_own_property_keys() const;

+ 2 - 0
Userland/Libraries/LibJS/Runtime/PropertyDescriptor.h

@@ -40,6 +40,8 @@ public:
     Optional<bool> writable {};
     Optional<bool> enumerable {};
     Optional<bool> configurable {};
+
+    Optional<u32> property_offset {};
 };
 
 }

+ 3 - 1
Userland/Libraries/LibJS/Runtime/ProxyObject.cpp

@@ -452,8 +452,10 @@ ThrowCompletionOr<bool> ProxyObject::internal_has_property(PropertyKey const& pr
 }
 
 // 10.5.8 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver
-ThrowCompletionOr<Value> ProxyObject::internal_get(PropertyKey const& property_key, Value receiver) const
+ThrowCompletionOr<Value> ProxyObject::internal_get(PropertyKey const& property_key, Value receiver, CacheablePropertyMetadata*) const
 {
+    // NOTE: We don't return any cacheable metadata for proxy lookups.
+
     VERIFY(!receiver.is_empty());
 
     auto& vm = this->vm();

+ 1 - 1
Userland/Libraries/LibJS/Runtime/ProxyObject.h

@@ -38,7 +38,7 @@ public:
     virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyKey const&) const override;
     virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyKey const&, PropertyDescriptor const&) override;
     virtual ThrowCompletionOr<bool> internal_has_property(PropertyKey const&) const override;
-    virtual ThrowCompletionOr<Value> internal_get(PropertyKey const&, Value receiver) const override;
+    virtual ThrowCompletionOr<Value> internal_get(PropertyKey const&, Value receiver, CacheablePropertyMetadata*) const override;
     virtual ThrowCompletionOr<bool> internal_set(PropertyKey const&, Value value, Value receiver) override;
     virtual ThrowCompletionOr<bool> internal_delete(PropertyKey const&) override;
     virtual ThrowCompletionOr<MarkedVector<Value>> internal_own_property_keys() const override;

+ 2 - 2
Userland/Libraries/LibJS/Runtime/TypedArray.h

@@ -288,7 +288,7 @@ public:
     }
 
     // 10.4.5.4 [[Get]] ( P, Receiver ), 10.4.5.4 [[Get]] ( P, Receiver )
-    virtual ThrowCompletionOr<Value> internal_get(PropertyKey const& property_key, Value receiver) const override
+    virtual ThrowCompletionOr<Value> internal_get(PropertyKey const& property_key, Value receiver, CacheablePropertyMetadata* cacheable_metadata) const override
     {
         VERIFY(!receiver.is_empty());
 
@@ -310,7 +310,7 @@ public:
         }
 
         // 2. Return ? OrdinaryGet(O, P, Receiver).
-        return Object::internal_get(property_key, receiver);
+        return Object::internal_get(property_key, receiver, cacheable_metadata);
     }
 
     // 10.4.5.5 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-integer-indexed-exotic-objects-set-p-v-receiver

+ 3 - 3
Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp

@@ -396,13 +396,13 @@ JS::ThrowCompletionOr<bool> CSSStyleDeclaration::internal_has_property(JS::Prope
     return property_id_from_name(name.to_string()) != CSS::PropertyID::Invalid;
 }
 
-JS::ThrowCompletionOr<JS::Value> CSSStyleDeclaration::internal_get(JS::PropertyKey const& name, JS::Value receiver) const
+JS::ThrowCompletionOr<JS::Value> CSSStyleDeclaration::internal_get(JS::PropertyKey const& name, JS::Value receiver, JS::CacheablePropertyMetadata* cacheable_metadata) const
 {
     if (!name.is_string())
-        return Base::internal_get(name, receiver);
+        return Base::internal_get(name, receiver, cacheable_metadata);
     auto property_id = property_id_from_name(name.to_string());
     if (property_id == CSS::PropertyID::Invalid)
-        return Base::internal_get(name, receiver);
+        return Base::internal_get(name, receiver, cacheable_metadata);
     if (auto maybe_property = property(property_id); maybe_property.has_value())
         return { JS::PrimitiveString::create(vm(), maybe_property->value->to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string()) };
     return { JS::PrimitiveString::create(vm(), String {}) };

+ 1 - 1
Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h

@@ -41,7 +41,7 @@ public:
     virtual DeprecatedString serialized() const = 0;
 
     virtual JS::ThrowCompletionOr<bool> internal_has_property(JS::PropertyKey const& name) const override;
-    virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver) const override;
+    virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver, JS::CacheablePropertyMetadata*) const override;
     virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver) override;
 
 protected:

+ 2 - 2
Userland/Libraries/LibWeb/HTML/Location.cpp

@@ -442,13 +442,13 @@ JS::ThrowCompletionOr<bool> Location::internal_define_own_property(JS::PropertyK
 }
 
 // 7.10.5.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/history.html#location-get
-JS::ThrowCompletionOr<JS::Value> Location::internal_get(JS::PropertyKey const& property_key, JS::Value receiver) const
+JS::ThrowCompletionOr<JS::Value> Location::internal_get(JS::PropertyKey const& property_key, JS::Value receiver, JS::CacheablePropertyMetadata* cacheable_metadata) const
 {
     auto& vm = this->vm();
 
     // 1. If IsPlatformObjectSameOrigin(this) is true, then return ? OrdinaryGet(this, P, Receiver).
     if (HTML::is_platform_object_same_origin(*this))
-        return JS::Object::internal_get(property_key, receiver);
+        return JS::Object::internal_get(property_key, receiver, cacheable_metadata);
 
     // 2. Return ? CrossOriginGet(this, P, Receiver).
     return HTML::cross_origin_get(vm, static_cast<JS::Object const&>(*this), property_key, receiver);

+ 1 - 1
Userland/Libraries/LibWeb/HTML/Location.h

@@ -58,7 +58,7 @@ public:
     virtual JS::ThrowCompletionOr<bool> internal_prevent_extensions() override;
     virtual JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> internal_get_own_property(JS::PropertyKey const&) const override;
     virtual JS::ThrowCompletionOr<bool> internal_define_own_property(JS::PropertyKey const&, JS::PropertyDescriptor const&) override;
-    virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver) const override;
+    virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver, JS::CacheablePropertyMetadata*) const override;
     virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver) override;
     virtual JS::ThrowCompletionOr<bool> internal_delete(JS::PropertyKey const&) override;
     virtual JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> internal_own_property_keys() const override;

+ 2 - 2
Userland/Libraries/LibWeb/HTML/WindowProxy.cpp

@@ -144,7 +144,7 @@ JS::ThrowCompletionOr<bool> WindowProxy::internal_define_own_property(JS::Proper
 }
 
 // 7.4.7 [[Get]] ( P, Receiver ), https://html.spec.whatwg.org/multipage/window-object.html#windowproxy-get
-JS::ThrowCompletionOr<JS::Value> WindowProxy::internal_get(JS::PropertyKey const& property_key, JS::Value receiver) const
+JS::ThrowCompletionOr<JS::Value> WindowProxy::internal_get(JS::PropertyKey const& property_key, JS::Value receiver, JS::CacheablePropertyMetadata* cacheable_metadata) const
 {
     auto& vm = this->vm();
 
@@ -156,7 +156,7 @@ JS::ThrowCompletionOr<JS::Value> WindowProxy::internal_get(JS::PropertyKey const
     // 3. If IsPlatformObjectSameOrigin(W) is true, then return ? OrdinaryGet(this, P, Receiver).
     // NOTE: this is passed rather than W as OrdinaryGet and CrossOriginGet will invoke the [[GetOwnProperty]] internal method.
     if (is_platform_object_same_origin(*m_window))
-        return JS::Object::internal_get(property_key, receiver);
+        return JS::Object::internal_get(property_key, receiver, cacheable_metadata);
 
     // 4. Return ? CrossOriginGet(this, P, Receiver).
     // NOTE: this is passed rather than W as OrdinaryGet and CrossOriginGet will invoke the [[GetOwnProperty]] internal method.

+ 1 - 1
Userland/Libraries/LibWeb/HTML/WindowProxy.h

@@ -26,7 +26,7 @@ public:
     virtual JS::ThrowCompletionOr<bool> internal_prevent_extensions() override;
     virtual JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> internal_get_own_property(JS::PropertyKey const&) const override;
     virtual JS::ThrowCompletionOr<bool> internal_define_own_property(JS::PropertyKey const&, JS::PropertyDescriptor const&) override;
-    virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver) const override;
+    virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver, JS::CacheablePropertyMetadata*) const override;
     virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver) override;
     virtual JS::ThrowCompletionOr<bool> internal_delete(JS::PropertyKey const&) override;
     virtual JS::ThrowCompletionOr<JS::MarkedVector<JS::Value>> internal_own_property_keys() const override;