Skip to content

Truly independent web browser

Meta+LibWeb: Throw DOMExceptions from the Rust bindings

Andreas Kling29e8ef970ebb
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs
index 49135b1ae7c..6de307c5ecb 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs
@@ -21,8 +21,12 @@ pub(crate) mod interface;
pub(crate) mod realm_data;
pub(crate) mod services;
+use core::ptr::NonNull;
+
use ak::Utf16String;
-use libjs_rust::api::{ErrorKind, Gc, Realm, Throw, ThrowCompletionOr, Vm};
+use libjs_rust::api::{ErrorKind, Gc, Object, Realm, Throw, ThrowCompletionOr, Value, Vm};
+
+use crate::bindings::services::web_bindings_create_dom_exception;
/// What the bindings pass the implementation of an interface: the VM, and the realm that the implementation creates
/// its results in. For an attribute or an operation, that is the realm of the object it runs on; for a constructor,
@@ -53,6 +57,10 @@ pub(crate) enum Exception {
/// A simple exception with the message, which the bindings create in the current realm.
TypeError(Utf16String),
RangeError(Utf16String),
+ /// A DOMException with the name, one of https://webidl.spec.whatwg.org/#dfn-error-names-table, and the message,
+ /// which the bindings create in the realm of the call context, as the C++ bindings create the DOMExceptions of
+ /// the implementations they call.
+ Dom(&'static str, Utf16String),
}
impl From<Throw> for Exception {
@@ -62,11 +70,33 @@ impl From<Throw> for Exception {
}
impl Exception {
- pub(crate) fn throw(self, vm: &Vm) -> Throw {
+ pub(crate) fn index_size_error(message: Utf16String) -> Self {
+ Self::Dom("IndexSizeError", message)
+ }
+
+ pub(crate) fn syntax_error(message: Utf16String) -> Self {
+ Self::Dom("SyntaxError", message)
+ }
+
+ pub(crate) fn throw(self, cx: &CallContext) -> Throw {
+ let vm = cx.vm();
match self {
Self::Thrown(throw) => throw,
Self::TypeError(message) => thrown(vm.throw_completion_with_utf16_message(ErrorKind::TypeError, message)),
Self::RangeError(message) => thrown(vm.throw_completion_with_utf16_message(ErrorKind::RangeError, message)),
+ Self::Dom(name, message) => {
+ // SAFETY: The realm is live, and the name and the message are lent for the call.
+ let exception = unsafe {
+ web_bindings_create_dom_exception(
+ cx.realm().as_ptr().cast(),
+ ak::Span::new(name.as_bytes()),
+ message.raw_identity(),
+ )
+ };
+ let exception = NonNull::new(exception.cast::<Object>()).expect("LibWeb returns the DOMException");
+ // SAFETY: LibWeb returns a live wrapper.
+ Throw::new(Value::from_object(unsafe { Gc::from_non_null(exception) }))
+ }
}
}
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
index 6a92d4662dd..0eb710d1ae9 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
@@ -33,6 +33,14 @@ 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;
+ /// The wrapper in the realm of a new DOMException with the name, which is ASCII, and the message, a borrowed
+ /// AK::Utf16String.
+ pub(crate) fn web_bindings_create_dom_exception(
+ realm: *mut c_void,
+ name: ak::Span<'_, u8>,
+ message: usize,
+ ) -> *mut c_void;
+
/// A promise rejected with the reason, in the realm of the this value of an operation that ran in the current
/// realm: the realm of the object, or the current realm for a value that is not an object.
pub(crate) fn web_bindings_rejected_promise(
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index 3cf3b3998a4..b7a10abbb74 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -7,8 +7,11 @@
#include <LibJS/HostClassBuilder.h>
#include <LibJS/Runtime/HostObject.h>
#include <LibJS/Runtime/Realm.h>
+#include <LibWeb/Bindings/DOMException.h>
#include <LibWeb/Bindings/RustInterface.h>
+#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Bindings/WrapperWorld.h>
+#include <LibWeb/WebIDL/DOMException.h>
#include <LibWeb/WebIDL/Promise.h>
// What the Rust bindings of the interfaces implemented in Rust ask of LibWeb's C++ bindings, declared on the Rust
@@ -38,6 +41,7 @@ JSObject* web_bindings_rust_wrapper_for(void* realm, void* cell);
void web_bindings_set_rust_wrapper(void* realm, void* cell, JSObject* wrapper);
JSObject* web_bindings_rejected_promise(void* current_realm, JSValue this_value, JSValue reason);
bool web_bindings_object_implements_interface(JSObject* object, u16 interface_name);
+JSObject* web_bindings_create_dom_exception(void* realm, ReadonlyBytes name, FlatPtr message);
void* web_bindings_wrapper_world(void* realm, bool* is_main_world)
{
@@ -65,6 +69,13 @@ bool web_bindings_object_implements_interface(JSObject* object, u16 interface_na
return wrappable && wrappable->implements_interface(static_cast<InterfaceName>(interface_name));
}
+JSObject* web_bindings_create_dom_exception(void* realm, ReadonlyBytes name, FlatPtr message)
+{
+ auto& js_realm = *static_cast<JS::Realm*>(realm);
+ auto exception = WebIDL::DOMException::create(FlyString::from_utf8_without_validation(name), Utf16String::from_raw(message));
+ return JS::HostABI::object_to_abi(wrap(host_defined_wrapper_world(js_realm), js_realm, exception).ptr());
+}
+
JSObject* web_bindings_rejected_promise(void* current_realm, JSValue this_value, JSValue reason)
{
auto& realm = *static_cast<JS::Realm*>(current_realm);
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index 4af68fff863..6a22dc327a1 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -454,7 +454,7 @@ def write_rust_interface(
fn {attribute.getter_function}(vm: &Vm) -> ThrowCompletionOr<Value> {{
let (implementation, mut cx) = this_implementation(vm)?;
let result = Implementation::{attribute.getter_method}(&implementation, &mut cx)
- .map_err(|exception| exception.throw(vm))?;
+ .map_err(|exception| exception.throw(&cx))?;
{conversion}
}}
"""
@@ -679,7 +679,7 @@ def write_rust_attribute_setter(
let idl_value = {conversion}?;
drop(type_error_realm_scope);
Implementation::{attribute.setter_method}(&implementation, &mut cx, idl_value)
- .map_err(|exception| exception.throw(vm))?;
+ .map_err(|exception| exception.throw(&cx))?;
Ok(Value::UNDEFINED)
}}
"""
@@ -707,7 +707,7 @@ def write_rust_operation(
# NB: An operation that returns nothing has no result to name, and the conversion of nothing is undefined.
steps.write(
f""" {call}
- .map_err(|exception| exception.throw(vm))?;
+ .map_err(|exception| exception.throw(&cx))?;
Ok(Value::UNDEFINED)
"""
)
@@ -718,7 +718,7 @@ def write_rust_operation(
conversion = rust_to_js_value(operation.return_type, "result", "&cx", context, what)
steps.write(
f""" let result = {call}
- .map_err(|exception| exception.throw(vm))?;
+ .map_err(|exception| exception.throw(&cx))?;
{conversion}
"""
)
@@ -760,7 +760,7 @@ def write_rust_constructor(
out.write(
f""" let mut cx = CallContext::new(vm, realm);
let implementation =
- Implementation::{constructor.method}(&mut cx{arguments}).map_err(|exception| exception.throw(vm))?;
+ Implementation::{constructor.method}(&mut cx{arguments}).map_err(|exception| exception.throw(&cx))?;
Ok(Value::from_object(construct_wrapper(vm, realm, prototype, implementation)))
}}
"""