2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-06-21 13:53:52 -04:00
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
|
|
|
/// <reference path="../webidl/internal.d.ts" />
|
|
|
|
/// <reference path="./internal.d.ts" />
|
|
|
|
/// <reference path="./lib.deno_web.d.ts" />
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
const { InterruptedPrototype, ops } = core;
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
defineEventHandler,
|
|
|
|
EventTarget,
|
|
|
|
MessageEvent,
|
|
|
|
setEventTargetData,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_web/02_event.js";
|
|
|
|
import DOMException from "ext:deno_web/01_dom_exception.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const {
|
|
|
|
ArrayBufferPrototype,
|
2023-04-02 13:41:41 -04:00
|
|
|
ArrayBufferPrototypeGetByteLength,
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypeFilter,
|
|
|
|
ArrayPrototypeIncludes,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
ObjectSetPrototypeOf,
|
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
|
|
|
SymbolIterator,
|
|
|
|
TypeError,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
class MessageChannel {
|
|
|
|
/** @type {MessagePort} */
|
|
|
|
#port1;
|
|
|
|
/** @type {MessagePort} */
|
|
|
|
#port2;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
const { 0: port1Id, 1: port2Id } = opCreateEntangledMessagePort();
|
|
|
|
const port1 = createMessagePort(port1Id);
|
|
|
|
const port2 = createMessagePort(port2Id);
|
|
|
|
this.#port1 = port1;
|
|
|
|
this.#port2 = port2;
|
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get port1() {
|
|
|
|
webidl.assertBranded(this, MessageChannelPrototype);
|
|
|
|
return this.#port1;
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get port2() {
|
|
|
|
webidl.assertBranded(this, MessageChannelPrototype);
|
|
|
|
return this.#port2;
|
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolFor("Deno.inspect")](inspect) {
|
|
|
|
return `MessageChannel ${
|
|
|
|
inspect({ port1: this.port1, port2: this.port2 })
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
webidl.configurePrototype(MessageChannel);
|
|
|
|
const MessageChannelPrototype = MessageChannel.prototype;
|
|
|
|
|
|
|
|
const _id = Symbol("id");
|
|
|
|
const _enabled = Symbol("enabled");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} id
|
|
|
|
* @returns {MessagePort}
|
|
|
|
*/
|
|
|
|
function createMessagePort(id) {
|
|
|
|
const port = core.createHostObject();
|
|
|
|
ObjectSetPrototypeOf(port, MessagePortPrototype);
|
|
|
|
port[webidl.brand] = webidl.brand;
|
|
|
|
setEventTargetData(port);
|
|
|
|
port[_id] = id;
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MessagePort extends EventTarget {
|
|
|
|
/** @type {number | null} */
|
|
|
|
[_id] = null;
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_enabled] = false;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
webidl.illegalConstructor();
|
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
|
|
|
|
/**
|
2023-02-07 14:22:46 -05:00
|
|
|
* @param {any} message
|
|
|
|
* @param {object[] | StructuredSerializeOptions} transferOrOptions
|
2021-06-21 13:53:52 -04:00
|
|
|
*/
|
2023-02-07 14:22:46 -05:00
|
|
|
postMessage(message, transferOrOptions = {}) {
|
|
|
|
webidl.assertBranded(this, MessagePortPrototype);
|
|
|
|
const prefix = "Failed to execute 'postMessage' on 'MessagePort'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-02-07 14:22:46 -05:00
|
|
|
message = webidl.converters.any(message);
|
|
|
|
let options;
|
|
|
|
if (
|
|
|
|
webidl.type(transferOrOptions) === "Object" &&
|
|
|
|
transferOrOptions !== undefined &&
|
|
|
|
transferOrOptions[SymbolIterator] !== undefined
|
|
|
|
) {
|
|
|
|
const transfer = webidl.converters["sequence<object>"](
|
|
|
|
transferOrOptions,
|
2023-05-01 06:47:13 -04:00
|
|
|
prefix,
|
|
|
|
"Argument 2",
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
options = { transfer };
|
|
|
|
} else {
|
|
|
|
options = webidl.converters.StructuredSerializeOptions(
|
|
|
|
transferOrOptions,
|
2023-05-01 06:47:13 -04:00
|
|
|
prefix,
|
|
|
|
"Argument 2",
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const { transfer } = options;
|
|
|
|
if (ArrayPrototypeIncludes(transfer, this)) {
|
|
|
|
throw new DOMException("Can not tranfer self", "DataCloneError");
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const data = serializeJsMessageData(message, transfer);
|
|
|
|
if (this[_id] === null) return;
|
|
|
|
ops.op_message_port_post_message(this[_id], data);
|
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
start() {
|
|
|
|
webidl.assertBranded(this, MessagePortPrototype);
|
|
|
|
if (this[_enabled]) return;
|
|
|
|
(async () => {
|
|
|
|
this[_enabled] = true;
|
|
|
|
while (true) {
|
|
|
|
if (this[_id] === null) break;
|
|
|
|
let data;
|
|
|
|
try {
|
|
|
|
data = await core.opAsync(
|
|
|
|
"op_message_port_recv_message",
|
|
|
|
this[_id],
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(InterruptedPrototype, err)) break;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
if (data === null) break;
|
|
|
|
let message, transferables;
|
|
|
|
try {
|
|
|
|
const v = deserializeJsMessageData(data);
|
|
|
|
message = v[0];
|
|
|
|
transferables = v[1];
|
|
|
|
} catch (err) {
|
|
|
|
const event = new MessageEvent("messageerror", { data: err });
|
2021-06-21 13:53:52 -04:00
|
|
|
this.dispatchEvent(event);
|
2023-02-07 14:22:46 -05:00
|
|
|
return;
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const event = new MessageEvent("message", {
|
|
|
|
data: message,
|
|
|
|
ports: ArrayPrototypeFilter(
|
|
|
|
transferables,
|
|
|
|
(t) => ObjectPrototypeIsPrototypeOf(MessagePortPrototype, t),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_enabled] = false;
|
|
|
|
})();
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close() {
|
|
|
|
webidl.assertBranded(this, MessagePortPrototype);
|
|
|
|
if (this[_id] !== null) {
|
|
|
|
core.close(this[_id]);
|
|
|
|
this[_id] = null;
|
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
defineEventHandler(MessagePort.prototype, "message", function (self) {
|
|
|
|
self.start();
|
|
|
|
});
|
|
|
|
defineEventHandler(MessagePort.prototype, "messageerror");
|
|
|
|
|
|
|
|
webidl.configurePrototype(MessagePort);
|
|
|
|
const MessagePortPrototype = MessagePort.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {[number, number]}
|
|
|
|
*/
|
|
|
|
function opCreateEntangledMessagePort() {
|
|
|
|
return ops.op_message_port_create_entangled();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {messagePort.MessageData} messageData
|
|
|
|
* @returns {[any, object[]]}
|
|
|
|
*/
|
|
|
|
function deserializeJsMessageData(messageData) {
|
|
|
|
/** @type {object[]} */
|
|
|
|
const transferables = [];
|
|
|
|
const hostObjects = [];
|
|
|
|
const arrayBufferIdsInTransferables = [];
|
|
|
|
const transferredArrayBuffers = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < messageData.transferables.length; ++i) {
|
|
|
|
const transferable = messageData.transferables[i];
|
|
|
|
switch (transferable.kind) {
|
|
|
|
case "messagePort": {
|
|
|
|
const port = createMessagePort(transferable.data);
|
|
|
|
ArrayPrototypePush(transferables, port);
|
|
|
|
ArrayPrototypePush(hostObjects, port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "arrayBuffer": {
|
|
|
|
ArrayPrototypePush(transferredArrayBuffers, transferable.data);
|
|
|
|
const index = ArrayPrototypePush(transferables, null);
|
|
|
|
ArrayPrototypePush(arrayBufferIdsInTransferables, index);
|
|
|
|
break;
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new TypeError("Unreachable");
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-06-21 13:53:52 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const data = core.deserialize(messageData.data, {
|
|
|
|
hostObjects,
|
|
|
|
transferredArrayBuffers,
|
|
|
|
});
|
2021-08-25 07:48:53 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let i = 0; i < arrayBufferIdsInTransferables.length; ++i) {
|
|
|
|
const id = arrayBufferIdsInTransferables[i];
|
|
|
|
transferables[id] = transferredArrayBuffers[i];
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return [data, transferables];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any} data
|
|
|
|
* @param {object[]} transferables
|
|
|
|
* @returns {messagePort.MessageData}
|
|
|
|
*/
|
|
|
|
function serializeJsMessageData(data, transferables) {
|
|
|
|
const transferredArrayBuffers = [];
|
|
|
|
for (let i = 0, j = 0; i < transferables.length; i++) {
|
|
|
|
const ab = transferables[i];
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, ab)) {
|
2023-04-02 13:41:41 -04:00
|
|
|
if (
|
|
|
|
ArrayBufferPrototypeGetByteLength(ab) === 0 &&
|
|
|
|
ops.op_arraybuffer_was_detached(ab)
|
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException(
|
|
|
|
`ArrayBuffer at index ${j} is already detached`,
|
|
|
|
"DataCloneError",
|
|
|
|
);
|
2021-08-25 07:48:53 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
j++;
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(transferredArrayBuffers, ab);
|
2021-08-25 07:48:53 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-08-25 07:48:53 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const serializedData = core.serialize(data, {
|
|
|
|
hostObjects: ArrayPrototypeFilter(
|
|
|
|
transferables,
|
|
|
|
(a) => ObjectPrototypeIsPrototypeOf(MessagePortPrototype, a),
|
|
|
|
),
|
|
|
|
transferredArrayBuffers,
|
|
|
|
}, (err) => {
|
|
|
|
throw new DOMException(err, "DataCloneError");
|
|
|
|
});
|
2021-06-21 13:53:52 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @type {messagePort.Transferable[]} */
|
|
|
|
const serializedTransferables = [];
|
|
|
|
|
|
|
|
let arrayBufferI = 0;
|
|
|
|
for (let i = 0; i < transferables.length; ++i) {
|
|
|
|
const transferable = transferables[i];
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(MessagePortPrototype, transferable)) {
|
|
|
|
webidl.assertBranded(transferable, MessagePortPrototype);
|
|
|
|
const id = transferable[_id];
|
|
|
|
if (id === null) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Can not transfer disentangled message port",
|
|
|
|
"DataCloneError",
|
|
|
|
);
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
transferable[_id] = null;
|
|
|
|
ArrayPrototypePush(serializedTransferables, {
|
|
|
|
kind: "messagePort",
|
|
|
|
data: id,
|
|
|
|
});
|
|
|
|
} else if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, transferable)
|
|
|
|
) {
|
|
|
|
ArrayPrototypePush(serializedTransferables, {
|
|
|
|
kind: "arrayBuffer",
|
|
|
|
data: transferredArrayBuffers[arrayBufferI],
|
|
|
|
});
|
|
|
|
arrayBufferI++;
|
|
|
|
} else {
|
|
|
|
throw new DOMException("Value not transferable", "DataCloneError");
|
2021-06-21 13:53:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return {
|
|
|
|
data: serializedData,
|
|
|
|
transferables: serializedTransferables,
|
2021-06-21 13:53:52 -04:00
|
|
|
};
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
webidl.converters.StructuredSerializeOptions = webidl
|
|
|
|
.createDictionaryConverter(
|
|
|
|
"StructuredSerializeOptions",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key: "transfer",
|
|
|
|
converter: webidl.converters["sequence<object>"],
|
|
|
|
get defaultValue() {
|
|
|
|
return [];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
function structuredClone(value, options) {
|
|
|
|
const prefix = "Failed to execute 'structuredClone'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
options = webidl.converters.StructuredSerializeOptions(
|
|
|
|
options,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 2",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
const messageData = serializeJsMessageData(value, options.transfer);
|
|
|
|
return deserializeJsMessageData(messageData)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
deserializeJsMessageData,
|
|
|
|
MessageChannel,
|
|
|
|
MessagePort,
|
|
|
|
MessagePortPrototype,
|
|
|
|
serializeJsMessageData,
|
|
|
|
structuredClone,
|
|
|
|
};
|