Make DOMException serializable (#36535)

Follow the implementation of making DOMPoint and DOMPointReadOnly
serializable in PR #35989

Testing: Passed a test previously expected to fail.
Fixes: #36463

---------

Signed-off-by: Kingsley Yung <kingsley@kkoyung.dev>
This commit is contained in:
Kingsley Yung 2025-04-15 21:25:31 +08:00 committed by GitHub
parent cc04caa8ce
commit dacd951c9f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 138 additions and 12 deletions

View file

@ -2,6 +2,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::collections::HashMap;
use std::num::NonZeroU32;
use base::id::{DomExceptionId, DomExceptionIndex, PipelineNamespaceId};
use constellation_traits::DomException;
use dom_struct::dom_struct;
use js::rust::HandleObject;
@ -13,7 +18,9 @@ use crate::dom::bindings::reflector::{
Reflector, reflect_dom_object, reflect_dom_object_with_proto,
};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::serializable::{IntoStorageKey, Serializable, StorageKey};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::structuredclone::{StructuredData, StructuredDataReader};
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;
@ -215,3 +222,68 @@ impl DOMExceptionMethods<crate::DomTypeHolder> for DOMException {
self.message.clone()
}
}
impl Serializable for DOMException {
type Id = DomExceptionId;
type Data = DomException;
// https://webidl.spec.whatwg.org/#idl-DOMException
fn serialize(&self) -> Result<(Self::Id, Self::Data), ()> {
let serialized = DomException {
message: self.message.to_string(),
name: self.name.to_string(),
};
Ok((DomExceptionId::new(), serialized))
}
// https://webidl.spec.whatwg.org/#idl-DOMException
fn deserialize(
owner: &GlobalScope,
serialized: Self::Data,
can_gc: CanGc,
) -> Result<DomRoot<Self>, ()>
where
Self: Sized,
{
Ok(Self::new_with_custom_message(
owner,
DOMErrorName::from(&DOMString::from_string(serialized.name)).ok_or(())?,
serialized.message,
can_gc,
))
}
fn serialized_storage(data: StructuredData<'_>) -> &mut Option<HashMap<Self::Id, Self::Data>> {
match data {
StructuredData::Reader(reader) => &mut reader.exceptions,
StructuredData::Writer(writer) => &mut writer.exceptions,
}
}
fn deserialized_storage(
reader: &mut StructuredDataReader,
) -> &mut Option<HashMap<StorageKey, DomRoot<Self>>> {
&mut reader.dom_exceptions
}
}
impl From<StorageKey> for DomExceptionId {
fn from(storage_key: StorageKey) -> DomExceptionId {
let namespace_id = PipelineNamespaceId(storage_key.name_space);
let index = DomExceptionIndex(
NonZeroU32::new(storage_key.index).expect("Deserialized exception index is zero"),
);
DomExceptionId {
namespace_id,
index,
}
}
}
impl IntoStorageKey for DomExceptionId {
fn into_storage_key(self) -> StorageKey {
let DomExceptionIndex(index) = self.index;
StorageKey::new(self.namespace_id, index)
}
}