Truly independent web browser
Meta: Support callback interface types in the Rust bindings
Andreas Kling967246f3cf0e
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/callback.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/callback.rsindex 9fd13bce0e2..e55e3d207f2 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/callback.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/callback.rs@@ -4,8 +4,9 @@* SPDX-License-Identifier: BSD-2-Clause*/-//! Callback function values, https://webidl.spec.whatwg.org/#idl-callback-function, which LibWeb's C++ code-//! represents as a WebIDL::CallbackType: the function object and its callback context.+//! Callback function values, https://webidl.spec.whatwg.org/#idl-callback-function, and callback interface values,+//! https://webidl.spec.whatwg.org/#idl-callback-interface, which LibWeb's C++ code represents as a WebIDL::CallbackType:+//! the object and its callback context.use core::ffi::c_void;use core::ptr::NonNull;@@ -17,7 +18,7 @@ use libjs_rust::api::{use crate::bindings::promise::{WebIdlPromise, promise_from_abi};unsafe extern "C" {- /// A new WebIDL::CallbackType of the callable object, whose callback context is the incumbent settings object.+ /// A new WebIDL::CallbackType of the object, whose callback context is the incumbent settings object.fn web_bindings_create_callback_function(callable: *mut c_void, returns_promise: bool) -> *mut c_void;/// WebIDL::invoke_callback() of the WebIDL::CallbackType with the this value, if `has_this_value`, and the@@ -49,7 +50,12 @@ pub(crate) struct CallbackFunction(CallbackType);#[gc(crate = libjs_rust)]pub(crate) struct PromiseCallbackFunction(CallbackType);-/// The WebIDL::CallbackType of a callback function value.+/// A callback interface value, https://webidl.spec.whatwg.org/#idl-callback-interface+#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct CallbackInterface(CallbackType);++/// The WebIDL::CallbackType of a callback function value or a callback interface value.#[derive(Trace)]#[gc(crate = libjs_rust)]struct CallbackType(ForeignCellSlot);@@ -114,6 +120,31 @@ impl CallbackFunction {}}+impl CallbackInterface {+ // https://webidl.spec.whatwg.org/#js-callback-interface+ pub(crate) fn from_js(vm: &Vm, value: Value) -> ThrowCompletionOr<Self> {+ // 1. If V is not an Object, then throw a TypeError.+ if !value.is_object() {+ return vm.throw_completion(ErrorKind::TypeError, ErrorType::NotAnObject, &[&value]);+ }++ // 2. Return the IDL callback interface type value that represents a reference to V, with the incumbent realm+ // as the callback context.+ // SAFETY: The value is a live object.+ Ok(Self(unsafe {+ CallbackType::from_raw(web_bindings_create_callback_function(+ value.as_object().as_ptr().cast(),+ false,+ ))+ }))+ }++ /// The WebIDL::CallbackType, which this keeps alive.+ pub(crate) fn as_ptr(&self) -> *mut c_void {+ self.0.0.as_ptr()+ }+}+impl PromiseCallbackFunction {/// The callback function value of a JS value, https://webidl.spec.whatwg.org/#js-callback-functionpub(crate) fn from_js(vm: &Vm, value: Value) -> ThrowCompletionOr<Self> {diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.pyindex 1d4be482075..3fa0e0aeca7 100644--- a/Meta/Generators/libweb_bindings/rust_bindings.py+++ b/Meta/Generators/libweb_bindings/rust_bindings.py@@ -885,7 +885,7 @@ RUST_BINDINGS_IMPORTS = """ #![allow(unused_imports)]DefaultIterator, DefaultIteratorClasses, define_pair_iterator_properties, entries, for_each, iterator_next, keys,values,};- use crate::bindings::callback::{CallbackFunction, PromiseCallbackFunction};+ use crate::bindings::callback::{CallbackFunction, CallbackInterface, PromiseCallbackFunction};use crate::bindings::event_target::{event_handler_attribute, set_event_handler_attribute};use crate::bindings::promise::WebIdlPromise;use crate::bindings::structured_data;@@ -1991,7 +1991,7 @@ pub(crate) mod definitions {};use crate::bindings::CallContext;- use crate::bindings::callback::{CallbackFunction, PromiseCallbackFunction};+ use crate::bindings::callback::{CallbackFunction, CallbackInterface, PromiseCallbackFunction};use crate::bindings::conversions::{ArrayBufferView, BufferSource, BufferSourceRules, CppInterface, EmptyDictionary, FromJs, ToJs, UsvString,buffer_source_if, dictionary_member, dictionary_object, enumeration_value_to_js, implements_cpp_interface,diff --git a/Meta/Generators/libweb_bindings/rust_to_idl_value.py b/Meta/Generators/libweb_bindings/rust_to_idl_value.pyindex c319c27daec..08720af5885 100644--- a/Meta/Generators/libweb_bindings/rust_to_idl_value.py+++ b/Meta/Generators/libweb_bindings/rust_to_idl_value.py@@ -26,6 +26,7 @@ from Generators.libweb_bindings.rust_types import cpp_interface_name_constantfrom Generators.libweb_bindings.rust_types import dictionary_holds_sequence_of_cellsfrom Generators.libweb_bindings.rust_types import holds_cellsfrom Generators.libweb_bindings.rust_types import inheriting_interfaces+from Generators.libweb_bindings.rust_types import is_callback_interfacefrom Generators.libweb_bindings.rust_types import is_named_unionfrom Generators.libweb_bindings.rust_types import is_promise_typefrom Generators.libweb_bindings.rust_types import is_sequence_of_cells@@ -158,6 +159,8 @@ def rust_to_idl_value(if is_promise_type(callback_function.return_type):return f"PromiseCallbackFunction::from_js(vm, {value})"return f"CallbackFunction::from_js(vm, {value})"+ if is_callback_interface(idl_type, context):+ return f"CallbackInterface::from_js(vm, {value})"interface = rust_interface(idl_type, context)if interface is not None:if inheriting_interfaces(interface, context):diff --git a/Meta/Generators/libweb_bindings/rust_types.py b/Meta/Generators/libweb_bindings/rust_types.pyindex e7354219ee7..564d7ca186a 100644--- a/Meta/Generators/libweb_bindings/rust_types.py+++ b/Meta/Generators/libweb_bindings/rust_types.py@@ -171,6 +171,14 @@ def cpp_interface(idl_type: IDLType, context: GenerationContext) -> Optional[Intreturn interface+def is_callback_interface(idl_type: IDLType, context: GenerationContext) -> bool:+ """Whether the type names a callback interface."""+ if isinstance(idl_type, (IDLParameterizedType, IDLUnionType)):+ return False+ interface = context.interfaces.get(idl_type.name)+ return interface is not None and interface.is_callback_interface++def cpp_interface_name_constant(interface: Interface) -> str:"""The constant of the generated Rust bindings that is the Web::Bindings::InterfaceName of an interface thatLibWeb implements in C++."""@@ -436,6 +444,8 @@ def rust_type(idl_type: IDLType, context: GenerationContext, what: str, is_resulif is_result:return "Gc<FunctionObject>"return "PromiseCallbackFunction" if is_promise_type(callback_function.return_type) else "CallbackFunction"+ if is_callback_interface(idl_type, context) and not is_result:+ return "CallbackInterface"interface = rust_interface(idl_type, context)if interface is not None:if inheriting_interfaces(interface, context) and not is_result: