Skip to content

lib: implement webidl dictionary converter and use it in structuredClone #55489

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

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 84 additions & 12 deletions lib/internal/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ArrayPrototypePush,
ArrayPrototypeToSorted,
MathAbs,
MathMax,
MathMin,
Expand Down Expand Up @@ -29,6 +30,15 @@ const { kEmptyObject } = require('internal/util');

const converters = { __proto__: null };

const UNDEFINED = 1;
const BOOLEAN = 2;
const STRING = 3;
const SYMBOL = 4;
const NUMBER = 5;
const BIGINT = 6;
const NULL = 7;
const OBJECT = 8;

/**
* @see https://webidl.spec.whatwg.org/#es-any
* @param {any} V
Expand All @@ -39,7 +49,7 @@ converters.any = (V) => {
};

converters.object = (V, opts = kEmptyObject) => {
if (type(V) !== 'Object') {
if (type(V) !== OBJECT) {
throw makeException(
'is not an object',
kEmptyObject,
Expand Down Expand Up @@ -236,37 +246,98 @@ function createEnumConverter(name, values) {

// https://tc39.es/ecma262/#sec-ecmascript-data-types-and-values
function type(V) {
if (V === null)
return 'Null';

switch (typeof V) {
case 'undefined':
return 'Undefined';
return UNDEFINED;
case 'boolean':
return 'Boolean';
return BOOLEAN;
case 'number':
return 'Number';
return NUMBER;
case 'string':
return 'String';
return STRING;
case 'symbol':
return 'Symbol';
return SYMBOL;
case 'bigint':
return 'BigInt';
return BIGINT;
case 'object': // Fall through
case 'function': // Fall through
default:
if (V === null) {
return NULL;
}
// Per ES spec, typeof returns an implementation-defined value that is not
// any of the existing ones for uncallable non-standard exotic objects.
// Yet Type() which the Web IDL spec depends on returns Object for such
// cases. So treat the default case as an object.
return 'Object';
return OBJECT;
}
}

// https://webidl.spec.whatwg.org/#js-dictionary
function createDictionaryConverter(members) {
// The spec requires us to operate the members of a dictionary in
// lexicographical order. We are doing this in the outer scope to
// reduce the overhead that could happen in the returned function.
const sortedMembers = ArrayPrototypeToSorted(members, (a, b) => {
if (a.key === b.key) {
return 0;
}
return a.key < b.key ? -1 : 1;
});

return function(
V,
opts = kEmptyObject,
) {
if (V != null && type(V) !== OBJECT) {
throw makeException(
'cannot be converted to a dictionary',
opts,
);
}

const idlDict = { __proto__: null };
for (let i = 0; i < sortedMembers.length; i++) {
const member = sortedMembers[i];
const key = member.key;
let jsMemberValue;
if (V == null) {
jsMemberValue = undefined;
} else {
jsMemberValue = V[key];
}

if (jsMemberValue !== undefined) {
const memberContext = opts.context ? `${key} in ${opts.context}` : `${key}`;
const converter = member.converter;
const idlMemberValue = converter(
jsMemberValue,
{
__proto__: null,
prefix: opts.prefix,
context: memberContext,
},
);
idlDict[key] = idlMemberValue;
} else if (typeof member.defaultValue === 'function') {
const idlMemberValue = member.defaultValue();
idlDict[key] = idlMemberValue;
} else if (member.required) {
throw makeException(
`cannot be converted because of the missing '${key}'`,
opts,
);
}
}

return idlDict;
};
}

// https://webidl.spec.whatwg.org/#es-sequence
function createSequenceConverter(converter) {
return function(V, opts = kEmptyObject) {
if (type(V) !== 'Object') {
if (type(V) !== OBJECT) {
throw makeException(
'can not be converted to sequence.',
opts);
Expand Down Expand Up @@ -318,6 +389,7 @@ module.exports = {
createEnumConverter,
createInterfaceConverter,
createSequenceConverter,
createDictionaryConverter,
evenRound,
makeException,
};
37 changes: 19 additions & 18 deletions lib/internal/worker/js_transferable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const {
} = primordials;
const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_MISSING_ARGS,
},
} = require('internal/errors');
Expand Down Expand Up @@ -98,29 +97,31 @@ function markTransferMode(obj, cloneable = false, transferable = false) {
obj[transfer_mode_private_symbol] = mode;
}


webidl.converters.StructuredSerializeOptions = webidl
.createDictionaryConverter(
[
{
key: 'transfer',
converter: webidl.converters['sequence<object>'],
defaultValue: () => [],
},
],
);

function structuredClone(value, options) {
if (arguments.length === 0) {
throw new ERR_MISSING_ARGS('The value argument must be specified');
}

// TODO(jazelly): implement generic webidl dictionary converter
const prefix = 'Options';
const optionsType = webidl.type(options);
if (optionsType !== 'Undefined' && optionsType !== 'Null' && optionsType !== 'Object') {
throw new ERR_INVALID_ARG_TYPE(
prefix,
['object', 'null', 'undefined'],
options,
);
}
const key = 'transfer';
const idlOptions = { __proto__: null, [key]: [] };
if (options != null && key in options && options[key] !== undefined) {
idlOptions[key] = webidl.converters['sequence<object>'](options[key], {
const idlOptions = webidl.converters.StructuredSerializeOptions(
options,
{
__proto__: null,
context: 'Transfer',
});
}
prefix: "Failed to execute 'structuredClone'",
context: 'Options',
},
);

const serializedData = nativeStructuredClone(value, idlOptions);
return serializedData;
Expand Down
21 changes: 16 additions & 5 deletions test/parallel/test-structuredClone-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
require('../common');
const assert = require('assert');

const prefix = "Failed to execute 'structuredClone'";
const key = 'transfer';
const context = 'Options';
const memberConverterError = `${prefix}: ${key} in ${context} can not be converted to sequence.`;
const dictionaryConverterError = `${prefix}: ${context} cannot be converted to a dictionary`;

assert.throws(() => structuredClone(), { code: 'ERR_MISSING_ARGS' });
assert.throws(() => structuredClone(undefined, ''), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => structuredClone(undefined, 1), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => structuredClone(undefined, { transfer: 1 }), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => structuredClone(undefined, { transfer: '' }), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => structuredClone(undefined, { transfer: null }), { code: 'ERR_INVALID_ARG_TYPE' });
assert.throws(() => structuredClone(undefined, ''),
{ code: 'ERR_INVALID_ARG_TYPE', message: dictionaryConverterError });
assert.throws(() => structuredClone(undefined, 1),
{ code: 'ERR_INVALID_ARG_TYPE', message: dictionaryConverterError });
assert.throws(() => structuredClone(undefined, { transfer: 1 }),
{ code: 'ERR_INVALID_ARG_TYPE', message: memberConverterError });
assert.throws(() => structuredClone(undefined, { transfer: '' }),
{ code: 'ERR_INVALID_ARG_TYPE', message: memberConverterError });
assert.throws(() => structuredClone(undefined, { transfer: null }),
{ code: 'ERR_INVALID_ARG_TYPE', message: memberConverterError });

// Options can be null or undefined.
assert.strictEqual(structuredClone(undefined), undefined);
Expand Down
Loading