Skip to content

Truly independent web browser

Meta+LibWeb: Support [CachedAccessor] in the Rust bindings

Andreas Kling62f332fd2461
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
index 36d9617323b..37cca5e6047 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
@@ -23,8 +23,8 @@ use core::ptr::NonNull;
use ak::Utf16FlyString;
use libjs_rust::api::{
Attribute, EmbedderCell, EmbedderClass, EmbedderValue, ErrorKind, ErrorType, FunctionObject, Gc, HostObject,
- JSHostClass, Object, PrimitiveString, PropertyAttributes, PropertyKey, RawNativeFunctionPointer, Realm, Throw,
- ThrowCompletionOr, Trace, Value, Vm, get_function_realm, host_data_of, is_host_instance_of,
+ JSHostClass, Object, PrimitiveString, PropertyAttributes, PropertyKey, RawNativeFunction, RawNativeFunctionPointer,
+ Realm, Throw, ThrowCompletionOr, Trace, Value, Vm, get_function_realm, host_data_of, is_host_instance_of,
};
use crate::bindings::CallContext;
@@ -390,6 +390,28 @@ pub(crate) fn define_attribute(
);
}
+/// Defines a regular attribute that is read only and [CachedAccessor] on the interface prototype object: its getter
+/// returns the same value for as long as the object lives, so the engine keeps the value on the object after the first
+/// get and does not call the getter again, as LibWeb's C++ bindings define such an attribute.
+pub(crate) fn define_cached_attribute(
+ vm: &Vm,
+ realm: Gc<Realm>,
+ prototype: Gc<Object>,
+ name: &str,
+ getter: RawNativeFunctionPointer,
+) {
+ let property_key = PropertyKey::from_utf8(name);
+ let getter = RawNativeFunction::create(vm, getter, 0, &property_key, Some(realm), Some("get"), None);
+ prototype.define_direct_cached_accessor(
+ vm,
+ &property_key,
+ Some(getter.upcast()),
+ None,
+ PropertyAttributes::new(Attribute::ENUMERABLE | Attribute::CONFIGURABLE),
+ true,
+ );
+}
+
/// Defines a regular operation on the interface prototype object, https://webidl.spec.whatwg.org/#es-operations
pub(crate) fn define_operation(
vm: &Vm,
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index 4e9fab244aa..b18c7eda503 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -399,10 +399,10 @@ def write_rust_interface(
}};
use crate::bindings::interface::{{
Inherits, Interface, Ref, Wrapped, construct_arguments, construct_wrapper, define_attribute,
- define_constants, define_interface_object_properties, define_operation, define_to_string_tag,
- define_unimplemented_property, entry_point_arguments, implementation_if, implementation_of,
- prototype_from_new_target, rejected_promise, set_interface_prototype_object_prototype, this_object,
- to_interface,
+ define_cached_attribute, define_constants, define_interface_object_properties, define_operation,
+ define_to_string_tag, define_unimplemented_property, entry_point_arguments, implementation_if,
+ implementation_of, prototype_from_new_target, rejected_promise, set_interface_prototype_object_prototype,
+ this_object, to_interface,
}};
use crate::bindings::{{CallContext, WebIdlResult}};
@@ -506,6 +506,10 @@ def write_rust_interface(
continue
name = attribute.attribute.name
getter = f"raw_native!({attribute.getter_function})"
+ if attribute.is_cached:
+ out.write(f' define_cached_attribute(vm, realm, prototype, "{name}", {getter});\n')
+ defined_property_names.append(name)
+ continue
setter = "None" if attribute.setter_method is None else f"raw_native!({attribute.setter_function})"
out.write(f' define_attribute(vm, realm, prototype, "{name}", {getter}, {setter});\n')
defined_property_names.append(name)
diff --git a/Meta/Generators/libweb_bindings/rust_members.py b/Meta/Generators/libweb_bindings/rust_members.py
index 35cee23bf35..b31dfb1168b 100644
--- a/Meta/Generators/libweb_bindings/rust_members.py
+++ b/Meta/Generators/libweb_bindings/rust_members.py
@@ -30,7 +30,7 @@ from Utils.webidl_parser import Operation
RUST_METHOD_NAME = re.compile(r"[a-z][a-z0-9_]*")
# The extended attributes of the members of an interface that the Rust bindings handle.
-SUPPORTED_ATTRIBUTE_EXTENDED_ATTRIBUTES = {"FIXME", "ImplementedAs", "NewObject", "SameObject"}
+SUPPORTED_ATTRIBUTE_EXTENDED_ATTRIBUTES = {"CachedAccessor", "FIXME", "ImplementedAs", "NewObject", "SameObject"}
SUPPORTED_OPERATION_EXTENDED_ATTRIBUTES = {"FIXME", "ImplementedAs", "NewObject", "SameObject"}
@@ -48,6 +48,12 @@ class RustAttribute:
# The method of the implementation that the setter calls, for an attribute that is not read only.
setter_method: Optional[str]
+ @property
+ def is_cached(self) -> bool:
+ """Whether the getter returns the same value for as long as the object lives, which the engine then caches on
+ the object: [CachedAccessor]."""
+ return "CachedAccessor" in self.attribute.extended_attributes
+
@property
def getter_function(self) -> str:
"""The generated native function of the getter."""
@@ -214,6 +220,8 @@ def rust_members(interface: Interface) -> RustMembers:
if "FIXME" in attribute.extended_attributes:
members.attributes_in_order.append(attribute.name)
continue
+ if "CachedAccessor" in attribute.extended_attributes and not attribute.readonly:
+ fail(f"its attribute {attribute.name} is [CachedAccessor] and not read only")
getter_method = method_of(attribute.name, attribute.extended_attributes)
setter_method = None if attribute.readonly else f"set_{generated_function_stem(getter_method)}"
rust_attribute = RustAttribute(attribute, getter_method, setter_method)