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
|
|
|
|
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;
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
const ops = core.ops;
|
|
|
|
const internals = globalThis.__bootstrap.internals;
|
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const {
|
|
|
|
ArrayPrototypeIndexOf,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeShift,
|
|
|
|
ArrayPrototypeSplice,
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
DateNow,
|
|
|
|
Error,
|
|
|
|
ErrorPrototype,
|
|
|
|
FunctionPrototypeCall,
|
|
|
|
FunctionPrototypeBind,
|
|
|
|
ObjectAssign,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectFreeze,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
ObjectSetPrototypeOf,
|
|
|
|
PromiseResolve,
|
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
|
|
|
SymbolIterator,
|
|
|
|
PromisePrototypeThen,
|
|
|
|
SafeWeakMap,
|
|
|
|
TypeError,
|
|
|
|
WeakMapPrototypeDelete,
|
|
|
|
WeakMapPrototypeGet,
|
|
|
|
WeakMapPrototypeSet,
|
|
|
|
} = primordials;
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
import * as util from "internal:runtime/06_util.js";
|
2023-02-07 16:09:50 -05:00
|
|
|
import * as event from "internal:deno_web/02_event.js";
|
|
|
|
import * as location from "internal:deno_web/12_location.js";
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
import * as version from "internal:runtime/01_version.ts";
|
|
|
|
import * as os from "internal:runtime/30_os.js";
|
2023-02-07 16:09:50 -05:00
|
|
|
import * as timers from "internal:deno_web/02_timers.js";
|
|
|
|
import * as colors from "internal:deno_console/01_colors.js";
|
|
|
|
import * as net from "internal:deno_net/01_net.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
inspectArgs,
|
|
|
|
quoteString,
|
|
|
|
wrapConsole,
|
2023-02-07 16:09:50 -05:00
|
|
|
} from "internal:deno_console/02_console.js";
|
|
|
|
import * as performance from "internal:deno_web/15_performance.js";
|
|
|
|
import * as url from "internal:deno_url/00_url.js";
|
|
|
|
import * as fetch from "internal:deno_fetch/26_fetch.js";
|
|
|
|
import * as messagePort from "internal:deno_web/13_message_port.js";
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
import { denoNs, denoNsUnstable } from "internal:runtime/90_deno_ns.js";
|
|
|
|
import { errors } from "internal:runtime/01_errors.js";
|
2023-02-07 16:09:50 -05:00
|
|
|
import * as webidl from "internal:deno_webidl/00_webidl.js";
|
|
|
|
import DOMException from "internal:deno_web/01_dom_exception.js";
|
|
|
|
import * as flash from "internal:deno_flash/01_http.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
mainRuntimeGlobalProperties,
|
|
|
|
setLanguage,
|
|
|
|
setNumCpus,
|
|
|
|
setUserAgent,
|
|
|
|
unstableWindowOrWorkerGlobalScope,
|
|
|
|
windowOrWorkerGlobalScope,
|
|
|
|
workerRuntimeGlobalProperties,
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
} from "internal:runtime/98_global_scope.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
let windowIsClosing = false;
|
|
|
|
let globalThis_;
|
|
|
|
|
|
|
|
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.
|
|
|
|
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
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function workerClose() {
|
|
|
|
if (isClosing) {
|
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
isClosing = true;
|
|
|
|
ops.op_worker_close();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 &&
|
|
|
|
transferOrOptions[SymbolIterator] !== undefined
|
|
|
|
) {
|
|
|
|
const transfer = webidl.converters["sequence<object>"](
|
|
|
|
transferOrOptions,
|
|
|
|
{ prefix, context: "Argument 2" },
|
|
|
|
);
|
|
|
|
options = { transfer };
|
|
|
|
} else {
|
|
|
|
options = webidl.converters.StructuredSerializeOptions(
|
|
|
|
transferOrOptions,
|
|
|
|
{
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
},
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const { transfer } = options;
|
|
|
|
const data = messagePort.serializeJsMessageData(message, transfer);
|
|
|
|
ops.op_worker_post_message(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
let isClosing = false;
|
|
|
|
let globalDispatchEvent;
|
|
|
|
|
|
|
|
async function pollForMessages() {
|
|
|
|
if (!globalDispatchEvent) {
|
|
|
|
globalDispatchEvent = FunctionPrototypeBind(
|
|
|
|
globalThis.dispatchEvent,
|
|
|
|
globalThis,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
while (!isClosing) {
|
|
|
|
const data = await core.opAsync("op_worker_recv_message");
|
|
|
|
if (data === null) break;
|
|
|
|
const v = messagePort.deserializeJsMessageData(data);
|
|
|
|
const message = v[0];
|
|
|
|
const transferables = v[1];
|
|
|
|
|
|
|
|
const msgEvent = new event.MessageEvent("message", {
|
|
|
|
cancelable: false,
|
|
|
|
data: message,
|
|
|
|
ports: transferables.filter((t) =>
|
|
|
|
ObjectPrototypeIsPrototypeOf(messagePort.MessagePortPrototype, t)
|
|
|
|
),
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
|
|
|
globalDispatchEvent(msgEvent);
|
|
|
|
} catch (e) {
|
|
|
|
const errorEvent = new event.ErrorEvent("error", {
|
|
|
|
cancelable: true,
|
|
|
|
message: e.message,
|
|
|
|
lineno: e.lineNumber ? e.lineNumber + 1 : undefined,
|
|
|
|
colno: e.columnNumber ? e.columnNumber + 1 : undefined,
|
|
|
|
filename: e.fileName,
|
|
|
|
error: e,
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
globalDispatchEvent(errorEvent);
|
|
|
|
if (!errorEvent.defaultPrevented) {
|
|
|
|
throw e;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let loadedMainWorkerScript = false;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function importScripts(...urls) {
|
|
|
|
if (ops.op_worker_get_type() === "module") {
|
|
|
|
throw new TypeError("Can't import scripts in a module worker.");
|
|
|
|
}
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const baseUrl = location.getLocationHref();
|
|
|
|
const parsedUrls = ArrayPrototypeMap(urls, (scriptUrl) => {
|
|
|
|
try {
|
|
|
|
return new url.URL(scriptUrl, baseUrl ?? undefined).href;
|
|
|
|
} catch {
|
|
|
|
throw new DOMException(
|
|
|
|
"Failed to parse URL.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 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()`.
|
|
|
|
const scripts = ops.op_worker_sync_fetch(
|
|
|
|
parsedUrls,
|
|
|
|
!loadedMainWorkerScript,
|
|
|
|
);
|
|
|
|
loadedMainWorkerScript = true;
|
|
|
|
|
|
|
|
for (let i = 0; i < scripts.length; ++i) {
|
|
|
|
const { url, script } = scripts[i];
|
|
|
|
const err = core.evalContext(script, url)[1];
|
|
|
|
if (err !== null) {
|
|
|
|
throw err.thrown;
|
2021-08-16 08:29:54 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function opMainModule() {
|
|
|
|
return ops.op_main_module();
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatException(error) {
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(ErrorPrototype, 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-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 19:55:31 -05:00
|
|
|
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("WouldBlock", errors.WouldBlock);
|
|
|
|
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);
|
|
|
|
core.registerErrorClass("NotSupported", errors.NotSupported);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionOperationError",
|
|
|
|
function DOMExceptionOperationError(msg) {
|
|
|
|
return new DOMException(msg, "OperationError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionQuotaExceededError",
|
|
|
|
function DOMExceptionQuotaExceededError(msg) {
|
|
|
|
return new DOMException(msg, "QuotaExceededError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionNotSupportedError",
|
|
|
|
function DOMExceptionNotSupportedError(msg) {
|
|
|
|
return new DOMException(msg, "NotSupported");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionNetworkError",
|
|
|
|
function DOMExceptionNetworkError(msg) {
|
|
|
|
return new DOMException(msg, "NetworkError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionAbortError",
|
|
|
|
function DOMExceptionAbortError(msg) {
|
|
|
|
return new DOMException(msg, "AbortError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionInvalidCharacterError",
|
|
|
|
function DOMExceptionInvalidCharacterError(msg) {
|
|
|
|
return new DOMException(msg, "InvalidCharacterError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
core.registerErrorBuilder(
|
|
|
|
"DOMExceptionDataError",
|
|
|
|
function DOMExceptionDataError(msg) {
|
|
|
|
return new DOMException(msg, "DataError");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function runtimeStart(runtimeOptions, source) {
|
|
|
|
core.setMacrotaskCallback(timers.handleTimerMacrotask);
|
|
|
|
core.setMacrotaskCallback(promiseRejectMacrotaskCallback);
|
|
|
|
core.setWasmStreamingCallback(fetch.handleWasmStreaming);
|
|
|
|
core.setReportExceptionCallback(event.reportException);
|
|
|
|
ops.op_set_format_exception_callback(formatException);
|
|
|
|
version.setVersions(
|
|
|
|
runtimeOptions.denoVersion,
|
|
|
|
runtimeOptions.v8Version,
|
|
|
|
runtimeOptions.tsVersion,
|
|
|
|
);
|
2023-03-05 17:18:13 -05:00
|
|
|
core.setBuildInfo(runtimeOptions.target);
|
2023-02-07 14:22:46 -05:00
|
|
|
util.setLogDebug(runtimeOptions.debugFlag, source);
|
|
|
|
colors.setNoColor(runtimeOptions.noColor || !runtimeOptions.isTty);
|
|
|
|
// deno-lint-ignore prefer-primordials
|
|
|
|
Error.prepareStackTrace = core.prepareStackTrace;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pendingRejections = [];
|
|
|
|
const pendingRejectionsReasons = new SafeWeakMap();
|
|
|
|
|
|
|
|
function promiseRejectCallback(type, promise, reason) {
|
|
|
|
switch (type) {
|
|
|
|
case 0: {
|
|
|
|
ops.op_store_pending_promise_rejection(promise, reason);
|
|
|
|
ArrayPrototypePush(pendingRejections, promise);
|
|
|
|
WeakMapPrototypeSet(pendingRejectionsReasons, promise, reason);
|
|
|
|
break;
|
2022-06-06 14:26:57 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case 1: {
|
|
|
|
ops.op_remove_pending_promise_rejection(promise);
|
|
|
|
const index = ArrayPrototypeIndexOf(pendingRejections, promise);
|
|
|
|
if (index > -1) {
|
|
|
|
ArrayPrototypeSplice(pendingRejections, index, 1);
|
|
|
|
WeakMapPrototypeDelete(pendingRejectionsReasons, promise);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
2022-06-06 14:26:57 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return !!globalThis_.onunhandledrejection ||
|
|
|
|
event.listenerCount(globalThis_, "unhandledrejection") > 0;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function promiseRejectMacrotaskCallback() {
|
|
|
|
while (pendingRejections.length > 0) {
|
|
|
|
const promise = ArrayPrototypeShift(pendingRejections);
|
|
|
|
const hasPendingException = ops.op_has_pending_promise_rejection(
|
|
|
|
promise,
|
2021-05-10 06:02:47 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
const reason = WeakMapPrototypeGet(pendingRejectionsReasons, promise);
|
|
|
|
WeakMapPrototypeDelete(pendingRejectionsReasons, promise);
|
|
|
|
|
|
|
|
if (!hasPendingException) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rejectionEvent = new event.PromiseRejectionEvent(
|
|
|
|
"unhandledrejection",
|
|
|
|
{
|
|
|
|
cancelable: true,
|
|
|
|
promise,
|
|
|
|
reason,
|
2021-09-16 03:58:29 -04:00
|
|
|
},
|
|
|
|
);
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const errorEventCb = (event) => {
|
|
|
|
if (event.error === reason) {
|
2023-01-16 10:19:04 -05:00
|
|
|
ops.op_remove_pending_promise_rejection(promise);
|
2022-07-20 14:28:19 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
|
|
|
// Add a callback for "error" event - it will be dispatched
|
|
|
|
// if error is thrown during dispatch of "unhandledrejection"
|
|
|
|
// event.
|
|
|
|
globalThis_.addEventListener("error", errorEventCb);
|
|
|
|
globalThis_.dispatchEvent(rejectionEvent);
|
|
|
|
globalThis_.removeEventListener("error", errorEventCb);
|
|
|
|
|
|
|
|
// If event was not prevented (or "unhandledrejection" listeners didn't
|
|
|
|
// throw) we will let Rust side handle it.
|
|
|
|
if (rejectionEvent.defaultPrevented) {
|
|
|
|
ops.op_remove_pending_promise_rejection(promise);
|
2022-07-20 14:28:19 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let hasBootstrapped = false;
|
2023-03-07 10:11:54 -05:00
|
|
|
// Set up global properties shared by main and worker runtime.
|
|
|
|
ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope);
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function bootstrapMainRuntime(runtimeOptions) {
|
|
|
|
if (hasBootstrapped) {
|
|
|
|
throw new Error("Worker runtime already bootstrapped");
|
2022-07-20 14:28:19 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
performance.setTimeOrigin(DateNow());
|
|
|
|
globalThis_ = globalThis;
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Remove bootstrapping data from the global scope
|
|
|
|
delete globalThis.__bootstrap;
|
|
|
|
delete globalThis.bootstrap;
|
|
|
|
hasBootstrapped = true;
|
2022-07-22 18:40:42 -04:00
|
|
|
|
2023-02-07 14:22:46 -05: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.
|
|
|
|
if (runtimeOptions.location == null) {
|
|
|
|
mainRuntimeGlobalProperties.location = {
|
|
|
|
writable: true,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
location.setLocationHref(runtimeOptions.location);
|
2022-07-20 14:28:19 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (runtimeOptions.unstableFlag) {
|
|
|
|
ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope);
|
|
|
|
}
|
|
|
|
ObjectDefineProperties(globalThis, mainRuntimeGlobalProperties);
|
|
|
|
ObjectDefineProperties(globalThis, {
|
|
|
|
close: util.writable(windowClose),
|
|
|
|
closed: util.getterOnly(() => windowIsClosing),
|
|
|
|
});
|
|
|
|
ObjectSetPrototypeOf(globalThis, Window.prototype);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (runtimeOptions.inspectFlag) {
|
2023-03-07 10:11:54 -05:00
|
|
|
const consoleFromV8 = core.console;
|
2023-02-07 14:22:46 -05:00
|
|
|
const consoleFromDeno = globalThis.console;
|
|
|
|
wrapConsole(consoleFromDeno, consoleFromV8);
|
|
|
|
}
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
event.setEventTargetData(globalThis);
|
|
|
|
event.saveGlobalThisReference(globalThis);
|
2022-08-10 14:57:30 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
event.defineEventHandler(globalThis, "error");
|
|
|
|
event.defineEventHandler(globalThis, "load");
|
|
|
|
event.defineEventHandler(globalThis, "beforeunload");
|
|
|
|
event.defineEventHandler(globalThis, "unload");
|
|
|
|
event.defineEventHandler(globalThis, "unhandledrejection");
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
core.setPromiseRejectCallback(promiseRejectCallback);
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const isUnloadDispatched = SymbolFor("isUnloadDispatched");
|
|
|
|
// 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.
|
|
|
|
globalThis[isUnloadDispatched] = false;
|
|
|
|
globalThis.addEventListener("unload", () => {
|
|
|
|
globalThis_[isUnloadDispatched] = true;
|
|
|
|
});
|
2020-11-26 16:27:55 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
runtimeStart(runtimeOptions);
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
setNumCpus(runtimeOptions.cpuCount);
|
|
|
|
setUserAgent(runtimeOptions.userAgent);
|
|
|
|
setLanguage(runtimeOptions.locale);
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const internalSymbol = Symbol("Deno.internal");
|
2021-01-21 02:44:48 -05:00
|
|
|
|
2023-02-07 14:22:46 -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: {
|
|
|
|
serve: flash.createServe(ops.op_node_unstable_flash_serve),
|
|
|
|
upgradeHttpRaw: flash.upgradeHttpRaw,
|
|
|
|
listenDatagram: net.createListenDatagram(
|
|
|
|
ops.op_node_unstable_net_listen_udp,
|
|
|
|
ops.op_node_unstable_net_listen_unixpacket,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
});
|
2021-01-07 13:06:08 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// FIXME(bartlomieju): temporarily add whole `Deno.core` to
|
|
|
|
// `Deno[Deno.internal]` namespace. It should be removed and only necessary
|
|
|
|
// methods should be left there.
|
|
|
|
ObjectAssign(internals, {
|
|
|
|
core,
|
|
|
|
});
|
2020-08-07 16:47:18 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const finalDenoNs = {
|
|
|
|
internal: internalSymbol,
|
|
|
|
[internalSymbol]: internals,
|
|
|
|
resources: core.resources,
|
|
|
|
close: core.close,
|
|
|
|
...denoNs,
|
|
|
|
};
|
|
|
|
ObjectDefineProperties(finalDenoNs, {
|
|
|
|
pid: util.readOnly(runtimeOptions.pid),
|
|
|
|
ppid: util.readOnly(runtimeOptions.ppid),
|
|
|
|
noColor: util.readOnly(runtimeOptions.noColor),
|
|
|
|
args: util.readOnly(ObjectFreeze(runtimeOptions.args)),
|
|
|
|
mainModule: util.getterOnly(opMainModule),
|
|
|
|
});
|
2021-03-12 15:23:59 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (runtimeOptions.unstableFlag) {
|
|
|
|
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
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectAssign(finalDenoNs, {
|
|
|
|
serve: flash.createServe(ops.op_flash_serve),
|
|
|
|
listenDatagram: net.createListenDatagram(
|
|
|
|
ops.op_net_listen_udp,
|
|
|
|
ops.op_net_listen_unixpacket,
|
|
|
|
),
|
2022-11-10 16:03:28 -05:00
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-11-10 16:03:28 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Setup `Deno` global - we're actually overriding already existing global
|
|
|
|
// `Deno` with `Deno` namespace from "./deno.ts".
|
|
|
|
ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs));
|
2023-01-24 12:54:10 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
util.log("args", runtimeOptions.args);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function bootstrapWorkerRuntime(
|
|
|
|
runtimeOptions,
|
|
|
|
name,
|
|
|
|
internalName,
|
|
|
|
) {
|
|
|
|
if (hasBootstrapped) {
|
|
|
|
throw new Error("Worker runtime already bootstrapped");
|
|
|
|
}
|
|
|
|
|
|
|
|
performance.setTimeOrigin(DateNow());
|
|
|
|
globalThis_ = globalThis;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const consoleFromV8 = globalThis.Deno.core.console;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Remove bootstrapping data from the global scope
|
|
|
|
delete globalThis.__bootstrap;
|
|
|
|
delete globalThis.bootstrap;
|
|
|
|
hasBootstrapped = true;
|
2023-03-07 10:11:54 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (runtimeOptions.unstableFlag) {
|
|
|
|
ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope);
|
|
|
|
}
|
|
|
|
ObjectDefineProperties(globalThis, workerRuntimeGlobalProperties);
|
|
|
|
ObjectDefineProperties(globalThis, {
|
|
|
|
name: util.writable(name),
|
|
|
|
// TODO(bartlomieju): should be readonly?
|
|
|
|
close: util.nonEnumerable(workerClose),
|
|
|
|
postMessage: util.writable(postMessage),
|
|
|
|
});
|
|
|
|
if (runtimeOptions.enableTestingFeaturesFlag) {
|
|
|
|
ObjectDefineProperty(
|
|
|
|
globalThis,
|
|
|
|
"importScripts",
|
|
|
|
util.writable(importScripts),
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectSetPrototypeOf(globalThis, DedicatedWorkerGlobalScope.prototype);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const consoleFromDeno = globalThis.console;
|
|
|
|
wrapConsole(consoleFromDeno, consoleFromV8);
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
event.setEventTargetData(globalThis);
|
|
|
|
event.saveGlobalThisReference(globalThis);
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
event.defineEventHandler(self, "message");
|
|
|
|
event.defineEventHandler(self, "error", undefined, true);
|
|
|
|
event.defineEventHandler(self, "unhandledrejection");
|
2021-06-26 20:27:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
core.setPromiseRejectCallback(promiseRejectCallback);
|
2020-12-11 12:49:26 -05:00
|
|
|
|
2023-02-07 14:22:46 -05: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();
|
|
|
|
});
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
runtimeStart(
|
|
|
|
runtimeOptions,
|
|
|
|
internalName ?? name,
|
|
|
|
);
|
2022-07-20 14:28:19 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
location.setLocationHref(runtimeOptions.location);
|
2021-07-10 17:32:10 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
setNumCpus(runtimeOptions.cpuCount);
|
|
|
|
setLanguage(runtimeOptions.locale);
|
2022-09-28 06:28:38 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
globalThis.pollForMessages = pollForMessages;
|
2022-11-21 08:36:26 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const internalSymbol = Symbol("Deno.internal");
|
2020-08-07 16:47:18 -04:00
|
|
|
|
2023-02-07 14:22:46 -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: {
|
|
|
|
serve: flash.createServe(ops.op_node_unstable_flash_serve),
|
|
|
|
upgradeHttpRaw: flash.upgradeHttpRaw,
|
|
|
|
listenDatagram: net.createListenDatagram(
|
|
|
|
ops.op_node_unstable_net_listen_udp,
|
|
|
|
ops.op_node_unstable_net_listen_unixpacket,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
});
|
2021-05-11 15:09:09 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// FIXME(bartlomieju): temporarily add whole `Deno.core` to
|
|
|
|
// `Deno[Deno.internal]` namespace. It should be removed and only necessary
|
|
|
|
// methods should be left there.
|
|
|
|
ObjectAssign(internals, {
|
|
|
|
core,
|
|
|
|
});
|
2021-03-12 15:23:59 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const finalDenoNs = {
|
|
|
|
internal: internalSymbol,
|
|
|
|
[internalSymbol]: internals,
|
|
|
|
resources: core.resources,
|
|
|
|
close: core.close,
|
|
|
|
...denoNs,
|
|
|
|
};
|
|
|
|
if (runtimeOptions.unstableFlag) {
|
|
|
|
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
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectAssign(finalDenoNs, {
|
|
|
|
serve: flash.createServe(ops.op_flash_serve),
|
|
|
|
listenDatagram: net.createListenDatagram(
|
|
|
|
ops.op_net_listen_udp,
|
|
|
|
ops.op_net_listen_unixpacket,
|
|
|
|
),
|
2022-05-17 16:27:17 -04:00
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectDefineProperties(finalDenoNs, {
|
|
|
|
pid: util.readOnly(runtimeOptions.pid),
|
|
|
|
noColor: util.readOnly(runtimeOptions.noColor),
|
|
|
|
args: util.readOnly(ObjectFreeze(runtimeOptions.args)),
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
// Setup `Deno` global - we're actually overriding already
|
|
|
|
// existing global `Deno` with `Deno` namespace from "./deno.ts".
|
|
|
|
ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs));
|
|
|
|
}
|
|
|
|
|
2023-03-07 10:11:54 -05:00
|
|
|
globalThis.bootstrap = {
|
|
|
|
mainRuntime: bootstrapMainRuntime,
|
|
|
|
workerRuntime: bootstrapWorkerRuntime,
|
|
|
|
};
|