mirror of
https://github.com/denoland/deno.git
synced 2024-11-26 16:09:27 -05:00
fc4819e1e0
Refactors Event and EventTarget so that they better encapsulate their non-public data as well as are more forward compatible with things like DOM Nodes. Moves `dom_types.ts` -> `dom_types.d.ts` which was always the intention, it was a legacy of when we used to build the types from the code and the limitations of the compiler. There was a lot of cruft in `dom_types` which shouldn't have been there, and mis-alignment to the DOM standards. This generally has been eliminated, though we still have some minor differences from the DOM (like the removal of some deprecated methods/properties). Adds `DOMException`. Strictly it shouldn't inherit from `Error`, but most browsers provide a stack trace when one is thrown, so the behaviour in Deno actually better matches the browser. `Event` still doesn't log to console like it does in the browser. I wanted to get this raised and that could be an enhancement later on (it currently doesn't either).
122 lines
3.8 KiB
TypeScript
122 lines
3.8 KiB
TypeScript
// 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.
|
|
|
|
import * as Deno from "./deno.ts";
|
|
import * as csprng from "./ops/get_random_values.ts";
|
|
import { exit } from "./ops/os.ts";
|
|
import {
|
|
readOnly,
|
|
getterOnly,
|
|
writable,
|
|
windowOrWorkerGlobalScopeMethods,
|
|
windowOrWorkerGlobalScopeProperties,
|
|
eventTargetProperties,
|
|
setEventTargetData,
|
|
} from "./globals.ts";
|
|
import { internalObject } from "./internals.ts";
|
|
import { setSignals } from "./signals.ts";
|
|
import { replLoop } from "./repl.ts";
|
|
import { LocationImpl } from "./web/location.ts";
|
|
import { setTimeout } from "./web/timers.ts";
|
|
import * as runtime from "./runtime.ts";
|
|
import { symbols } from "./symbols.ts";
|
|
import { log, immutableDefine } from "./util.ts";
|
|
|
|
// 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
|
|
Deno[symbols.internal] = internalObject;
|
|
|
|
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
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
export const mainRuntimeGlobalProperties = {
|
|
window: readOnly(globalThis),
|
|
self: readOnly(globalThis),
|
|
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 available to workers as well
|
|
onload: writable(null),
|
|
onunload: writable(null),
|
|
close: writable(windowClose),
|
|
closed: getterOnly(() => windowIsClosing),
|
|
};
|
|
|
|
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);
|
|
setEventTargetData(globalThis);
|
|
// Registers the handler for window.onload function.
|
|
globalThis.addEventListener("load", (e) => {
|
|
const { onload } = globalThis;
|
|
if (typeof onload === "function") {
|
|
onload(e);
|
|
}
|
|
});
|
|
// Registers the handler for window.onunload function.
|
|
globalThis.addEventListener("unload", (e) => {
|
|
const { onunload } = globalThis;
|
|
if (typeof onunload === "function") {
|
|
onunload(e);
|
|
}
|
|
});
|
|
|
|
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),
|
|
args: readOnly(Object.freeze(s.args)),
|
|
});
|
|
// 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);
|
|
setSignals();
|
|
|
|
log("cwd", s.cwd);
|
|
log("args", Deno.args);
|
|
|
|
if (s.repl) {
|
|
replLoop();
|
|
}
|
|
}
|