Skip to content

Truly independent web browser

Meta: Convert to unions with Rust interfaces in the Rust bindings

Andreas Kling53db62f91a9c
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index 6a22dc327a1..4e9fab244aa 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -806,6 +806,7 @@ pub(crate) mod definitions {
use ak::Utf16String;
use libjs_rust::api::{
ErrorKind, ErrorType, Gc, Object, PropertyKey, ThrowCompletionOr, TypedArrayBase, TypedArrayKind, Value, Vm,
+ is_host_instance_of,
};
use crate::bindings::CallContext;
@@ -817,7 +818,9 @@ pub(crate) mod definitions {
to_dom_string, to_double, to_enumeration_value, to_float, to_long, to_long_long, to_typed_array,
to_unrestricted_double, to_unrestricted_float, to_unsigned_long, to_unsigned_long_long, to_usv_string,
};
- use crate::bindings::interface::{Ref, create_dictionary_object, set_dictionary_member, to_interface};
+ use crate::bindings::interface::{
+ Interface, Ref, create_dictionary_object, implementation_of, set_dictionary_member, to_interface,
+ };
"""
)
for name, directions in usage.rust_dictionaries.items():
diff --git a/Meta/Generators/libweb_bindings/rust_to_idl_value.py b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
index 12ba630a31c..21c085ed300 100644
--- a/Meta/Generators/libweb_bindings/rust_to_idl_value.py
+++ b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
@@ -155,7 +155,8 @@ class UnionMembers:
string_type: Optional[IDLType] = None
sequence_type: Optional[IDLParameterizedType] = None
typed_array_types: List[IDLType] = field(default_factory=list)
- cpp_interface_types: List[IDLType] = field(default_factory=list)
+ # The interfaces, implemented in either language, in the order of the union.
+ interface_types: List[IDLType] = field(default_factory=list)
def union_members_by_kind(union_type: IDLUnionType, context: GenerationContext, what: str) -> UnionMembers:
@@ -167,8 +168,8 @@ def union_members_by_kind(union_type: IDLUnionType, context: GenerationContext,
members.string_type = member_type
elif member_type.name in TYPED_ARRAY_NAMES:
members.typed_array_types.append(member_type)
- elif cpp_interface(member_type, context) is not None:
- members.cpp_interface_types.append(member_type)
+ elif cpp_interface(member_type, context) is not None or rust_interface(member_type, context) is not None:
+ members.interface_types.append(member_type)
elif (
isinstance(member_type, IDLParameterizedType)
and member_type.name == "sequence"
@@ -333,7 +334,7 @@ def write_union_from_js(out: TextIO, context: GenerationContext, union_type: IDL
dictionary_conversion = (
f"Ok(Self::{rust_union_variant(dictionary_type)}(<{dictionary_path}>::from_js(vm, value)?))"
)
- if members.typed_array_types or members.sequence_type is not None or members.cpp_interface_types:
+ if members.typed_array_types or members.sequence_type is not None or members.interface_types:
if dictionary_conversion is not None:
steps.append(
f""" // 3. If V is null or undefined, then:
@@ -345,13 +346,25 @@ def write_union_from_js(out: TextIO, context: GenerationContext, union_type: IDL
"""
)
steps.append(" if value.is_object() {\n")
- for interface_type in members.cpp_interface_types:
+ for interface_type in members.interface_types:
interface = context.interfaces[interface_type.name]
steps.append(
- f""" // 5. If V is a platform object, then:
+ """ // 5. If V is a platform object, then:
// 1. If types includes an interface type that V implements, then return the IDL value that is a
// reference to the object V.
- if implements_cpp_interface(value.as_object(), {cpp_interface_name_constant(interface)}) {{
+"""
+ )
+ if rust_interface(interface_type, context) is not None:
+ interface_path = rust_interface_path(interface)
+ steps.append(
+ f""" if is_host_instance_of(&value.as_object(), <{interface_path} as Interface>::wrapper_class()) {{
+ return Ok(Self::{rust_union_variant(interface_type)}(implementation_of::<{interface_path}>(value.as_object())));
+ }}
+"""
+ )
+ continue
+ steps.append(
+ f""" if implements_cpp_interface(value.as_object(), {cpp_interface_name_constant(interface)}) {{
// SAFETY: The object is a wrapper of an object that implements the interface.
let object = unsafe {{ CppInterface::from_wrapper(value.as_object()) }};
return Ok(Self::{rust_union_variant(interface_type)}(object));