script: Propagate a pending JS exception on structured cloning (#37964)

During the object (de)serialization steps on structured cloning
https://html.spec.whatwg.org/multipage/#safe-passing-of-structured-data
it is possible to throw a JS exception.

`self.postMessage({ get whatever() { throw customError } }));`

Require to propagate a pending JS exception and not throw the default
"DataCloneError" DOM exception.

Testing: Improvements in the following tests
- html/infrastructure/safe-passing-of-structured-data/*
- html/webappapis/structured-clone/structured-clone.any.js*
- wasm/serialization/arraybuffer/transfer.window.js
- webmessaging/without-ports/026.html
- workers/semantics/structured-clone/dedicated.html

Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
This commit is contained in:
Andrei Volykhin 2025-07-09 17:47:55 +03:00 committed by GitHub
parent 34c31ee418
commit 26f4da8249
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 15 additions and 44 deletions

View file

@ -24,7 +24,7 @@ use js::glue::{
NewJSAutoStructuredCloneBuffer, WriteBytesToJSStructuredCloneData,
};
use js::jsapi::{
CloneDataPolicy, HandleObject as RawHandleObject, Heap, JS_ClearPendingException,
CloneDataPolicy, HandleObject as RawHandleObject, Heap, JS_IsExceptionPending,
JS_ReadUint32Pair, JS_STRUCTURED_CLONE_VERSION, JS_WriteUint32Pair, JSContext, JSObject,
JSStructuredCloneCallbacks, JSStructuredCloneReader, JSStructuredCloneWriter,
MutableHandleObject as RawMutableHandleObject, StructuredCloneScope, TransferableOwnership,
@ -619,8 +619,13 @@ pub(crate) fn write(
val.handle(),
);
if !result {
JS_ClearPendingException(*cx);
return Err(sc_writer.error.unwrap_or(Error::DataClone(None)));
let error = if JS_IsExceptionPending(*cx) {
Error::JSFailed
} else {
sc_writer.error.unwrap_or(Error::DataClone(None))
};
return Err(error);
}
let nbytes = GetLengthOfJSStructuredCloneData(scdata);
@ -696,8 +701,13 @@ pub(crate) fn read(
sc_reader_ptr as *mut raw::c_void,
);
if !result {
JS_ClearPendingException(*cx);
return Err(sc_reader.error.unwrap_or(Error::DataClone(None)));
let error = if JS_IsExceptionPending(*cx) {
Error::JSFailed
} else {
sc_reader.error.unwrap_or(Error::DataClone(None))
};
return Err(error);
}
DeleteJSAutoStructuredCloneBuffer(scbuf);

View file

@ -5,9 +5,6 @@
[A subclass instance will deserialize as its closest serializable superclass]
expected: FAIL
[Object with a getter that throws]
expected: FAIL
[Growable SharedArrayBuffer]
expected: FAIL
@ -52,8 +49,5 @@
[A subclass instance will deserialize as its closest serializable superclass]
expected: FAIL
[Object with a getter that throws]
expected: FAIL
[Growable SharedArrayBuffer]
expected: FAIL

View file

@ -1,3 +0,0 @@
[structured-cloning-error-extra.html]
[Throwing name getter fails serialization]
expected: FAIL

View file

@ -1,12 +0,0 @@
[transfer-errors.window.html]
[Serialize should throw before a detached ArrayBuffer is found]
expected: FAIL
[Serialize should throw before a detached MessagePort is found]
expected: FAIL
[Serialize should throw before a detached ImageBitmap is found]
expected: FAIL
[Serialize should throw before a detached OffscreenCanvas is found]
expected: FAIL

View file

@ -32,8 +32,5 @@
[A subclass instance will deserialize as its closest serializable superclass]
expected: FAIL
[Object with a getter that throws]
expected: FAIL
[Growable SharedArrayBuffer]
expected: FAIL

View file

@ -5,9 +5,6 @@
[A subclass instance will deserialize as its closest serializable superclass]
expected: FAIL
[Object with a getter that throws]
expected: FAIL
[Growable SharedArrayBuffer]
expected: FAIL
@ -46,8 +43,5 @@
[A subclass instance will deserialize as its closest serializable superclass]
expected: FAIL
[Object with a getter that throws]
expected: FAIL
[Growable SharedArrayBuffer]
expected: FAIL

View file

@ -1,3 +0,0 @@
[transfer.window.html]
[Transfering a WebAssembly.Memory-backed ArrayBuffer throws]
expected: FAIL

View file

@ -1,3 +0,0 @@
[026.html]
[Cloning objects with getter properties]
expected: FAIL

View file

@ -29,9 +29,6 @@
[Object ImageData object, ImageData 1x1 non-transparent non-black]
expected: FAIL
[Object with a getter that throws]
expected: FAIL
[A subclass instance will deserialize as its closest serializable superclass]
expected: FAIL