From 16ae4a0d5799c9a4ed776f32929f73b1063ae4e8 Mon Sep 17 00:00:00 2001 From: Leo K Date: Mon, 9 Aug 2021 10:39:00 +0200 Subject: feat(extensions/web): add structuredClone function (#11572) Co-authored-by: Luca Casonato --- extensions/web/13_message_port.js | 49 ++++++++++++++++++++++++++------------- extensions/web/lib.deno_web.d.ts | 17 ++++++++++++-- 2 files changed, 48 insertions(+), 18 deletions(-) (limited to 'extensions') diff --git a/extensions/web/13_message_port.js b/extensions/web/13_message_port.js index d111b5e01..d5014fdb9 100644 --- a/extensions/web/13_message_port.js +++ b/extensions/web/13_message_port.js @@ -89,7 +89,7 @@ /** * @param {any} message - * @param {object[] | PostMessageOptions} transferOrOptions + * @param {object[] | StructuredSerializeOptions} transferOrOptions */ postMessage(message, transferOrOptions = {}) { webidl.assertBranded(this, MessagePort); @@ -108,10 +108,13 @@ ); options = { transfer }; } else { - options = webidl.converters.PostMessageOptions(transferOrOptions, { - prefix, - context: "Argument 2", - }); + options = webidl.converters.StructuredSerializeOptions( + transferOrOptions, + { + prefix, + context: "Argument 2", + }, + ); } const { transfer } = options; if (transfer.includes(this)) { @@ -247,23 +250,37 @@ }; } - webidl.converters.PostMessageOptions = webidl.createDictionaryConverter( - "PostMessageOptions", - [ - { - key: "transfer", - converter: webidl.converters["sequence"], - get defaultValue() { - return []; + webidl.converters.StructuredSerializeOptions = webidl + .createDictionaryConverter( + "StructuredSerializeOptions", + [ + { + key: "transfer", + converter: webidl.converters["sequence"], + get defaultValue() { + return []; + }, }, - }, - ], - ); + ], + ); + + function structuredClone(value, options) { + const prefix = "Failed to execute 'structuredClone'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + options = webidl.converters.StructuredSerializeOptions(options, { + prefix, + context: "Argument 2", + }); + const messageData = serializeJsMessageData(value, options.transfer); + const [data] = deserializeJsMessageData(messageData); + return data; + } window.__bootstrap.messagePort = { MessageChannel, MessagePort, deserializeJsMessageData, serializeJsMessageData, + structuredClone, }; })(globalThis); diff --git a/extensions/web/lib.deno_web.d.ts b/extensions/web/lib.deno_web.d.ts index 6c79f61bd..3f110353f 100644 --- a/extensions/web/lib.deno_web.d.ts +++ b/extensions/web/lib.deno_web.d.ts @@ -673,7 +673,15 @@ declare class MessageEvent extends Event { type Transferable = ArrayBuffer | MessagePort; -interface PostMessageOptions { +/** + * @deprecated + * + * This type has been renamed to StructuredSerializeOptions. Use that type for + * new code. + */ +type PostMessageOptions = StructuredSerializeOptions; + +interface StructuredSerializeOptions { transfer?: Transferable[]; } @@ -710,7 +718,7 @@ declare class MessagePort extends EventTarget { * objects or port, or if message could not be cloned. */ postMessage(message: any, transfer: Transferable[]): void; - postMessage(message: any, options?: PostMessageOptions): void; + postMessage(message: any, options?: StructuredSerializeOptions): void; /** * Begins dispatching messages received on the port. This is implictly called * when assiging a value to `this.onmessage`. @@ -737,3 +745,8 @@ declare class MessagePort extends EventTarget { options?: boolean | EventListenerOptions, ): void; } + +declare function structuredClone( + value: any, + options?: StructuredSerializeOptions, +): any; -- cgit v1.2.3