Skip to content

Truly independent web browser

LibWeb/Bindings: Convert WindowProxy values in the Rust bindings

Shannon Booth331defd84adb
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
index 2ebb0a5254c..1614529d7ff 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
@@ -9,6 +9,7 @@
use core::mem::MaybeUninit;
use core::ops::Range;
+use core::ptr::NonNull;
use std::borrow::Cow;
use ak::{Utf16String, Utf16StringUnits, convert_valid_utf16_to_utf8, utf8_length_from_valid_utf16};
@@ -24,7 +25,10 @@ use libjs_rust::api::{
use crate::ak_format::ak_formatted;
use crate::bindings::interface::{Interface, Ref, create_dictionary_object, wrap};
use crate::bindings::promise::WebIdlPromise;
-use crate::bindings::services::web_bindings_object_implements_interface;
+use crate::bindings::services::{
+ web_bindings_object_implements_interface, web_bindings_object_is_window_proxy,
+ web_bindings_window_proxy_object_in_realm,
+};
use crate::bindings::{CallContext, WebIdlResult};
/// A USVString, a string without lone surrogates. Its UTF-16 code units are always well-formed.
@@ -1412,6 +1416,36 @@ pub(crate) fn to_cpp_interface_in_union(
throw_no_union_types_matched(vm)
}
+/// The WindowProxy whose exotic object this is, if any.
+pub(crate) fn window_proxy_from_object(object: Gc<Object>) -> Option<CppInterface> {
+ // SAFETY: The object is live.
+ unsafe { web_bindings_object_is_window_proxy(object.as_ptr().cast()) }.then_some(CppInterface(object))
+}
+
+/// A WindowProxy converted from a JS value, https://webidl.spec.whatwg.org/#js-interface
+pub(crate) fn to_window_proxy(vm: &Vm, value: Value) -> ThrowCompletionOr<CppInterface> {
+ // 1. If V implements I, then return the IDL interface type value that represents a reference to that platform
+ // object.
+ if value.is_object()
+ && let Some(window_proxy) = window_proxy_from_object(value.as_object())
+ {
+ return Ok(window_proxy);
+ }
+ // 2. Throw a TypeError.
+ vm.throw_completion(ErrorKind::TypeError, ErrorType::NotAnObjectOfType, &[&"WindowProxy"])
+}
+
+/// A WindowProxy converted to a JS value, as the object that scripts of the realm see for it.
+pub(crate) fn window_proxy_to_js(cx: &CallContext, window_proxy: CppInterface) -> Value {
+ // SAFETY: The object and the realm are live.
+ let object = unsafe {
+ web_bindings_window_proxy_object_in_realm(window_proxy.0.as_ptr().cast(), cx.realm().as_ptr().cast())
+ };
+ let object = NonNull::new(object.cast::<Object>()).expect("a WindowProxy has an object");
+ // SAFETY: LibWeb returns a live object.
+ Value::from_object(unsafe { Gc::from_non_null(object) })
+}
+
/// https://webidl.spec.whatwg.org/#js-interface
impl ToJs for CppInterface {
fn to_js(self, _: &CallContext) -> ThrowCompletionOr<Value> {
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
index 4f3582681f2..719de81fa8b 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
@@ -64,6 +64,13 @@ unsafe extern "C" {
/// an interface that LibWeb implements in C++.
pub(crate) fn web_bindings_object_implements_interface(object: *mut c_void, interface_name: u16) -> bool;
+ /// Whether the object is the exotic object of a WindowProxy.
+ pub(crate) fn web_bindings_object_is_window_proxy(object: *mut c_void) -> bool;
+
+ /// The object that scripts of the realm see for the WindowProxy whose exotic object this is: that of the
+ /// WindowProxy of its browsing context for the wrapper world of the realm, or the object itself if it has none.
+ pub(crate) fn web_bindings_window_proxy_object_in_realm(object: *mut c_void, realm: *mut c_void) -> *mut c_void;
+
/// Runs the extract an origin operation of the object, https://html.spec.whatwg.org/multipage/browsers.html#extract-an-origin,
/// if it is a platform object of an interface implemented in C++ or a WindowProxy, and calls `on_origin` with the
/// origin, which it lends for the call, if that is not null. Returns whether it called `on_origin`.
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index 80a382c551c..85a6281b252 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -17,6 +17,7 @@
#include <LibWeb/Bindings/WrapperWorld.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/DocumentObserver.h>
+#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/CrossOrigin/AbstractOperations.h>
#include <LibWeb/HTML/CustomElements/CustomElementReactions.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
@@ -148,6 +149,8 @@ double web_bindings_environment_settings_object_current_wall_time(HTML::Environm
void web_bindings_environment_settings_object_encoding_parse_url(HTML::EnvironmentSettingsObject*, FlatPtr url, void* parsed_url, void (*on_url)(void*, URL::FFI::RustUrl const*));
void web_bindings_with_temporary_execution_context(void* realm, bool callbacks_enabled, void* context, void (*steps)(void*));
bool web_bindings_object_implements_interface(JSObject* object, u16 interface_name);
+bool web_bindings_object_is_window_proxy(JSObject* object);
+JSObject* web_bindings_window_proxy_object_in_realm(JSObject* object, void* realm);
JSObject* web_bindings_create_dom_exception(void* realm, ReadonlyBytes name, FlatPtr message);
JSCompletion web_bindings_perform_a_security_check(void* realm, JSValue platform_object, ReadonlyBytes identifier, u8 type);
bool web_bindings_extract_an_origin(JSObject* object, void* ctx, void (*on_origin)(void*, URL::FFI::FfiOrigin const*));
@@ -460,6 +463,20 @@ bool web_bindings_object_implements_interface(JSObject* object, u16 interface_na
return wrappable && wrappable->implements_interface(static_cast<InterfaceName>(interface_name));
}
+bool web_bindings_object_is_window_proxy(JSObject* object)
+{
+ return HTML::WindowProxy::from_object(*JS::HostABI::object_from_abi(object));
+}
+
+JSObject* web_bindings_window_proxy_object_in_realm(JSObject* object, void* realm)
+{
+ auto& js_realm = *static_cast<JS::Realm*>(realm);
+ auto& window_proxy = *HTML::WindowProxy::from_object(*JS::HostABI::object_from_abi(object));
+ if (auto browsing_context = window_proxy.associated_browsing_context())
+ return JS::HostABI::object_to_abi(&browsing_context->window_proxy_for(host_defined_wrapper_world(js_realm), js_realm)->object());
+ return object;
+}
+
JSObject* web_bindings_create_dom_exception(void* realm, ReadonlyBytes name, FlatPtr message)
{
auto& js_realm = *static_cast<JS::Realm*>(realm);
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index 464e7bda67a..ab9e23e3226 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -1154,7 +1154,8 @@ RUST_BINDINGS_IMPORTS = """ #![allow(unused_imports)]
to_long_long_clamping, to_long_long_enforcing_range, to_object, to_promise, to_shared_array_buffer, to_typed_array,
to_unrestricted_double, to_unrestricted_float, to_unsigned_long, to_unsigned_long_clamping,
to_unsigned_long_enforcing_range,
- to_unsigned_long_long, to_unsigned_long_long_enforcing_range, to_usv_string,
+ to_unsigned_long_long, to_unsigned_long_long_enforcing_range, to_usv_string, to_window_proxy,
+ window_proxy_from_object, window_proxy_to_js,
};
use crate::bindings::interface::{
Inherits, Interface, Ref, Wrapped, cell_class, construct_arguments, construct_wrapper, create_dictionary_object,
@@ -2576,7 +2577,8 @@ pub(crate) mod definitions {
to_long_enforcing_range, to_long_long, to_long_long_enforcing_range, to_object, to_shared_array_buffer, to_typed_array,
to_unrestricted_double, to_unrestricted_float, to_unsigned_long, to_unsigned_long_clamping,
to_unsigned_long_enforcing_range,
- to_unsigned_long_long, to_unsigned_long_long_enforcing_range, to_usv_string,
+ to_unsigned_long_long, to_unsigned_long_long_enforcing_range, to_usv_string, to_window_proxy,
+ window_proxy_from_object, window_proxy_to_js,
};
use crate::bindings::interface::{
Interface, Ref, create_dictionary_object, implementation_if, implementation_of, set_dictionary_member,
diff --git a/Meta/Generators/libweb_bindings/rust_to_idl_value.py b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
index 62df42a4b9e..bf3e368f53c 100644
--- a/Meta/Generators/libweb_bindings/rust_to_idl_value.py
+++ b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
@@ -32,6 +32,7 @@ from Generators.libweb_bindings.rust_types import is_promise_type
from Generators.libweb_bindings.rust_types import is_record_of_cells
from Generators.libweb_bindings.rust_types import is_sequence_of_cells
from Generators.libweb_bindings.rust_types import is_unnamed_union_of_cpp_interfaces
+from Generators.libweb_bindings.rust_types import is_window_proxy
from Generators.libweb_bindings.rust_types import member_other_than_undefined
from Generators.libweb_bindings.rust_types import resolved_dictionary
from Generators.libweb_bindings.rust_types import runtime_union_type
@@ -184,6 +185,8 @@ def rust_to_idl_value(
if inheriting_interfaces(interface, context):
return f"<{rust_any_interface_path(interface)}>::from_js(vm, {value})"
return f"to_interface::<{rust_interface_path(interface)}>(vm, {value})"
+ if is_window_proxy(idl_type):
+ return f"to_window_proxy(vm, {value})"
interface = cpp_interface(idl_type, context)
if interface is not None:
return f'to_cpp_interface(vm, {value}, {cpp_interface_name_constant(interface)}, "{interface.name}")'
@@ -305,7 +308,11 @@ def union_members_by_kind(union_type: IDLUnionType, context: GenerationContext,
members.enumeration_types.append(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 or rust_interface(member_type, context) is not None:
+ elif (
+ is_window_proxy(member_type)
+ or 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)
@@ -643,13 +650,21 @@ def write_union_from_js(out: TextIO, context: GenerationContext, union_type: IDL
)
steps.append(" if value.is_object() {\n")
for interface_type in members.interface_types:
- interface = context.interfaces[interface_type.name]
steps.append(
""" // 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 is_window_proxy(interface_type):
+ steps.append(
+ f""" if let Some(window_proxy) = window_proxy_from_object(value.as_object()) {{
+ return Ok(Self::{rust_union_variant(interface_type)}(window_proxy));
+ }}
+"""
+ )
+ continue
+ interface = context.interfaces[interface_type.name]
if rust_interface(interface_type, context) is not None and inheriting_interfaces(interface, context):
# NB: A wrapper of the interface wraps an object of the interface or of one that inherits from it,
# which the variant holds as such.
diff --git a/Meta/Generators/libweb_bindings/rust_to_js_value.py b/Meta/Generators/libweb_bindings/rust_to_js_value.py
index d5dc284c320..34a86381463 100644
--- a/Meta/Generators/libweb_bindings/rust_to_js_value.py
+++ b/Meta/Generators/libweb_bindings/rust_to_js_value.py
@@ -16,6 +16,7 @@ from Generators.libweb_bindings.rust_types import UnsupportedByRustBindings
from Generators.libweb_bindings.rust_types import dictionary_holds_sequence_of_cells
from Generators.libweb_bindings.rust_types import holds_cells
from Generators.libweb_bindings.rust_types import is_sequence_of_cells
+from Generators.libweb_bindings.rust_types import is_window_proxy
from Generators.libweb_bindings.rust_types import member_other_than_undefined
from Generators.libweb_bindings.rust_types import rust_dictionary_path
from Generators.libweb_bindings.rust_types import rust_enumeration_path
@@ -79,6 +80,8 @@ def rust_to_js_value(
"bindings do not convert yet"
)
return f"record_to_js({call_context}, {expression}, |value| {inner(value_type)})"
+ if is_window_proxy(idl_type):
+ return f"Ok(window_proxy_to_js({call_context}, {expression}))"
return f"{expression}.to_js({call_context})"
diff --git a/Meta/Generators/libweb_bindings/rust_types.py b/Meta/Generators/libweb_bindings/rust_types.py
index ba8430c9c8f..192cb62275e 100644
--- a/Meta/Generators/libweb_bindings/rust_types.py
+++ b/Meta/Generators/libweb_bindings/rust_types.py
@@ -224,6 +224,12 @@ def cpp_interface(idl_type: IDLType, context: GenerationContext) -> Optional[Int
return interface
+def is_window_proxy(idl_type: IDLType) -> bool:
+ """Whether the type is WindowProxy, which no IDL file defines. Rust holds its values as the objects that scripts
+ see."""
+ return not isinstance(idl_type, (IDLParameterizedType, IDLUnionType)) and idl_type.name == "WindowProxy"
+
+
def is_callback_interface(idl_type: IDLType, context: GenerationContext) -> bool:
"""Whether the type names a callback interface."""
if isinstance(idl_type, (IDLParameterizedType, IDLUnionType)):
@@ -511,7 +517,11 @@ def rust_type(idl_type: IDLType, context: GenerationContext, what: str, is_resul
# that an implementation returns is one of exactly the interface, whose cell it created.
return rust_any_interface_path(interface)
return f"Ref<{rust_interface_path(interface)}>"
- if cpp_interface(idl_type, context) is not None or is_unnamed_union_of_cpp_interfaces(idl_type, context):
+ if (
+ is_window_proxy(idl_type)
+ or cpp_interface(idl_type, context) is not None
+ or is_unnamed_union_of_cpp_interfaces(idl_type, context)
+ ):
return "CppInterface"
dictionary = resolved_dictionary(idl_type, context)
if dictionary is not None: