-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Add structuredClone global method #39759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
d70cbcc
1ed93dd
ea273ce
784c169
8e83568
d0dedb0
525a4ce
bc45172
6d40a85
cdde45c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
const { MessageChannel, receiveMessageOnPort } = require('internal/worker/io'); | ||
|
||
|
||
function structuredClone(value, transfer) { | ||
Ethan-Arrowood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const channel = new MessageChannel(); | ||
Ethan-Arrowood marked this conversation as resolved.
Show resolved
Hide resolved
Ethan-Arrowood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
channel.port1.unref(); | ||
channel.port2.unref(); | ||
channel.port1.postMessage(value, transfer); | ||
return receiveMessageOnPort(channel.port2).message; | ||
} | ||
|
||
module.exports = { | ||
structuredClone | ||
} | ||
Ethan-Arrowood marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Runs a collection of tests that determine if an API implements structured clone | ||
* correctly. | ||
* | ||
* The `runner` parameter has the following properties: | ||
* - `setup()`: An optional function run once before testing starts | ||
* - `teardown()`: An option function run once after all tests are done | ||
* - `preTest()`: An optional, async function run before a test | ||
* - `postTest()`: An optional, async function run after a test is done | ||
* - `structuredClone(obj, transferList)`: Required function that somehow | ||
* structurally clones an object. | ||
Comment on lines
+10
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious: why is this called with |
||
* - `hasDocument`: When true, disables tests that require a document. True by default. | ||
*/ | ||
|
||
function runStructuredCloneBatteryOfTests(runner) { | ||
const defaultRunner = { | ||
setup() {}, | ||
preTest() {}, | ||
postTest() {}, | ||
teardown() {}, | ||
hasDocument: true | ||
}; | ||
runner = Object.assign({}, defaultRunner, runner); | ||
|
||
let setupPromise = runner.setup(); | ||
const allTests = structuredCloneBatteryOfTests.map(test => { | ||
|
||
if (!runner.hasDocument && test.requiresDocument) { | ||
return; | ||
} | ||
|
||
return new Promise(resolve => { | ||
promise_test(async _ => { | ||
test = await test; | ||
await setupPromise; | ||
await runner.preTest(test); | ||
await test.f(runner) | ||
await runner.postTest(test); | ||
resolve(); | ||
}, test.description); | ||
}).catch(_ => {}); | ||
}); | ||
Promise.all(allTests).then(_ => runner.teardown()); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
structuredCloneBatteryOfTests.push({ | ||
description: 'ArrayBuffer', | ||
async f(runner) { | ||
const buffer = new Uint8Array([1]).buffer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of using |
||
const copy = await runner.structuredClone(buffer, [buffer]); | ||
assert_equals(buffer.byteLength, 0); | ||
assert_equals(copy.byteLength, 1); | ||
} | ||
}); | ||
|
||
structuredCloneBatteryOfTests.push({ | ||
description: 'MessagePort', | ||
async f(runner) { | ||
const {port1, port2} = new MessageChannel(); | ||
const copy = await runner.structuredClone(port2, [port2]); | ||
const msg = new Promise(resolve => port1.onmessage = resolve); | ||
copy.postMessage('ohai'); | ||
assert_equals((await msg).data, 'ohai'); | ||
} | ||
}); | ||
|
||
// TODO: ImageBitmap |
Uh oh!
There was an error while loading. Please reload this page.