Skip to content

Commit

Permalink
apply suggestions from review
Browse files Browse the repository at this point in the history
  • Loading branch information
BasixKOR committed Sep 16, 2021
1 parent f02e40c commit 422802c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
37 changes: 20 additions & 17 deletions packages/core-js/internals/structured-clone.js
@@ -1,10 +1,13 @@
/* eslint-disable es/no-map -- safe */
/* eslint-disable es/no-set -- safe */
'use const';
var isSymbol = require('./is-symbol');
var toObject = require('./to-object');
var getOwnPropertyNames = require('./object-get-own-property-names');
var classof = require('./classof');
'use strict';
var isSymbol = require('../internals/is-symbol');
var getOwnPropertyNames = require('../internals/object-get-own-property-names');
var classof = require('../internals/classof');
var getBuiltin = require('../internals/get-built-in');
var isObject = require('../internals/is-object');
var has = require('../internals/has');

var Set = getBuiltin('Set');
var Map = getBuiltin('Map');

function createDataCloneError(message) {
if (typeof DOMException === 'function') {
Expand All @@ -21,18 +24,19 @@ function createDataCloneError(message) {
*/
module.exports = function structuredCloneInternal(weakmap, value) {
if (isSymbol(value)) throw createDataCloneError('Symbols are not cloneable');
if (typeof value !== 'function' && typeof value !== 'object') return value;
if (!isObject(value)) return value;
if (value === null) return null;
if (weakmap.has(value)) return weakmap.get(value); // effectively preserves circular references

var cloned, i, deep;
var type = classof(value);

switch (classof(value)) {
switch (type) {
case 'Boolean':
case 'BigInt':
case 'Number':
case 'String':
cloned = toObject(value.valueOf());
cloned = Object(value.valueOf());
break;
case 'Date':
cloned = new Date(value.valueOf());
Expand Down Expand Up @@ -67,12 +71,12 @@ module.exports = function structuredCloneInternal(weakmap, value) {
deep = true;
break;
default:
throw createDataCloneError('Uncloneable type: ' + classof(value));
throw createDataCloneError('Uncloneable type: ' + type);
}

weakmap.set(value, cloned);

if (deep) switch (classof(value)) {
if (deep) switch (type) {
case 'Map':
value.forEach(function (v, k) {
cloned.set(structuredCloneInternal(weakmap, k), structuredCloneInternal(weakmap, v));
Expand All @@ -86,14 +90,13 @@ module.exports = function structuredCloneInternal(weakmap, value) {
case 'Error':
// Attempt to clone the stack.
if (
!Object.prototype.hasOwnProperty.call(value, 'stack') && // Chrome, Safari
!Object.prototype.hasOwnProperty.call(Error.prototype, 'stack') // Firefox
!has(value, 'stack') && // Chrome, Safari
!has(Error.prototype, 'stack') // Firefox
) break;
try {
cloned.stack = structuredCloneInternal(weakmap, value.stack);
} catch (error) {
if (classof(error) === 'TypeError') return cloned; // Stack cloning not avaliable.
throw error; // Unexpected error while cloning.
} catch (_) {
return cloned; // Stack cloning not avaliable.
}
break;
case 'Array':
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/web/structured-clone.js
@@ -1,4 +1,6 @@

require('../modules/es.weak-map');
require('../modules/es.map');
require('../modules/es.set');
require('../modules/web.structured-clone');
var path = require('../internals/path');

Expand Down
6 changes: 3 additions & 3 deletions tests/compat/tests.js
Expand Up @@ -1546,6 +1546,9 @@ GLOBAL.tests = {
'web.queue-microtask': function () {
return Object.getOwnPropertyDescriptor(GLOBAL, 'queueMicrotask').value;
},
'web.structured-clone': function () {
return typeof structuredClone === 'function';
},
'web.timers': function () {
return !/MSIE .\./.test(USERAGENT);
},
Expand All @@ -1554,7 +1557,4 @@ GLOBAL.tests = {
return URL.prototype.toJSON;
}],
'web.url-search-params': URL_AND_URL_SEARCH_PARAMS_SUPPORT,
'web.structured-clone': function () {
return typeof structuredClone === 'function';
}
};

0 comments on commit 422802c

Please sign in to comment.