Truly independent web browser
Meta: Support more of Web IDL in the Rust bindings
Andreas Klingbfae2928325f
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.pyindex c3a1cf58d90..cd53cac5052 100644--- a/Meta/Generators/libweb_bindings/rust_bindings.py+++ b/Meta/Generators/libweb_bindings/rust_bindings.py@@ -29,6 +29,8 @@ from Generators.libweb_bindings.operations import idl_type_is_json_typefrom Generators.libweb_bindings.operations import operation_is_default_to_jsonfrom Generators.libweb_bindings.rust_members import RustAttributefrom Generators.libweb_bindings.rust_members import RustConstructor+from Generators.libweb_bindings.rust_members import RustIndexedPropertyGetter+from Generators.libweb_bindings.rust_members import RustIndexedPropertySetterfrom Generators.libweb_bindings.rust_members import RustMembersfrom Generators.libweb_bindings.rust_members import RustOperationfrom Generators.libweb_bindings.rust_members import RustOperationSet@@ -52,6 +54,7 @@ from Generators.libweb_bindings.rust_types import CPP_PARENT_INTERFACESfrom Generators.libweb_bindings.rust_types import UnsupportedByRustBindingsfrom Generators.libweb_bindings.rust_types import cpp_ancestorfrom Generators.libweb_bindings.rust_types import cpp_interface_name_constant+from Generators.libweb_bindings.rust_types import holds_cellsfrom Generators.libweb_bindings.rust_types import inheriting_interfacesfrom Generators.libweb_bindings.rust_types import is_implemented_in_rustfrom Generators.libweb_bindings.rust_types import is_promise_type@@ -263,6 +266,7 @@ void {interface.namespace_class}::initialize(JS::Realm& realm, JS::Object& objecinitialize_constructor = rust_entry_point_name(interface, "initialize_constructor")construct = rust_entry_point_name(interface, "construct")supports_indexed_properties = rust_members(interface).indexed_property_getter is not None+ has_indexed_property_setter = rust_members(interface).indexed_property_setter is not Noneif supports_indexed_properties and inheriting_interfaces(interface, context):raise UnsupportedByRustBindings(f"{interface.path}: {interface.name} supports indexed properties and others inherit from it, which the "@@ -281,6 +285,11 @@ void {initialize_constructor}(JSVM*, void* realm, JSObject* constructor);f"bool {item_value_entry_point(interface)}(JSVM*, void* realm, JSObject* wrapper, size_t index, ""JSValue* out_value);\n")+ if has_indexed_property_setter:+ out.writelines(+ f"JSCompletion {entry_point}(JSVM*, void* realm, JSObject* wrapper, u32 index, JSValue value);\n"+ for entry_point in indexed_property_setter_entry_points(interface)+ )if interface.constructors:out.write(f"JSCompletion {construct}(JSVM*, JSObject* constructor, JSObject* new_target);\n")if has_cpp_ancestor:@@ -314,6 +323,25 @@ constexpr JSHostClass {wrapper_host_class_symbol(interface)} = JS::make_host_claelse:includes.add("LibWeb/Bindings/PlatformObject.h")legacy_info = f"{wrapper_name(interface)}_legacy_platform_object_info"+ setter_functions = ""+ has_setter_info = ""+ setter_info = ""+ if has_indexed_property_setter:+ includes.add("LibWeb/WebIDL/ExceptionOr.h")+ has_setter_info = " .has_indexed_property_setter = true,\n"+ for hook, entry_point in zip(+ ("set_value_of_new_indexed_property", "set_value_of_existing_indexed_property"),+ indexed_property_setter_entry_points(interface),+ ):+ function = f"{wrapper_name(interface)}_{hook}"+ setter_functions += f"""static WebIDL::ExceptionOr<void> {function}(JS::HostObject& wrapper, JS::Realm& realm, u32 index, JS::Value value)+{{+ TRY(JS::HostABI::completion_from_abi<JS::Value>({entry_point}(JS::HostABI::vm_to_abi(realm.vm()), &realm, JS::HostABI::object_to_abi(&wrapper), index, JS::HostABI::value_to_abi(value))));+ return {{}};+}}++"""+ setter_info += f" .{hook} = {function},\n"out.write(f"""}}@@ -326,10 +354,10 @@ static Optional<JS::Value> {wrapper_name(interface)}_item_value(JS::HostObject creturn JS::HostABI::value_from_abi(value);}}-static constexpr LegacyPlatformObjectInfo {legacy_info} {{+{setter_functions}static constexpr LegacyPlatformObjectInfo {legacy_info} {{.supports_indexed_properties = true,- .item_value = {wrapper_name(interface)}_item_value,-}};+{has_setter_info} .item_value = {wrapper_name(interface)}_item_value,+{setter_info}}};// The wrappers are legacy platform objects, whose indexed properties the Rust bindings read: their implementation is a// cell in their host data, not an implementation object in their wrappable slot.@@ -431,12 +459,12 @@ JSObject* {function}(void* realm, u16 interface_name)switch (static_cast<InterfaceName>(interface_name)) {{""")- for interface in interfaces:- out.write(- f""" case InterfaceName::{interface.name}:+ out.writelines(+ f""" case InterfaceName::{interface.name}:return JS::HostABI::object_to_abi(&{object_of(interface)});"""- )+ for interface in interfaces+ )out.write(""" default:VERIFY_NOT_REACHED();@@ -510,8 +538,6 @@ def write_parameter_conversions() -> None:for index, parameter in enumerate(parameters):parameter_what = f"the parameter {parameter.name} of {what}"- if parameter.variadic:- raise UnsupportedByRustBindings(f"{parameter_what} is variadic")unsupported = set(parameter.extended_attributes) - {"AllowShared","AllowResizable",@@ -522,6 +548,23 @@ def write_parameter_conversions(if unsupported:raise UnsupportedByRustBindings(f"{parameter_what} has the extended attributes {sorted(unsupported)}")name = rust_local_name(parameter.name)+ if parameter.variadic:+ # NB: A variadic parameter takes every remaining argument, so it is always the last parameter.+ conversion = rust_to_idl_value(+ parameter.type, parameter.extended_attributes, "argument", parameter.name, context, parameter_what+ )+ keeps_cells_alive = holds_cells(parameter.type, context)+ storage = "MarkedVec::new(vm)" if keeps_cells_alive else "Vec::new()"+ mutability = "" if keeps_cells_alive else "mut "+ out.write(+ f""" let {mutability}{name} = {storage};+ for index in {index}..vm.argument_count() {{+ let argument = vm.argument(index);+ {name}.push({conversion}?);+ }}+"""+ )+ continueconversion = rust_to_idl_value(parameter.type,parameter.extended_attributes,@@ -628,6 +671,13 @@ def implementation_method_signatures(f"fn {getter.method}(&self, cx: &mut CallContext{parameters}) -> "f"WebIdlResult<{rust_type(getter.operation.return_type, context, what, is_result=True)}>")+ if members.indexed_property_setter is not None:+ what = f"the indexed property setter of {interface.name}"+ value_type = rust_parameter_type(members.indexed_property_setter.value_parameter, context, what)+ for method in ("set_value_of_new_indexed_property", "set_value_of_existing_indexed_property"):+ signatures.append(+ f"fn {method}(&self, cx: &mut CallContext, index: u32, value: {value_type}) -> WebIdlResult<()>"+ )for operation_set in members.static_operation_sets:for overload in operation_set.overloads:what = f"{interface.name}.{operation_set.name}()"@@ -969,9 +1019,13 @@ def write_rust_interface(if members.indexed_property_getter is not None:getter = members.indexed_property_getter.operation- getter_set = RustOperationSet(getter.operation.name, [getter])- write_rust_operation(out, context, interface, getter_set.name, getter_set.function, getter)- write_rust_item_value(out, context, interface, getter)+ if getter.operation.name:+ getter_set = RustOperationSet(getter.operation.name, [getter])+ write_rust_operation(out, context, interface, getter_set.name, getter_set.function, getter)+ write_rust_item_value(out, context, interface, members.indexed_property_getter)++ if members.indexed_property_setter is not None:+ write_rust_indexed_property_setter(out, context, interface, members.indexed_property_setter)if members.pair_iterator is not None:write_rust_pair_iterator(out, interface)@@ -1033,12 +1087,13 @@ def write_rust_interface(defined_property_names.append("toString")if members.indexed_property_getter is not None:getter = members.indexed_property_getter.operation- getter_set = RustOperationSet(getter.operation.name, [getter])- out.write(- f' define_operation(vm, realm, prototype, "{getter_set.name}", raw_native!({getter_set.function}), '- f"{getter_set.length});\n"- )- defined_property_names.append(getter_set.name)+ if getter.operation.name:+ getter_set = RustOperationSet(getter.operation.name, [getter])+ out.write(+ f' define_operation(vm, realm, prototype, "{getter_set.name}", raw_native!({getter_set.function}), '+ f"{getter_set.length});\n"+ )+ defined_property_names.append(getter_set.name)out.write(" define_indexed_properties_iterator(vm, realm, prototype);\n")if members.indexed_property_getter.has_value_iterator:out.write(" define_value_iterator_properties(vm, realm, prototype);\n")@@ -1298,18 +1353,31 @@ def item_value_entry_point(interface: Interface) -> str:return rust_entry_point_name(interface, "item_value")-def write_rust_item_value(out: TextIO, context: GenerationContext, interface: Interface, getter: RustOperation) -> None:+def write_rust_item_value(+ out: TextIO, context: GenerationContext, interface: Interface, indexed_property_getter: RustIndexedPropertyGetter+) -> None:"""The entry point through which the internal methods of a legacy platform object that LibWeb's C++ defines readan indexed property of a wrapper of the interface, which has a value if the index is a supported property index,https://webidl.spec.whatwg.org/#legacy-platform-object-getownproperty"""+ getter = indexed_property_getter.operationreturn_type = getter.operation.return_type- if not return_type.nullable:- raise UnsupportedByRustBindings(- f"{interface.path}: the indexed property getter of {interface.name} does not return a nullable type, by "- "which the Rust bindings tell the indices that are not supported property indices"- )what = f"the indexed property getter of {interface.name}"conversion = rust_to_js_value(return_type.without_nullable(), "value", "&cx", context, what)+ if return_type.nullable:+ value_steps = f"""let Ok(Some(value)) = Implementation::{getter.method}(&implementation, &mut cx, index) else {{+ return false;+ }};"""+ else:+ # NB: A getter that does not return a nullable type has no value for an index that is not a supported one.+ value_steps = f"""let Ok(length) = Implementation::{indexed_property_getter.length_method}(&implementation, &mut cx) else {{+ return false;+ }};+ if index >= length {{+ return false;+ }}+ let Ok(value) = Implementation::{getter.method}(&implementation, &mut cx, index) else {{+ return false;+ }};"""out.write(f"""/// The value of the indexed property of the wrapper, which LibWeb's C++ asks for, if the index is a supported@@ -1333,9 +1401,7 @@ def write_rust_item_value(out: TextIO, context: GenerationContext, interface: In}};let implementation = implementation_of::<Implementation>(wrapper);let mut cx = CallContext::new(vm, realm);- let Ok(Some(value)) = Implementation::{getter.method}(&implementation, &mut cx, index) else {{- return false;- }};+ {value_steps}let value = {conversion}.expect("converting an object to a JS value does not throw");*out_value = value.to_raw();true@@ -1344,6 +1410,68 @@ def write_rust_item_value(out: TextIO, context: GenerationContext, interface: In)+def indexed_property_setter_entry_points(interface: Interface) -> List[str]:+ return [+ rust_entry_point_name(interface, "set_value_of_new_indexed_property"),+ rust_entry_point_name(interface, "set_value_of_existing_indexed_property"),+ ]+++def write_rust_indexed_property_setter(+ out: TextIO, context: GenerationContext, interface: Interface, setter: RustIndexedPropertySetter+) -> None:+ """The entry points through which the internal methods of a legacy platform object that LibWeb's C++ defines run the+ steps of the indexed property setter, https://webidl.spec.whatwg.org/#invoke-indexed-setter"""+ parameter = setter.value_parameter+ what = f"the indexed property setter of {interface.name}"+ conversion = rust_to_idl_value(+ parameter.type,+ parameter.extended_attributes,+ "value",+ parameter.name,+ context,+ what,+ keeps_cells_alive=True,+ )+ out.writelines(+ f"""+ /// The steps of the indexed property setter, which LibWeb's C++ runs on the wrapper.+ ///+ /// # Safety+ ///+ /// The pointers must be the VM, a live realm and a live wrapper of the interface.+ #[unsafe(no_mangle)]+ pub unsafe extern "C" fn {entry_point}(+ vm: *mut c_void,+ realm: *mut c_void,+ wrapper: *mut c_void,+ index: u32,+ value: u64,+ ) -> RawNativeFunctionResult {{+ // SAFETY: The caller passes live pointers.+ let (vm, realm, wrapper) = unsafe {{ entry_point_arguments(vm, realm, wrapper) }};+ // SAFETY: The caller passes the raw bits of a value.+ let value = unsafe {{ Value::from_raw(value) }};+ let steps = || -> ThrowCompletionOr<Value> {{+ let implementation = implementation_of::<Implementation>(wrapper);+ let mut cx = CallContext::new(vm, realm);+ let type_error_realm_scope = vm.type_error_realm_scope(cx.realm());+ let value = {conversion}?;+ drop(type_error_realm_scope);+ Implementation::{method}(&implementation, &mut cx, index, value)+ .map_err(|exception| exception.throw(&cx))?;+ Ok(Value::UNDEFINED)+ }};+ RawNativeFunctionResult::from_completion(steps())+ }}+"""+ for method, entry_point in zip(+ ("set_value_of_new_indexed_property", "set_value_of_existing_indexed_property"),+ indexed_property_setter_entry_points(interface),+ )+ )++def write_rust_stringifier(out: TextIO, function: str, method: str) -> None:"""The native function of the stringifier, which calls the method of the implementation,https://webidl.spec.whatwg.org/#es-stringifier"""diff --git a/Meta/Generators/libweb_bindings/rust_members.py b/Meta/Generators/libweb_bindings/rust_members.pyindex 1378493faed..2b8a7820e25 100644--- a/Meta/Generators/libweb_bindings/rust_members.py+++ b/Meta/Generators/libweb_bindings/rust_members.py@@ -165,14 +165,27 @@ class RustStringifier:class RustIndexedPropertyGetter:"""The indexed property getter of an interface, which has an identifier: the operation of that identifier, whosemethod the getter steps of a supported property index call as well, https://webidl.spec.whatwg.org/#idl-indexed-properties- An index for which the method returns nothing is not a supported property index."""+ An index for which a nullable method returns nothing is not a supported property index, nor is an index that is not+ less than the length attribute of an interface whose method does not return a nullable type. An unnamed getter+ defines no operation, and the method of the implementation is item() unless the IDL names it."""operation: RustOperation+ # The getter of the length attribute of the interface.+ length_method: str# Whether the interface declares a value iterator, which it implements through the indexed properties,# https://webidl.spec.whatwg.org/#dfn-value-iteratorhas_value_iterator: bool+@dataclass+class RustIndexedPropertySetter:+ """The unnamed indexed property setter of an interface, whose steps the implementation defines as the methods+ set_value_of_new_indexed_property() and set_value_of_existing_indexed_property(),+ https://webidl.spec.whatwg.org/#legacy-platform-object-set"""++ value_parameter: OperationParameter++@dataclassclass RustPairIterator:"""The pair iterator of an interface, https://webidl.spec.whatwg.org/#dfn-pair-iterator, whose value pairs to@@ -234,6 +247,7 @@ class RustMembers:static_operation_sets: List[RustOperationSet] = field(default_factory=list)stringifier: Optional[RustStringifier] = Noneindexed_property_getter: Optional[RustIndexedPropertyGetter] = None+ indexed_property_setter: Optional[RustIndexedPropertySetter] = Nonepair_iterator: Optional[RustPairIterator] = Noneasync_iterator: Optional[RustAsyncIterator] = Noneconstructors: List[RustConstructor] = field(default_factory=list)@@ -257,7 +271,7 @@ class RustMembers:names.extend(operation_set.name for operation_set in self.operation_sets)if self.stringifier is not None:names.append("toString")- if self.indexed_property_getter is not None:+ if self.indexed_property_getter is not None and self.indexed_property_getter.operation.operation.name:names.append(self.indexed_property_getter.operation.operation.name)if self.pair_iterator is not None:names.extend(self.pair_iterator.function_names)@@ -309,11 +323,10 @@ def rust_members(interface: Interface) -> RustMembers:special_operations = [interface.named_property_getter,interface.named_property_setter,- interface.indexed_property_setter,interface.named_property_deleter,]if any(special_operations):- fail("it has special operations other than an indexed property getter")+ fail("it has special operations other than an indexed property getter and setter")if interface.maplike or interface.setlike:fail("it is a maplike or setlike")if interface.async_iterable is not None and interface.async_iterable.key_type is not None:@@ -334,18 +347,28 @@ def rust_members(interface: Interface) -> RustMembers:if has_value_iterator and getter is None:fail("it declares a value iterator and supports no indexed properties")if getter is not None:- if not getter.name:- fail("its indexed property getter has no identifier, which the Rust bindings do not handle yet")- if interface.parent_name:- fail("it supports indexed properties and inherits from another interface")- if not any(attribute.name == "length" for attribute in interface.regular_attributes):+ length_attribute = next(+ (attribute for attribute in interface.regular_attributes if attribute.name == "length"), None+ )+ if length_attribute is None:fail("it supports indexed properties and has no length attribute")+ assert length_attribute is not Nonecheck_extended_attributes(getter.name, getter.extended_attributes, {"ImplementedAs"})getter_operation = Operation(getter.name, getter.return_type, getter.parameters, getter.extended_attributes)members.indexed_property_getter = RustIndexedPropertyGetter(- RustOperation(getter_operation, method_of(getter.name, getter.extended_attributes)), has_value_iterator+ RustOperation(getter_operation, method_of(getter.name or "item", getter.extended_attributes)),+ method_of(length_attribute.name, length_attribute.extended_attributes),+ has_value_iterator,)+ setter = interface.indexed_property_setter+ if setter is not None:+ if getter is None:+ fail("it has an indexed property setter and no indexed property getter")+ if setter.name:+ fail("its indexed property setter has an identifier, which the Rust bindings do not handle yet")+ members.indexed_property_setter = RustIndexedPropertySetter(setter.parameters[1])+def check_exposure(member_name: str, extended_attributes: dict) -> None:exposure = extended_attributes.get("Exposed")if exposure is not None and exposure != SUPPORTED_MEMBER_EXPOSURE:@@ -472,6 +495,8 @@ def rust_members(interface: Interface) -> RustMembers:methods.append(members.stringifier.method)if members.indexed_property_getter is not None:methods.append(members.indexed_property_getter.operation.method)+ if members.indexed_property_setter is not None:+ methods.extend(["set_value_of_new_indexed_property", "set_value_of_existing_indexed_property"])methods.extend(constructor.method for constructor in members.constructors)repeated_methods = sorted({method for method in methods if methods.count(method) > 1})if repeated_methods:diff --git a/Meta/Generators/libweb_bindings/rust_types.py b/Meta/Generators/libweb_bindings/rust_types.pyindex 52f2c07796c..86225a3fde1 100644--- a/Meta/Generators/libweb_bindings/rust_types.py+++ b/Meta/Generators/libweb_bindings/rust_types.py@@ -7,9 +7,9 @@import re+from collections.abc import Iterablefrom pathlib import Pathfrom typing import FrozenSet-from typing import Iterablefrom typing import Listfrom typing import Optional@@ -532,6 +532,11 @@ def rust_dictionary_type(dictionary: Dictionary, context: GenerationContext) ->def rust_parameter_type(parameter: OperationParameter, context: GenerationContext, what: str) -> str:"""The Rust type the implementation receives for the parameter."""+ if parameter.variadic:+ element_type = rust_type(parameter.type, context, what)+ if holds_cells(parameter.type, context):+ return f"MarkedVec<'_, {element_type}>"+ return f"Vec<{element_type}>"if is_sequence_of_cells(parameter.type, context):assert isinstance(parameter.type, IDLParameterizedType)value_type = f"MarkedVec<'_, {rust_type(parameter.type.parameters[0], context, what)}>"diff --git a/Meta/Generators/libweb_bindings/rust_usage.py b/Meta/Generators/libweb_bindings/rust_usage.pyindex 9311251e763..f6b78361136 100644--- a/Meta/Generators/libweb_bindings/rust_usage.py+++ b/Meta/Generators/libweb_bindings/rust_usage.py@@ -6,9 +6,9 @@# data that the bindings of both languages can convert, so it gets a representation in each language that names it,# wherever its IDL defines it.+from collections.abc import Iterablefrom dataclasses import dataclassfrom typing import Dict-from typing import Iterablefrom typing import Listfrom typing import Set@@ -134,6 +134,8 @@ class DefinitionUsage:]if members.indexed_property_getter is not None:operations.append(members.indexed_property_getter.operation.operation)+ if members.indexed_property_setter is not None:+ self._name_for_rust(members.indexed_property_setter.value_parameter.type, from_js=True, to_js=False)for operation in operations:self._name_for_rust(operation.return_type, from_js=False, to_js=True)for parameter in operation.parameters: