Skip to content

Commit

Permalink
lib: throw error in structuedClone when no arguments are passed
Browse files Browse the repository at this point in the history
PR-URL: #41651
Fixes: #41450
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
Reviewed-By: Mestery <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
  • Loading branch information
gioragutt authored and ruyadorno committed Feb 7, 2022
1 parent 2bba6cd commit 8ab0540
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 9 additions & 1 deletion lib/internal/structured_clone.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
'use strict';

const {
codes: { ERR_MISSING_ARGS },
} = require('internal/errors');

const {
MessageChannel,
receiveMessageOnPort,
} = require('internal/worker/io');

let channel;
function structuredClone(value, options = undefined) {
if (arguments.length === 0) {
throw new ERR_MISSING_ARGS('value');
}

// TODO: Improve this with a more efficient solution that avoids
// instantiating a MessageChannel
channel ??= new MessageChannel();
Expand All @@ -17,5 +25,5 @@ function structuredClone(value, options = undefined) {
}

module.exports = {
structuredClone
structuredClone,
};
8 changes: 6 additions & 2 deletions test/parallel/test-structuredClone-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
require('../common');

const {
structuredClone: _structuredClone
structuredClone: _structuredClone,
} = require('internal/structured_clone');

const {
strictEqual
strictEqual,
throws,
} = require('assert');

strictEqual(globalThis.structuredClone, _structuredClone);
Expand All @@ -17,3 +19,5 @@ strictEqual(globalThis.structuredClone, undefined);

// Restore the value for the known globals check.
structuredClone = _structuredClone;

throws(() => _structuredClone(), /ERR_MISSING_ARGS/);

0 comments on commit 8ab0540

Please sign in to comment.