From 06c316bb664310ea3a39e3dd78b82e9b5b2efcb0 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Wed, 14 Dec 2022 08:59:10 +0700 Subject: [PATCH] fix handling of sparse arrays in `structuredClone`, close #1156 --- CHANGELOG.md | 1 + packages/core-js/modules/web.structured-clone.js | 3 ++- tests/unit-global/web.structured-clone.js | 1 + tests/unit-pure/web.structured-clone.js | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ff966ba589a..3d0ef4e63899 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,6 +74,7 @@ - Added Samsung Internet 19.0 compat data mapping - Added Quest Browser 24.0 compat data mapping - Fixed the first version in the Chromium-based Edge compat data mapping +- Fixed handling of sparse arrays in `structuredClone`, [#1156](https://github.com/zloirock/core-js/issues/1156) - `{ Map, WeakMap }.prototype.emplace` became stricter [by the spec draft](https://tc39.es/proposal-upsert/) - Fixed a theoretically possible future conflict of polyfills definitions in the pure version - Added pure version of the `Number` constructor, [#1154](https://github.com/zloirock/core-js/issues/1154), [#1155](https://github.com/zloirock/core-js/issues/1155), thanks [@trosos](https://github.com/trosos) diff --git a/packages/core-js/modules/web.structured-clone.js b/packages/core-js/modules/web.structured-clone.js index acd2b3f36eab..75c598089049 100644 --- a/packages/core-js/modules/web.structured-clone.js +++ b/packages/core-js/modules/web.structured-clone.js @@ -28,6 +28,7 @@ var IS_DENO = require('../internals/engine-is-deno'); var IS_NODE = require('../internals/engine-is-node'); var Object = global.Object; +var Array = global.Array; var Date = global.Date; var Error = global.Error; var EvalError = global.EvalError; @@ -153,7 +154,7 @@ var structuredCloneInternal = function (value, map) { switch (type) { case 'Array': - cloned = []; + cloned = Array(lengthOfArrayLike(value)); deep = true; break; case 'Object': diff --git a/tests/unit-global/web.structured-clone.js b/tests/unit-global/web.structured-clone.js index b4c91156d22d..576e061f5e36 100644 --- a/tests/unit-global/web.structured-clone.js +++ b/tests/unit-global/web.structured-clone.js @@ -239,6 +239,7 @@ QUnit.module('structuredClone', () => { const arrays = [ [], [1, 2, 3], + Array(1), assign( ['foo', 'bar'], { 10: true, 11: false, 20: 123, 21: 456, 30: null }), diff --git a/tests/unit-pure/web.structured-clone.js b/tests/unit-pure/web.structured-clone.js index cf6cd451ed03..b577d1af27c6 100644 --- a/tests/unit-pure/web.structured-clone.js +++ b/tests/unit-pure/web.structured-clone.js @@ -247,6 +247,7 @@ QUnit.module('structuredClone', () => { const arrays = [ [], [1, 2, 3], + Array(1), assign( ['foo', 'bar'], { 10: true, 11: false, 20: 123, 21: 456, 30: null }),