From d70cbcc659730c3510377d676f680e1d7b66b936 Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Fri, 13 Aug 2021 11:31:04 -0600 Subject: [PATCH 01/10] init commit of structured-clone global --- lib/internal/bootstrap/node.js | 6 + lib/internal/structured_clone.js | 15 + test/fixtures/wpt/LICENSE.md | 2 +- test/fixtures/wpt/README.md | 1 + ...ructured-clone-battery-of-tests-harness.js | 44 ++ ...one-battery-of-tests-with-transferables.js | 22 + .../structured-clone-battery-of-tests.js | 618 ++++++++++++++++++ .../structured-clone/structured-clone.any.js | 9 + test/fixtures/wpt/versions.json | 6 +- .../html/webappapis/structured-clone.json | 1 + test/wpt/test-structured-clone.js | 13 + 11 files changed, 735 insertions(+), 2 deletions(-) create mode 100644 lib/internal/structured_clone.js create mode 100644 test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests-harness.js create mode 100644 test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests-with-transferables.js create mode 100644 test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests.js create mode 100644 test/fixtures/wpt/html/webappapis/structured-clone/structured-clone.any.js create mode 100644 test/wpt/status/html/webappapis/structured-clone.json create mode 100644 test/wpt/test-structured-clone.js diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index ab63dd0604ea41..d33535a2499b58 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -254,6 +254,11 @@ if (!config.noBrowserGlobals) { // Non-standard extensions: defineOperation(globalThis, 'clearImmediate', timers.clearImmediate); defineOperation(globalThis, 'setImmediate', timers.setImmediate); + + const { + structuredClone + } = require('internal/structured_clone'); + defineOperation(globalThis, 'structuredClone', structuredClone); } // Set the per-Environment callback that will be called @@ -338,6 +343,7 @@ ObjectDefineProperty(process, 'features', { } const { emitWarning } = require('internal/process/warning'); +const { structuredClone } = require('internal/structured_clone'); process.emitWarning = emitWarning; // We initialize the tick callbacks and the timer callbacks last during diff --git a/lib/internal/structured_clone.js b/lib/internal/structured_clone.js new file mode 100644 index 00000000000000..5da1cb76b3dab8 --- /dev/null +++ b/lib/internal/structured_clone.js @@ -0,0 +1,15 @@ +'use strict'; + +const { MessageChannel, receiveMessageOnPort } = require('internal/worker/io'); + +const channel = new MessageChannel(); +channel.unref(); + +function structuredClone(value, transfer) { + channel.port1.postMessage(value, transfer); + return receiveMessageOnPort(channel.port2).message; +} + +module.exports = { + structuredClone +} \ No newline at end of file diff --git a/test/fixtures/wpt/LICENSE.md b/test/fixtures/wpt/LICENSE.md index ad4858c8745cfa..39c46d03ac2988 100644 --- a/test/fixtures/wpt/LICENSE.md +++ b/test/fixtures/wpt/LICENSE.md @@ -1,6 +1,6 @@ # The 3-Clause BSD License -Copyright 2019 web-platform-tests contributors +Copyright © web-platform-tests contributors Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/test/fixtures/wpt/README.md b/test/fixtures/wpt/README.md index 1d9fc500c26dc7..f2298c2f70c25d 100644 --- a/test/fixtures/wpt/README.md +++ b/test/fixtures/wpt/README.md @@ -18,6 +18,7 @@ Last update: - hr-time: https://github.com/web-platform-tests/wpt/tree/9910784394/hr-time - html/webappapis/atob: https://github.com/web-platform-tests/wpt/tree/f267e1dca6/html/webappapis/atob - html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/2c5c3c4c27/html/webappapis/microtask-queuing +- html/webappapis/structured-clone: https://github.com/web-platform-tests/wpt/tree/47d3fb280c/html/webappapis/structured-clone - html/webappapis/timers: https://github.com/web-platform-tests/wpt/tree/5873f2d8f1/html/webappapis/timers - interfaces: https://github.com/web-platform-tests/wpt/tree/fc086c82d5/interfaces - performance-timeline: https://github.com/web-platform-tests/wpt/tree/17ebc3aea0/performance-timeline diff --git a/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests-harness.js b/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests-harness.js new file mode 100644 index 00000000000000..660477dca4c621 --- /dev/null +++ b/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests-harness.js @@ -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()); +} diff --git a/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests-with-transferables.js b/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests-with-transferables.js new file mode 100644 index 00000000000000..744f1168196001 --- /dev/null +++ b/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests-with-transferables.js @@ -0,0 +1,22 @@ +structuredCloneBatteryOfTests.push({ + description: 'ArrayBuffer', + async f(runner) { + const buffer = new Uint8Array([1]).buffer; + 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 diff --git a/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests.js b/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests.js new file mode 100644 index 00000000000000..0c96ded2088357 --- /dev/null +++ b/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone-battery-of-tests.js @@ -0,0 +1,618 @@ +/* This file is mostly a remix of @zcorpan’s web worker test suite */ + +structuredCloneBatteryOfTests = []; + +function check(description, input, callback, requiresDocument = false) { + testObjMock = { + done() {}, + step_func(f) {return _ => f()}, + }; + + structuredCloneBatteryOfTests.push({ + description, + async f(runner) { + let newInput = input; + if (typeof input === 'function') { + newInput = input(); + } + const copy = await runner.structuredClone(newInput); + await callback(copy, newInput, testObjMock); + }, + requiresDocument + }); +} + +function compare_primitive(actual, input, test_obj) { + assert_equals(actual, input); + if (test_obj) + test_obj.done(); +} +function compare_Array(callback, callback_is_async) { + return function(actual, input, test_obj) { + if (typeof actual === 'string') + assert_unreached(actual); + assert_true(actual instanceof Array, 'instanceof Array'); + assert_not_equals(actual, input); + assert_equals(actual.length, input.length, 'length'); + callback(actual, input); + if (test_obj && !callback_is_async) + test_obj.done(); + } +} + +function compare_Object(callback, callback_is_async) { + return function(actual, input, test_obj) { + if (typeof actual === 'string') + assert_unreached(actual); + assert_true(actual instanceof Object, 'instanceof Object'); + assert_false(actual instanceof Array, 'instanceof Array'); + assert_not_equals(actual, input); + callback(actual, input); + if (test_obj && !callback_is_async) + test_obj.done(); + } +} + +function enumerate_props(compare_func, test_obj) { + return function(actual, input) { + for (const x in input) { + compare_func(actual[x], input[x], test_obj); + } + }; +} + +check('primitive undefined', undefined, compare_primitive); +check('primitive null', null, compare_primitive); +check('primitive true', true, compare_primitive); +check('primitive false', false, compare_primitive); +check('primitive string, empty string', '', compare_primitive); +check('primitive string, lone high surrogate', '\uD800', compare_primitive); +check('primitive string, lone low surrogate', '\uDC00', compare_primitive); +check('primitive string, NUL', '\u0000', compare_primitive); +check('primitive string, astral character', '\uDBFF\uDFFD', compare_primitive); +check('primitive number, 0.2', 0.2, compare_primitive); +check('primitive number, 0', 0, compare_primitive); +check('primitive number, -0', -0, compare_primitive); +check('primitive number, NaN', NaN, compare_primitive); +check('primitive number, Infinity', Infinity, compare_primitive); +check('primitive number, -Infinity', -Infinity, compare_primitive); +check('primitive number, 9007199254740992', 9007199254740992, compare_primitive); +check('primitive number, -9007199254740992', -9007199254740992, compare_primitive); +check('primitive number, 9007199254740994', 9007199254740994, compare_primitive); +check('primitive number, -9007199254740994', -9007199254740994, compare_primitive); +check('primitive BigInt, 0n', 0n, compare_primitive); +check('primitive BigInt, -0n', -0n, compare_primitive); +check('primitive BigInt, -9007199254740994000n', -9007199254740994000n, compare_primitive); +check('primitive BigInt, -9007199254740994000900719925474099400090071992547409940009007199254740994000n', -9007199254740994000900719925474099400090071992547409940009007199254740994000n, compare_primitive); + +check('Array primitives', [undefined, + null, + true, + false, + '', + '\uD800', + '\uDC00', + '\u0000', + '\uDBFF\uDFFD', + 0.2, + 0, + -0, + NaN, + Infinity, + -Infinity, + 9007199254740992, + -9007199254740992, + 9007199254740994, + -9007199254740994, + -12n, + -0n, + 0n], compare_Array(enumerate_props(compare_primitive))); +check('Object primitives', {'undefined':undefined, + 'null':null, + 'true':true, + 'false':false, + 'empty':'', + 'high surrogate':'\uD800', + 'low surrogate':'\uDC00', + 'nul':'\u0000', + 'astral':'\uDBFF\uDFFD', + '0.2':0.2, + '0':0, + '-0':-0, + 'NaN':NaN, + 'Infinity':Infinity, + '-Infinity':-Infinity, + '9007199254740992':9007199254740992, + '-9007199254740992':-9007199254740992, + '9007199254740994':9007199254740994, + '-9007199254740994':-9007199254740994}, compare_Object(enumerate_props(compare_primitive))); + +function compare_Boolean(actual, input, test_obj) { + if (typeof actual === 'string') + assert_unreached(actual); + assert_true(actual instanceof Boolean, 'instanceof Boolean'); + assert_equals(String(actual), String(input), 'converted to primitive'); + assert_not_equals(actual, input); + if (test_obj) + test_obj.done(); +} +check('Boolean true', new Boolean(true), compare_Boolean); +check('Boolean false', new Boolean(false), compare_Boolean); +check('Array Boolean objects', [new Boolean(true), new Boolean(false)], compare_Array(enumerate_props(compare_Boolean))); +check('Object Boolean objects', {'true':new Boolean(true), 'false':new Boolean(false)}, compare_Object(enumerate_props(compare_Boolean))); + +function compare_obj(what) { + const Type = self[what]; + return function(actual, input, test_obj) { + if (typeof actual === 'string') + assert_unreached(actual); + assert_true(actual instanceof Type, 'instanceof '+what); + assert_equals(Type(actual), Type(input), 'converted to primitive'); + assert_not_equals(actual, input); + if (test_obj) + test_obj.done(); + }; +} +check('String empty string', new String(''), compare_obj('String')); +check('String lone high surrogate', new String('\uD800'), compare_obj('String')); +check('String lone low surrogate', new String('\uDC00'), compare_obj('String')); +check('String NUL', new String('\u0000'), compare_obj('String')); +check('String astral character', new String('\uDBFF\uDFFD'), compare_obj('String')); +check('Array String objects', [new String(''), + new String('\uD800'), + new String('\uDC00'), + new String('\u0000'), + new String('\uDBFF\uDFFD')], compare_Array(enumerate_props(compare_obj('String')))); +check('Object String objects', {'empty':new String(''), + 'high surrogate':new String('\uD800'), + 'low surrogate':new String('\uDC00'), + 'nul':new String('\u0000'), + 'astral':new String('\uDBFF\uDFFD')}, compare_Object(enumerate_props(compare_obj('String')))); + +check('Number 0.2', new Number(0.2), compare_obj('Number')); +check('Number 0', new Number(0), compare_obj('Number')); +check('Number -0', new Number(-0), compare_obj('Number')); +check('Number NaN', new Number(NaN), compare_obj('Number')); +check('Number Infinity', new Number(Infinity), compare_obj('Number')); +check('Number -Infinity', new Number(-Infinity), compare_obj('Number')); +check('Number 9007199254740992', new Number(9007199254740992), compare_obj('Number')); +check('Number -9007199254740992', new Number(-9007199254740992), compare_obj('Number')); +check('Number 9007199254740994', new Number(9007199254740994), compare_obj('Number')); +check('Number -9007199254740994', new Number(-9007199254740994), compare_obj('Number')); +// BigInt does not have a non-throwing constructor +check('BigInt -9007199254740994n', Object(-9007199254740994n), compare_obj('BigInt')); + +check('Array Number objects', [new Number(0.2), + new Number(0), + new Number(-0), + new Number(NaN), + new Number(Infinity), + new Number(-Infinity), + new Number(9007199254740992), + new Number(-9007199254740992), + new Number(9007199254740994), + new Number(-9007199254740994)], compare_Array(enumerate_props(compare_obj('Number')))); +check('Object Number objects', {'0.2':new Number(0.2), + '0':new Number(0), + '-0':new Number(-0), + 'NaN':new Number(NaN), + 'Infinity':new Number(Infinity), + '-Infinity':new Number(-Infinity), + '9007199254740992':new Number(9007199254740992), + '-9007199254740992':new Number(-9007199254740992), + '9007199254740994':new Number(9007199254740994), + '-9007199254740994':new Number(-9007199254740994)}, compare_Object(enumerate_props(compare_obj('Number')))); + +function compare_Date(actual, input, test_obj) { + if (typeof actual === 'string') + assert_unreached(actual); + assert_true(actual instanceof Date, 'instanceof Date'); + assert_equals(Number(actual), Number(input), 'converted to primitive'); + assert_not_equals(actual, input); + if (test_obj) + test_obj.done(); +} +check('Date 0', new Date(0), compare_Date); +check('Date -0', new Date(-0), compare_Date); +check('Date -8.64e15', new Date(-8.64e15), compare_Date); +check('Date 8.64e15', new Date(8.64e15), compare_Date); +check('Array Date objects', [new Date(0), + new Date(-0), + new Date(-8.64e15), + new Date(8.64e15)], compare_Array(enumerate_props(compare_Date))); +check('Object Date objects', {'0':new Date(0), + '-0':new Date(-0), + '-8.64e15':new Date(-8.64e15), + '8.64e15':new Date(8.64e15)}, compare_Object(enumerate_props(compare_Date))); + +function compare_RegExp(expected_source) { + // XXX ES6 spec doesn't define exact serialization for `source` (it allows several ways to escape) + return function(actual, input, test_obj) { + if (typeof actual === 'string') + assert_unreached(actual); + assert_true(actual instanceof RegExp, 'instanceof RegExp'); + assert_equals(actual.global, input.global, 'global'); + assert_equals(actual.ignoreCase, input.ignoreCase, 'ignoreCase'); + assert_equals(actual.multiline, input.multiline, 'multiline'); + assert_equals(actual.source, expected_source, 'source'); + assert_equals(actual.sticky, input.sticky, 'sticky'); + assert_equals(actual.unicode, input.unicode, 'unicode'); + assert_equals(actual.lastIndex, 0, 'lastIndex'); + assert_not_equals(actual, input); + if (test_obj) + test_obj.done(); + } +} +function func_RegExp_flags_lastIndex() { + const r = /foo/gim; + r.lastIndex = 2; + return r; +} +function func_RegExp_sticky() { + return new RegExp('foo', 'y'); +} +function func_RegExp_unicode() { + return new RegExp('foo', 'u'); +} +check('RegExp flags and lastIndex', func_RegExp_flags_lastIndex, compare_RegExp('foo')); +check('RegExp sticky flag', func_RegExp_sticky, compare_RegExp('foo')); +check('RegExp unicode flag', func_RegExp_unicode, compare_RegExp('foo')); +check('RegExp empty', new RegExp(''), compare_RegExp('(?:)')); +check('RegExp slash', new RegExp('/'), compare_RegExp('\\/')); +check('RegExp new line', new RegExp('\n'), compare_RegExp('\\n')); +check('Array RegExp object, RegExp flags and lastIndex', [func_RegExp_flags_lastIndex()], compare_Array(enumerate_props(compare_RegExp('foo')))); +check('Array RegExp object, RegExp sticky flag', function() { return [func_RegExp_sticky()]; }, compare_Array(enumerate_props(compare_RegExp('foo')))); +check('Array RegExp object, RegExp unicode flag', function() { return [func_RegExp_unicode()]; }, compare_Array(enumerate_props(compare_RegExp('foo')))); +check('Array RegExp object, RegExp empty', [new RegExp('')], compare_Array(enumerate_props(compare_RegExp('(?:)')))); +check('Array RegExp object, RegExp slash', [new RegExp('/')], compare_Array(enumerate_props(compare_RegExp('\\/')))); +check('Array RegExp object, RegExp new line', [new RegExp('\n')], compare_Array(enumerate_props(compare_RegExp('\\n')))); +check('Object RegExp object, RegExp flags and lastIndex', {'x':func_RegExp_flags_lastIndex()}, compare_Object(enumerate_props(compare_RegExp('foo')))); +check('Object RegExp object, RegExp sticky flag', function() { return {'x':func_RegExp_sticky()}; }, compare_Object(enumerate_props(compare_RegExp('foo')))); +check('Object RegExp object, RegExp unicode flag', function() { return {'x':func_RegExp_unicode()}; }, compare_Object(enumerate_props(compare_RegExp('foo')))); +check('Object RegExp object, RegExp empty', {'x':new RegExp('')}, compare_Object(enumerate_props(compare_RegExp('(?:)')))); +check('Object RegExp object, RegExp slash', {'x':new RegExp('/')}, compare_Object(enumerate_props(compare_RegExp('\\/')))); +check('Object RegExp object, RegExp new line', {'x':new RegExp('\n')}, compare_Object(enumerate_props(compare_RegExp('\\n')))); + +async function compare_Blob(actual, input, test_obj, expect_File) { + if (typeof actual === 'string') + assert_unreached(actual); + assert_true(actual instanceof Blob, 'instanceof Blob'); + if (!expect_File) + assert_false(actual instanceof File, 'instanceof File'); + assert_equals(actual.size, input.size, 'size'); + assert_equals(actual.type, input.type, 'type'); + assert_not_equals(actual, input); + const ab1 = await new Response(actual).arrayBuffer(); + const ab2 = await new Response(input).arrayBuffer(); + assert_equals(ab1.byteLength, ab2.byteLength, 'byteLength'); + const ta1 = new Uint8Array(ab1); + const ta2 = new Uint8Array(ab2); + for(let i = 0; i < ta1.size; i++) { + assert_equals(ta1[i], ta2[i]); + } +} +function func_Blob_basic() { + return new Blob(['foo'], {type:'text/x-bar'}); +} +check('Blob basic', func_Blob_basic, compare_Blob); + +function b(str) { + return parseInt(str, 2); +} +function encode_cesu8(codeunits) { + // http://www.unicode.org/reports/tr26/ section 2.2 + // only the 3-byte form is supported + const rv = []; + codeunits.forEach(function(codeunit) { + rv.push(b('11100000') + ((codeunit & b('1111000000000000')) >> 12)); + rv.push(b('10000000') + ((codeunit & b('0000111111000000')) >> 6)); + rv.push(b('10000000') + (codeunit & b('0000000000111111'))); + }); + return rv; +} +function func_Blob_bytes(arr) { + return function() { + const buffer = new ArrayBuffer(arr.length); + const view = new DataView(buffer); + for (let i = 0; i < arr.length; ++i) { + view.setUint8(i, arr[i]); + } + return new Blob([view]); + }; +} +check('Blob unpaired high surrogate (invalid utf-8)', func_Blob_bytes(encode_cesu8([0xD800])), compare_Blob); +check('Blob unpaired low surrogate (invalid utf-8)', func_Blob_bytes(encode_cesu8([0xDC00])), compare_Blob); +check('Blob paired surrogates (invalid utf-8)', func_Blob_bytes(encode_cesu8([0xD800, 0xDC00])), compare_Blob); + +function func_Blob_empty() { + return new Blob(['']); +} +check('Blob empty', func_Blob_empty , compare_Blob); +function func_Blob_NUL() { + return new Blob(['\u0000']); +} +check('Blob NUL', func_Blob_NUL, compare_Blob); + +check('Array Blob object, Blob basic', [func_Blob_basic()], compare_Array(enumerate_props(compare_Blob), true)); +check('Array Blob object, Blob unpaired high surrogate (invalid utf-8)', [func_Blob_bytes([0xD800])()], compare_Array(enumerate_props(compare_Blob), true)); +check('Array Blob object, Blob unpaired low surrogate (invalid utf-8)', [func_Blob_bytes([0xDC00])()], compare_Array(enumerate_props(compare_Blob), true)); +check('Array Blob object, Blob paired surrogates (invalid utf-8)', [func_Blob_bytes([0xD800, 0xDC00])()], compare_Array(enumerate_props(compare_Blob), true)); +check('Array Blob object, Blob empty', [func_Blob_empty()], compare_Array(enumerate_props(compare_Blob), true)); +check('Array Blob object, Blob NUL', [func_Blob_NUL()], compare_Array(enumerate_props(compare_Blob), true)); +check('Array Blob object, two Blobs', [func_Blob_basic(), func_Blob_empty()], compare_Array(enumerate_props(compare_Blob), true)); + +check('Object Blob object, Blob basic', {'x':func_Blob_basic()}, compare_Object(enumerate_props(compare_Blob), true)); +check('Object Blob object, Blob unpaired high surrogate (invalid utf-8)', {'x':func_Blob_bytes([0xD800])()}, compare_Object(enumerate_props(compare_Blob), true)); +check('Object Blob object, Blob unpaired low surrogate (invalid utf-8)', {'x':func_Blob_bytes([0xDC00])()}, compare_Object(enumerate_props(compare_Blob), true)); +check('Object Blob object, Blob paired surrogates (invalid utf-8)', {'x':func_Blob_bytes([0xD800, 0xDC00])() }, compare_Object(enumerate_props(compare_Blob), true)); +check('Object Blob object, Blob empty', {'x':func_Blob_empty()}, compare_Object(enumerate_props(compare_Blob), true)); +check('Object Blob object, Blob NUL', {'x':func_Blob_NUL()}, compare_Object(enumerate_props(compare_Blob), true)); + +function compare_File(actual, input, test_obj) { + assert_true(actual instanceof File, 'instanceof File'); + assert_equals(actual.name, input.name, 'name'); + assert_equals(actual.lastModified, input.lastModified, 'lastModified'); + compare_Blob(actual, input, test_obj, true); +} +function func_File_basic() { + return new File(['foo'], 'bar', {type:'text/x-bar', lastModified:42}); +} +check('File basic', func_File_basic, compare_File); + +function compare_FileList(actual, input, test_obj) { + if (typeof actual === 'string') + assert_unreached(actual); + assert_true(actual instanceof FileList, 'instanceof FileList'); + assert_equals(actual.length, input.length, 'length'); + assert_not_equals(actual, input); + // XXX when there's a way to populate or construct a FileList, + // check the items in the FileList + if (test_obj) + test_obj.done(); +} +function func_FileList_empty() { + const input = document.createElement('input'); + input.type = 'file'; + return input.files; +} +check('FileList empty', func_FileList_empty, compare_FileList, true); +check('Array FileList object, FileList empty', () => ([func_FileList_empty()]), compare_Array(enumerate_props(compare_FileList)), true); +check('Object FileList object, FileList empty', () => ({'x':func_FileList_empty()}), compare_Object(enumerate_props(compare_FileList)), true); + +function compare_ArrayBufferView(view) { + const Type = self[view]; + return function(actual, input, test_obj) { + if (typeof actual === 'string') + assert_unreached(actual); + assert_true(actual instanceof Type, 'instanceof '+view); + assert_equals(actual.length, input.length, 'length'); + assert_not_equals(actual.buffer, input.buffer, 'buffer'); + for (let i = 0; i < actual.length; ++i) { + assert_equals(actual[i], input[i], 'actual['+i+']'); + } + if (test_obj) + test_obj.done(); + }; +} +function compare_ImageData(actual, input, test_obj) { + if (typeof actual === 'string') + assert_unreached(actual); + assert_equals(actual.width, input.width, 'width'); + assert_equals(actual.height, input.height, 'height'); + assert_not_equals(actual.data, input.data, 'data'); + compare_ArrayBufferView('Uint8ClampedArray')(actual.data, input.data, null); + if (test_obj) + test_obj.done(); +} +function func_ImageData_1x1_transparent_black() { + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + return ctx.createImageData(1, 1); +} +check('ImageData 1x1 transparent black', func_ImageData_1x1_transparent_black, compare_ImageData, true); +function func_ImageData_1x1_non_transparent_non_black() { + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + const imagedata = ctx.createImageData(1, 1); + imagedata.data[0] = 100; + imagedata.data[1] = 101; + imagedata.data[2] = 102; + imagedata.data[3] = 103; + return imagedata; +} +check('ImageData 1x1 non-transparent non-black', func_ImageData_1x1_non_transparent_non_black, compare_ImageData, true); +check('Array ImageData object, ImageData 1x1 transparent black', () => ([func_ImageData_1x1_transparent_black()]), compare_Array(enumerate_props(compare_ImageData)), true); +check('Array ImageData object, ImageData 1x1 non-transparent non-black', () => ([func_ImageData_1x1_non_transparent_non_black()]), compare_Array(enumerate_props(compare_ImageData)), true); +check('Object ImageData object, ImageData 1x1 transparent black', () => ({'x':func_ImageData_1x1_transparent_black()}), compare_Object(enumerate_props(compare_ImageData)), true); +check('Object ImageData object, ImageData 1x1 non-transparent non-black', () => ({'x':func_ImageData_1x1_non_transparent_non_black()}), compare_Object(enumerate_props(compare_ImageData)), true); + + +check('Array sparse', new Array(10), compare_Array(enumerate_props(compare_primitive))); +check('Array with non-index property', function() { + const rv = []; + rv.foo = 'bar'; + return rv; +}, compare_Array(enumerate_props(compare_primitive))); +check('Object with index property and length', {'0':'foo', 'length':1}, compare_Object(enumerate_props(compare_primitive))); +function check_circular_property(prop) { + return function(actual) { + assert_equals(actual[prop], actual); + }; +} +check('Array with circular reference', function() { + const rv = []; + rv[0] = rv; + return rv; +}, compare_Array(check_circular_property('0'))); +check('Object with circular reference', function() { + const rv = {}; + rv['x'] = rv; + return rv; +}, compare_Object(check_circular_property('x'))); +function check_identical_property_values(prop1, prop2) { + return function(actual) { + assert_equals(actual[prop1], actual[prop2]); + }; +} +check('Array with identical property values', function() { + const obj = {} + return [obj, obj]; +}, compare_Array(check_identical_property_values('0', '1'))); +check('Object with identical property values', function() { + const obj = {} + return {'x':obj, 'y':obj}; +}, compare_Object(check_identical_property_values('x', 'y'))); + +function check_absent_property(prop) { + return function(actual) { + assert_false(prop in actual); + }; +} +check('Object with property on prototype', function() { + const Foo = function() {}; + Foo.prototype = {'foo':'bar'}; + return new Foo(); +}, compare_Object(check_absent_property('foo'))); + +check('Object with non-enumerable property', function() { + const rv = {}; + Object.defineProperty(rv, 'foo', {value:'bar', enumerable:false, writable:true, configurable:true}); + return rv; +}, compare_Object(check_absent_property('foo'))); + +function check_writable_property(prop) { + return function(actual, input) { + assert_equals(actual[prop], input[prop]); + actual[prop] += ' baz'; + assert_equals(actual[prop], input[prop] + ' baz'); + }; +} +check('Object with non-writable property', function() { + const rv = {}; + Object.defineProperty(rv, 'foo', {value:'bar', enumerable:true, writable:false, configurable:true}); + return rv; +}, compare_Object(check_writable_property('foo'))); + +function check_configurable_property(prop) { + return function(actual, input) { + assert_equals(actual[prop], input[prop]); + delete actual[prop]; + assert_false('prop' in actual); + }; +} +check('Object with non-configurable property', function() { + const rv = {}; + Object.defineProperty(rv, 'foo', {value:'bar', enumerable:true, writable:true, configurable:false}); + return rv; +}, compare_Object(check_configurable_property('foo'))); + +/* The tests below are inspired by @zcorpan’s work but got some +more substantial changed due to their previous async setup */ + +function get_canvas_1x1_transparent_black() { + const canvas = document.createElement('canvas'); + canvas.width = 1; + canvas.height = 1; + return canvas; +} + +function get_canvas_1x1_non_transparent_non_black() { + const canvas = document.createElement('canvas'); + canvas.width = 1; + canvas.height = 1; + const ctx = canvas.getContext('2d'); + const imagedata = ctx.getImageData(0, 0, 1, 1); + imagedata.data[0] = 100; + imagedata.data[1] = 101; + imagedata.data[2] = 102; + imagedata.data[3] = 103; + return canvas; +} + +function compare_ImageBitmap(actual, input) { + if (typeof actual === 'string') + assert_unreached(actual); + assert_true(actual instanceof ImageBitmap, 'instanceof ImageBitmap'); + assert_not_equals(actual, input); + // XXX paint the ImageBitmap on a canvas and check the data +} + +structuredCloneBatteryOfTests.push({ + description: 'ImageBitmap 1x1 transparent black', + async f(runner) { + const canvas = get_canvas_1x1_transparent_black(); + const bm = await createImageBitmap(canvas); + const copy = await runner.structuredClone(bm); + compare_ImageBitmap(bm, copy); + }, + requiresDocument: true +}); + +structuredCloneBatteryOfTests.push({ + description: 'ImageBitmap 1x1 non-transparent non-black', + async f(runner) { + const canvas = get_canvas_1x1_non_transparent_non_black(); + const bm = await createImageBitmap(canvas); + const copy = await runner.structuredClone(bm); + compare_ImageBitmap(bm, copy); + }, + requiresDocument: true +}); + +structuredCloneBatteryOfTests.push({ + description: 'Array ImageBitmap object, ImageBitmap 1x1 transparent black', + async f(runner) { + const canvas = get_canvas_1x1_transparent_black(); + const bm = [await createImageBitmap(canvas)]; + const copy = await runner.structuredClone(bm); + compare_Array(enumerate_props(compare_ImageBitmap))(bm, copy); + }, + requiresDocument: true +}); + +structuredCloneBatteryOfTests.push({ + description: 'Array ImageBitmap object, ImageBitmap 1x1 transparent non-black', + async f(runner) { + const canvas = get_canvas_1x1_non_transparent_non_black(); + const bm = [await createImageBitmap(canvas)]; + const copy = await runner.structuredClone(bm); + compare_Array(enumerate_props(compare_ImageBitmap))(bm, copy); + }, + requiresDocument: true +}); + +structuredCloneBatteryOfTests.push({ + description: 'Object ImageBitmap object, ImageBitmap 1x1 transparent black', + async f(runner) { + const canvas = get_canvas_1x1_transparent_black(); + const bm = {x: await createImageBitmap(canvas)}; + const copy = await runner.structuredClone(bm); + compare_Object(enumerate_props(compare_ImageBitmap))(bm, copy); + }, + requiresDocument: true +}); + +structuredCloneBatteryOfTests.push({ + description: 'Object ImageBitmap object, ImageBitmap 1x1 transparent non-black', + async f(runner) { + const canvas = get_canvas_1x1_non_transparent_non_black(); + const bm = {x: await createImageBitmap(canvas)}; + const copy = await runner.structuredClone(bm); + compare_Object(enumerate_props(compare_ImageBitmap))(bm, copy); + }, + requiresDocument: true +}); + +check('ObjectPrototype must lose its exotic-ness when cloned', + () => Object.prototype, + (copy, original) => { + assert_not_equals(copy, original); + assert_true(copy instanceof Object); + + const newProto = { some: 'proto' }; + // Must not throw: + Object.setPrototypeOf(copy, newProto); + + assert_equals(Object.getPrototypeOf(copy), newProto); + } +); diff --git a/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone.any.js b/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone.any.js new file mode 100644 index 00000000000000..34f96f33fdf881 --- /dev/null +++ b/test/fixtures/wpt/html/webappapis/structured-clone/structured-clone.any.js @@ -0,0 +1,9 @@ +// META: title=structuredClone() tests +// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests.js +// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests-with-transferables.js +// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests-harness.js + +runStructuredCloneBatteryOfTests({ + structuredClone: (obj, transfer) => self.structuredClone(obj, { transfer }), + hasDocument: typeof document !== "undefined", +}); diff --git a/test/fixtures/wpt/versions.json b/test/fixtures/wpt/versions.json index 8e3bdc2e528be8..8f483da6972a09 100644 --- a/test/fixtures/wpt/versions.json +++ b/test/fixtures/wpt/versions.json @@ -31,6 +31,10 @@ "commit": "2c5c3c4c27d27a419c1fdba3e9879c2d22037074", "path": "html/webappapis/microtask-queuing" }, + "html/webappapis/structured-clone": { + "commit": "47d3fb280c9c632e684dee3b78ae1f4c5d5ba640", + "path": "html/webappapis/structured-clone" + }, "html/webappapis/timers": { "commit": "5873f2d8f1f7bbb9c64689e52d04498614632906", "path": "html/webappapis/timers" @@ -63,4 +67,4 @@ "commit": "cdd0f03df41b222aed098fbbb11c6a3cc500a86b", "path": "WebCryptoAPI" } -} +} \ No newline at end of file diff --git a/test/wpt/status/html/webappapis/structured-clone.json b/test/wpt/status/html/webappapis/structured-clone.json new file mode 100644 index 00000000000000..9e26dfeeb6e641 --- /dev/null +++ b/test/wpt/status/html/webappapis/structured-clone.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/wpt/test-structured-clone.js b/test/wpt/test-structured-clone.js new file mode 100644 index 00000000000000..bbd835c3bf2452 --- /dev/null +++ b/test/wpt/test-structured-clone.js @@ -0,0 +1,13 @@ +'use strict'; + +require('../common'); +const { WPTRunner } = require('../common/wpt'); + +const runner = new WPTRunner('html/webappapis/structured-clone'); + +runner.setInitScript(` + const { Blob } = require('buffer'); + global.Blob = Blob; +`); + +runner.runJsTests(); \ No newline at end of file From 1ed93ddd1edf87b7b3946f1742c047bb99d796b8 Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Fri, 13 Aug 2021 12:00:37 -0600 Subject: [PATCH 02/10] skip wpt tests cause of missing File api --- .eslintrc.js | 1 + lib/internal/structured_clone.js | 5 +++-- test/parallel/test-global.js | 1 + test/parallel/test-structuredClone-global.js | 18 ++++++++++++++++++ .../html/webappapis/structured-clone.json | 6 +++++- 5 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-structuredClone-global.js diff --git a/.eslintrc.js b/.eslintrc.js index be332590ba7260..2696bdee9ef6c4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -362,5 +362,6 @@ module.exports = { btoa: 'readable', atob: 'readable', performance: 'readable', + structuredClone: 'readable' }, }; diff --git a/lib/internal/structured_clone.js b/lib/internal/structured_clone.js index 5da1cb76b3dab8..94adf4d195b387 100644 --- a/lib/internal/structured_clone.js +++ b/lib/internal/structured_clone.js @@ -2,10 +2,11 @@ const { MessageChannel, receiveMessageOnPort } = require('internal/worker/io'); -const channel = new MessageChannel(); -channel.unref(); function structuredClone(value, transfer) { + const channel = new MessageChannel(); + channel.port1.unref(); + channel.port2.unref(); channel.port1.postMessage(value, transfer); return receiveMessageOnPort(channel.port2).message; } diff --git a/test/parallel/test-global.js b/test/parallel/test-global.js index dbd3b06d290d31..f9d085d6e29af9 100644 --- a/test/parallel/test-global.js +++ b/test/parallel/test-global.js @@ -51,6 +51,7 @@ builtinModules.forEach((moduleName) => { 'setImmediate', 'setInterval', 'setTimeout', + 'structuredClone' ]; assert.deepStrictEqual(new Set(Object.keys(global)), new Set(expected)); } diff --git a/test/parallel/test-structuredClone-global.js b/test/parallel/test-structuredClone-global.js new file mode 100644 index 00000000000000..eb5938ad392376 --- /dev/null +++ b/test/parallel/test-structuredClone-global.js @@ -0,0 +1,18 @@ +// Flags: --expose-internals +'use strict'; +/* eslint-disable no-global-assign */ + +require('../common'); + +const { structuredClone: _structuredClone } = require('internal/structuredClone'); +const { + strictEqual +} = require('assert'); + +strictEqual(globalThis.structuredClone, _structuredClone); +structuredClone = undefined; +strictEqual(globalThis.structuredClone, undefined); +strictEqual(typeof perf_hooks.performance.now, 'function'); + +// Restore the value of performance for the known globals check +structuredClone = _structuredClone; diff --git a/test/wpt/status/html/webappapis/structured-clone.json b/test/wpt/status/html/webappapis/structured-clone.json index 9e26dfeeb6e641..8610075afa209d 100644 --- a/test/wpt/status/html/webappapis/structured-clone.json +++ b/test/wpt/status/html/webappapis/structured-clone.json @@ -1 +1,5 @@ -{} \ No newline at end of file +{ + "structured-clone.any.js": { + "skip": "Missing File API" + } +} \ No newline at end of file From ea273ce80c6473ae314eb98e4030d0c205aa1fcb Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Fri, 13 Aug 2021 13:25:51 -0600 Subject: [PATCH 03/10] code fixes --- lib/internal/structured_clone.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/internal/structured_clone.js b/lib/internal/structured_clone.js index 94adf4d195b387..d1ab82ebff67fb 100644 --- a/lib/internal/structured_clone.js +++ b/lib/internal/structured_clone.js @@ -1,9 +1,13 @@ 'use strict'; -const { MessageChannel, receiveMessageOnPort } = require('internal/worker/io'); +const { + MessageChannel, + receiveMessageOnPort +} = require('internal/worker/io'); function structuredClone(value, transfer) { + // TODO: Improve this with a more efficient solution that avoids instantiating a MessageChannel const channel = new MessageChannel(); channel.port1.unref(); channel.port2.unref(); @@ -13,4 +17,4 @@ function structuredClone(value, transfer) { module.exports = { structuredClone -} \ No newline at end of file +} From 784c169b8ff763b9d4bca2b0039079f378f757ac Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Fri, 13 Aug 2021 13:31:54 -0600 Subject: [PATCH 04/10] reuse messagechannel and lazily instantiate --- lib/internal/structured_clone.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/internal/structured_clone.js b/lib/internal/structured_clone.js index d1ab82ebff67fb..530fc748634943 100644 --- a/lib/internal/structured_clone.js +++ b/lib/internal/structured_clone.js @@ -5,10 +5,12 @@ const { receiveMessageOnPort } = require('internal/worker/io'); - +let channel function structuredClone(value, transfer) { // TODO: Improve this with a more efficient solution that avoids instantiating a MessageChannel - const channel = new MessageChannel(); + if (!channel) { + channel = new MessageChannel(); + } channel.port1.unref(); channel.port2.unref(); channel.port1.postMessage(value, transfer); From 8e8356887a2e85e2fe2caf879feeb96c3af1a1ea Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Fri, 13 Aug 2021 15:20:24 -0600 Subject: [PATCH 05/10] fixes --- lib/internal/bootstrap/node.js | 1 - lib/internal/structured_clone.js | 7 ++++--- test/fixtures/wpt/versions.json | 2 +- test/parallel/test-global.js | 2 +- test/parallel/test-structuredClone-global.js | 5 +++-- test/wpt/status/html/webappapis/structured-clone.json | 2 +- test/wpt/test-structured-clone.js | 2 +- 7 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index d33535a2499b58..315d9ebcb7a8c8 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -343,7 +343,6 @@ ObjectDefineProperty(process, 'features', { } const { emitWarning } = require('internal/process/warning'); -const { structuredClone } = require('internal/structured_clone'); process.emitWarning = emitWarning; // We initialize the tick callbacks and the timer callbacks last during diff --git a/lib/internal/structured_clone.js b/lib/internal/structured_clone.js index 530fc748634943..e5e173edadc29a 100644 --- a/lib/internal/structured_clone.js +++ b/lib/internal/structured_clone.js @@ -5,9 +5,10 @@ const { receiveMessageOnPort } = require('internal/worker/io'); -let channel +let channel; function structuredClone(value, transfer) { - // TODO: Improve this with a more efficient solution that avoids instantiating a MessageChannel + // TODO: Improve this with a more efficient solution that avoids + // instantiating a MessageChannel if (!channel) { channel = new MessageChannel(); } @@ -19,4 +20,4 @@ function structuredClone(value, transfer) { module.exports = { structuredClone -} +}; diff --git a/test/fixtures/wpt/versions.json b/test/fixtures/wpt/versions.json index 8f483da6972a09..b47cc08dd1050c 100644 --- a/test/fixtures/wpt/versions.json +++ b/test/fixtures/wpt/versions.json @@ -67,4 +67,4 @@ "commit": "cdd0f03df41b222aed098fbbb11c6a3cc500a86b", "path": "WebCryptoAPI" } -} \ No newline at end of file +} diff --git a/test/parallel/test-global.js b/test/parallel/test-global.js index f9d085d6e29af9..60df2c1d73f463 100644 --- a/test/parallel/test-global.js +++ b/test/parallel/test-global.js @@ -51,7 +51,7 @@ builtinModules.forEach((moduleName) => { 'setImmediate', 'setInterval', 'setTimeout', - 'structuredClone' + 'structuredClone', ]; assert.deepStrictEqual(new Set(Object.keys(global)), new Set(expected)); } diff --git a/test/parallel/test-structuredClone-global.js b/test/parallel/test-structuredClone-global.js index eb5938ad392376..2a489e07780d5f 100644 --- a/test/parallel/test-structuredClone-global.js +++ b/test/parallel/test-structuredClone-global.js @@ -4,7 +4,9 @@ require('../common'); -const { structuredClone: _structuredClone } = require('internal/structuredClone'); +const { + structuredClone: _structuredClone +} = require('internal/structuredClone'); const { strictEqual } = require('assert'); @@ -12,7 +14,6 @@ const { strictEqual(globalThis.structuredClone, _structuredClone); structuredClone = undefined; strictEqual(globalThis.structuredClone, undefined); -strictEqual(typeof perf_hooks.performance.now, 'function'); // Restore the value of performance for the known globals check structuredClone = _structuredClone; diff --git a/test/wpt/status/html/webappapis/structured-clone.json b/test/wpt/status/html/webappapis/structured-clone.json index 8610075afa209d..d2a7539641ffb5 100644 --- a/test/wpt/status/html/webappapis/structured-clone.json +++ b/test/wpt/status/html/webappapis/structured-clone.json @@ -2,4 +2,4 @@ "structured-clone.any.js": { "skip": "Missing File API" } -} \ No newline at end of file +} diff --git a/test/wpt/test-structured-clone.js b/test/wpt/test-structured-clone.js index bbd835c3bf2452..98ef87184a6ce2 100644 --- a/test/wpt/test-structured-clone.js +++ b/test/wpt/test-structured-clone.js @@ -10,4 +10,4 @@ runner.setInitScript(` global.Blob = Blob; `); -runner.runJsTests(); \ No newline at end of file +runner.runJsTests(); From d0dedb00b10b029e34af2b1cbe20d5a3fe1cd22d Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Fri, 13 Aug 2021 17:59:12 -0600 Subject: [PATCH 06/10] nits --- .eslintrc.js | 2 +- lib/internal/bootstrap/node.js | 2 +- lib/internal/structured_clone.js | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 2696bdee9ef6c4..a8ab92feb0338b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -362,6 +362,6 @@ module.exports = { btoa: 'readable', atob: 'readable', performance: 'readable', - structuredClone: 'readable' + structuredClone: 'readable', }, }; diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 315d9ebcb7a8c8..ae4eac502803e7 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -256,7 +256,7 @@ if (!config.noBrowserGlobals) { defineOperation(globalThis, 'setImmediate', timers.setImmediate); const { - structuredClone + structuredClone, } = require('internal/structured_clone'); defineOperation(globalThis, 'structuredClone', structuredClone); } diff --git a/lib/internal/structured_clone.js b/lib/internal/structured_clone.js index e5e173edadc29a..1d5d0ed511c9cc 100644 --- a/lib/internal/structured_clone.js +++ b/lib/internal/structured_clone.js @@ -2,16 +2,14 @@ const { MessageChannel, - receiveMessageOnPort + receiveMessageOnPort, } = require('internal/worker/io'); let channel; function structuredClone(value, transfer) { // TODO: Improve this with a more efficient solution that avoids // instantiating a MessageChannel - if (!channel) { - channel = new MessageChannel(); - } + channel ??= new MessageChannel(); channel.port1.unref(); channel.port2.unref(); channel.port1.postMessage(value, transfer); From 525a4ce5653cda9867bfd8fee03a8e1f7a75e615 Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Tue, 17 Aug 2021 12:05:52 -0600 Subject: [PATCH 07/10] add eslint warning --- lib/.eslintrc.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index 49013e1f8ee8b5..652dd00b7d3850 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -57,6 +57,8 @@ rules: # disabled with --no-harmony-sharedarraybuffer CLI flag. - name: SharedArrayBuffer message: "Use `const { SharedArrayBuffer } = globalThis;` instead of the global." + - name: StructuredClone + message: "Use `const { structuredClone } = require('internal/structured_clone');` instead of the global." - name: TextDecoder message: "Use `const { TextDecoder } = require('internal/encoding');` instead of the global." - name: TextEncoder From bc45172a5fe100ae3e452bfbf60c09a95cbf62ce Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Tue, 17 Aug 2021 13:00:03 -0600 Subject: [PATCH 08/10] case sensitive and order --- lib/.eslintrc.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index 652dd00b7d3850..9f209505db9c33 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -57,8 +57,6 @@ rules: # disabled with --no-harmony-sharedarraybuffer CLI flag. - name: SharedArrayBuffer message: "Use `const { SharedArrayBuffer } = globalThis;` instead of the global." - - name: StructuredClone - message: "Use `const { structuredClone } = require('internal/structured_clone');` instead of the global." - name: TextDecoder message: "Use `const { TextDecoder } = require('internal/encoding');` instead of the global." - name: TextEncoder @@ -83,6 +81,8 @@ rules: message: "Use `const { performance } = require('perf_hooks');` instead of the global." - name: queueMicrotask message: "Use `const { queueMicrotask } = require('internal/process/task_queues');` instead of the global." + - name: structuredClone + message: "Use `const { structuredClone } = require('internal/structured_clone');` instead of the global." # Custom rules in tools/eslint-rules node-core/lowercase-name-for-primitive: error node-core/non-ascii-character: error From 6d40a85e848c87f67e1577965e35402589a3f35a Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Mon, 23 Aug 2021 22:47:21 -0600 Subject: [PATCH 09/10] fix tests --- test/common/index.js | 7 +++++++ test/parallel/test-bootstrap-modules.js | 1 + test/parallel/test-structuredClone-global.js | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/test/common/index.js b/test/common/index.js index 2ac4538cbea804..18d6444419152d 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -293,6 +293,13 @@ if (global.PerformanceMeasure) { knownGlobals.push(global.PerformanceMeasure); } +// TODO(@ethan-arrowood): Similar to previous checks, this can be temporary +// until v16.x is EOL. Once all supported versions have structuredClone we +// can add this to the list above instead. +if (global.structuredClone) { + knownGlobals.push(global.structuredClone); +} + function allowGlobals(...allowlist) { knownGlobals = knownGlobals.concat(allowlist); } diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index d02f0c71860554..d306418fe6e9c1 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -114,6 +114,7 @@ const expectedModules = new Set([ 'NativeModule internal/streams/transform', 'NativeModule internal/streams/utils', 'NativeModule internal/streams/writable', + 'NativeModule internal/structured_clone', 'NativeModule internal/timers', 'NativeModule internal/url', 'NativeModule internal/util', diff --git a/test/parallel/test-structuredClone-global.js b/test/parallel/test-structuredClone-global.js index 2a489e07780d5f..fbd08ef9a66e83 100644 --- a/test/parallel/test-structuredClone-global.js +++ b/test/parallel/test-structuredClone-global.js @@ -6,7 +6,7 @@ require('../common'); const { structuredClone: _structuredClone -} = require('internal/structuredClone'); +} = require('internal/structured_clone'); const { strictEqual } = require('assert'); From cdde45c9e88de0299fbb3c2cdf8d9bf70d502835 Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Wed, 25 Aug 2021 10:28:45 -0600 Subject: [PATCH 10/10] Update test/parallel/test-structuredClone-global.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michaël Zasso --- test/parallel/test-structuredClone-global.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-structuredClone-global.js b/test/parallel/test-structuredClone-global.js index fbd08ef9a66e83..ae6120c04e0005 100644 --- a/test/parallel/test-structuredClone-global.js +++ b/test/parallel/test-structuredClone-global.js @@ -15,5 +15,5 @@ strictEqual(globalThis.structuredClone, _structuredClone); structuredClone = undefined; strictEqual(globalThis.structuredClone, undefined); -// Restore the value of performance for the known globals check +// Restore the value for the known globals check. structuredClone = _structuredClone;