Truly independent web browser
Meta: Convert arguments to inherited Rust interfaces
Shannon Boothe5baa163234d
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.pyindex 862ce9025fd..791aecaf41d 100644--- a/Meta/Generators/libweb_bindings/rust_bindings.py+++ b/Meta/Generators/libweb_bindings/rust_bindings.py@@ -40,6 +40,7 @@ from Generators.libweb_bindings.rust_overloads import write_constructor_overloadfrom Generators.libweb_bindings.rust_overloads import write_operation_overload_resolutionfrom Generators.libweb_bindings.rust_to_idl_value import rust_default_valuefrom Generators.libweb_bindings.rust_to_idl_value import rust_to_idl_value+from Generators.libweb_bindings.rust_to_idl_value import write_any_interface_from_jsfrom Generators.libweb_bindings.rust_to_idl_value import write_dictionary_from_jsfrom Generators.libweb_bindings.rust_to_idl_value import write_enumeration_from_jsfrom Generators.libweb_bindings.rust_to_idl_value import write_union_from_js@@ -1765,7 +1766,12 @@ pub(crate) mod cpp_interface_names {def write_rust_definitions(out: TextIO, context: GenerationContext, usage: DefinitionUsage) -> None:"""The conversions of the dictionaries and unions that the interfaces implemented in Rust name, each written once,whichever interfaces name it."""- if not usage.rust_dictionaries and not usage.rust_unions and not usage.rust_enumerations:+ if (+ not usage.rust_dictionaries+ and not usage.rust_unions+ and not usage.rust_enumerations+ and not usage.rust_any_interfaces+ ):returnout.write("""@@ -1817,6 +1823,8 @@ pub(crate) mod definitions {write_enumeration_from_js(out, context, enumeration)if directions.to_js:write_enumeration_to_js(out, context, enumeration)+ for interface in usage.rust_any_interfaces.values():+ write_any_interface_from_js(out, interface, context)for union_type, directions in usage.rust_unions.items():if directions.from_js:write_union_from_js(out, context, union_type)diff --git a/Meta/Generators/libweb_bindings/rust_to_idl_value.py b/Meta/Generators/libweb_bindings/rust_to_idl_value.pyindex 40307237a79..11e6abf47ad 100644--- a/Meta/Generators/libweb_bindings/rust_to_idl_value.py+++ b/Meta/Generators/libweb_bindings/rust_to_idl_value.py@@ -49,6 +49,7 @@ from Utils.webidl_parser import Enumerationfrom Utils.webidl_parser import IDLParameterizedTypefrom Utils.webidl_parser import IDLTypefrom Utils.webidl_parser import IDLUnionType+from Utils.webidl_parser import Interfacefrom Utils.webidl_parser import OperationParameterSTRING_TYPES = ("DOMString", "USVString")@@ -154,10 +155,7 @@ def rust_to_idl_value(interface = rust_interface(idl_type, context)if interface is not None:if inheriting_interfaces(interface, context):- raise UnsupportedByRustBindings(- f"{what} has the type {idl_type}, an interface that others inherit from, which the Rust bindings only "- "convert to as a member of a union yet"- )+ return f"<{rust_any_interface_path(interface)}>::from_js(vm, {value})"return f"to_interface::<{rust_interface_path(interface)}>(vm, {value})"interface = cpp_interface(idl_type, context)if interface is not None:@@ -449,6 +447,36 @@ def write_enumeration_from_js(out: TextIO, context: GenerationContext, enumerati)+def write_any_interface_from_js(out: TextIO, interface: Interface, context: GenerationContext) -> None:+ """The conversion of a JS value to the enum of an object of an interface that others inherit from,+ rust_any_interface_path(), whose variant is the interface of the object."""+ interface_path = rust_interface_path(interface)+ arms = "".join(+ f""" if let Some(object) = implementation_if::<{rust_interface_path(implementing)}>(value.as_object()) {{+ return Ok(Self::{implementing.name}(object));+ }}+"""+ for implementing in [interface] + inheriting_interfaces(interface, context)+ )+ out.write(+ f"""+ /// The conversion of a JS value to an object of the interface {interface.name} or of one that inherits from it,+ /// https://webidl.spec.whatwg.org/#js-interface+ impl FromJs for {rust_any_interface_path(interface)} {{+ fn from_js(vm: &Vm, value: Value) -> ThrowCompletionOr<Self> {{+ // 1. If V implements I, then return the IDL interface type value that represents a reference to that+ // platform object.+ if value.is_object() && is_host_instance_of(&value.as_object(), <{interface_path} as Interface>::wrapper_class()) {{+{arms} unreachable!("a wrapper of the interface wraps an object of it or of an interface that inherits from it");+ }}+ // 2. Throw a TypeError.+ vm.throw_completion(ErrorKind::TypeError, ErrorType::NotAnObjectOfType, &[&"{interface.name}"])+ }}+ }}+"""+ )++def write_union_from_js(out: TextIO, context: GenerationContext, union_type: IDLUnionType) -> None:"""The conversion of a JS value to the enum of a union, with the steps of https://webidl.spec.whatwg.org/#js-unionthat apply to its members, in the order of the C++ bindings."""diff --git a/Meta/Generators/libweb_bindings/rust_usage.py b/Meta/Generators/libweb_bindings/rust_usage.pyindex ee84a8f9d0d..9311251e763 100644--- a/Meta/Generators/libweb_bindings/rust_usage.py+++ b/Meta/Generators/libweb_bindings/rust_usage.py@@ -15,9 +15,11 @@ from typing import Setfrom Generators.libweb_bindings.context import GenerationContextfrom Generators.libweb_bindings.rust_members import rust_membersfrom Generators.libweb_bindings.rust_types import cpp_interface+from 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_named_unionfrom Generators.libweb_bindings.rust_types import runtime_union_type+from Generators.libweb_bindings.rust_types import rust_interfacefrom Utils.webidl_parser import Dictionaryfrom Utils.webidl_parser import IDLTypefrom Utils.webidl_parser import IDLUnionType@@ -93,6 +95,9 @@ class DefinitionUsage:self.cpp_dictionary_names: Set[str] = set()# The interfaces implemented in Rust that the C++ bindings convert objects of, and so need a C++ class for.self.rust_interfaces_named_by_cpp: Dict[str, Interface] = {}+ # The interfaces implemented in Rust that others inherit from and that the interfaces implemented in Rust+ # convert JS values to.+ self.rust_any_interfaces: Dict[str, Interface] = {}self._collect_rust_dictionaries()self._collect_cpp_dictionaries()self._collect_rust_interfaces_named_by_cpp()@@ -160,6 +165,11 @@ class DefinitionUsage:if named_cpp_interface is not None:self.cpp_interfaces_named_by_rust[named_cpp_interface.name] = named_cpp_interfacereturn+ named_rust_interface = rust_interface(idl_type, self.context)+ if named_rust_interface is not None:+ if from_js and inheriting_interfaces(named_rust_interface, self.context):+ self.rust_any_interfaces[named_rust_interface.name] = named_rust_interface+ returnif idl_type.name in self.context.enumerations:directions = self.rust_enumerations.setdefault(idl_type.name, ConversionDirections())directions.from_js = directions.from_js or from_js