Skip to content

Truly independent web browser

Meta: Support static attributes in the Rust bindings

Shannon Boothe28c2ff2f812
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index 6e3a9d4482f..1e387efe33f 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -882,6 +882,14 @@ def implementation_method_signatures(
)
if members.named_properties is not None:
signatures.append("fn supported_property_names(&self, cx: &mut CallContext) -> Vec<Utf16String>")
+ for attribute in members.static_attributes:
+ what = f"{interface.name}.{attribute.attribute.name}"
+ result_type = (
+ "Value"
+ if attribute.returns_js_value
+ else rust_type(attribute.attribute.type, context, what, is_result=True)
+ )
+ signatures.append(f"fn {attribute.getter_method}(cx: &mut CallContext) -> WebIdlResult<{result_type}>")
for operation_set in members.static_operation_sets:
for overload in operation_set.overloads:
what = f"{interface.name}.{operation_set.name}()"
@@ -1351,6 +1359,9 @@ def write_rust_interface(
if members.async_iterator is not None:
write_rust_async_iterator(out, context, interface)
+ for attribute in members.static_attributes:
+ write_rust_static_attribute_getter(out, context, interface, attribute)
+
for operation_set in members.static_operation_sets:
write_rust_operation_set(out, context, interface, operation_set, write_rust_static_operation)
@@ -1495,6 +1506,18 @@ def write_rust_interface(
)
if members.constants:
out.write(" define_constants(vm, constructor, &CONSTANTS);\n")
+ for attribute in members.static_attributes:
+ definition = (
+ f'define_attribute(vm, realm, constructor, "{attribute.attribute.name}", '
+ f"raw_native!({attribute.getter_function}), None);"
+ )
+ out.write(
+ member_exposure_definition(
+ definition,
+ secure_context_only="SecureContext" in attribute.attribute.extended_attributes,
+ window_only=attribute.is_window_only,
+ )
+ )
for operation_set in members.static_operation_sets:
name = operation_set.name
definition = (
@@ -2033,6 +2056,29 @@ def write_rust_stringifier(out: TextIO, function: str, method: str) -> None:
)
+def write_rust_static_attribute_getter(
+ out: TextIO, context: GenerationContext, interface: Interface, attribute: RustAttribute
+) -> None:
+ """The native function of the getter of a static attribute, which runs in the current realm,
+ https://webidl.spec.whatwg.org/#dfn-attribute-getter"""
+ what = f"{interface.name}.{attribute.attribute.name}"
+ conversion = (
+ "Ok(result)"
+ if attribute.returns_js_value
+ else rust_to_js_value(attribute.attribute.type, "result", "&cx", context, what)
+ )
+ out.write(
+ f"""
+ fn {attribute.getter_function}(vm: &Vm) -> ThrowCompletionOr<Value> {{
+ let mut cx = CallContext::new(vm, vm.current_realm().expect("a native function runs in a realm"));
+ let result = Implementation::{attribute.getter_method}(&mut cx)
+ .map_err(|exception| exception.throw_in_current_realm(cx.vm()))?;
+ {conversion}
+ }}
+"""
+ )
+
+
def write_rust_static_operation(
out: TextIO, context: GenerationContext, interface: Interface, name: str, function: str, overload: RustOperation
) -> None:
diff --git a/Meta/Generators/libweb_bindings/rust_members.py b/Meta/Generators/libweb_bindings/rust_members.py
index 8048786fba7..10f7fc3d3ad 100644
--- a/Meta/Generators/libweb_bindings/rust_members.py
+++ b/Meta/Generators/libweb_bindings/rust_members.py
@@ -322,6 +322,8 @@ class RustConstructor:
class RustMembers:
# The attributes and the operations that the implementation has methods for.
attributes: List[RustAttribute] = field(default_factory=list)
+ # The static attributes, which the interface object has.
+ static_attributes: List[RustAttribute] = field(default_factory=list)
operation_sets: List[RustOperationSet] = field(default_factory=list)
# The static operations, which the interface object has.
static_operation_sets: List[RustOperationSet] = field(default_factory=list)
@@ -410,8 +412,6 @@ def rust_members(interface: Interface) -> RustMembers:
if interface.async_iterable is not None and interface.async_iterable.key_type is not None:
fail("it has a pair asynchronously iterable declaration")
has_value_iterator = interface.iterable is not None and interface.iterable.key_type is None
- if interface.static_attributes:
- fail("it has static attributes")
members = RustMembers()
if interface.async_iterable is not None:
@@ -562,6 +562,17 @@ def rust_members(interface: Interface) -> RustMembers:
members.attributes.append(rust_attribute)
members.attributes_in_order.append(rust_attribute)
+ for attribute in interface.static_attributes:
+ check_extended_attributes(
+ attribute.name, attribute.extended_attributes, SUPPORTED_ATTRIBUTE_EXTENDED_ATTRIBUTES - {"FIXME"}
+ )
+ check_exposure(attribute.name, attribute.extended_attributes)
+ if not attribute.readonly:
+ fail(f"its static attribute {attribute.name} is not read only, which the Rust bindings do not handle yet")
+ members.static_attributes.append(
+ RustAttribute(attribute, method_of(attribute.name, attribute.extended_attributes), None)
+ )
+
def operation_sets(static: bool) -> List[RustOperationSet]:
operations_of_interface = interface.static_operations if static else interface.regular_operations
for operation in operations_of_interface:
@@ -654,6 +665,7 @@ def rust_members(interface: Interface) -> RustMembers:
# NB: The spellings of a CSS descriptor or property are attributes with the same steps, which name the same methods.
attribute_methods = {attribute.getter_method for attribute in members.attributes if attribute.event_type is None}
+ attribute_methods.update(attribute.getter_method for attribute in members.static_attributes)
attribute_methods.update(
attribute.setter_method for attribute in members.attributes if attribute.setter_method is not None
)