-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Add structuredClone global method #39759
Closed
Ethan-Arrowood
wants to merge
10
commits into
nodejs:master
from
Ethan-Arrowood:feature/global-structured-clone
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d70cbcc
init commit of structured-clone global
Ethan-Arrowood 1ed93dd
skip wpt tests cause of missing File api
Ethan-Arrowood ea273ce
code fixes
Ethan-Arrowood 784c169
reuse messagechannel and lazily instantiate
Ethan-Arrowood 8e83568
fixes
Ethan-Arrowood d0dedb0
nits
Ethan-Arrowood 525a4ce
add eslint warning
Ethan-Arrowood bc45172
case sensitive and order
Ethan-Arrowood 6d40a85
fix tests
Ethan-Arrowood cdde45c
Update test/parallel/test-structuredClone-global.js
Ethan-Arrowood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const { | ||
MessageChannel, | ||
receiveMessageOnPort, | ||
} = require('internal/worker/io'); | ||
|
||
let channel; | ||
function structuredClone(value, transfer) { | ||
// TODO: Improve this with a more efficient solution that avoids | ||
// instantiating a MessageChannel | ||
channel ??= new MessageChannel(); | ||
channel.port1.unref(); | ||
channel.port2.unref(); | ||
channel.port1.postMessage(value, transfer); | ||
return receiveMessageOnPort(channel.port2).message; | ||
} | ||
|
||
module.exports = { | ||
structuredClone | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests-harness.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* - `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()); | ||
} |
22 changes: 22 additions & 0 deletions
22
.../html/webappapis/structured-clone/structured-clone-battery-of-tests-with-transferables.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious: why is this called with
await
in all of the tests?structuredClone
doesn't return a promise, does it?