structuredclone::read throws a DataClone error on failure (#36361)

In the structured clone writing API, the read function should throw a
DataClone error on failure, just like the write function.

Testing: It doesn't require tests.
Fixes: #36217

Signed-off-by: Kingsley Yung <kingsley@kkoyung.dev>
This commit is contained in:
Kingsley Yung 2025-04-05 16:38:02 +08:00 committed by GitHub
parent aef8537d75
commit 3f24b44e15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -566,7 +566,7 @@ pub(crate) fn read(
global: &GlobalScope,
mut data: StructuredSerializedData,
rval: MutableHandleValue,
) -> Result<Vec<DomRoot<MessagePort>>, ()> {
) -> Fallible<Vec<DomRoot<MessagePort>>> {
let cx = GlobalScope::get_cx();
let _ac = enter_realm(global);
let mut sc_reader = StructuredDataReader {
@ -605,18 +605,16 @@ pub(crate) fn read(
&STRUCTURED_CLONE_CALLBACKS,
sc_reader_ptr as *mut raw::c_void,
);
if !result {
JS_ClearPendingException(*cx);
return Err(Error::DataClone);
}
DeleteJSAutoStructuredCloneBuffer(scbuf);
if result {
// Any transfer-received port-impls should have been taken out.
assert!(sc_reader.port_impls.is_none());
// Any transfer-received port-impls should have been taken out.
assert!(sc_reader.port_impls.is_none());
match sc_reader.message_ports.take() {
Some(ports) => return Ok(ports),
None => return Ok(Vec::with_capacity(0)),
}
}
Err(())
Ok(sc_reader.message_ports.take().unwrap_or_default())
}
}