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-03-04 08:26:00 -05:00
|
|
|
import * as Deno from "./deno.ts";
|
2020-03-05 07:05:41 -05:00
|
|
|
import * as domTypes from "./web/dom_types.ts";
|
2020-03-08 08:09:22 -04:00
|
|
|
import * as csprng from "./ops/get_random_values.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-01-27 21:12:25 -05:00
|
|
|
} from "./globals.ts";
|
|
|
|
import { internalObject } 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-11 16:57:24 -04:00
|
|
|
import { LocationImpl } from "./web/location.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";
|
|
|
|
import { symbols } from "./symbols.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.
|
|
|
|
// @ts-ignore
|
2020-03-04 08:26:00 -05:00
|
|
|
Deno[symbols.internal] = 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
|
|
|
crypto: readOnly(csprng),
|
|
|
|
// TODO(bartlomieju): from MDN docs (https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope)
|
|
|
|
// it seems those two properties should be availble to workers as well
|
|
|
|
onload: writable(undefined),
|
2020-03-24 23:56:40 -04:00
|
|
|
onunload: writable(undefined),
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
log("bootstrapMainRuntime");
|
|
|
|
hasBootstrapped = true;
|
|
|
|
Object.defineProperties(globalThis, windowOrWorkerGlobalScopeMethods);
|
|
|
|
Object.defineProperties(globalThis, windowOrWorkerGlobalScopeProperties);
|
|
|
|
Object.defineProperties(globalThis, eventTargetProperties);
|
|
|
|
Object.defineProperties(globalThis, mainRuntimeGlobalProperties);
|
|
|
|
// Registers the handler for window.onload function.
|
|
|
|
globalThis.addEventListener("load", (e: domTypes.Event): void => {
|
|
|
|
const { onload } = globalThis;
|
|
|
|
if (typeof onload === "function") {
|
|
|
|
onload(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Registers the handler for window.onunload function.
|
|
|
|
globalThis.addEventListener("unload", (e: domTypes.Event): void => {
|
|
|
|
const { onunload } = globalThis;
|
|
|
|
if (typeof onunload === "function") {
|
|
|
|
onunload(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-11 16:57:24 -04:00
|
|
|
const s = runtime.start();
|
|
|
|
|
|
|
|
const location = new LocationImpl(s.location);
|
|
|
|
immutableDefine(globalThis, "location", location);
|
|
|
|
Object.freeze(globalThis.location);
|
|
|
|
|
|
|
|
Object.defineProperties(Deno, {
|
|
|
|
pid: readOnly(s.pid),
|
|
|
|
noColor: readOnly(s.noColor),
|
2020-03-28 13:03:49 -04:00
|
|
|
args: readOnly(Object.freeze(s.args)),
|
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".
|
|
|
|
immutableDefine(globalThis, "Deno", Deno);
|
|
|
|
Object.freeze(globalThis.Deno);
|
|
|
|
Object.freeze(globalThis.Deno.core);
|
|
|
|
Object.freeze(globalThis.Deno.core.sharedQueue);
|
2020-01-27 21:12:25 -05:00
|
|
|
setSignals();
|
|
|
|
|
|
|
|
log("cwd", s.cwd);
|
2020-03-04 08:26:00 -05:00
|
|
|
log("args", Deno.args);
|
2020-01-27 21:12:25 -05:00
|
|
|
|
2020-02-04 14:24:33 -05:00
|
|
|
if (s.repl) {
|
2020-01-27 21:12:25 -05:00
|
|
|
replLoop();
|
|
|
|
}
|
|
|
|
}
|