Skip to content

Truly independent web browser

LibWeb: Serialize DOMExceptions from Rust

Shannon Booth33c24811e7b8
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/cpp.rs b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/cpp.rs
index 24232a564de..d0aa44cdb5e 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/cpp.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/cpp.rs
@@ -159,7 +159,7 @@ unsafe extern "C" {
memory: *mut c_void,
) -> RawNativeFunctionResult;
fn web_structured_serialize_is_exposed(interface_name: u16, realm: *mut c_void) -> bool;
- fn web_structured_serialize_serializable_interface_is_implemented_in_rust(interface_name: u16) -> bool;
+ fn web_structured_serialize_serializable_steps(interface_name: u16) -> u8;
fn web_structured_serialize_deserialize_rust_platform_object(
interface_name: u16,
realm: *mut c_void,
@@ -355,6 +355,14 @@ impl StructuredSerializeWriter {
unsafe { web_structured_serialize_writer_encode_f64(self.as_raw(), value) };
}
+ /// An Optional<double>, as HTML::StructuredSerializeWriter encodes one: whether it has a value, then the value.
+ pub(crate) fn encode_optional_f64(self, value: Option<f64>) {
+ self.encode_bool(value.is_some());
+ if let Some(value) = value {
+ self.encode_f64(value);
+ }
+ }
+
pub(crate) fn encode_utf16_string(self, string: &Utf16String) {
// SAFETY: The writer is lent, and the call borrows the string.
unsafe { web_structured_serialize_writer_encode_utf16_string(self.as_raw(), string.raw_identity()) };
@@ -540,6 +548,13 @@ impl StructuredSerializeReader {
Ok(value)
}
+ pub(crate) fn decode_optional_f64(self) -> WebIdlResult<Option<f64>> {
+ if !self.decode_bool()? {
+ return Ok(None);
+ }
+ Ok(Some(self.decode_f64()?))
+ }
+
pub(crate) fn decode_utf16_string(self) -> WebIdlResult<Utf16String> {
let mut string = 0;
// SAFETY: The reader is lent, and the realm is live.
@@ -838,9 +853,28 @@ pub(crate) fn is_exposed(interface_name: u16, realm: Gc<Realm>) -> bool {
unsafe { web_structured_serialize_is_exposed(interface_name, realm.as_ptr().cast()) }
}
-pub(crate) fn serializable_interface_is_implemented_in_rust(interface_name: u16) -> bool {
+/// Whose steps serialize and deserialize the objects of a serializable interface.
+#[derive(Clone, Copy, PartialEq, Eq)]
+pub(crate) enum SerializableSteps {
+ /// The steps of an interface implemented in Rust, which its bindings run.
+ Rust,
+ /// The steps of DOMException, which run in crate::webidl::dom_exception.
+ DOMException,
+ /// The steps of QuotaExceededError, which run in crate::webidl::quota_exceeded_error.
+ QuotaExceededError,
+ /// The steps of an interface implemented in C++, which C++ runs.
+ Cpp,
+}
+
+pub(crate) fn serializable_steps(interface_name: u16) -> SerializableSteps {
// SAFETY: The call takes a value.
- unsafe { web_structured_serialize_serializable_interface_is_implemented_in_rust(interface_name) }
+ match unsafe { web_structured_serialize_serializable_steps(interface_name) } {
+ 0 => SerializableSteps::Rust,
+ 1 => SerializableSteps::DOMException,
+ 2 => SerializableSteps::QuotaExceededError,
+ 3 => SerializableSteps::Cpp,
+ steps => unreachable!("LibWeb names no serializable steps {steps}"),
+ }
}
/// The deserialization steps of an interface implemented in Rust, which create the new instance from what they read.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rs
index 936428c603d..1556045707c 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rs
@@ -30,15 +30,17 @@ use libjs_rust::api::{
use crate::bindings::{CallContext, Exception, WebIdlResult};
use crate::html::structured_serialize::cpp::{
DeserializationMemory, OwnedDeserializationMemory, OwnedSerializedTransferRecord, OwnedStructuredSerializeWriter,
- SerializationMemory, SerializationType, SerializedTransferRecord, StructuredSerializeReader,
+ SerializableSteps, SerializationMemory, SerializationType, SerializedTransferRecord, StructuredSerializeReader,
StructuredSerializeWriter, TransferDataDecoder, TransferDataHolder, create_serialized_platform_object,
create_transferred_platform_object, current_settings_object_agent_cluster,
current_settings_object_cross_origin_isolated_capability, data_clone_error_from_serialization_error,
deserialization_steps, deserialize_rust_platform_object, is_exposed, is_platform_object, is_transferable,
- is_transferable_interface_exposed, realm_agent_cluster, serializable_interface_is_implemented_in_rust,
- serializable_interface_name, serializable_payload_version, serialization_steps, transfer_steps,
- transferable_is_detached, transferable_primary_interface, transferable_set_detached,
+ is_transferable_interface_exposed, realm_agent_cluster, serializable_interface_name, serializable_payload_version,
+ serializable_steps, serialization_steps, transfer_steps, transferable_is_detached, transferable_primary_interface,
+ transferable_set_detached,
};
+use crate::webidl::dom_exception::{self, DOMExceptionFields};
+use crate::webidl::quota_exceeded_error::{self, QuotaExceededErrorFields};
/// HTML::ValueTag, the tags of the values of a record, which are part of the stable storage serialization format.
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -842,15 +844,25 @@ impl Serializer<'_> {
}
}
// 3. Otherwise, if value is a platform object that is a serializable object, then perform the serialization steps for value's primary interface, given value, serialized, and forStorage.
- else if serializable_interface_name(object).is_some() {
- let realm = self.realm.unwrap_or_else(|| object.shape().realm());
- serialization_steps(
- &CallContext::new(vm, realm),
- object,
- serialized,
- self.for_storage,
- self.memory,
- )?;
+ else if let Some(interface_name) = serializable_interface_name(object) {
+ match serializable_steps(interface_name) {
+ SerializableSteps::DOMException => {
+ dom_exception::serialization_steps(&DOMExceptionFields::of(object), serialized);
+ }
+ SerializableSteps::QuotaExceededError => {
+ quota_exceeded_error::serialization_steps(&QuotaExceededErrorFields::of(object), serialized);
+ }
+ SerializableSteps::Rust | SerializableSteps::Cpp => {
+ let realm = self.realm.unwrap_or_else(|| object.shape().realm());
+ serialization_steps(
+ &CallContext::new(vm, realm),
+ object,
+ serialized,
+ self.for_storage,
+ self.memory,
+ )?;
+ }
+ }
}
// 4. Otherwise, for each key in ! EnumerableOwnProperties(value, key):
else {
@@ -1356,9 +1368,11 @@ impl Deserializer<'_, '_> {
return Err(data_clone_error("Unsupported type"));
}
- // NB: The deserialization steps of an interface implemented in Rust create the new instance from what they
- // read, so they run here, at the position in memory that the instance has.
- if serializable_interface_is_implemented_in_rust(interface_name) {
+ // NB: The deserialization steps of an interface implemented in Rust, and those that run here for
+ // DOMException and QuotaExceededError, create the new instance from what they read, so they run
+ // here, at the position in memory that the instance has.
+ let steps = serializable_steps(interface_name);
+ if steps != SerializableSteps::Cpp {
if version.is_some_and(|version| version != serializable_payload_version()) {
return Err(data_clone_error_from_serialization_error(
realm,
@@ -1367,7 +1381,17 @@ impl Deserializer<'_, '_> {
}
let memory_index = self.memory.size();
self.memory.append(Value::UNDEFINED);
- let value = deserialize_rust_platform_object(interface_name, self.serialized, self.memory)?;
+ let value = match steps {
+ SerializableSteps::DOMException => {
+ let fields = dom_exception::deserialization_steps(self.serialized)?;
+ Value::from_object(dom_exception::create(realm, &fields))
+ }
+ SerializableSteps::QuotaExceededError => {
+ let fields = quota_exceeded_error::deserialization_steps(self.serialized)?;
+ Value::from_object(quota_exceeded_error::create(realm, &fields))
+ }
+ _ => deserialize_rust_platform_object(interface_name, self.serialized, self.memory)?,
+ };
self.memory.set(memory_index, value);
return Ok(value);
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/lib.rs b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
index 920eecbb876..27185a22412 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/lib.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
@@ -143,6 +143,8 @@ pub(crate) mod web_assembly;
reason = "the bindings call every attribute and operation as one that can throw"
)]
pub(crate) mod web_gl;
+#[cfg(not(test))]
+pub(crate) mod webidl;
// The generators of the build script, whose tests run with those of the crate.
#[cfg(test)]
diff --git a/Libraries/LibWeb/Bindings/Rust/src/webidl/dom_exception.rs b/Libraries/LibWeb/Bindings/Rust/src/webidl/dom_exception.rs
new file mode 100644
index 00000000000..8a747d2d33f
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/webidl/dom_exception.rs
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2022, Andreas Kling <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The serialization steps of DOMException, https://webidl.spec.whatwg.org/#idl-DOMException, which LibWeb implements in
+//! C++ (Libraries/LibWeb/WebIDL/DOMException.cpp).
+
+use core::ffi::c_void;
+use core::ptr::NonNull;
+
+use ak::Utf16String;
+use libjs_rust::api::{Gc, Object, Realm};
+
+use crate::bindings::WebIdlResult;
+use crate::html::structured_serialize::cpp::{StructuredSerializeReader, StructuredSerializeWriter};
+
+unsafe extern "C" {
+ fn web_dom_exception_name_and_message(exception: *mut c_void, name: &mut usize, message: &mut usize);
+ fn web_dom_exception_create(realm: *mut c_void, name: usize, message: usize) -> *mut c_void;
+}
+
+/// The name and the message of a DOMException.
+pub(crate) struct DOMExceptionFields {
+ pub(crate) name: Utf16String,
+ pub(crate) message: Utf16String,
+}
+
+impl DOMExceptionFields {
+ /// The fields of the DOMException, or of an object of an interface that inherits from it.
+ pub(crate) fn of(exception: Gc<Object>) -> Self {
+ let (mut name, mut message) = (0, 0);
+ // SAFETY: The object is a live DOMException.
+ unsafe { web_dom_exception_name_and_message(exception.as_ptr().cast(), &mut name, &mut message) };
+ // SAFETY: LibWeb hands over the strings.
+ unsafe {
+ Self {
+ name: Utf16String::from_raw_owned(name),
+ message: Utf16String::from_raw_owned(message),
+ }
+ }
+ }
+}
+
+/// A new DOMException with the fields, created in the realm.
+pub(crate) fn create(realm: Gc<Realm>, fields: &DOMExceptionFields) -> Gc<Object> {
+ // SAFETY: The realm is live, and the call borrows the strings.
+ let exception = unsafe {
+ web_dom_exception_create(
+ realm.as_ptr().cast(),
+ fields.name.raw_identity(),
+ fields.message.raw_identity(),
+ )
+ };
+ // SAFETY: LibWeb returns a live object.
+ unsafe { Gc::from_non_null(NonNull::new(exception.cast()).expect("LibWeb creates the exception")) }
+}
+
+// https://webidl.spec.whatwg.org/#idl-DOMException
+pub(crate) fn serialization_steps(value: &DOMExceptionFields, serialized: StructuredSerializeWriter) {
+ // 1. Set serialized.[[Name]] to value’s name.
+ serialized.encode_utf16_string(&value.name);
+
+ // 2. Set serialized.[[Message]] to value’s message.
+ serialized.encode_utf16_string(&value.message);
+
+ // FIXME: 3. User agents should attach a serialized representation of any interesting accompanying data which are not yet specified, notably the stack property, to serialized.
+}
+
+// https://webidl.spec.whatwg.org/#idl-DOMException
+pub(crate) fn deserialization_steps(serialized: StructuredSerializeReader) -> WebIdlResult<DOMExceptionFields> {
+ // 1. Set value’s name to serialized.[[Name]].
+ let name = serialized.decode_utf16_string()?;
+
+ // 2. Set value’s message to serialized.[[Message]].
+ let message = serialized.decode_utf16_string()?;
+
+ // FIXME: 3. If any other data is attached to serialized, then deserialize and attach it to value.
+
+ Ok(DOMExceptionFields { name, message })
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/webidl/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/webidl/mod.rs
new file mode 100644
index 00000000000..985f2912687
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/webidl/mod.rs
@@ -0,0 +1,10 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The serialization steps of the serializable interfaces of Web IDL, which LibWeb implements in C++.
+
+pub(crate) mod dom_exception;
+pub(crate) mod quota_exceeded_error;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/webidl/quota_exceeded_error.rs b/Libraries/LibWeb/Bindings/Rust/src/webidl/quota_exceeded_error.rs
new file mode 100644
index 00000000000..359a6b63a7c
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/webidl/quota_exceeded_error.rs
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2025, Shannon Booth <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The serialization steps of QuotaExceededError, https://webidl.spec.whatwg.org/#quotaexceedederror, which LibWeb
+//! implements in C++ (Libraries/LibWeb/WebIDL/QuotaExceededError.cpp).
+
+use core::ffi::c_void;
+use core::ptr::NonNull;
+
+use libjs_rust::api::{Gc, Object, Realm};
+
+use super::dom_exception::{self, DOMExceptionFields};
+use crate::bindings::WebIdlResult;
+use crate::html::structured_serialize::cpp::{StructuredSerializeReader, StructuredSerializeWriter};
+
+unsafe extern "C" {
+ fn web_quota_exceeded_error_quota_and_requested(
+ error: *mut c_void,
+ has_quota: &mut bool,
+ quota: &mut f64,
+ has_requested: &mut bool,
+ requested: &mut f64,
+ );
+ #[allow(clippy::too_many_arguments, reason = "the fields of the error pass one by one")]
+ fn web_quota_exceeded_error_create(
+ realm: *mut c_void,
+ name: usize,
+ message: usize,
+ has_quota: bool,
+ quota: f64,
+ has_requested: bool,
+ requested: f64,
+ ) -> *mut c_void;
+}
+
+/// The fields of a QuotaExceededError: those of a DOMException, its quota and its requested.
+pub(crate) struct QuotaExceededErrorFields {
+ pub(crate) dom_exception: DOMExceptionFields,
+ pub(crate) quota: Option<f64>,
+ pub(crate) requested: Option<f64>,
+}
+
+impl QuotaExceededErrorFields {
+ /// The fields of the QuotaExceededError.
+ pub(crate) fn of(error: Gc<Object>) -> Self {
+ let (mut has_quota, mut quota, mut has_requested, mut requested) = (false, 0.0, false, 0.0);
+ // SAFETY: The object is a live QuotaExceededError.
+ unsafe {
+ web_quota_exceeded_error_quota_and_requested(
+ error.as_ptr().cast(),
+ &mut has_quota,
+ &mut quota,
+ &mut has_requested,
+ &mut requested,
+ );
+ }
+ Self {
+ dom_exception: DOMExceptionFields::of(error),
+ quota: has_quota.then_some(quota),
+ requested: has_requested.then_some(requested),
+ }
+ }
+}
+
+/// A new QuotaExceededError with the fields, created in the realm.
+pub(crate) fn create(realm: Gc<Realm>, fields: &QuotaExceededErrorFields) -> Gc<Object> {
+ // SAFETY: The realm is live, and the call borrows the strings.
+ let error = unsafe {
+ web_quota_exceeded_error_create(
+ realm.as_ptr().cast(),
+ fields.dom_exception.name.raw_identity(),
+ fields.dom_exception.message.raw_identity(),
+ fields.quota.is_some(),
+ fields.quota.unwrap_or_default(),
+ fields.requested.is_some(),
+ fields.requested.unwrap_or_default(),
+ )
+ };
+ // SAFETY: LibWeb returns a live object.
+ unsafe { Gc::from_non_null(NonNull::new(error.cast()).expect("LibWeb creates the error")) }
+}
+
+// https://webidl.spec.whatwg.org/#ref-for-quotaexceedederror⑦
+pub(crate) fn serialization_steps(value: &QuotaExceededErrorFields, serialized: StructuredSerializeWriter) {
+ // 1. Run the DOMException serialization steps given value and serialized.
+ dom_exception::serialization_steps(&value.dom_exception, serialized);
+
+ // 2. Set serialized.[[Quota]] to value’s quota.
+ serialized.encode_optional_f64(value.quota);
+
+ // 3. Set serialized.[[Requested]] to value’s requested.
+ serialized.encode_optional_f64(value.requested);
+}
+
+// https://webidl.spec.whatwg.org/#ref-for-quotaexceedederror⑦
+pub(crate) fn deserialization_steps(serialized: StructuredSerializeReader) -> WebIdlResult<QuotaExceededErrorFields> {
+ // 1. Run the DOMException deserialization steps given serialized and value.
+ let dom_exception = dom_exception::deserialization_steps(serialized)?;
+
+ // 2. Set value’s quota to serialized.[[Quota]].
+ let quota = serialized.decode_optional_f64()?;
+
+ // 3. Set value’s requested to serialized.[[Requested]].
+ let requested = serialized.decode_optional_f64()?;
+
+ Ok(QuotaExceededErrorFields {
+ dom_exception,
+ quota,
+ requested,
+ })
+}
diff --git a/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cpp b/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cpp
index 5d0d361f5f4..bcdfaee126e 100644
--- a/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cpp
+++ b/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cpp
@@ -22,6 +22,7 @@
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/Bindings/ReadableStream.h>
#include <LibWeb/Bindings/RustSerialization.h>
+#include <LibWeb/Bindings/Serializable.h>
#include <LibWeb/Bindings/TransformStream.h>
#include <LibWeb/Bindings/WritableStream.h>
#include <LibWeb/HTML/ImageBitmap.h>
@@ -38,6 +39,14 @@
using namespace Web;
using namespace Web::HTML;
+// Whose steps serialize and deserialize the objects of a serializable interface.
+enum class SerializableSteps : u8 {
+ Rust,
+ DOMException,
+ QuotaExceededError,
+ Cpp,
+};
+
extern "C" {
StructuredSerializeWriter* web_structured_serialize_writer_create_for_transfer();
@@ -101,7 +110,7 @@ bool web_structured_serialize_is_platform_object(JSObject*);
bool web_structured_serialize_serializable_interface_name(JSObject*, u16* interface_name);
JSCompletion web_structured_serialize_serialization_steps(void* realm, JSObject*, StructuredSerializeWriter*, bool for_storage, SerializationMemory*);
bool web_structured_serialize_is_exposed(u16 interface_name, void* realm);
-bool web_structured_serialize_serializable_interface_is_implemented_in_rust(u16 interface_name);
+u8 web_structured_serialize_serializable_steps(u16 interface_name);
JSCompletion web_structured_serialize_deserialize_rust_platform_object(u16 interface_name, void* realm, StructuredSerializeReader*, DeserializationMemory*);
JSObject* web_structured_serialize_create_serialized_platform_object(u16 interface_name, void* realm);
JSCompletion web_structured_serialize_deserialization_steps(void* realm, JSObject*, StructuredSerializeReader*, DeserializationMemory*);
@@ -556,9 +565,18 @@ bool web_structured_serialize_is_exposed(u16 interface_name, void* realm)
return Bindings::is_exposed(static_cast<Bindings::InterfaceName>(interface_name), realm_from_abi(realm));
}
-bool web_structured_serialize_serializable_interface_is_implemented_in_rust(u16 interface_name)
+u8 web_structured_serialize_serializable_steps(u16 interface_name)
{
- return Bindings::serializable_interface_is_implemented_in_rust(static_cast<Bindings::InterfaceName>(interface_name));
+ switch (static_cast<Bindings::InterfaceName>(interface_name)) {
+ case Bindings::InterfaceName::DOMException:
+ return to_underlying(SerializableSteps::DOMException);
+ case Bindings::InterfaceName::QuotaExceededError:
+ return to_underlying(SerializableSteps::QuotaExceededError);
+ default:
+ if (Bindings::serializable_interface_is_implemented_in_rust(static_cast<Bindings::InterfaceName>(interface_name)))
+ return to_underlying(SerializableSteps::Rust);
+ return to_underlying(SerializableSteps::Cpp);
+ }
}
JSCompletion web_structured_serialize_deserialize_rust_platform_object(u16 interface_name, void* realm, StructuredSerializeReader* reader, DeserializationMemory* memory)
diff --git a/Libraries/LibWeb/WebIDL/DOMException.cpp b/Libraries/LibWeb/WebIDL/DOMException.cpp
index 5448435b85e..930fe2f6e27 100644
--- a/Libraries/LibWeb/WebIDL/DOMException.cpp
+++ b/Libraries/LibWeb/WebIDL/DOMException.cpp
@@ -4,10 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibJS/HostClassBuilder.h>
#include <LibWeb/Bindings/DOMException.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Bindings/WrapperWorld.h>
-#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/WebIDL/DOMException.h>
namespace Web::WebIDL {
@@ -56,32 +56,6 @@ void DOMException::visit_edges(GC::Cell::Visitor& visitor)
visitor.visit(m_error_data);
}
-WebIDL::ExceptionOr<void> DOMException::serialization_steps(HTML::StructuredSerializeWriter& serialized, bool, HTML::SerializationMemory&)
-{
- // 1. Set serialized.[[Name]] to value’s name.
- serialized.encode(m_name.to_utf16_string());
-
- // 2. Set serialized.[[Message]] to value’s message.
- serialized.encode(m_message.to_utf16_string());
-
- // FIXME: 3. User agents should attach a serialized representation of any interesting accompanying data which are not yet specified, notably the stack property, to serialized.
-
- return {};
-}
-
-WebIDL::ExceptionOr<void> DOMException::deserialization_steps(JS::Realm& realm, HTML::StructuredSerializeReader& serialized, HTML::DeserializationMemory&)
-{
- // 1. Set value’s name to serialized.[[Name]].
- m_name = TRY(HTML::decode_or_throw_data_clone_error<Utf16String>(realm, serialized));
-
- // 2. Set value’s message to serialized.[[Message]].
- m_message = TRY(HTML::decode_or_throw_data_clone_error<Utf16String>(realm, serialized));
-
- // FIXME: 3. If any other data is attached to serialized, then deserialize and attach it to value.
-
- return {};
-}
-
}
namespace Web::Bindings {
@@ -112,3 +86,26 @@ JS::Completion throw_completion(JS::Realm& realm, GC::Ref<WebIDL::DOMException>
}
}
+
+// What the serialization steps of DOMException, which run in Rust (Libraries/LibWeb/Bindings/Rust/src/webidl), read of a
+// DOMException and create one with. Rust owns the strings that it reads, and lends those that it creates one with.
+extern "C" {
+
+void web_dom_exception_name_and_message(JSObject*, FlatPtr* name, FlatPtr* message);
+JSObject* web_dom_exception_create(void* realm, FlatPtr name, FlatPtr message);
+}
+
+void web_dom_exception_name_and_message(JSObject* object, FlatPtr* name, FlatPtr* message)
+{
+ auto& exception = Web::Bindings::impl_from<Web::WebIDL::DOMException>(*JS::HostABI::object_from_abi(object));
+ *name = exception.name().to_utf16_string().into_raw();
+ *message = exception.message().to_utf16_string().into_raw();
+}
+
+JSObject* web_dom_exception_create(void* realm, FlatPtr name, FlatPtr message)
+{
+ auto& js_realm = *static_cast<JS::Realm*>(realm);
+ auto name_string = Utf16String::from_raw(name);
+ auto exception = Web::WebIDL::DOMException::create(Utf16FlyString::from_utf16(name_string.utf16_view()), Utf16String::from_raw(message));
+ return JS::HostABI::object_to_abi(Web::Bindings::wrap(Web::Bindings::host_defined_wrapper_world(js_realm), js_realm, exception).ptr());
+}
diff --git a/Libraries/LibWeb/WebIDL/DOMException.h b/Libraries/LibWeb/WebIDL/DOMException.h
index ef55e17ba19..d69103edc92 100644
--- a/Libraries/LibWeb/WebIDL/DOMException.h
+++ b/Libraries/LibWeb/WebIDL/DOMException.h
@@ -12,7 +12,6 @@
#include <AK/Utf16String.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/ErrorData.h>
-#include <LibWeb/Bindings/Serializable.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Export.h>
#include <LibWeb/Forward.h>
@@ -96,9 +95,7 @@ static u16 get_legacy_code_for_name(Utf16FlyString const& name)
}
// https://webidl.spec.whatwg.org/#idl-DOMException
-class WEB_API DOMException
- : public Bindings::GCAllocatedWrappable
- , public Bindings::Serializable {
+class WEB_API DOMException : public Bindings::GCAllocatedWrappable {
WEB_WRAPPABLE(DOMException, Bindings::GCAllocatedWrappable);
GC_DECLARE_ALLOCATOR(DOMException);
@@ -117,9 +114,6 @@ public:
JS::ErrorDataCell& error_data() { return *m_error_data; }
JS::ErrorDataCell const& error_data() const { return *m_error_data; }
- virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::StructuredSerializeWriter&, bool for_storage, HTML::SerializationMemory&) override;
- virtual WebIDL::ExceptionOr<void> deserialization_steps(JS::Realm&, HTML::StructuredSerializeReader&, HTML::DeserializationMemory&) override;
-
protected:
DOMException(FlyString name, Utf16String const& message);
DOMException(Utf16FlyString name, Utf16String const& message);
diff --git a/Libraries/LibWeb/WebIDL/QuotaExceededError.cpp b/Libraries/LibWeb/WebIDL/QuotaExceededError.cpp
index 73eea826d3b..89b103b414a 100644
--- a/Libraries/LibWeb/WebIDL/QuotaExceededError.cpp
+++ b/Libraries/LibWeb/WebIDL/QuotaExceededError.cpp
@@ -5,8 +5,10 @@
*/
#include <LibGC/Heap.h>
+#include <LibJS/HostClassBuilder.h>
#include <LibWeb/Bindings/QuotaExceededError.h>
-#include <LibWeb/HTML/StructuredSerialize.h>
+#include <LibWeb/Bindings/WrapperWorld.h>
+#include <LibWeb/WebIDL/ExceptionOr.h>
#include <LibWeb/WebIDL/QuotaExceededError.h>
namespace Web::WebIDL {
@@ -23,6 +25,13 @@ QuotaExceededError::QuotaExceededError(Utf16String const& message)
{
}
+QuotaExceededError::QuotaExceededError(Utf16FlyString name, Utf16String const& message, Optional<double> quota, Optional<double> requested)
+ : DOMException(move(name), message)
+ , m_quota(quota)
+ , m_requested(requested)
+{
+}
+
GC::Ref<QuotaExceededError> QuotaExceededError::create()
{
return GC::Heap::the().allocate<QuotaExceededError>();
@@ -33,6 +42,11 @@ GC::Ref<QuotaExceededError> QuotaExceededError::create(Utf16String const& messag
return GC::Heap::the().allocate<QuotaExceededError>(message);
}
+GC::Ref<QuotaExceededError> QuotaExceededError::create(Utf16FlyString name, Utf16String const& message, Optional<double> quota, Optional<double> requested)
+{
+ return GC::Heap::the().allocate<QuotaExceededError>(move(name), message, quota, requested);
+}
+
// https://webidl.spec.whatwg.org/#dom-quotaexceedederror-quotaexceedederror
ExceptionOr<GC::Ref<QuotaExceededError>> QuotaExceededError::create(JS::VM& vm, Utf16String const& message, QuotaExceededErrorOptions const& options)
{
@@ -68,34 +82,30 @@ ExceptionOr<GC::Ref<QuotaExceededError>> QuotaExceededError::create(JS::VM& vm,
return error;
}
-// https://webidl.spec.whatwg.org/#ref-for-quotaexceedederror⑦
-WebIDL::ExceptionOr<void> QuotaExceededError::serialization_steps(HTML::StructuredSerializeWriter& serialized, bool for_storage, HTML::SerializationMemory& memory)
-{
- // 1. Run the DOMException serialization steps given value and serialized.
- MUST(DOMException::serialization_steps(serialized, for_storage, memory));
-
- // 2. Set serialized.[[Quota]] to value’s quota.
- serialized.encode(m_quota);
+}
- // 3. Set serialized.[[Requested]] to value’s requested.
- serialized.encode(m_requested);
+// What the serialization steps of QuotaExceededError, which run in Rust (Libraries/LibWeb/Bindings/Rust/src/webidl),
+// read of a QuotaExceededError and create one with. Rust lends the strings that it creates one with.
+extern "C" {
- return {};
+void web_quota_exceeded_error_quota_and_requested(JSObject*, bool* has_quota, double* quota, bool* has_requested, double* requested);
+JSObject* web_quota_exceeded_error_create(void* realm, FlatPtr name, FlatPtr message, bool has_quota, double quota, bool has_requested, double requested);
}
-// https://webidl.spec.whatwg.org/#ref-for-quotaexceedederror⑦
-WebIDL::ExceptionOr<void> QuotaExceededError::deserialization_steps(JS::Realm& realm, HTML::StructuredSerializeReader& serialized, HTML::DeserializationMemory& memory)
+void web_quota_exceeded_error_quota_and_requested(JSObject* object, bool* has_quota, double* quota, bool* has_requested, double* requested)
{
- // 1. Run the DOMException deserialization steps given serialized and value.
- TRY(DOMException::deserialization_steps(realm, serialized, memory));
-
- // 2. Set value’s quota to serialized.[[Quota]].
- m_quota = TRY(HTML::decode_or_throw_data_clone_error<Optional<double>>(realm, serialized));
-
- // 3. Set value’s requested to serialized.[[Requested]].
- m_requested = TRY(HTML::decode_or_throw_data_clone_error<Optional<double>>(realm, serialized));
-
- return {};
+ auto& error = Web::Bindings::impl_from<Web::WebIDL::QuotaExceededError>(*JS::HostABI::object_from_abi(object));
+ *has_quota = error.quota().has_value();
+ *quota = error.quota().value_or(0);
+ *has_requested = error.requested().has_value();
+ *requested = error.requested().value_or(0);
}
+JSObject* web_quota_exceeded_error_create(void* realm, FlatPtr name, FlatPtr message, bool has_quota, double quota, bool has_requested, double requested)
+{
+ auto& js_realm = *static_cast<JS::Realm*>(realm);
+ auto name_string = Utf16String::from_raw(name);
+ auto error = Web::WebIDL::QuotaExceededError::create(Utf16FlyString::from_utf16(name_string.utf16_view()), Utf16String::from_raw(message),
+ has_quota ? Optional<double> { quota } : OptionalNone {}, has_requested ? Optional<double> { requested } : OptionalNone {});
+ return JS::HostABI::object_to_abi(Web::Bindings::wrap(Web::Bindings::host_defined_wrapper_world(js_realm), js_realm, error).ptr());
}
diff --git a/Libraries/LibWeb/WebIDL/QuotaExceededError.h b/Libraries/LibWeb/WebIDL/QuotaExceededError.h
index ebfb671f0c5..1f084bd4ca7 100644
--- a/Libraries/LibWeb/WebIDL/QuotaExceededError.h
+++ b/Libraries/LibWeb/WebIDL/QuotaExceededError.h
@@ -30,8 +30,8 @@ public:
static ExceptionOr<GC::Ref<QuotaExceededError>> create(JS::VM&, Utf16String const& message, QuotaExceededErrorOptions const&);
- virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::StructuredSerializeWriter&, bool for_storage, HTML::SerializationMemory&) override;
- virtual WebIDL::ExceptionOr<void> deserialization_steps(JS::Realm&, HTML::StructuredSerializeReader&, HTML::DeserializationMemory&) override;
+ // A QuotaExceededError with the fields that its deserialization steps read.
+ static GC::Ref<QuotaExceededError> create(Utf16FlyString name, Utf16String const& message, Optional<double> quota, Optional<double> requested);
// https://webidl.spec.whatwg.org/#dom-quotaexceedederror-quota
Optional<double> const& quota() const { return m_quota; }
@@ -41,6 +41,7 @@ public:
protected:
explicit QuotaExceededError(Utf16String const& message);
+ QuotaExceededError(Utf16FlyString name, Utf16String const& message, Optional<double> quota, Optional<double> requested);
QuotaExceededError();
private:
diff --git a/Meta/Generators/libweb_bindings/structured_serialize.py b/Meta/Generators/libweb_bindings/structured_serialize.py
index e099adffca8..2e1b7e8ed46 100644
--- a/Meta/Generators/libweb_bindings/structured_serialize.py
+++ b/Meta/Generators/libweb_bindings/structured_serialize.py
@@ -199,17 +199,29 @@ Optional<SerializablePlatformObject> serializable_from_object(JS::Object& object
return SerializablePlatformObject {{ .rust_object = cell, .interface_name = InterfaceName::{interface.name}, .realm = &object.shape().realm() }};
"""
)
+ # NB: An object is a serializable object if its primary interface is [Serializable]. The steps of some interfaces
+ # implemented in C++ run in Rust, so their classes have no C++ steps.
+ cpp_serializable = [interface for interface in serializable if not is_implemented_in_rust(interface)]
out.write(
"""
auto* wrappable = Bindings::wrappable_impl_from(&object);
if (!wrappable)
return {};
- auto* serializable = as_if<Serializable>(*wrappable);
- if (!serializable)
+ switch (wrappable->interface_name()) {
+"""
+ )
+ for interface in cpp_serializable:
+ out.write(f" case InterfaceName::{interface.name}:\n")
+ if cpp_serializable:
+ out.write(
+ " return SerializablePlatformObject { .serializable = as_if<Serializable>(*wrappable), "
+ ".interface_name = wrappable->interface_name(), .realm = &object.shape().realm() };\n"
+ )
+ out.write(
+ """ default:
return {};
-
- return SerializablePlatformObject { .serializable = serializable, .interface_name = wrappable->interface_name(), .realm = &object.shape().realm() };
+ }
}
WebIDL::ExceptionOr<void> SerializablePlatformObject::serialization_steps(HTML::StructuredSerializeWriter& serialized, bool for_storage, HTML::SerializationMemory& memory) const