2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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,
|
2022-12-19 21:37:50 -05:00
|
|
|
ErrorPrototype,
|
2021-07-03 18:17:52 -04:00
|
|
|
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;
|
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;
|
2022-01-17 17:23:49 -05:00
|
|
|
const colors = window.__bootstrap.colors;
|
2022-06-06 14:26:57 -04:00
|
|
|
const inspectArgs = window.__bootstrap.console.inspectArgs;
|
|
|
|
const quoteString = window.__bootstrap.console.quoteString;
|
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;
|
2022-10-25 16:50:55 -04:00
|
|
|
const net = window.__bootstrap.net;
|
2020-07-19 13:49:44 -04:00
|
|
|
const url = window.__bootstrap.url;
|
|
|
|
const fetch = window.__bootstrap.fetch;
|
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-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;
|
2022-11-21 08:36:26 -05:00
|
|
|
const {
|
|
|
|
windowOrWorkerGlobalScope,
|
|
|
|
unstableWindowOrWorkerGlobalScope,
|
|
|
|
workerRuntimeGlobalProperties,
|
|
|
|
mainRuntimeGlobalProperties,
|
|
|
|
setNumCpus,
|
|
|
|
setUserAgent,
|
|
|
|
setLanguage,
|
|
|
|
} = window.__bootstrap.globalScope;
|
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;
|
|
|
|
|
2023-01-06 07:45:23 -05:00
|
|
|
for (let i = 0; i < scripts.length; ++i) {
|
|
|
|
const { url, script } = scripts[i];
|
2021-08-16 08:29:54 -04:00
|
|
|
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) {
|
2022-12-19 21:37:50 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(ErrorPrototype, error)) {
|
2022-06-06 14:26:57 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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-10-28 07:20:17 -04:00
|
|
|
core.initializeAsyncOps();
|
2022-05-06 13:37:18 -04:00
|
|
|
performance.setTimeOrigin(DateNow());
|
2022-10-25 16:50:55 -04:00
|
|
|
net.setup(runtimeOptions.unstableFlag);
|
|
|
|
|
2023-01-06 16:37:42 -05:00
|
|
|
const consoleFromV8 = window.Deno.core.console;
|
2021-06-26 20:27:50 -04:00
|
|
|
const wrapConsole = window.__bootstrap.console.wrapConsole;
|
|
|
|
|
2020-09-04 07:52:19 -04:00
|
|
|
// Remove bootstrapping data from the global scope
|
2022-11-10 16:03:28 -05:00
|
|
|
const __bootstrap = globalThis.__bootstrap;
|
2020-09-04 07:52:19 -04:00
|
|
|
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);
|
2022-11-21 08:36:26 -05:00
|
|
|
ObjectDefineProperties(globalThis, {
|
|
|
|
close: util.writable(windowClose),
|
|
|
|
closed: util.getterOnly(() => windowIsClosing),
|
|
|
|
});
|
2021-07-03 18:17:52 -04:00
|
|
|
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-11-21 08:36:26 -05:00
|
|
|
setNumCpus(runtimeOptions.cpuCount);
|
|
|
|
setUserAgent(runtimeOptions.userAgent);
|
|
|
|
setLanguage(runtimeOptions.locale);
|
2020-08-07 16:47:18 -04:00
|
|
|
|
2021-03-12 15:23:59 -05:00
|
|
|
const internalSymbol = Symbol("Deno.internal");
|
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
// These have to initialized here and not in `90_deno_ns.js` because
|
|
|
|
// the op function that needs to be passed will be invalidated by creating
|
|
|
|
// a snapshot
|
|
|
|
ObjectAssign(internals, {
|
|
|
|
nodeUnstable: {
|
2022-12-09 10:43:36 -05:00
|
|
|
Command: __bootstrap.spawn.createCommand(
|
|
|
|
__bootstrap.spawn.createSpawn(ops.op_node_unstable_spawn_child),
|
|
|
|
__bootstrap.spawn.createSpawnSync(
|
|
|
|
ops.op_node_unstable_spawn_sync,
|
|
|
|
),
|
|
|
|
__bootstrap.spawn.createSpawnChild(
|
|
|
|
ops.op_node_unstable_spawn_child,
|
|
|
|
),
|
2022-11-10 16:03:28 -05:00
|
|
|
),
|
|
|
|
serve: __bootstrap.flash.createServe(ops.op_node_unstable_flash_serve),
|
|
|
|
upgradeHttpRaw: __bootstrap.flash.upgradeHttpRaw,
|
|
|
|
listenDatagram: __bootstrap.net.createListenDatagram(
|
|
|
|
ops.op_node_unstable_net_listen_udp,
|
|
|
|
ops.op_node_unstable_net_listen_unixpacket,
|
|
|
|
),
|
2022-12-28 08:56:05 -05:00
|
|
|
osUptime: __bootstrap.os.createOsUptime(ops.op_node_unstable_os_uptime),
|
2022-11-10 16:03:28 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
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);
|
2022-11-10 16:03:28 -05:00
|
|
|
// These have to initialized here and not in `90_deno_ns.js` because
|
|
|
|
// the op function that needs to be passed will be invalidated by creating
|
|
|
|
// a snapshot
|
|
|
|
ObjectAssign(finalDenoNs, {
|
2022-12-09 10:43:36 -05:00
|
|
|
Command: __bootstrap.spawn.createCommand(
|
|
|
|
__bootstrap.spawn.createSpawn(ops.op_spawn_child),
|
|
|
|
__bootstrap.spawn.createSpawnSync(ops.op_spawn_sync),
|
|
|
|
__bootstrap.spawn.createSpawnChild(ops.op_spawn_child),
|
|
|
|
),
|
2022-11-10 16:03:28 -05:00
|
|
|
serve: __bootstrap.flash.createServe(ops.op_flash_serve),
|
|
|
|
listenDatagram: __bootstrap.net.createListenDatagram(
|
|
|
|
ops.op_net_listen_udp,
|
|
|
|
ops.op_net_listen_unixpacket,
|
|
|
|
),
|
2022-12-28 08:56:05 -05:00
|
|
|
osUptime: __bootstrap.os.createOsUptime(ops.op_os_uptime),
|
2022-11-10 16:03:28 -05:00
|
|
|
});
|
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-10-28 07:20:17 -04:00
|
|
|
core.initializeAsyncOps();
|
2022-05-06 13:37:18 -04:00
|
|
|
performance.setTimeOrigin(DateNow());
|
2022-10-25 16:50:55 -04:00
|
|
|
net.setup(runtimeOptions.unstableFlag);
|
|
|
|
|
2023-01-06 16:37:42 -05:00
|
|
|
const consoleFromV8 = window.Deno.core.console;
|
2021-06-26 20:27:50 -04:00
|
|
|
const wrapConsole = window.__bootstrap.console.wrapConsole;
|
|
|
|
|
2020-09-04 07:52:19 -04:00
|
|
|
// Remove bootstrapping data from the global scope
|
2022-11-10 16:03:28 -05:00
|
|
|
const __bootstrap = globalThis.__bootstrap;
|
2020-09-04 07:52:19 -04:00
|
|
|
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);
|
2022-11-21 08:36:26 -05:00
|
|
|
ObjectDefineProperties(globalThis, {
|
|
|
|
name: util.writable(name),
|
|
|
|
// TODO(bartlomieju): should be readonly?
|
|
|
|
close: util.nonEnumerable(workerClose),
|
|
|
|
postMessage: util.writable(postMessage),
|
|
|
|
});
|
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);
|
2022-11-21 08:36:26 -05:00
|
|
|
|
|
|
|
setNumCpus(runtimeOptions.cpuCount);
|
|
|
|
setLanguage(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");
|
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
// These have to initialized here and not in `90_deno_ns.js` because
|
|
|
|
// the op function that needs to be passed will be invalidated by creating
|
|
|
|
// a snapshot
|
|
|
|
ObjectAssign(internals, {
|
|
|
|
nodeUnstable: {
|
2022-12-09 10:43:36 -05:00
|
|
|
Command: __bootstrap.spawn.createCommand(
|
|
|
|
__bootstrap.spawn.createSpawn(ops.op_node_unstable_spawn_child),
|
|
|
|
__bootstrap.spawn.createSpawnSync(
|
|
|
|
ops.op_node_unstable_spawn_sync,
|
|
|
|
),
|
|
|
|
__bootstrap.spawn.createSpawnChild(
|
|
|
|
ops.op_node_unstable_spawn_child,
|
|
|
|
),
|
2022-11-10 16:03:28 -05:00
|
|
|
),
|
|
|
|
serve: __bootstrap.flash.createServe(ops.op_node_unstable_flash_serve),
|
|
|
|
upgradeHttpRaw: __bootstrap.flash.upgradeHttpRaw,
|
|
|
|
listenDatagram: __bootstrap.net.createListenDatagram(
|
|
|
|
ops.op_node_unstable_net_listen_udp,
|
|
|
|
ops.op_node_unstable_net_listen_unixpacket,
|
|
|
|
),
|
2022-12-28 08:56:05 -05:00
|
|
|
osUptime: __bootstrap.os.createOsUptime(ops.op_node_unstable_os_uptime),
|
2022-11-10 16:03:28 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
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);
|
2022-11-10 16:03:28 -05:00
|
|
|
// These have to initialized here and not in `90_deno_ns.js` because
|
|
|
|
// the op function that needs to be passed will be invalidated by creating
|
|
|
|
// a snapshot
|
|
|
|
ObjectAssign(finalDenoNs, {
|
2022-12-09 10:43:36 -05:00
|
|
|
Command: __bootstrap.spawn.createCommand(
|
|
|
|
__bootstrap.spawn.createSpawn(ops.op_spawn_child),
|
|
|
|
__bootstrap.spawn.createSpawnSync(ops.op_spawn_sync),
|
|
|
|
__bootstrap.spawn.createSpawnChild(ops.op_spawn_child),
|
|
|
|
),
|
2022-11-10 16:03:28 -05:00
|
|
|
serve: __bootstrap.flash.createServe(ops.op_flash_serve),
|
|
|
|
listenDatagram: __bootstrap.net.createListenDatagram(
|
|
|
|
ops.op_net_listen_udp,
|
|
|
|
ops.op_net_listen_unixpacket,
|
|
|
|
),
|
2022-12-28 08:56:05 -05:00
|
|
|
osUptime: __bootstrap.os.createOsUptime(ops.op_os_uptime),
|
2022-11-10 16:03:28 -05:00
|
|
|
});
|
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);
|