-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: throw DataCloneError on transfering untransferable objects
The HTML StructuredSerializeWithTransfer algorithm defines that when an untransferable object is in the transfer list, a DataCloneError is thrown. An array buffer that is already transferred is also considered as untransferable. PR-URL: #47604 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
- Loading branch information
1 parent
3c82d48
commit 6454973
Showing
12 changed files
with
126 additions
and
24 deletions.
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
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
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
34 changes: 28 additions & 6 deletions
34
test/parallel/test-worker-message-transfer-port-mark-as-untransferable.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 |
---|---|---|
@@ -1,37 +1,59 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { MessageChannel, markAsUntransferable } = require('worker_threads'); | ||
const { MessageChannel, markAsUntransferable, isMarkedAsUntransferable } = require('worker_threads'); | ||
|
||
{ | ||
const ab = new ArrayBuffer(8); | ||
|
||
markAsUntransferable(ab); | ||
assert.ok(isMarkedAsUntransferable(ab)); | ||
assert.strictEqual(ab.byteLength, 8); | ||
|
||
const { port1, port2 } = new MessageChannel(); | ||
port1.postMessage(ab, [ ab ]); | ||
const { port1 } = new MessageChannel(); | ||
assert.throws(() => port1.postMessage(ab, [ ab ]), { | ||
code: 25, | ||
name: 'DataCloneError', | ||
}); | ||
|
||
assert.strictEqual(ab.byteLength, 8); // The AB is not detached. | ||
port2.once('message', common.mustCall()); | ||
} | ||
|
||
{ | ||
const channel1 = new MessageChannel(); | ||
const channel2 = new MessageChannel(); | ||
|
||
markAsUntransferable(channel2.port1); | ||
assert.ok(isMarkedAsUntransferable(channel2.port1)); | ||
|
||
assert.throws(() => { | ||
channel1.port1.postMessage(channel2.port1, [ channel2.port1 ]); | ||
}, /was found in message but not listed in transferList/); | ||
}, { | ||
code: 25, | ||
name: 'DataCloneError', | ||
}); | ||
|
||
channel2.port1.postMessage('still works, not closed/transferred'); | ||
channel2.port2.once('message', common.mustCall()); | ||
} | ||
|
||
{ | ||
for (const value of [0, null, false, true, undefined, [], {}]) { | ||
for (const value of [0, null, false, true, undefined]) { | ||
markAsUntransferable(value); // Has no visible effect. | ||
assert.ok(!isMarkedAsUntransferable(value)); | ||
} | ||
for (const value of [[], {}]) { | ||
markAsUntransferable(value); | ||
assert.ok(isMarkedAsUntransferable(value)); | ||
} | ||
} | ||
|
||
{ | ||
// Verifies that the mark is not inherited. | ||
class Foo {} | ||
markAsUntransferable(Foo.prototype); | ||
assert.ok(isMarkedAsUntransferable(Foo.prototype)); | ||
|
||
const foo = new Foo(); | ||
assert.ok(!isMarkedAsUntransferable(foo)); | ||
} |