summaryrefslogtreecommitdiff
path: root/ext/web/13_message_port.js
diff options
context:
space:
mode:
authorMarcos Casagrande <marcoscvp90@gmail.com>2022-10-25 14:22:37 +0200
committerGitHub <noreply@github.com>2022-10-25 14:22:37 +0200
commit34fb380ed38ff324202b7779ae850edab133e577 (patch)
tree86548e0a099f6fc151e069b089a53b46ef396f3e /ext/web/13_message_port.js
parenta189c5393e1b106687736635fea0b8aeca283826 (diff)
feat(ext/web): use ArrayBuffer.was_detached() (#16307)
This PR adds a way to reliably check if an ArrayBuffer was detached
Diffstat (limited to 'ext/web/13_message_port.js')
-rw-r--r--ext/web/13_message_port.js32
1 files changed, 12 insertions, 20 deletions
diff --git a/ext/web/13_message_port.js b/ext/web/13_message_port.js
index 253ed7ecd..7343fbe58 100644
--- a/ext/web/13_message_port.js
+++ b/ext/web/13_message_port.js
@@ -26,9 +26,6 @@
SymbolFor,
SymbolIterator,
TypeError,
- WeakSet,
- WeakSetPrototypeAdd,
- WeakSetPrototypeHas,
} = window.__bootstrap.primordials;
class MessageChannel {
@@ -239,30 +236,25 @@
return [data, transferables];
}
- const detachedArrayBuffers = new WeakSet();
-
/**
* @param {any} data
* @param {object[]} transferables
* @returns {globalThis.__bootstrap.messagePort.MessageData}
*/
function serializeJsMessageData(data, transferables) {
- const transferredArrayBuffers = ArrayPrototypeFilter(
- transferables,
- (a) => ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, a),
- );
-
- for (const arrayBuffer of transferredArrayBuffers) {
- // This is hacky with both false positives and false negatives for
- // detecting detached array buffers. V8 needs to add a way to tell if a
- // buffer is detached or not.
- if (WeakSetPrototypeHas(detachedArrayBuffers, arrayBuffer)) {
- throw new DOMException(
- "Can not transfer detached ArrayBuffer",
- "DataCloneError",
- );
+ const transferredArrayBuffers = [];
+ for (let i = 0, j = 0; i < transferables.length; i++) {
+ const ab = transferables[i];
+ if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, ab)) {
+ if (ab.byteLength === 0 && core.ops.op_arraybuffer_was_detached(ab)) {
+ throw new DOMException(
+ `ArrayBuffer at index ${j} is already detached`,
+ "DataCloneError",
+ );
+ }
+ j++;
+ transferredArrayBuffers.push(ab);
}
- WeakSetPrototypeAdd(detachedArrayBuffers, arrayBuffer);
}
const serializedData = core.serialize(data, {