2020-01-27 21:12:25 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
// This module is the entry point for "main" isolate, ie. the one
|
|
|
|
// that is created when you run "deno" executable.
|
|
|
|
//
|
|
|
|
// It provides a single function that should be called by Rust:
|
|
|
|
// - `bootstrapMainRuntime` - must be called once, when Isolate is created.
|
|
|
|
// It sets up runtime by providing globals for `WindowScope` and adds `Deno` global.
|
|
|
|
|
2020-04-30 11:23:40 -04:00
|
|
|
import * as denoNs from "./deno.ts";
|
|
|
|
import * as denoUnstableNs from "./deno_unstable.ts";
|
2020-06-10 23:00:29 -04:00
|
|
|
import { opMainModule } from "./ops/runtime.ts";
|
2020-03-24 23:56:40 -04:00
|
|
|
import { exit } from "./ops/os.ts";
|
2020-01-27 21:12:25 -05:00
|
|
|
import {
|
|
|
|
readOnly,
|
2020-03-24 23:56:40 -04:00
|
|
|
getterOnly,
|
2020-01-27 21:12:25 -05:00
|
|
|
writable,
|
|
|
|
windowOrWorkerGlobalScopeMethods,
|
|
|
|
windowOrWorkerGlobalScopeProperties,
|
2020-03-28 13:03:49 -04:00
|
|
|
eventTargetProperties,
|
2020-04-11 11:42:02 -04:00
|
|
|
setEventTargetData,
|
2020-01-27 21:12:25 -05:00
|
|
|
} from "./globals.ts";
|
2020-04-30 11:23:40 -04:00
|
|
|
import { unstableMethods, unstableProperties } from "./globals_unstable.ts";
|
2020-04-27 19:06:03 -04:00
|
|
|
import { internalObject, internalSymbol } from "./internals.ts";
|
2020-03-11 10:49:53 -04:00
|
|
|
import { setSignals } from "./signals.ts";
|
2020-03-04 08:26:00 -05:00
|
|
|
import { replLoop } from "./repl.ts";
|
2020-03-24 23:56:40 -04:00
|
|
|
import { setTimeout } from "./web/timers.ts";
|
2020-03-04 08:26:00 -05:00
|
|
|
import * as runtime from "./runtime.ts";
|
2020-03-11 16:57:24 -04:00
|
|
|
import { log, immutableDefine } from "./util.ts";
|
2020-01-27 21:12:25 -05:00
|
|
|
|
|
|
|
// TODO: factor out `Deno` global assignment to separate function
|
|
|
|
// Add internal object to Deno object.
|
|
|
|
// This is not exposed as part of the Deno types.
|
2020-06-02 00:24:44 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(denoNs as any)[internalSymbol] = internalObject;
|
2020-01-27 21:12:25 -05:00
|
|
|
|
2020-03-24 23:56:40 -04:00
|
|
|
let windowIsClosing = false;
|
|
|
|
|
|
|
|
function windowClose(): void {
|
|
|
|
if (!windowIsClosing) {
|
|
|
|
windowIsClosing = true;
|
|
|
|
// Push a macrotask to exit after a promise resolve.
|
|
|
|
// This is not perfect, but should be fine for first pass.
|
|
|
|
Promise.resolve().then(() =>
|
|
|
|
setTimeout.call(
|
|
|
|
null,
|
|
|
|
() => {
|
|
|
|
// This should be fine, since only Window/MainWorker has .close()
|
|
|
|
exit(0);
|
|
|
|
},
|
|
|
|
0
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:12:25 -05:00
|
|
|
export const mainRuntimeGlobalProperties = {
|
|
|
|
window: readOnly(globalThis),
|
2020-02-26 05:49:38 -05:00
|
|
|
self: readOnly(globalThis),
|
2020-01-27 21:12:25 -05:00
|
|
|
// TODO(bartlomieju): from MDN docs (https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope)
|
2020-04-11 11:42:02 -04:00
|
|
|
// it seems those two properties should be available to workers as well
|
|
|
|
onload: writable(null),
|
|
|
|
onunload: writable(null),
|
2020-03-24 23:56:40 -04:00
|
|
|
close: writable(windowClose),
|
2020-03-28 13:03:49 -04:00
|
|
|
closed: getterOnly(() => windowIsClosing),
|
2020-01-27 21:12:25 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let hasBootstrapped = false;
|
|
|
|
|
|
|
|
export function bootstrapMainRuntime(): void {
|
|
|
|
if (hasBootstrapped) {
|
|
|
|
throw new Error("Worker runtime already bootstrapped");
|
|
|
|
}
|
2020-04-24 19:03:45 -04:00
|
|
|
// Remove bootstrapping methods from global scope
|
2020-06-02 00:24:44 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(globalThis as any).bootstrap = undefined;
|
2020-01-27 21:12:25 -05:00
|
|
|
log("bootstrapMainRuntime");
|
|
|
|
hasBootstrapped = true;
|
|
|
|
Object.defineProperties(globalThis, windowOrWorkerGlobalScopeMethods);
|
|
|
|
Object.defineProperties(globalThis, windowOrWorkerGlobalScopeProperties);
|
|
|
|
Object.defineProperties(globalThis, eventTargetProperties);
|
|
|
|
Object.defineProperties(globalThis, mainRuntimeGlobalProperties);
|
2020-04-11 11:42:02 -04:00
|
|
|
setEventTargetData(globalThis);
|
2020-01-27 21:12:25 -05:00
|
|
|
// Registers the handler for window.onload function.
|
2020-04-11 11:42:02 -04:00
|
|
|
globalThis.addEventListener("load", (e) => {
|
2020-01-27 21:12:25 -05:00
|
|
|
const { onload } = globalThis;
|
|
|
|
if (typeof onload === "function") {
|
|
|
|
onload(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Registers the handler for window.onunload function.
|
2020-04-11 11:42:02 -04:00
|
|
|
globalThis.addEventListener("unload", (e) => {
|
2020-01-27 21:12:25 -05:00
|
|
|
const { onunload } = globalThis;
|
|
|
|
if (typeof onunload === "function") {
|
|
|
|
onunload(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-01 14:15:28 -04:00
|
|
|
const { args, cwd, noColor, pid, repl, unstableFlag } = runtime.start();
|
2020-03-11 16:57:24 -04:00
|
|
|
|
2020-04-30 11:23:40 -04:00
|
|
|
Object.defineProperties(denoNs, {
|
|
|
|
pid: readOnly(pid),
|
|
|
|
noColor: readOnly(noColor),
|
|
|
|
args: readOnly(Object.freeze(args)),
|
2020-03-11 16:57:24 -04:00
|
|
|
});
|
2020-04-30 11:23:40 -04:00
|
|
|
|
|
|
|
if (unstableFlag) {
|
|
|
|
Object.defineProperties(globalThis, unstableMethods);
|
|
|
|
Object.defineProperties(globalThis, unstableProperties);
|
2020-06-10 23:00:29 -04:00
|
|
|
Object.defineProperty(denoNs, "mainModule", getterOnly(opMainModule));
|
2020-04-30 11:23:40 -04:00
|
|
|
Object.assign(denoNs, denoUnstableNs);
|
|
|
|
}
|
|
|
|
|
2020-03-11 16:57:24 -04:00
|
|
|
// Setup `Deno` global - we're actually overriding already
|
|
|
|
// existing global `Deno` with `Deno` namespace from "./deno.ts".
|
2020-04-30 11:23:40 -04:00
|
|
|
immutableDefine(globalThis, "Deno", denoNs);
|
2020-03-11 16:57:24 -04:00
|
|
|
Object.freeze(globalThis.Deno);
|
|
|
|
Object.freeze(globalThis.Deno.core);
|
|
|
|
Object.freeze(globalThis.Deno.core.sharedQueue);
|
2020-01-27 21:12:25 -05:00
|
|
|
setSignals();
|
|
|
|
|
2020-04-30 11:23:40 -04:00
|
|
|
log("cwd", cwd);
|
|
|
|
log("args", args);
|
2020-01-27 21:12:25 -05:00
|
|
|
|
2020-04-30 11:23:40 -04:00
|
|
|
if (repl) {
|
2020-01-27 21:12:25 -05:00
|
|
|
replLoop();
|
|
|
|
}
|
|
|
|
}
|