Skip to content

Truly independent web browser

Meta: Let a Rust event handler attribute name its setter

Shannon Booth7c5a68f6d0fa
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index 791aecaf41d..06c45a857bd 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -575,6 +575,10 @@ def implementation_method_signatures(
for attribute in members.attributes:
what = f"{interface.name}.{attribute.attribute.name}"
if attribute.event_type is not None:
+ # NB: The bindings get and set an event handler on the EventTarget that the object owns, unless the IDL
+ # names the method of the setter.
+ if attribute.setter_method is not None:
+ signatures.append(f"fn {attribute.setter_method}(&self, cx: &mut CallContext, value: Value)")
continue
if not attribute.attribute.inherit:
result_type = rust_type(attribute.attribute.type, context, what, is_result=True)
@@ -1014,7 +1018,7 @@ def write_rust_interface(
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})"
+ setter = "None" if attribute.attribute.readonly else f"raw_native!({attribute.setter_function})"
out.write(f' define_attribute(vm, realm, prototype, "{name}", {getter}, {setter});\n')
defined_property_names.append(name)
for operation_set in members.operation_sets:
@@ -1579,7 +1583,8 @@ def write_this_implementation(out: TextIO, context: GenerationContext, interface
def write_rust_event_handler_attribute(out: TextIO, attribute: RustAttribute) -> None:
"""The getter and the setter of an event handler IDL attribute, which get and set the event handler of the C++
- EventTarget that the object owns, https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-idl-attributes"""
+ EventTarget that the object owns, https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-idl-attributes
+ The setter calls the method of the implementation that the IDL names instead, which sets the event handler itself."""
out.write(
f"""
pub(super) fn {attribute.getter_function}(vm: &Vm) -> ThrowCompletionOr<Value> {{
@@ -1587,7 +1592,22 @@ def write_rust_event_handler_attribute(out: TextIO, attribute: RustAttribute) ->
let object: &Implementation = &implementation;
Ok(event_handler_attribute(object, "{attribute.event_type}"))
}}
-
+"""
+ )
+ if attribute.setter_method is not None:
+ out.write(
+ f"""
+ fn {attribute.setter_function}(vm: &Vm) -> ThrowCompletionOr<Value> {{
+ let value = vm.argument(0);
+ let (implementation, mut cx) = this_implementation(vm)?;
+ Implementation::{attribute.setter_method}(&implementation, &mut cx, value);
+ Ok(Value::UNDEFINED)
+ }}
+"""
+ )
+ return
+ out.write(
+ f"""
fn {attribute.setter_function}(vm: &Vm) -> ThrowCompletionOr<Value> {{
let value = vm.argument(0);
let (implementation, _) = this_implementation(vm)?;
diff --git a/Meta/Generators/libweb_bindings/rust_members.py b/Meta/Generators/libweb_bindings/rust_members.py
index 8cec842cf39..1378493faed 100644
--- a/Meta/Generators/libweb_bindings/rust_members.py
+++ b/Meta/Generators/libweb_bindings/rust_members.py
@@ -69,7 +69,9 @@ class RustAttribute:
attribute: Attribute
# The method of the implementation that the getter calls.
getter_method: str
- # The method of the implementation that the setter calls, for an attribute that is not read only.
+ # The method of the implementation that the setter calls, for an attribute that is not read only. An event handler
+ # IDL attribute has one only when the IDL names it with [ImplementedAs]: the setter of an interface that does more
+ # than set the event handler, which the method does itself.
setter_method: Optional[str]
# The event type of an event handler IDL attribute, whose getter and setter work on the C++ EventTarget that the
# object owns: https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-idl-attributes
@@ -291,6 +293,19 @@ def rust_members(interface: Interface) -> RustMembers:
fail(f"[ImplementedAs={method}] of {member_name} does not name a Rust method")
return method
+ def event_handler_attribute(attribute: Attribute) -> RustAttribute:
+ """An event handler IDL attribute, which the bindings get and set on the EventTarget that the object owns. The
+ IDL names the method of its setter with [ImplementedAs] when the interface's setter does more than set the
+ event handler."""
+ if attribute.readonly or not attribute.name.startswith("on"):
+ fail(f"its event handler IDL attribute {attribute.name} is not a plain attribute named on<event type>")
+ check_extended_attributes(attribute.name, attribute.extended_attributes, {"ImplementedAs"})
+ setter_method = None
+ if "ImplementedAs" in attribute.extended_attributes:
+ setter_method = method_of(attribute.name, attribute.extended_attributes)
+ event_type = attribute.name.removeprefix("on")
+ return RustAttribute(attribute, rust_identifier(attribute.name), setter_method, event_type)
+
special_operations = [
interface.named_property_getter,
interface.named_property_setter,
@@ -383,14 +398,12 @@ def rust_members(interface: Interface) -> RustMembers:
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)}"
- event_type = None
if attribute.type.name in ("EventHandler", "EventHandlerNonNull"):
- if attribute.readonly or not attribute.name.startswith("on") or attribute.extended_attributes:
- fail(f"its event handler IDL attribute {attribute.name} is not a plain attribute named on<event type>")
- event_type = attribute.name.removeprefix("on")
- rust_attribute = RustAttribute(attribute, getter_method, setter_method, event_type)
+ rust_attribute = event_handler_attribute(attribute)
+ else:
+ 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)
members.attributes.append(rust_attribute)
members.attributes_in_order.append(rust_attribute)
@@ -448,11 +461,7 @@ def rust_members(interface: Interface) -> RustMembers:
members.constants.append(RustConstant(constant.name, value))
methods = [attribute.getter_method for attribute in members.attributes if attribute.event_type is None]
- methods.extend(
- attribute.setter_method
- for attribute in members.attributes
- if attribute.setter_method is not None and attribute.event_type is None
- )
+ methods.extend(attribute.setter_method for attribute in members.attributes if attribute.setter_method is not None)
methods.extend(
overload.method
for operation_set in members.operation_sets + members.static_operation_sets