2023-01-02 21:00:42 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-06-21 17:53:52 +00: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" />
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
((window) => {
|
|
|
|
const core = window.Deno.core;
|
2022-08-11 13:56:56 +00:00
|
|
|
const { InterruptedPrototype, ops } = core;
|
2021-06-21 17:53:52 +00:00
|
|
|
const webidl = window.__bootstrap.webidl;
|
2022-10-24 14:14:17 +00:00
|
|
|
const { EventTarget, setEventTargetData } = window.__bootstrap.eventTarget;
|
|
|
|
const { MessageEvent, defineEventHandler } = window.__bootstrap.event;
|
2021-07-03 19:32:28 +00:00
|
|
|
const { DOMException } = window.__bootstrap.domException;
|
2021-07-26 11:52:59 +00:00
|
|
|
const {
|
2022-02-01 17:06:11 +00:00
|
|
|
ArrayBufferPrototype,
|
2021-08-25 11:48:53 +00:00
|
|
|
ArrayPrototypeFilter,
|
2021-08-13 08:13:46 +00:00
|
|
|
ArrayPrototypeIncludes,
|
|
|
|
ArrayPrototypePush,
|
2022-02-01 17:06:11 +00:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2021-07-26 11:52:59 +00:00
|
|
|
ObjectSetPrototypeOf,
|
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
2022-02-01 17:06:11 +00:00
|
|
|
SymbolIterator,
|
2021-07-26 11:52:59 +00:00
|
|
|
TypeError,
|
|
|
|
} = window.__bootstrap.primordials;
|
2021-06-21 17:53:52 +00:00
|
|
|
|
|
|
|
class MessageChannel {
|
|
|
|
/** @type {MessagePort} */
|
|
|
|
#port1;
|
|
|
|
/** @type {MessagePort} */
|
|
|
|
#port2;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this[webidl.brand] = webidl.brand;
|
2023-01-16 16:17:18 +00:00
|
|
|
const { 0: port1Id, 1: port2Id } = opCreateEntangledMessagePort();
|
2021-06-21 17:53:52 +00:00
|
|
|
const port1 = createMessagePort(port1Id);
|
|
|
|
const port2 = createMessagePort(port2Id);
|
|
|
|
this.#port1 = port1;
|
|
|
|
this.#port2 = port2;
|
|
|
|
}
|
|
|
|
|
|
|
|
get port1() {
|
2022-02-01 17:06:11 +00:00
|
|
|
webidl.assertBranded(this, MessageChannelPrototype);
|
2021-06-21 17:53:52 +00:00
|
|
|
return this.#port1;
|
|
|
|
}
|
|
|
|
|
|
|
|
get port2() {
|
2022-02-01 17:06:11 +00:00
|
|
|
webidl.assertBranded(this, MessageChannelPrototype);
|
2021-06-21 17:53:52 +00:00
|
|
|
return this.#port2;
|
|
|
|
}
|
|
|
|
|
2021-07-26 11:52:59 +00:00
|
|
|
[SymbolFor("Deno.inspect")](inspect) {
|
2021-06-21 17:53:52 +00:00
|
|
|
return `MessageChannel ${
|
|
|
|
inspect({ port1: this.port1, port2: this.port2 })
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
webidl.configurePrototype(MessageChannel);
|
2022-02-01 17:06:11 +00:00
|
|
|
const MessageChannelPrototype = MessageChannel.prototype;
|
2021-06-21 17:53:52 +00:00
|
|
|
|
|
|
|
const _id = Symbol("id");
|
|
|
|
const _enabled = Symbol("enabled");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} id
|
|
|
|
* @returns {MessagePort}
|
|
|
|
*/
|
|
|
|
function createMessagePort(id) {
|
2021-06-26 09:17:05 +00:00
|
|
|
const port = core.createHostObject();
|
2022-02-01 17:06:11 +00:00
|
|
|
ObjectSetPrototypeOf(port, MessagePortPrototype);
|
2021-06-26 09:17:05 +00:00
|
|
|
port[webidl.brand] = webidl.brand;
|
2021-06-21 17:53:52 +00:00
|
|
|
setEventTargetData(port);
|
|
|
|
port[_id] = id;
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MessagePort extends EventTarget {
|
|
|
|
/** @type {number | null} */
|
|
|
|
[_id] = null;
|
|
|
|
/** @type {boolean} */
|
|
|
|
[_enabled] = false;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
webidl.illegalConstructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any} message
|
2021-08-09 08:39:00 +00:00
|
|
|
* @param {object[] | StructuredSerializeOptions} transferOrOptions
|
2021-06-21 17:53:52 +00:00
|
|
|
*/
|
|
|
|
postMessage(message, transferOrOptions = {}) {
|
2022-02-01 17:06:11 +00:00
|
|
|
webidl.assertBranded(this, MessagePortPrototype);
|
2021-06-21 17:53:52 +00:00
|
|
|
const prefix = "Failed to execute 'postMessage' on 'MessagePort'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
message = webidl.converters.any(message);
|
|
|
|
let options;
|
|
|
|
if (
|
|
|
|
webidl.type(transferOrOptions) === "Object" &&
|
|
|
|
transferOrOptions !== undefined &&
|
2022-02-01 17:06:11 +00:00
|
|
|
transferOrOptions[SymbolIterator] !== undefined
|
2021-06-21 17:53:52 +00:00
|
|
|
) {
|
|
|
|
const transfer = webidl.converters["sequence<object>"](
|
|
|
|
transferOrOptions,
|
|
|
|
{ prefix, context: "Argument 2" },
|
|
|
|
);
|
|
|
|
options = { transfer };
|
|
|
|
} else {
|
2021-08-09 08:39:00 +00:00
|
|
|
options = webidl.converters.StructuredSerializeOptions(
|
|
|
|
transferOrOptions,
|
|
|
|
{
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
},
|
|
|
|
);
|
2021-06-21 17:53:52 +00:00
|
|
|
}
|
|
|
|
const { transfer } = options;
|
2021-08-13 08:13:46 +00:00
|
|
|
if (ArrayPrototypeIncludes(transfer, this)) {
|
2021-06-21 17:53:52 +00:00
|
|
|
throw new DOMException("Can not tranfer self", "DataCloneError");
|
|
|
|
}
|
|
|
|
const data = serializeJsMessageData(message, transfer);
|
|
|
|
if (this[_id] === null) return;
|
2022-08-11 13:56:56 +00:00
|
|
|
ops.op_message_port_post_message(this[_id], data);
|
2021-06-21 17:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2022-02-01 17:06:11 +00:00
|
|
|
webidl.assertBranded(this, MessagePortPrototype);
|
2021-06-21 17:53:52 +00:00
|
|
|
if (this[_enabled]) return;
|
|
|
|
(async () => {
|
|
|
|
this[_enabled] = true;
|
|
|
|
while (true) {
|
|
|
|
if (this[_id] === null) break;
|
2021-10-09 20:37:19 +00:00
|
|
|
let data;
|
|
|
|
try {
|
|
|
|
data = await core.opAsync(
|
|
|
|
"op_message_port_recv_message",
|
|
|
|
this[_id],
|
|
|
|
);
|
|
|
|
} catch (err) {
|
2022-02-01 17:06:11 +00:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(InterruptedPrototype, err)) break;
|
2021-10-09 20:37:19 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2021-06-21 17:53:52 +00:00
|
|
|
if (data === null) break;
|
2021-08-25 11:48:53 +00:00
|
|
|
let message, transferables;
|
2021-06-21 17:53:52 +00:00
|
|
|
try {
|
|
|
|
const v = deserializeJsMessageData(data);
|
|
|
|
message = v[0];
|
2021-08-25 11:48:53 +00:00
|
|
|
transferables = v[1];
|
2021-06-21 17:53:52 +00:00
|
|
|
} catch (err) {
|
|
|
|
const event = new MessageEvent("messageerror", { data: err });
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const event = new MessageEvent("message", {
|
|
|
|
data: message,
|
2021-08-25 11:48:53 +00:00
|
|
|
ports: ArrayPrototypeFilter(
|
|
|
|
transferables,
|
2022-02-01 17:06:11 +00:00
|
|
|
(t) => ObjectPrototypeIsPrototypeOf(MessagePortPrototype, t),
|
2021-08-25 11:48:53 +00:00
|
|
|
),
|
2021-06-21 17:53:52 +00:00
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
this[_enabled] = false;
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2022-02-01 17:06:11 +00:00
|
|
|
webidl.assertBranded(this, MessagePortPrototype);
|
2021-06-21 17:53:52 +00:00
|
|
|
if (this[_id] !== null) {
|
|
|
|
core.close(this[_id]);
|
|
|
|
this[_id] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defineEventHandler(MessagePort.prototype, "message", function (self) {
|
|
|
|
self.start();
|
|
|
|
});
|
|
|
|
defineEventHandler(MessagePort.prototype, "messageerror");
|
|
|
|
|
|
|
|
webidl.configurePrototype(MessagePort);
|
2022-02-01 17:06:11 +00:00
|
|
|
const MessagePortPrototype = MessagePort.prototype;
|
2021-06-21 17:53:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {[number, number]}
|
|
|
|
*/
|
|
|
|
function opCreateEntangledMessagePort() {
|
2022-08-11 13:56:56 +00:00
|
|
|
return ops.op_message_port_create_entangled();
|
2021-06-21 17:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {globalThis.__bootstrap.messagePort.MessageData} messageData
|
|
|
|
* @returns {[any, object[]]}
|
|
|
|
*/
|
|
|
|
function deserializeJsMessageData(messageData) {
|
|
|
|
/** @type {object[]} */
|
|
|
|
const transferables = [];
|
2021-08-25 11:48:53 +00:00
|
|
|
const hostObjects = [];
|
|
|
|
const arrayBufferIdsInTransferables = [];
|
2022-06-07 09:25:10 +00:00
|
|
|
const transferredArrayBuffers = [];
|
2021-06-21 17:53:52 +00:00
|
|
|
|
2023-01-06 12:45:23 +00:00
|
|
|
for (let i = 0; i < messageData.transferables.length; ++i) {
|
|
|
|
const transferable = messageData.transferables[i];
|
2021-06-21 17:53:52 +00:00
|
|
|
switch (transferable.kind) {
|
|
|
|
case "messagePort": {
|
|
|
|
const port = createMessagePort(transferable.data);
|
2021-08-13 08:13:46 +00:00
|
|
|
ArrayPrototypePush(transferables, port);
|
2021-08-25 11:48:53 +00:00
|
|
|
ArrayPrototypePush(hostObjects, port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "arrayBuffer": {
|
2022-06-07 09:25:10 +00:00
|
|
|
ArrayPrototypePush(transferredArrayBuffers, transferable.data);
|
2023-01-06 12:45:23 +00:00
|
|
|
const index = ArrayPrototypePush(transferables, null);
|
|
|
|
ArrayPrototypePush(arrayBufferIdsInTransferables, index);
|
2021-06-21 17:53:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new TypeError("Unreachable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 09:17:05 +00:00
|
|
|
const data = core.deserialize(messageData.data, {
|
2021-08-25 11:48:53 +00:00
|
|
|
hostObjects,
|
2022-06-07 09:25:10 +00:00
|
|
|
transferredArrayBuffers,
|
2021-06-26 09:17:05 +00:00
|
|
|
});
|
2021-06-21 17:53:52 +00:00
|
|
|
|
2022-12-22 01:54:38 +00:00
|
|
|
for (let i = 0; i < arrayBufferIdsInTransferables.length; ++i) {
|
2021-08-25 11:48:53 +00:00
|
|
|
const id = arrayBufferIdsInTransferables[i];
|
2022-06-07 09:25:10 +00:00
|
|
|
transferables[id] = transferredArrayBuffers[i];
|
2021-08-25 11:48:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 17:53:52 +00:00
|
|
|
return [data, transferables];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any} data
|
2021-08-25 11:48:53 +00:00
|
|
|
* @param {object[]} transferables
|
2021-06-21 17:53:52 +00:00
|
|
|
* @returns {globalThis.__bootstrap.messagePort.MessageData}
|
|
|
|
*/
|
2021-08-25 11:48:53 +00:00
|
|
|
function serializeJsMessageData(data, transferables) {
|
2022-10-25 12:22:37 +00:00
|
|
|
const transferredArrayBuffers = [];
|
|
|
|
for (let i = 0, j = 0; i < transferables.length; i++) {
|
|
|
|
const ab = transferables[i];
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, ab)) {
|
|
|
|
if (ab.byteLength === 0 && core.ops.op_arraybuffer_was_detached(ab)) {
|
|
|
|
throw new DOMException(
|
|
|
|
`ArrayBuffer at index ${j} is already detached`,
|
|
|
|
"DataCloneError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
transferredArrayBuffers.push(ab);
|
2021-08-25 11:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:14:38 +00:00
|
|
|
const serializedData = core.serialize(data, {
|
|
|
|
hostObjects: ArrayPrototypeFilter(
|
|
|
|
transferables,
|
|
|
|
(a) => ObjectPrototypeIsPrototypeOf(MessagePortPrototype, a),
|
|
|
|
),
|
2022-06-07 09:25:10 +00:00
|
|
|
transferredArrayBuffers,
|
2022-05-26 15:14:38 +00:00
|
|
|
}, (err) => {
|
|
|
|
throw new DOMException(err, "DataCloneError");
|
|
|
|
});
|
2021-06-21 17:53:52 +00:00
|
|
|
|
|
|
|
/** @type {globalThis.__bootstrap.messagePort.Transferable[]} */
|
|
|
|
const serializedTransferables = [];
|
|
|
|
|
2021-08-25 11:48:53 +00:00
|
|
|
let arrayBufferI = 0;
|
2023-01-06 12:45:23 +00:00
|
|
|
for (let i = 0; i < transferables.length; ++i) {
|
|
|
|
const transferable = transferables[i];
|
2022-02-01 17:06:11 +00:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(MessagePortPrototype, transferable)) {
|
|
|
|
webidl.assertBranded(transferable, MessagePortPrototype);
|
2021-06-21 17:53:52 +00:00
|
|
|
const id = transferable[_id];
|
|
|
|
if (id === null) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Can not transfer disentangled message port",
|
|
|
|
"DataCloneError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
transferable[_id] = null;
|
2021-08-13 08:13:46 +00:00
|
|
|
ArrayPrototypePush(serializedTransferables, {
|
|
|
|
kind: "messagePort",
|
|
|
|
data: id,
|
|
|
|
});
|
2022-02-01 17:06:11 +00:00
|
|
|
} else if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, transferable)
|
|
|
|
) {
|
2021-08-25 11:48:53 +00:00
|
|
|
ArrayPrototypePush(serializedTransferables, {
|
|
|
|
kind: "arrayBuffer",
|
2022-06-07 09:25:10 +00:00
|
|
|
data: transferredArrayBuffers[arrayBufferI],
|
2021-08-25 11:48:53 +00:00
|
|
|
});
|
|
|
|
arrayBufferI++;
|
2021-06-21 17:53:52 +00:00
|
|
|
} else {
|
|
|
|
throw new DOMException("Value not transferable", "DataCloneError");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: serializedData,
|
|
|
|
transferables: serializedTransferables,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-09 08:39:00 +00:00
|
|
|
webidl.converters.StructuredSerializeOptions = webidl
|
|
|
|
.createDictionaryConverter(
|
|
|
|
"StructuredSerializeOptions",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key: "transfer",
|
|
|
|
converter: webidl.converters["sequence<object>"],
|
|
|
|
get defaultValue() {
|
|
|
|
return [];
|
|
|
|
},
|
2021-06-21 17:53:52 +00:00
|
|
|
},
|
2021-08-09 08:39:00 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
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);
|
2023-01-16 16:17:18 +00:00
|
|
|
return deserializeJsMessageData(messageData)[0];
|
2021-08-09 08:39:00 +00:00
|
|
|
}
|
2021-06-21 17:53:52 +00:00
|
|
|
|
|
|
|
window.__bootstrap.messagePort = {
|
|
|
|
MessageChannel,
|
|
|
|
MessagePort,
|
2022-02-01 17:06:11 +00:00
|
|
|
MessagePortPrototype,
|
2021-06-21 17:53:52 +00:00
|
|
|
deserializeJsMessageData,
|
|
|
|
serializeJsMessageData,
|
2021-08-09 08:39:00 +00:00
|
|
|
structuredClone,
|
2021-06-21 17:53:52 +00:00
|
|
|
};
|
|
|
|
})(globalThis);
|