2020-01-27 21:12:25 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
// This module is the entry point for "worker" isolate, ie. the one
|
|
|
|
// that is created using `new Worker()` JS API.
|
|
|
|
//
|
2020-02-11 04:04:59 -05:00
|
|
|
// It provides a single function that should be called by Rust:
|
2020-01-27 21:12:25 -05:00
|
|
|
// - `bootstrapWorkerRuntime` - must be called once, when Isolate is created.
|
|
|
|
// It sets up runtime by providing globals for `DedicatedWorkerScope`.
|
|
|
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
import {
|
|
|
|
readOnly,
|
|
|
|
writable,
|
|
|
|
nonEnumerable,
|
|
|
|
windowOrWorkerGlobalScopeMethods,
|
|
|
|
windowOrWorkerGlobalScopeProperties,
|
2020-03-28 13:03:49 -04:00
|
|
|
eventTargetProperties,
|
2020-01-27 21:12:25 -05:00
|
|
|
} from "./globals.ts";
|
2020-03-09 10:18:02 -04:00
|
|
|
import * as webWorkerOps from "./ops/web_worker.ts";
|
2020-03-11 16:57:24 -04:00
|
|
|
import { LocationImpl } from "./web/location.ts";
|
|
|
|
import { log, assert, immutableDefine } from "./util.ts";
|
2020-03-05 07:05:41 -05:00
|
|
|
import { TextEncoder } from "./web/text_encoding.ts";
|
2020-01-27 21:12:25 -05:00
|
|
|
import * as runtime from "./runtime.ts";
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
|
|
|
// TODO(bartlomieju): remove these funtions
|
|
|
|
// Stuff for workers
|
|
|
|
export const onmessage: (e: { data: any }) => void = (): void => {};
|
|
|
|
export const onerror: (e: { data: any }) => void = (): void => {};
|
|
|
|
|
|
|
|
export function postMessage(data: any): void {
|
|
|
|
const dataJson = JSON.stringify(data);
|
|
|
|
const dataIntArray = encoder.encode(dataJson);
|
2020-03-09 10:18:02 -04:00
|
|
|
webWorkerOps.postMessage(dataIntArray);
|
2020-01-27 21:12:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let isClosing = false;
|
|
|
|
let hasBootstrapped = false;
|
|
|
|
|
2020-01-29 12:54:23 -05:00
|
|
|
export function close(): void {
|
2020-02-11 04:04:59 -05:00
|
|
|
if (isClosing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:12:25 -05:00
|
|
|
isClosing = true;
|
2020-03-09 10:18:02 -04:00
|
|
|
webWorkerOps.close();
|
2020-01-27 21:12:25 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
export async function workerMessageRecvCallback(data: string): Promise<void> {
|
|
|
|
let result: void | Promise<void>;
|
|
|
|
const event = { data };
|
2020-01-27 21:12:25 -05:00
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
try {
|
|
|
|
//
|
|
|
|
if (globalThis["onmessage"]) {
|
2020-01-27 21:12:25 -05:00
|
|
|
result = globalThis.onmessage!(event);
|
|
|
|
if (result && "then" in result) {
|
|
|
|
await result;
|
|
|
|
}
|
2020-02-11 04:04:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: run the rest of liteners
|
|
|
|
} catch (e) {
|
|
|
|
if (globalThis["onerror"]) {
|
|
|
|
const result = globalThis.onerror(
|
|
|
|
e.message,
|
|
|
|
e.fileName,
|
|
|
|
e.lineNumber,
|
|
|
|
e.columnNumber,
|
|
|
|
e
|
|
|
|
);
|
|
|
|
if (result === true) {
|
|
|
|
return;
|
2020-01-27 21:12:25 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-11 04:04:59 -05:00
|
|
|
throw e;
|
2020-01-27 21:12:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const workerRuntimeGlobalProperties = {
|
|
|
|
self: readOnly(globalThis),
|
|
|
|
onmessage: writable(onmessage),
|
|
|
|
onerror: writable(onerror),
|
2020-02-11 04:04:59 -05:00
|
|
|
// TODO: should be readonly?
|
2020-01-29 12:54:23 -05:00
|
|
|
close: nonEnumerable(close),
|
2020-02-11 04:04:59 -05:00
|
|
|
postMessage: writable(postMessage),
|
2020-03-28 13:03:49 -04:00
|
|
|
workerMessageRecvCallback: nonEnumerable(workerMessageRecvCallback),
|
2020-01-27 21:12:25 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export function bootstrapWorkerRuntime(name: string): void {
|
|
|
|
if (hasBootstrapped) {
|
|
|
|
throw new Error("Worker runtime already bootstrapped");
|
|
|
|
}
|
|
|
|
log("bootstrapWorkerRuntime");
|
|
|
|
hasBootstrapped = true;
|
|
|
|
Object.defineProperties(globalThis, windowOrWorkerGlobalScopeMethods);
|
|
|
|
Object.defineProperties(globalThis, windowOrWorkerGlobalScopeProperties);
|
|
|
|
Object.defineProperties(globalThis, workerRuntimeGlobalProperties);
|
|
|
|
Object.defineProperties(globalThis, eventTargetProperties);
|
2020-01-29 12:54:23 -05:00
|
|
|
Object.defineProperties(globalThis, { name: readOnly(name) });
|
2020-03-11 16:57:24 -04:00
|
|
|
const s = runtime.start(name);
|
|
|
|
|
|
|
|
const location = new LocationImpl(s.location);
|
|
|
|
immutableDefine(globalThis, "location", location);
|
|
|
|
Object.freeze(globalThis.location);
|
|
|
|
|
|
|
|
// globalThis.Deno is not available in worker scope
|
|
|
|
delete globalThis.Deno;
|
|
|
|
assert(globalThis.Deno === undefined);
|
2020-01-27 21:12:25 -05:00
|
|
|
}
|