Skip to content

Commit 1bb8546

Browse files
rubennortefacebook-github-bot
authored andcommitted
Introduce better mechanism for non-serializable built-ins in structuredClone (#51255)
Summary: Pull Request resolved: #51255 Changelog: [internal] Just a small refactor of how we handle non-serializable built-ins in structuredClone Reviewed By: hoxyq Differential Revision: D74398113 fbshipit-source-id: 26211920fcd4cf11c5da278e213eaa2b6b9aa1b5
1 parent caaa5c9 commit 1bb8546

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

packages/react-native/src/private/webapis/structuredClone/structuredClone.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,7 @@ function structuredCloneInternal<T>(value: T): T {
155155
}
156156

157157
// Known non-serializable objects.
158-
// TODO: Handle this more holistically
159-
if (
160-
value instanceof WeakMap ||
161-
value instanceof WeakSet ||
162-
value instanceof Promise
163-
) {
158+
if (isNonSerializableObject(value)) {
164159
throw new DOMException(
165160
`Failed to execute 'structuredClone' on 'Window': ${String(value)} could not be cloned.`,
166161
'DataCloneError',
@@ -208,3 +203,20 @@ export default function structuredClone<T>(value: T): T {
208203
memory.clear();
209204
}
210205
}
206+
207+
const NON_SERIALIZABLE_OBJECT_KEY = Symbol('nonSerializableObject');
208+
209+
function isNonSerializableObject<T: interface {}>(obj: T): boolean {
210+
// $FlowExpectedError[invalid-in-lhs]
211+
return NON_SERIALIZABLE_OBJECT_KEY in obj;
212+
}
213+
214+
function markClassAsNonSerializable<T>(cls: Class<T>): void {
215+
// $FlowExpectedError[incompatible-use]
216+
cls.prototype[NON_SERIALIZABLE_OBJECT_KEY] = true;
217+
}
218+
219+
// Non-serializable built-ins.
220+
markClassAsNonSerializable(WeakMap);
221+
markClassAsNonSerializable(WeakSet);
222+
markClassAsNonSerializable(Promise);

0 commit comments

Comments
 (0)