2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2022-06-14 23:04:30 -04:00
|
|
|
"use strict";
|
|
|
|
|
2022-08-03 23:09:16 -04:00
|
|
|
// Removes the `__proto__` for security reasons.
|
|
|
|
// https://tc39.es/ecma262/#sec-get-object.prototype.__proto__
|
2020-07-19 13:49:44 -04:00
|
|
|
delete Object.prototype.__proto__;
|
|
|
|
|
2022-06-14 23:04:30 -04:00
|
|
|
// Remove Intl.v8BreakIterator because it is a non-standard API.
|
|
|
|
delete Intl.v8BreakIterator;
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
((window) => {
|
|
|
|
const core = Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2021-07-03 18:17:52 -04:00
|
|
|
const {
|
2022-07-20 14:28:19 -04:00
|
|
|
ArrayPrototypeIndexOf,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeShift,
|
|
|
|
ArrayPrototypeSplice,
|
2021-08-16 08:29:54 -04:00
|
|
|
ArrayPrototypeMap,
|
2022-05-06 13:37:18 -04:00
|
|
|
DateNow,
|
2021-07-03 18:17:52 -04:00
|
|
|
Error,
|
|
|
|
FunctionPrototypeCall,
|
|
|
|
FunctionPrototypeBind,
|
|
|
|
ObjectAssign,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectFreeze,
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2021-07-03 18:17:52 -04:00
|
|
|
ObjectSetPrototypeOf,
|
|
|
|
PromiseResolve,
|
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
|
|
|
SymbolIterator,
|
2021-07-04 11:26:38 -04:00
|
|
|
PromisePrototypeThen,
|
2022-07-20 14:28:19 -04:00
|
|
|
SafeWeakMap,
|
2021-08-19 19:14:20 -04:00
|
|
|
TypeError,
|
2022-07-20 14:28:19 -04:00
|
|
|
WeakMapPrototypeDelete,
|
|
|
|
WeakMapPrototypeGet,
|
|
|
|
WeakMapPrototypeSet,
|
2021-07-03 18:17:52 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2020-07-19 13:49:44 -04:00
|
|
|
const util = window.__bootstrap.util;
|
2022-10-24 10:14:17 -04:00
|
|
|
const event = window.__bootstrap.event;
|
2020-07-19 13:49:44 -04:00
|
|
|
const eventTarget = window.__bootstrap.eventTarget;
|
2020-10-20 00:05:42 -04:00
|
|
|
const globalInterfaces = window.__bootstrap.globalInterfaces;
|
2021-01-07 13:06:08 -05:00
|
|
|
const location = window.__bootstrap.location;
|
2020-07-19 13:49:44 -04:00
|
|
|
const build = window.__bootstrap.build;
|
|
|
|
const version = window.__bootstrap.version;
|
|
|
|
const os = window.__bootstrap.os;
|
|
|
|
const timers = window.__bootstrap.timers;
|
2021-06-05 17:10:07 -04:00
|
|
|
const base64 = window.__bootstrap.base64;
|
|
|
|
const encoding = window.__bootstrap.encoding;
|
2022-01-17 17:23:49 -05:00
|
|
|
const colors = window.__bootstrap.colors;
|
2020-07-19 13:49:44 -04:00
|
|
|
const Console = window.__bootstrap.console.Console;
|
2022-09-28 08:11:12 -04:00
|
|
|
const caches = window.__bootstrap.caches;
|
2022-06-06 14:26:57 -04:00
|
|
|
const inspectArgs = window.__bootstrap.console.inspectArgs;
|
|
|
|
const quoteString = window.__bootstrap.console.quoteString;
|
2022-01-24 12:03:06 -05:00
|
|
|
const compression = window.__bootstrap.compression;
|
2020-07-19 13:49:44 -04:00
|
|
|
const worker = window.__bootstrap.worker;
|
2021-03-12 15:23:59 -05:00
|
|
|
const internals = window.__bootstrap.internals;
|
2020-07-19 13:49:44 -04:00
|
|
|
const performance = window.__bootstrap.performance;
|
|
|
|
const crypto = window.__bootstrap.crypto;
|
|
|
|
const url = window.__bootstrap.url;
|
2021-09-08 05:14:29 -04:00
|
|
|
const urlPattern = window.__bootstrap.urlPattern;
|
2020-07-19 13:49:44 -04:00
|
|
|
const headers = window.__bootstrap.headers;
|
|
|
|
const streams = window.__bootstrap.streams;
|
2020-08-11 08:00:53 -04:00
|
|
|
const fileReader = window.__bootstrap.fileReader;
|
2021-03-01 05:31:13 -05:00
|
|
|
const webgpu = window.__bootstrap.webgpu;
|
2020-09-05 10:39:25 -04:00
|
|
|
const webSocket = window.__bootstrap.webSocket;
|
2021-05-10 06:02:47 -04:00
|
|
|
const webStorage = window.__bootstrap.webStorage;
|
2021-05-22 12:08:24 -04:00
|
|
|
const broadcastChannel = window.__bootstrap.broadcastChannel;
|
2021-02-04 09:05:36 -05:00
|
|
|
const file = window.__bootstrap.file;
|
2021-04-14 16:49:16 -04:00
|
|
|
const formData = window.__bootstrap.formData;
|
2020-07-19 13:49:44 -04:00
|
|
|
const fetch = window.__bootstrap.fetch;
|
2020-10-13 09:31:59 -04:00
|
|
|
const prompt = window.__bootstrap.prompt;
|
2021-06-21 13:53:52 -04:00
|
|
|
const messagePort = window.__bootstrap.messagePort;
|
2020-07-19 13:49:44 -04:00
|
|
|
const denoNs = window.__bootstrap.denoNs;
|
|
|
|
const denoNsUnstable = window.__bootstrap.denoNsUnstable;
|
2020-08-07 16:47:18 -04:00
|
|
|
const errors = window.__bootstrap.errors.errors;
|
2021-03-08 07:27:49 -05:00
|
|
|
const webidl = window.__bootstrap.webidl;
|
2021-07-03 15:32:28 -04:00
|
|
|
const domException = window.__bootstrap.domException;
|
2022-10-24 10:14:17 -04:00
|
|
|
const abortSignal = window.__bootstrap.abortSignal;
|
2022-08-21 14:16:42 -04:00
|
|
|
const { defineEventHandler, reportException } = window.__bootstrap.event;
|
2021-06-22 10:30:16 -04:00
|
|
|
const { deserializeJsMessageData, serializeJsMessageData } =
|
|
|
|
window.__bootstrap.messagePort;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
let windowIsClosing = false;
|
|
|
|
|
|
|
|
function windowClose() {
|
|
|
|
if (!windowIsClosing) {
|
|
|
|
windowIsClosing = true;
|
|
|
|
// Push a macrotask to exit after a promise resolve.
|
|
|
|
// This is not perfect, but should be fine for first pass.
|
2021-07-04 11:26:38 -04:00
|
|
|
PromisePrototypeThen(
|
|
|
|
PromiseResolve(),
|
|
|
|
() =>
|
|
|
|
FunctionPrototypeCall(timers.setTimeout, null, () => {
|
|
|
|
// This should be fine, since only Window/MainWorker has .close()
|
|
|
|
os.exit(0);
|
|
|
|
}, 0),
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function workerClose() {
|
|
|
|
if (isClosing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isClosing = true;
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_worker_close();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-06-22 10:30:16 -04:00
|
|
|
function postMessage(message, transferOrOptions = {}) {
|
|
|
|
const prefix =
|
|
|
|
"Failed to execute 'postMessage' on 'DedicatedWorkerGlobalScope'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
message = webidl.converters.any(message);
|
|
|
|
let options;
|
|
|
|
if (
|
|
|
|
webidl.type(transferOrOptions) === "Object" &&
|
|
|
|
transferOrOptions !== undefined &&
|
2021-07-03 18:17:52 -04:00
|
|
|
transferOrOptions[SymbolIterator] !== undefined
|
2021-06-22 10:30:16 -04:00
|
|
|
) {
|
|
|
|
const transfer = webidl.converters["sequence<object>"](
|
|
|
|
transferOrOptions,
|
|
|
|
{ prefix, context: "Argument 2" },
|
|
|
|
);
|
|
|
|
options = { transfer };
|
|
|
|
} else {
|
2021-08-09 04:39:00 -04:00
|
|
|
options = webidl.converters.StructuredSerializeOptions(
|
|
|
|
transferOrOptions,
|
|
|
|
{
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
},
|
|
|
|
);
|
2021-06-22 10:30:16 -04:00
|
|
|
}
|
|
|
|
const { transfer } = options;
|
|
|
|
const data = serializeJsMessageData(message, transfer);
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_worker_post_message(data);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let isClosing = false;
|
2021-06-09 08:21:02 -04:00
|
|
|
let globalDispatchEvent;
|
|
|
|
|
2021-05-11 15:09:09 -04:00
|
|
|
async function pollForMessages() {
|
2021-06-09 08:21:02 -04:00
|
|
|
if (!globalDispatchEvent) {
|
2021-07-03 18:17:52 -04:00
|
|
|
globalDispatchEvent = FunctionPrototypeBind(
|
|
|
|
globalThis.dispatchEvent,
|
|
|
|
globalThis,
|
|
|
|
);
|
2021-06-09 08:21:02 -04:00
|
|
|
}
|
2021-05-11 15:09:09 -04:00
|
|
|
while (!isClosing) {
|
2021-06-22 10:30:16 -04:00
|
|
|
const data = await core.opAsync("op_worker_recv_message");
|
|
|
|
if (data === null) break;
|
|
|
|
const v = deserializeJsMessageData(data);
|
|
|
|
const message = v[0];
|
2021-08-25 07:48:53 -04:00
|
|
|
const transferables = v[1];
|
2021-05-11 15:09:09 -04:00
|
|
|
|
2022-10-24 10:14:17 -04:00
|
|
|
const msgEvent = new event.MessageEvent("message", {
|
2021-05-11 15:09:09 -04:00
|
|
|
cancelable: false,
|
2021-06-22 10:30:16 -04:00
|
|
|
data: message,
|
2022-02-01 12:06:11 -05:00
|
|
|
ports: transferables.filter((t) =>
|
|
|
|
ObjectPrototypeIsPrototypeOf(messagePort.MessagePortPrototype, t)
|
|
|
|
),
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
|
|
|
|
2021-05-11 15:09:09 -04:00
|
|
|
try {
|
2021-06-09 08:21:02 -04:00
|
|
|
globalDispatchEvent(msgEvent);
|
2021-05-11 15:09:09 -04:00
|
|
|
} catch (e) {
|
2022-10-24 10:14:17 -04:00
|
|
|
const errorEvent = new event.ErrorEvent("error", {
|
2021-05-11 15:09:09 -04:00
|
|
|
cancelable: true,
|
|
|
|
message: e.message,
|
|
|
|
lineno: e.lineNumber ? e.lineNumber + 1 : undefined,
|
|
|
|
colno: e.columnNumber ? e.columnNumber + 1 : undefined,
|
|
|
|
filename: e.fileName,
|
2021-07-10 17:32:10 -04:00
|
|
|
error: e,
|
2021-05-11 15:09:09 -04:00
|
|
|
});
|
|
|
|
|
2021-06-09 08:21:02 -04:00
|
|
|
globalDispatchEvent(errorEvent);
|
2021-07-10 17:32:10 -04:00
|
|
|
if (!errorEvent.defaultPrevented) {
|
2021-10-01 05:30:55 -04:00
|
|
|
throw e;
|
2021-05-11 15:09:09 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 08:29:54 -04:00
|
|
|
let loadedMainWorkerScript = false;
|
|
|
|
|
|
|
|
function importScripts(...urls) {
|
2022-08-11 09:56:56 -04:00
|
|
|
if (ops.op_worker_get_type() === "module") {
|
2021-08-16 08:29:54 -04:00
|
|
|
throw new TypeError("Can't import scripts in a module worker.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const baseUrl = location.getLocationHref();
|
|
|
|
const parsedUrls = ArrayPrototypeMap(urls, (scriptUrl) => {
|
|
|
|
try {
|
|
|
|
return new url.URL(scriptUrl, baseUrl ?? undefined).href;
|
|
|
|
} catch {
|
|
|
|
throw new domException.DOMException(
|
|
|
|
"Failed to parse URL.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// A classic worker's main script has looser MIME type checks than any
|
|
|
|
// imported scripts, so we use `loadedMainWorkerScript` to distinguish them.
|
|
|
|
// TODO(andreubotella) Refactor worker creation so the main script isn't
|
|
|
|
// loaded with `importScripts()`.
|
2022-08-11 09:56:56 -04:00
|
|
|
const scripts = ops.op_worker_sync_fetch(
|
2021-08-16 08:29:54 -04:00
|
|
|
parsedUrls,
|
|
|
|
!loadedMainWorkerScript,
|
|
|
|
);
|
|
|
|
loadedMainWorkerScript = true;
|
|
|
|
|
|
|
|
for (const { url, script } of scripts) {
|
|
|
|
const err = core.evalContext(script, url)[1];
|
|
|
|
if (err !== null) {
|
|
|
|
throw err.thrown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
function opMainModule() {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_main_module();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-06-06 14:26:57 -04:00
|
|
|
function formatException(error) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
return null;
|
|
|
|
} else if (typeof error == "string") {
|
|
|
|
return `Uncaught ${
|
|
|
|
inspectArgs([quoteString(error)], {
|
|
|
|
colors: !colors.getNoColor(),
|
|
|
|
})
|
|
|
|
}`;
|
|
|
|
} else {
|
|
|
|
return `Uncaught ${
|
|
|
|
inspectArgs([error], { colors: !colors.getNoColor() })
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
function runtimeStart(runtimeOptions, source) {
|
|
|
|
core.setMacrotaskCallback(timers.handleTimerMacrotask);
|
2022-07-20 14:28:19 -04:00
|
|
|
core.setMacrotaskCallback(promiseRejectMacrotaskCallback);
|
2021-07-03 17:33:36 -04:00
|
|
|
core.setWasmStreamingCallback(fetch.handleWasmStreaming);
|
2022-08-21 14:16:42 -04:00
|
|
|
core.setReportExceptionCallback(reportException);
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_set_format_exception_callback(formatException);
|
2020-12-11 12:49:26 -05:00
|
|
|
version.setVersions(
|
|
|
|
runtimeOptions.denoVersion,
|
|
|
|
runtimeOptions.v8Version,
|
|
|
|
runtimeOptions.tsVersion,
|
|
|
|
);
|
|
|
|
build.setBuildInfo(runtimeOptions.target);
|
|
|
|
util.setLogDebug(runtimeOptions.debugFlag, source);
|
2022-09-28 06:28:38 -04:00
|
|
|
colors.setNoColor(runtimeOptions.noColor || !runtimeOptions.isTty);
|
2021-08-19 19:14:20 -04:00
|
|
|
// deno-lint-ignore prefer-primordials
|
2022-05-03 13:45:57 -04:00
|
|
|
Error.prepareStackTrace = core.prepareStackTrace;
|
2022-09-28 06:28:38 -04:00
|
|
|
registerErrors();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-08-07 16:47:18 -04:00
|
|
|
function registerErrors() {
|
|
|
|
core.registerErrorClass("NotFound", errors.NotFound);
|
|
|
|
core.registerErrorClass("PermissionDenied", errors.PermissionDenied);
|
|
|
|
core.registerErrorClass("ConnectionRefused", errors.ConnectionRefused);
|
|
|
|
core.registerErrorClass("ConnectionReset", errors.ConnectionReset);
|
|
|
|
core.registerErrorClass("ConnectionAborted", errors.ConnectionAborted);
|
|
|
|
core.registerErrorClass("NotConnected", errors.NotConnected);
|
|
|
|
core.registerErrorClass("AddrInUse", errors.AddrInUse);
|
|
|
|
core.registerErrorClass("AddrNotAvailable", errors.AddrNotAvailable);
|
|
|
|
core.registerErrorClass("BrokenPipe", errors.BrokenPipe);
|
|
|
|
core.registerErrorClass("AlreadyExists", errors.AlreadyExists);
|
|
|
|
core.registerErrorClass("InvalidData", errors.InvalidData);
|
|
|
|
core.registerErrorClass("TimedOut", errors.TimedOut);
|
|
|
|
core.registerErrorClass("Interrupted", errors.Interrupted);
|
|
|
|
core.registerErrorClass("WriteZero", errors.WriteZero);
|
|
|
|
core.registerErrorClass("UnexpectedEof", errors.UnexpectedEof);
|
|
|
|
core.registerErrorClass("BadResource", errors.BadResource);
|
|
|
|
core.registerErrorClass("Http", errors.Http);
|
|
|
|
core.registerErrorClass("Busy", errors.Busy);
|
2020-08-25 18:22:15 -04:00
|
|
|
core.registerErrorClass("NotSupported", errors.NotSupported);
|
2021-05-03 11:30:41 -04:00
|
|
|
core.registerErrorBuilder(
|
2021-03-01 05:31:13 -05:00
|
|
|
"DOMExceptionOperationError",
|
2021-04-21 20:50:50 -04:00
|
|
|
function DOMExceptionOperationError(msg) {
|
2021-07-03 15:32:28 -04:00
|
|
|
return new domException.DOMException(msg, "OperationError");
|
2021-04-21 20:50:50 -04:00
|
|
|
},
|
2021-03-01 05:31:13 -05:00
|
|
|
);
|
2021-05-10 06:02:47 -04:00
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionQuotaExceededError",
|
|
|
|
function DOMExceptionQuotaExceededError(msg) {
|
2021-07-03 15:32:28 -04:00
|
|
|
return new domException.DOMException(msg, "QuotaExceededError");
|
2021-05-10 06:02:47 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionNotSupportedError",
|
|
|
|
function DOMExceptionNotSupportedError(msg) {
|
2021-07-03 15:32:28 -04:00
|
|
|
return new domException.DOMException(msg, "NotSupported");
|
2021-05-10 06:02:47 -04:00
|
|
|
},
|
|
|
|
);
|
2021-08-09 18:28:17 -04:00
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionNetworkError",
|
|
|
|
function DOMExceptionNetworkError(msg) {
|
|
|
|
return new domException.DOMException(msg, "NetworkError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionAbortError",
|
|
|
|
function DOMExceptionAbortError(msg) {
|
|
|
|
return new domException.DOMException(msg, "AbortError");
|
|
|
|
},
|
|
|
|
);
|
2021-06-05 17:10:07 -04:00
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionInvalidCharacterError",
|
|
|
|
function DOMExceptionInvalidCharacterError(msg) {
|
2021-07-03 15:32:28 -04:00
|
|
|
return new domException.DOMException(msg, "InvalidCharacterError");
|
2021-06-05 17:10:07 -04:00
|
|
|
},
|
|
|
|
);
|
2021-09-16 03:58:29 -04:00
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionDataError",
|
|
|
|
function DOMExceptionDataError(msg) {
|
|
|
|
return new domException.DOMException(msg, "DataError");
|
|
|
|
},
|
|
|
|
);
|
2020-08-07 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
2021-03-08 07:27:49 -05:00
|
|
|
class Navigator {
|
|
|
|
constructor() {
|
|
|
|
webidl.illegalConstructor();
|
|
|
|
}
|
|
|
|
|
2021-07-03 18:17:52 -04:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect) {
|
2021-03-08 07:27:49 -05:00
|
|
|
return `${this.constructor.name} ${inspect({})}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const navigator = webidl.createBranded(Navigator);
|
|
|
|
|
2022-10-18 09:33:35 -04:00
|
|
|
let numCpus, userAgent, language;
|
2021-07-29 15:45:11 -04:00
|
|
|
|
2021-07-03 18:17:52 -04:00
|
|
|
ObjectDefineProperties(Navigator.prototype, {
|
2021-03-08 07:27:49 -05:00
|
|
|
gpu: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, NavigatorPrototype);
|
2021-03-08 07:27:49 -05:00
|
|
|
return webgpu.gpu;
|
|
|
|
},
|
|
|
|
},
|
2021-07-29 15:45:11 -04:00
|
|
|
hardwareConcurrency: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, NavigatorPrototype);
|
2021-07-29 15:45:11 -04:00
|
|
|
return numCpus;
|
|
|
|
},
|
|
|
|
},
|
2022-05-14 06:00:02 -04:00
|
|
|
userAgent: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
webidl.assertBranded(this, NavigatorPrototype);
|
|
|
|
return userAgent;
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 09:33:35 -04:00
|
|
|
language: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
webidl.assertBranded(this, NavigatorPrototype);
|
|
|
|
return language;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
languages: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
webidl.assertBranded(this, NavigatorPrototype);
|
|
|
|
return [language];
|
|
|
|
},
|
|
|
|
},
|
2021-03-08 07:27:49 -05:00
|
|
|
});
|
2022-02-01 12:06:11 -05:00
|
|
|
const NavigatorPrototype = Navigator.prototype;
|
2021-03-08 07:27:49 -05:00
|
|
|
|
|
|
|
class WorkerNavigator {
|
|
|
|
constructor() {
|
|
|
|
webidl.illegalConstructor();
|
|
|
|
}
|
|
|
|
|
2021-07-03 18:17:52 -04:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect) {
|
2021-03-08 07:27:49 -05:00
|
|
|
return `${this.constructor.name} ${inspect({})}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const workerNavigator = webidl.createBranded(WorkerNavigator);
|
|
|
|
|
2021-07-03 18:17:52 -04:00
|
|
|
ObjectDefineProperties(WorkerNavigator.prototype, {
|
2021-03-08 07:27:49 -05:00
|
|
|
gpu: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WorkerNavigatorPrototype);
|
2021-03-08 07:27:49 -05:00
|
|
|
return webgpu.gpu;
|
|
|
|
},
|
|
|
|
},
|
2021-07-29 15:45:11 -04:00
|
|
|
hardwareConcurrency: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WorkerNavigatorPrototype);
|
2021-07-29 15:45:11 -04:00
|
|
|
return numCpus;
|
|
|
|
},
|
2022-10-18 09:33:35 -04:00
|
|
|
language: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
webidl.assertBranded(this, WorkerNavigatorPrototype);
|
|
|
|
return language;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
languages: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
webidl.assertBranded(this, WorkerNavigatorPrototype);
|
|
|
|
return [language];
|
|
|
|
},
|
|
|
|
},
|
2021-07-29 15:45:11 -04:00
|
|
|
},
|
2021-03-08 07:27:49 -05:00
|
|
|
});
|
2022-02-01 12:06:11 -05:00
|
|
|
const WorkerNavigatorPrototype = WorkerNavigator.prototype;
|
2021-03-08 07:27:49 -05:00
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope
|
2020-09-16 12:41:01 -04:00
|
|
|
const windowOrWorkerGlobalScope = {
|
2022-10-24 10:14:17 -04:00
|
|
|
AbortController: util.nonEnumerable(abortSignal.AbortController),
|
|
|
|
AbortSignal: util.nonEnumerable(abortSignal.AbortSignal),
|
2021-02-04 09:05:36 -05:00
|
|
|
Blob: util.nonEnumerable(file.Blob),
|
2020-07-19 13:49:44 -04:00
|
|
|
ByteLengthQueuingStrategy: util.nonEnumerable(
|
2020-09-18 09:20:55 -04:00
|
|
|
streams.ByteLengthQueuingStrategy,
|
2020-07-19 13:49:44 -04:00
|
|
|
),
|
2022-10-24 10:14:17 -04:00
|
|
|
CloseEvent: util.nonEnumerable(event.CloseEvent),
|
2022-01-24 12:03:06 -05:00
|
|
|
CompressionStream: util.nonEnumerable(compression.CompressionStream),
|
2020-07-19 13:49:44 -04:00
|
|
|
CountQueuingStrategy: util.nonEnumerable(
|
2020-09-18 09:20:55 -04:00
|
|
|
streams.CountQueuingStrategy,
|
2020-07-19 13:49:44 -04:00
|
|
|
),
|
2021-07-06 08:16:04 -04:00
|
|
|
CryptoKey: util.nonEnumerable(crypto.CryptoKey),
|
2022-10-24 10:14:17 -04:00
|
|
|
CustomEvent: util.nonEnumerable(event.CustomEvent),
|
2022-01-24 12:03:06 -05:00
|
|
|
DecompressionStream: util.nonEnumerable(compression.DecompressionStream),
|
2021-07-03 15:32:28 -04:00
|
|
|
DOMException: util.nonEnumerable(domException.DOMException),
|
2022-10-24 10:14:17 -04:00
|
|
|
ErrorEvent: util.nonEnumerable(event.ErrorEvent),
|
|
|
|
Event: util.nonEnumerable(event.Event),
|
|
|
|
EventTarget: util.nonEnumerable(eventTarget.EventTarget),
|
2021-02-04 09:05:36 -05:00
|
|
|
File: util.nonEnumerable(file.File),
|
2020-09-16 12:41:01 -04:00
|
|
|
FileReader: util.nonEnumerable(fileReader.FileReader),
|
2021-04-14 16:49:16 -04:00
|
|
|
FormData: util.nonEnumerable(formData.FormData),
|
2020-09-16 12:41:01 -04:00
|
|
|
Headers: util.nonEnumerable(headers.Headers),
|
2022-10-24 10:14:17 -04:00
|
|
|
MessageEvent: util.nonEnumerable(event.MessageEvent),
|
2020-07-19 13:49:44 -04:00
|
|
|
Performance: util.nonEnumerable(performance.Performance),
|
|
|
|
PerformanceEntry: util.nonEnumerable(performance.PerformanceEntry),
|
|
|
|
PerformanceMark: util.nonEnumerable(performance.PerformanceMark),
|
|
|
|
PerformanceMeasure: util.nonEnumerable(performance.PerformanceMeasure),
|
2022-10-24 10:14:17 -04:00
|
|
|
PromiseRejectionEvent: util.nonEnumerable(event.PromiseRejectionEvent),
|
|
|
|
ProgressEvent: util.nonEnumerable(event.ProgressEvent),
|
2020-09-16 12:41:01 -04:00
|
|
|
ReadableStream: util.nonEnumerable(streams.ReadableStream),
|
2021-01-14 16:57:19 -05:00
|
|
|
ReadableStreamDefaultReader: util.nonEnumerable(
|
|
|
|
streams.ReadableStreamDefaultReader,
|
|
|
|
),
|
2020-09-18 09:20:55 -04:00
|
|
|
Request: util.nonEnumerable(fetch.Request),
|
2020-09-16 12:41:01 -04:00
|
|
|
Response: util.nonEnumerable(fetch.Response),
|
2021-06-05 17:10:07 -04:00
|
|
|
TextDecoder: util.nonEnumerable(encoding.TextDecoder),
|
|
|
|
TextEncoder: util.nonEnumerable(encoding.TextEncoder),
|
2021-06-05 21:23:16 -04:00
|
|
|
TextDecoderStream: util.nonEnumerable(encoding.TextDecoderStream),
|
|
|
|
TextEncoderStream: util.nonEnumerable(encoding.TextEncoderStream),
|
2020-07-19 13:49:44 -04:00
|
|
|
TransformStream: util.nonEnumerable(streams.TransformStream),
|
|
|
|
URL: util.nonEnumerable(url.URL),
|
2021-09-29 04:42:32 -04:00
|
|
|
URLPattern: util.nonEnumerable(urlPattern.URLPattern),
|
2020-07-19 13:49:44 -04:00
|
|
|
URLSearchParams: util.nonEnumerable(url.URLSearchParams),
|
2020-09-16 12:41:01 -04:00
|
|
|
WebSocket: util.nonEnumerable(webSocket.WebSocket),
|
2021-06-21 13:53:52 -04:00
|
|
|
MessageChannel: util.nonEnumerable(messagePort.MessageChannel),
|
|
|
|
MessagePort: util.nonEnumerable(messagePort.MessagePort),
|
2020-07-19 13:49:44 -04:00
|
|
|
Worker: util.nonEnumerable(worker.Worker),
|
|
|
|
WritableStream: util.nonEnumerable(streams.WritableStream),
|
2021-01-14 16:57:19 -05:00
|
|
|
WritableStreamDefaultWriter: util.nonEnumerable(
|
|
|
|
streams.WritableStreamDefaultWriter,
|
|
|
|
),
|
2021-06-15 07:46:02 -04:00
|
|
|
WritableStreamDefaultController: util.nonEnumerable(
|
|
|
|
streams.WritableStreamDefaultController,
|
|
|
|
),
|
2021-06-05 13:32:05 -04:00
|
|
|
ReadableByteStreamController: util.nonEnumerable(
|
|
|
|
streams.ReadableByteStreamController,
|
|
|
|
),
|
2021-11-03 05:47:40 -04:00
|
|
|
ReadableStreamBYOBReader: util.nonEnumerable(
|
|
|
|
streams.ReadableStreamBYOBReader,
|
|
|
|
),
|
|
|
|
ReadableStreamBYOBRequest: util.nonEnumerable(
|
|
|
|
streams.ReadableStreamBYOBRequest,
|
|
|
|
),
|
2021-06-15 07:46:02 -04:00
|
|
|
ReadableStreamDefaultController: util.nonEnumerable(
|
|
|
|
streams.ReadableStreamDefaultController,
|
|
|
|
),
|
2021-06-05 13:32:05 -04:00
|
|
|
TransformStreamDefaultController: util.nonEnumerable(
|
|
|
|
streams.TransformStreamDefaultController,
|
|
|
|
),
|
2021-06-05 17:10:07 -04:00
|
|
|
atob: util.writable(base64.atob),
|
|
|
|
btoa: util.writable(base64.btoa),
|
2020-09-16 12:41:01 -04:00
|
|
|
clearInterval: util.writable(timers.clearInterval),
|
|
|
|
clearTimeout: util.writable(timers.clearTimeout),
|
2022-09-28 08:11:12 -04:00
|
|
|
caches: {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
get: caches.cacheStorage,
|
|
|
|
},
|
|
|
|
CacheStorage: util.nonEnumerable(caches.CacheStorage),
|
|
|
|
Cache: util.nonEnumerable(caches.Cache),
|
2021-09-11 18:20:30 -04:00
|
|
|
console: util.nonEnumerable(
|
2021-03-18 14:25:25 -04:00
|
|
|
new Console((msg, level) => core.print(msg, level > 1)),
|
|
|
|
),
|
2021-06-05 16:56:59 -04:00
|
|
|
crypto: util.readOnly(crypto.crypto),
|
|
|
|
Crypto: util.nonEnumerable(crypto.Crypto),
|
2021-06-06 06:57:10 -04:00
|
|
|
SubtleCrypto: util.nonEnumerable(crypto.SubtleCrypto),
|
2020-09-16 12:41:01 -04:00
|
|
|
fetch: util.writable(fetch.fetch),
|
2020-09-19 17:30:59 -04:00
|
|
|
performance: util.writable(performance.performance),
|
2022-10-24 10:14:17 -04:00
|
|
|
reportError: util.writable(event.reportError),
|
2020-09-16 12:41:01 -04:00
|
|
|
setInterval: util.writable(timers.setInterval),
|
|
|
|
setTimeout: util.writable(timers.setTimeout),
|
2021-08-09 04:39:00 -04:00
|
|
|
structuredClone: util.writable(messagePort.structuredClone),
|
2022-10-24 10:14:17 -04:00
|
|
|
// Branding as a WebIDL object
|
|
|
|
[webidl.brand]: util.nonEnumerable(webidl.brand),
|
2021-08-09 18:28:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const unstableWindowOrWorkerGlobalScope = {
|
|
|
|
BroadcastChannel: util.nonEnumerable(broadcastChannel.BroadcastChannel),
|
2021-09-08 05:14:29 -04:00
|
|
|
WebSocketStream: util.nonEnumerable(webSocket.WebSocketStream),
|
2021-03-01 05:31:13 -05:00
|
|
|
|
|
|
|
GPU: util.nonEnumerable(webgpu.GPU),
|
|
|
|
GPUAdapter: util.nonEnumerable(webgpu.GPUAdapter),
|
2021-07-08 05:07:49 -04:00
|
|
|
GPUSupportedLimits: util.nonEnumerable(webgpu.GPUSupportedLimits),
|
2021-05-06 10:48:45 -04:00
|
|
|
GPUSupportedFeatures: util.nonEnumerable(webgpu.GPUSupportedFeatures),
|
2021-03-01 05:31:13 -05:00
|
|
|
GPUDevice: util.nonEnumerable(webgpu.GPUDevice),
|
|
|
|
GPUQueue: util.nonEnumerable(webgpu.GPUQueue),
|
|
|
|
GPUBuffer: util.nonEnumerable(webgpu.GPUBuffer),
|
|
|
|
GPUBufferUsage: util.nonEnumerable(webgpu.GPUBufferUsage),
|
|
|
|
GPUMapMode: util.nonEnumerable(webgpu.GPUMapMode),
|
|
|
|
GPUTexture: util.nonEnumerable(webgpu.GPUTexture),
|
|
|
|
GPUTextureUsage: util.nonEnumerable(webgpu.GPUTextureUsage),
|
|
|
|
GPUTextureView: util.nonEnumerable(webgpu.GPUTextureView),
|
|
|
|
GPUSampler: util.nonEnumerable(webgpu.GPUSampler),
|
|
|
|
GPUBindGroupLayout: util.nonEnumerable(webgpu.GPUBindGroupLayout),
|
|
|
|
GPUPipelineLayout: util.nonEnumerable(webgpu.GPUPipelineLayout),
|
|
|
|
GPUBindGroup: util.nonEnumerable(webgpu.GPUBindGroup),
|
|
|
|
GPUShaderModule: util.nonEnumerable(webgpu.GPUShaderModule),
|
|
|
|
GPUShaderStage: util.nonEnumerable(webgpu.GPUShaderStage),
|
|
|
|
GPUComputePipeline: util.nonEnumerable(webgpu.GPUComputePipeline),
|
|
|
|
GPURenderPipeline: util.nonEnumerable(webgpu.GPURenderPipeline),
|
|
|
|
GPUColorWrite: util.nonEnumerable(webgpu.GPUColorWrite),
|
|
|
|
GPUCommandEncoder: util.nonEnumerable(webgpu.GPUCommandEncoder),
|
|
|
|
GPURenderPassEncoder: util.nonEnumerable(webgpu.GPURenderPassEncoder),
|
|
|
|
GPUComputePassEncoder: util.nonEnumerable(webgpu.GPUComputePassEncoder),
|
|
|
|
GPUCommandBuffer: util.nonEnumerable(webgpu.GPUCommandBuffer),
|
|
|
|
GPURenderBundleEncoder: util.nonEnumerable(webgpu.GPURenderBundleEncoder),
|
|
|
|
GPURenderBundle: util.nonEnumerable(webgpu.GPURenderBundle),
|
|
|
|
GPUQuerySet: util.nonEnumerable(webgpu.GPUQuerySet),
|
|
|
|
GPUOutOfMemoryError: util.nonEnumerable(webgpu.GPUOutOfMemoryError),
|
|
|
|
GPUValidationError: util.nonEnumerable(webgpu.GPUValidationError),
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const mainRuntimeGlobalProperties = {
|
2021-01-17 10:28:54 -05:00
|
|
|
Location: location.locationConstructorDescriptor,
|
|
|
|
location: location.locationDescriptor,
|
2020-10-20 00:05:42 -04:00
|
|
|
Window: globalInterfaces.windowConstructorDescriptor,
|
2020-07-19 13:49:44 -04:00
|
|
|
window: util.readOnly(globalThis),
|
2021-10-11 12:50:18 -04:00
|
|
|
self: util.writable(globalThis),
|
2021-03-08 07:27:49 -05:00
|
|
|
Navigator: util.nonEnumerable(Navigator),
|
|
|
|
navigator: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: () => navigator,
|
|
|
|
},
|
2020-07-19 13:49:44 -04:00
|
|
|
close: util.writable(windowClose),
|
|
|
|
closed: util.getterOnly(() => windowIsClosing),
|
2020-10-13 09:31:59 -04:00
|
|
|
alert: util.writable(prompt.alert),
|
|
|
|
confirm: util.writable(prompt.confirm),
|
|
|
|
prompt: util.writable(prompt.prompt),
|
2021-05-10 06:02:47 -04:00
|
|
|
localStorage: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: webStorage.localStorage,
|
|
|
|
},
|
|
|
|
sessionStorage: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: webStorage.sessionStorage,
|
|
|
|
},
|
|
|
|
Storage: util.nonEnumerable(webStorage.Storage),
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const workerRuntimeGlobalProperties = {
|
2021-01-17 10:28:54 -05:00
|
|
|
WorkerLocation: location.workerLocationConstructorDescriptor,
|
|
|
|
location: location.workerLocationDescriptor,
|
2020-10-20 00:05:42 -04:00
|
|
|
WorkerGlobalScope: globalInterfaces.workerGlobalScopeConstructorDescriptor,
|
|
|
|
DedicatedWorkerGlobalScope:
|
|
|
|
globalInterfaces.dedicatedWorkerGlobalScopeConstructorDescriptor,
|
2021-03-08 07:27:49 -05:00
|
|
|
WorkerNavigator: util.nonEnumerable(WorkerNavigator),
|
|
|
|
navigator: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: () => workerNavigator,
|
|
|
|
},
|
2020-07-19 13:49:44 -04:00
|
|
|
self: util.readOnly(globalThis),
|
2021-01-16 18:32:59 -05:00
|
|
|
// TODO(bartlomieju): should be readonly?
|
2020-07-19 13:49:44 -04:00
|
|
|
close: util.nonEnumerable(workerClose),
|
|
|
|
postMessage: util.writable(postMessage),
|
|
|
|
};
|
|
|
|
|
2022-07-20 14:28:19 -04:00
|
|
|
const pendingRejections = [];
|
|
|
|
const pendingRejectionsReasons = new SafeWeakMap();
|
|
|
|
|
|
|
|
function promiseRejectCallback(type, promise, reason) {
|
|
|
|
switch (type) {
|
|
|
|
case 0: {
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_store_pending_promise_exception(promise, reason);
|
2022-07-20 14:28:19 -04:00
|
|
|
ArrayPrototypePush(pendingRejections, promise);
|
|
|
|
WeakMapPrototypeSet(pendingRejectionsReasons, promise, reason);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: {
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_remove_pending_promise_exception(promise);
|
2022-07-20 14:28:19 -04:00
|
|
|
const index = ArrayPrototypeIndexOf(pendingRejections, promise);
|
|
|
|
if (index > -1) {
|
|
|
|
ArrayPrototypeSplice(pendingRejections, index, 1);
|
|
|
|
WeakMapPrototypeDelete(pendingRejectionsReasons, promise);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!globalThis.onunhandledrejection ||
|
|
|
|
eventTarget.listenerCount(globalThis, "unhandledrejection") > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function promiseRejectMacrotaskCallback() {
|
|
|
|
while (pendingRejections.length > 0) {
|
|
|
|
const promise = ArrayPrototypeShift(pendingRejections);
|
2022-08-11 09:56:56 -04:00
|
|
|
const hasPendingException = ops.op_has_pending_promise_exception(
|
2022-07-20 14:28:19 -04:00
|
|
|
promise,
|
|
|
|
);
|
|
|
|
const reason = WeakMapPrototypeGet(pendingRejectionsReasons, promise);
|
|
|
|
WeakMapPrototypeDelete(pendingRejectionsReasons, promise);
|
|
|
|
|
|
|
|
if (!hasPendingException) {
|
2022-07-22 18:40:42 -04:00
|
|
|
continue;
|
2022-07-20 14:28:19 -04:00
|
|
|
}
|
|
|
|
|
2022-10-24 10:14:17 -04:00
|
|
|
const rejectionEvent = new event.PromiseRejectionEvent(
|
|
|
|
"unhandledrejection",
|
|
|
|
{
|
|
|
|
cancelable: true,
|
|
|
|
promise,
|
|
|
|
reason,
|
|
|
|
},
|
|
|
|
);
|
2022-07-22 18:40:42 -04:00
|
|
|
|
|
|
|
const errorEventCb = (event) => {
|
|
|
|
if (event.error === reason) {
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_remove_pending_promise_exception(promise);
|
2022-07-22 18:40:42 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// Add a callback for "error" event - it will be dispatched
|
|
|
|
// if error is thrown during dispatch of "unhandledrejection"
|
|
|
|
// event.
|
|
|
|
globalThis.addEventListener("error", errorEventCb);
|
2022-10-24 10:14:17 -04:00
|
|
|
globalThis.dispatchEvent(rejectionEvent);
|
2022-07-22 18:40:42 -04:00
|
|
|
globalThis.removeEventListener("error", errorEventCb);
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2022-07-22 18:40:42 -04:00
|
|
|
// If event was not prevented (or "unhandledrejection" listeners didn't
|
|
|
|
// throw) we will let Rust side handle it.
|
2022-10-24 10:14:17 -04:00
|
|
|
if (rejectionEvent.defaultPrevented) {
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_remove_pending_promise_exception(promise);
|
2022-07-20 14:28:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
let hasBootstrapped = false;
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
function bootstrapMainRuntime(runtimeOptions) {
|
2020-07-19 13:49:44 -04:00
|
|
|
if (hasBootstrapped) {
|
|
|
|
throw new Error("Worker runtime already bootstrapped");
|
|
|
|
}
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2022-05-06 13:37:18 -04:00
|
|
|
performance.setTimeOrigin(DateNow());
|
2021-06-26 20:27:50 -04:00
|
|
|
const consoleFromV8 = window.console;
|
|
|
|
const wrapConsole = window.__bootstrap.console.wrapConsole;
|
|
|
|
|
2020-09-04 07:52:19 -04:00
|
|
|
// Remove bootstrapping data from the global scope
|
|
|
|
delete globalThis.__bootstrap;
|
|
|
|
delete globalThis.bootstrap;
|
2020-07-19 13:49:44 -04:00
|
|
|
util.log("bootstrapMainRuntime");
|
|
|
|
hasBootstrapped = true;
|
2022-08-10 14:57:30 -04:00
|
|
|
|
|
|
|
// If the `--location` flag isn't set, make `globalThis.location` `undefined` and
|
|
|
|
// writable, so that they can mock it themselves if they like. If the flag was
|
|
|
|
// set, define `globalThis.location`, using the provided value.
|
2022-09-28 06:28:38 -04:00
|
|
|
if (runtimeOptions.location == null) {
|
2022-08-10 14:57:30 -04:00
|
|
|
mainRuntimeGlobalProperties.location = {
|
|
|
|
writable: true,
|
|
|
|
};
|
|
|
|
} else {
|
2022-09-28 06:28:38 -04:00
|
|
|
location.setLocationHref(runtimeOptions.location);
|
2022-08-10 14:57:30 -04:00
|
|
|
}
|
|
|
|
|
2021-07-03 18:17:52 -04:00
|
|
|
ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope);
|
2021-08-09 18:28:17 -04:00
|
|
|
if (runtimeOptions.unstableFlag) {
|
|
|
|
ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope);
|
|
|
|
}
|
2021-07-03 18:17:52 -04:00
|
|
|
ObjectDefineProperties(globalThis, mainRuntimeGlobalProperties);
|
|
|
|
ObjectSetPrototypeOf(globalThis, Window.prototype);
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2022-09-28 06:28:38 -04:00
|
|
|
if (runtimeOptions.inspectFlag) {
|
2022-09-17 06:04:43 -04:00
|
|
|
const consoleFromDeno = globalThis.console;
|
|
|
|
wrapConsole(consoleFromDeno, consoleFromV8);
|
|
|
|
}
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
eventTarget.setEventTargetData(globalThis);
|
2020-11-26 16:27:55 -05:00
|
|
|
|
2022-04-13 05:50:57 -04:00
|
|
|
defineEventHandler(window, "error");
|
2021-10-08 03:53:31 -04:00
|
|
|
defineEventHandler(window, "load");
|
2022-06-28 10:49:30 -04:00
|
|
|
defineEventHandler(window, "beforeunload");
|
2021-10-08 03:53:31 -04:00
|
|
|
defineEventHandler(window, "unload");
|
2022-07-20 14:28:19 -04:00
|
|
|
defineEventHandler(window, "unhandledrejection");
|
|
|
|
|
|
|
|
core.setPromiseRejectCallback(promiseRejectCallback);
|
|
|
|
|
2021-07-03 18:17:52 -04:00
|
|
|
const isUnloadDispatched = SymbolFor("isUnloadDispatched");
|
2021-01-21 02:44:48 -05:00
|
|
|
// Stores the flag for checking whether unload is dispatched or not.
|
|
|
|
// This prevents the recursive dispatches of unload events.
|
|
|
|
// See https://github.com/denoland/deno/issues/9201.
|
|
|
|
window[isUnloadDispatched] = false;
|
|
|
|
window.addEventListener("unload", () => {
|
|
|
|
window[isUnloadDispatched] = true;
|
|
|
|
});
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
runtimeStart(runtimeOptions);
|
2021-01-07 13:06:08 -05:00
|
|
|
|
2022-09-28 06:28:38 -04:00
|
|
|
numCpus = runtimeOptions.cpuCount;
|
|
|
|
userAgent = runtimeOptions.userAgent;
|
2022-10-18 09:33:35 -04:00
|
|
|
language = runtimeOptions.locale;
|
2020-08-07 16:47:18 -04:00
|
|
|
|
2021-03-12 15:23:59 -05:00
|
|
|
const internalSymbol = Symbol("Deno.internal");
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
const finalDenoNs = {
|
|
|
|
core,
|
|
|
|
internal: internalSymbol,
|
2021-03-12 15:23:59 -05:00
|
|
|
[internalSymbol]: internals,
|
2020-09-17 12:09:50 -04:00
|
|
|
resources: core.resources,
|
|
|
|
close: core.close,
|
2020-07-19 13:49:44 -04:00
|
|
|
...denoNs,
|
|
|
|
};
|
2021-07-03 18:17:52 -04:00
|
|
|
ObjectDefineProperties(finalDenoNs, {
|
2022-09-28 06:28:38 -04:00
|
|
|
pid: util.readOnly(runtimeOptions.pid),
|
|
|
|
ppid: util.readOnly(runtimeOptions.ppid),
|
|
|
|
noColor: util.readOnly(runtimeOptions.noColor),
|
|
|
|
args: util.readOnly(ObjectFreeze(runtimeOptions.args)),
|
2020-08-10 16:41:51 -04:00
|
|
|
mainModule: util.getterOnly(opMainModule),
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
|
|
|
|
2022-09-28 06:28:38 -04:00
|
|
|
if (runtimeOptions.unstableFlag) {
|
2021-07-03 18:17:52 -04:00
|
|
|
ObjectAssign(finalDenoNs, denoNsUnstable);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-06-21 17:17:35 -04:00
|
|
|
// Setup `Deno` global - we're actually overriding already existing global
|
|
|
|
// `Deno` with `Deno` namespace from "./deno.ts".
|
2021-07-03 18:17:52 -04:00
|
|
|
ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs));
|
|
|
|
ObjectFreeze(globalThis.Deno.core);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2022-09-28 06:28:38 -04:00
|
|
|
util.log("args", runtimeOptions.args);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
function bootstrapWorkerRuntime(
|
|
|
|
runtimeOptions,
|
|
|
|
name,
|
|
|
|
internalName,
|
|
|
|
) {
|
2020-07-19 13:49:44 -04:00
|
|
|
if (hasBootstrapped) {
|
|
|
|
throw new Error("Worker runtime already bootstrapped");
|
|
|
|
}
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2022-05-06 13:37:18 -04:00
|
|
|
performance.setTimeOrigin(DateNow());
|
2021-06-26 20:27:50 -04:00
|
|
|
const consoleFromV8 = window.console;
|
|
|
|
const wrapConsole = window.__bootstrap.console.wrapConsole;
|
|
|
|
|
2020-09-04 07:52:19 -04:00
|
|
|
// Remove bootstrapping data from the global scope
|
|
|
|
delete globalThis.__bootstrap;
|
|
|
|
delete globalThis.bootstrap;
|
2020-07-19 13:49:44 -04:00
|
|
|
util.log("bootstrapWorkerRuntime");
|
|
|
|
hasBootstrapped = true;
|
2021-07-03 18:17:52 -04:00
|
|
|
ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope);
|
2021-08-09 18:28:17 -04:00
|
|
|
if (runtimeOptions.unstableFlag) {
|
|
|
|
ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope);
|
|
|
|
}
|
2021-07-03 18:17:52 -04:00
|
|
|
ObjectDefineProperties(globalThis, workerRuntimeGlobalProperties);
|
2021-10-11 12:50:18 -04:00
|
|
|
ObjectDefineProperties(globalThis, { name: util.writable(name) });
|
2021-08-16 08:29:54 -04:00
|
|
|
if (runtimeOptions.enableTestingFeaturesFlag) {
|
|
|
|
ObjectDefineProperty(
|
|
|
|
globalThis,
|
|
|
|
"importScripts",
|
|
|
|
util.writable(importScripts),
|
|
|
|
);
|
|
|
|
}
|
2021-07-26 07:52:59 -04:00
|
|
|
ObjectSetPrototypeOf(globalThis, DedicatedWorkerGlobalScope.prototype);
|
2021-06-26 20:27:50 -04:00
|
|
|
|
|
|
|
const consoleFromDeno = globalThis.console;
|
|
|
|
wrapConsole(consoleFromDeno, consoleFromV8);
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
eventTarget.setEventTargetData(globalThis);
|
2020-12-11 12:49:26 -05:00
|
|
|
|
2021-10-08 03:53:31 -04:00
|
|
|
defineEventHandler(self, "message");
|
|
|
|
defineEventHandler(self, "error", undefined, true);
|
2022-07-20 14:28:19 -04:00
|
|
|
defineEventHandler(self, "unhandledrejection");
|
|
|
|
|
|
|
|
core.setPromiseRejectCallback(promiseRejectCallback);
|
|
|
|
|
2022-06-13 17:53:04 -04:00
|
|
|
// `Deno.exit()` is an alias to `self.close()`. Setting and exit
|
|
|
|
// code using an op in worker context is a no-op.
|
|
|
|
os.setExitHandler((_exitCode) => {
|
|
|
|
workerClose();
|
|
|
|
});
|
2021-07-10 17:32:10 -04:00
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
runtimeStart(
|
|
|
|
runtimeOptions,
|
2020-07-19 13:49:44 -04:00
|
|
|
internalName ?? name,
|
|
|
|
);
|
2022-09-28 06:28:38 -04:00
|
|
|
|
|
|
|
location.setLocationHref(runtimeOptions.location);
|
|
|
|
numCpus = runtimeOptions.cpuCount;
|
2022-10-18 09:33:35 -04:00
|
|
|
language = runtimeOptions.locale;
|
2020-08-07 16:47:18 -04:00
|
|
|
|
2022-02-11 07:41:56 -05:00
|
|
|
globalThis.pollForMessages = pollForMessages;
|
2021-05-11 15:09:09 -04:00
|
|
|
|
2021-03-12 15:23:59 -05:00
|
|
|
const internalSymbol = Symbol("Deno.internal");
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
const finalDenoNs = {
|
|
|
|
core,
|
|
|
|
internal: internalSymbol,
|
2021-03-12 15:23:59 -05:00
|
|
|
[internalSymbol]: internals,
|
2020-09-17 12:09:50 -04:00
|
|
|
resources: core.resources,
|
|
|
|
close: core.close,
|
2020-07-19 13:49:44 -04:00
|
|
|
...denoNs,
|
|
|
|
};
|
2022-09-28 06:28:38 -04:00
|
|
|
if (runtimeOptions.unstableFlag) {
|
2022-05-17 16:27:17 -04:00
|
|
|
ObjectAssign(finalDenoNs, denoNsUnstable);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2022-05-17 16:27:17 -04:00
|
|
|
ObjectDefineProperties(finalDenoNs, {
|
2022-09-28 06:28:38 -04:00
|
|
|
pid: util.readOnly(runtimeOptions.pid),
|
|
|
|
noColor: util.readOnly(runtimeOptions.noColor),
|
|
|
|
args: util.readOnly(ObjectFreeze(runtimeOptions.args)),
|
2022-05-17 16:27:17 -04:00
|
|
|
});
|
|
|
|
// Setup `Deno` global - we're actually overriding already
|
|
|
|
// existing global `Deno` with `Deno` namespace from "./deno.ts".
|
|
|
|
ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs));
|
|
|
|
ObjectFreeze(globalThis.Deno.core);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-07-03 18:17:52 -04:00
|
|
|
ObjectDefineProperties(globalThis, {
|
2020-07-19 13:49:44 -04:00
|
|
|
bootstrap: {
|
|
|
|
value: {
|
|
|
|
mainRuntime: bootstrapMainRuntime,
|
|
|
|
workerRuntime: bootstrapWorkerRuntime,
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})(this);
|