Skip to content

Commit

Permalink
structuredClone should throw an error if no arguments passed
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jan 9, 2022
1 parent 336eae4 commit 4f12ebe
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Changelog
##### Unreleased
- Detects and replaces broken third-party `Function#bind` polyfills, uses only native `Function#bind` in the internals
- `structuredClone` should throw an error if no arguments passed

##### 3.20.2 - 2022.01.02
- Added a fix of [a V8 ~ Chrome 36- `Object.{ defineProperty, defineProperties }` bug](https://bugs.chromium.org/p/v8/issues/detail?id=3334), [Babel issue](https://github.com/babel/babel/issues/14056)
Expand Down
8 changes: 8 additions & 0 deletions packages/core-js/internals/validate-arguments-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var global = require('../internals/global');

var TypeError = global.TypeError;

module.exports = function (passed, required) {
if (passed < required) throw TypeError('Not enough arguments');
return passed;
};
3 changes: 2 additions & 1 deletion packages/core-js/modules/web.structured-clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var hasOwn = require('../internals/has-own-property');
var createProperty = require('../internals/create-property');
var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
var lengthOfArrayLike = require('../internals/length-of-array-like');
var validateArgumentsLength = require('../internals/validate-arguments-length');
var regExpFlags = require('../internals/regexp-flags');
var ERROR_STACK_INSTALLABLE = require('../internals/error-stack-installable');

Expand Down Expand Up @@ -447,7 +448,7 @@ var tryToTransfer = function (rawTransfer, map) {

$({ global: true, enumerable: true, sham: !PROPER_TRANSFER, forced: FORCED_REPLACEMENT }, {
structuredClone: function structuredClone(value /* , { transfer } */) {
var options = arguments.length > 1 ? anObject(arguments[1]) : undefined;
var options = validateArgumentsLength(arguments.length, 1) > 1 ? anObject(arguments[1]) : undefined;
var transfer = options ? options.transfer : undefined;
var map;

Expand Down
5 changes: 1 addition & 4 deletions packages/core-js/modules/web.url-search-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var create = require('../internals/object-create');
var createPropertyDescriptor = require('../internals/create-property-descriptor');
var getIterator = require('../internals/get-iterator');
var getIteratorMethod = require('../internals/get-iterator-method');
var validateArgumentsLength = require('../internals/validate-arguments-length');
var wellKnownSymbol = require('../internals/well-known-symbol');
var arraySort = require('../internals/array-sort');

Expand Down Expand Up @@ -99,10 +100,6 @@ var serialize = function (it) {
return replace(encodeURIComponent(it), find, replacer);
};

var validateArgumentsLength = function (passed, required) {
if (passed < required) throw TypeError('Not enough arguments');
};

var URLSearchParamsIterator = createIteratorConstructor(function Iterator(params, kind) {
setInternalState(this, {
type: URL_SEARCH_PARAMS_ITERATOR,
Expand Down
1 change: 1 addition & 0 deletions tests/pure/web.structured-clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ QUnit.module('structuredClone', () => {
assert.isFunction(structuredClone, 'structuredClone is a function');
assert.name(structuredClone, 'structuredClone');
assert.arity(structuredClone, 1);
assert.throws(() => structuredClone(), 'throws without arguments');
});

function cloneTest(value, verifyFunc) {
Expand Down
1 change: 1 addition & 0 deletions tests/tests/web.structured-clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ QUnit.module('structuredClone', () => {
assert.name(structuredClone, 'structuredClone');
assert.arity(structuredClone, 1);
if (!NODE) assert.looksNative(structuredClone);
assert.throws(() => structuredClone(), 'throws without arguments');
});

function cloneTest(value, verifyFunc) {
Expand Down

0 comments on commit 4f12ebe

Please sign in to comment.