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 @@ -117,6 +117,16 @@ describe('structuredClone', () => {
expectDataCloneError(() => structuredClone(Promise.resolve(4)));
});

it('does not write to WeakMap, WeakSet, or Promise prototypes', () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: *NOT

Object.freeze(WeakMap.prototype);
Object.freeze(WeakSet.prototype);
Object.freeze(Promise.prototype);

expectDataCloneError(() => structuredClone(new WeakMap()));
expectDataCloneError(() => structuredClone(new WeakSet()));
expectDataCloneError(() => structuredClone(new Promise(() => {})));
Comment on lines +124 to +127

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible comment worth it here clarifying

i.e. mutated prototypes throw a loud: cannot add a new property (#60002)
but fantom compiles the bundle sloppy, so would silently no-op instead

});

it('clones simple objects', () => {
const value = {foo: 'bar'};
const clone = structuredClone(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ export default function structuredClone<T>(value: T): T {
}
}

const NON_SERIALIZABLE_OBJECT_KEY = Symbol('nonSerializableObject');

function cloneArrayBuffer(value: ArrayBuffer): ArrayBuffer {
const existingClone = memory.get(value);
if (existingClone instanceof ArrayBuffer) {
Expand Down Expand Up @@ -392,16 +390,11 @@ function createErrorClone(value: Error): Error {
}

function isNonSerializableObject<T extends interface {}>(obj: T): boolean {
// $FlowExpectedError[invalid-in-lhs]
return NON_SERIALIZABLE_OBJECT_KEY in obj;
}

function markClassAsNonSerializable<T>(cls: Class<T>): void {
// $FlowExpectedError[incompatible-use]
cls.prototype[NON_SERIALIZABLE_OBJECT_KEY] = true;
// Built-in prototypes must not be written to because Hardened JS
// environments (SES) freeze them, so the non-serializable
// built-ins are detected with `instanceof` instead of marking the
// prototypes with e.g. Symbol('nonSerializableObject').
Comment on lines +393 to +396

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a required comment, can be deduced from git history (blame / prev commit)

but the human context is helpful

return (
obj instanceof WeakMap || obj instanceof WeakSet || obj instanceof Promise
);
}

// Non-serializable built-ins.
markClassAsNonSerializable(WeakMap);
markClassAsNonSerializable(WeakSet);
markClassAsNonSerializable(Promise);
Loading