Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,37 @@ describe('structuredClone', () => {
});

it('clones primitive value wrappers', () => {
const throwUnexpectedCoercion = () => {
throw new Error('Unexpected coercion');
};
const preventCoercion = (value: interface {}) => {
Object.defineProperty(value, Symbol.toPrimitive, {
value: throwUnexpectedCoercion,
});
};

// eslint-disable-next-line no-new-wrappers
const numberValue = new Number(1);
preventCoercion(numberValue);
const numberClone = structuredClone(numberValue);
expect(numberClone).not.toBe(numberValue);
expect(numberClone).toBeInstanceOf(Number);
expect(numberClone.valueOf()).toBe(1);

// eslint-disable-next-line no-new-wrappers
const stringValue = new String('foo');
preventCoercion(stringValue);
const stringClone = structuredClone(stringValue);
expect(stringClone).not.toBe(stringValue);
expect(stringClone).toBeInstanceOf(String);
expect(stringClone.valueOf()).toBe('foo');

// eslint-disable-next-line no-new-wrappers
const booleanValue = new Boolean(true);
const booleanValue = new Boolean(false);
const booleanClone = structuredClone(booleanValue);
expect(booleanClone).not.toBe(booleanValue);
expect(booleanClone).toBeInstanceOf(Boolean);
expect(booleanClone.valueOf()).toBe(true);
expect(booleanClone.valueOf()).toBe(false);
});

it('throws with symbols, functions, WeakMap, WeakSet, Promise', () => {
Expand Down Expand Up @@ -200,6 +211,11 @@ describe('structuredClone', () => {

it('clones dates', () => {
const value = new Date('1993-06-11T14:30:45.123Z');
Object.defineProperty(value, Symbol.toPrimitive, {
value() {
throw new Error('Unexpected coercion');
},
});
const clone = structuredClone(value);
expect(clone).not.toBe(value);
expect(clone).toBeInstanceOf(Date);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ const VALID_ERROR_NAMES = new Set([
'URIError',
]);

const BASIC_CONSTRUCTORS = [Number, String, Boolean, Date];

const ObjectPrototype = Object.prototype;
// $FlowFixMe[method-unbinding] this is always called with an explicit receiver.
const numberValueOf = Number.prototype.valueOf;
// $FlowFixMe[method-unbinding] this is always called with an explicit receiver.
const stringValueOf = String.prototype.valueOf;
// $FlowFixMe[method-unbinding] this is always called with an explicit receiver.
const booleanValueOf = Boolean.prototype.valueOf;
// $FlowFixMe[method-unbinding] this is always called with an explicit receiver.
const dateValueOf = Date.prototype.valueOf;

// Technically the memory value should be a parameter in
// `structuredCloneInternal` but as an optimization we can reuse the same map
Expand Down Expand Up @@ -95,13 +101,35 @@ function structuredCloneInternal<T>(value: T): T {

// Handles complex types (typeof === 'object').

for (const Cls of BASIC_CONSTRUCTORS) {
if (value instanceof Cls) {
const result = new Cls(value);
memory.set(value, result);
// $FlowExpectedError[incompatible-type] we know result is T
return result;
}
if (value instanceof Number) {
// eslint-disable-next-line no-new-wrappers
const result = new Number(numberValueOf.call(value));
memory.set(value, result);
// $FlowExpectedError[incompatible-type] we know result is T
return result;
}

if (value instanceof String) {
// eslint-disable-next-line no-new-wrappers
const result = new String(stringValueOf.call(value));
memory.set(value, result);
// $FlowExpectedError[incompatible-type] we know result is T
return result;
}

if (value instanceof Boolean) {
// eslint-disable-next-line no-new-wrappers
const result = new Boolean(booleanValueOf.call(value));
memory.set(value, result);
// $FlowExpectedError[incompatible-type] we know result is T
return result;
}

if (value instanceof Date) {
const result = new Date(dateValueOf.call(value));
memory.set(value, result);
// $FlowExpectedError[incompatible-type] we know result is T
return result;
}

if (value instanceof Map) {
Expand Down
Loading