1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 21:50:00 -05:00
denoland-deno/cli/js/runtime.ts
Bartek Iwańczuk 2d1b39bef3
reorg: remove dispatch.ts, move signals, factor out web utils (#4316)
- moves signal definition from "cli/js/process.ts" to "cli/js/signals.ts"
- removes "cli/js/dispatch.ts"
- removes "cli/js/types.ts"
- moves web specific utilities to "cli/js/web/util.ts"
2020-03-11 15:49:53 +01:00

79 lines
2.8 KiB
TypeScript

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { core } from "./core.ts";
import * as dispatchMinimal from "./ops/dispatch_minimal.ts";
import * as dispatchJson from "./ops/dispatch_json.ts";
import { assert } from "./util.ts";
import * as util from "./util.ts";
import { setBuildInfo } from "./build.ts";
import { setVersions } from "./version.ts";
import { setLocation } from "./web/location.ts";
import { setPrepareStackTrace } from "./error_stack.ts";
import { Start, start as startOp } from "./ops/runtime.ts";
export let OPS_CACHE: { [name: string]: number };
function getAsyncHandler(opName: string): (msg: Uint8Array) => void {
switch (opName) {
case "op_write":
case "op_read":
return dispatchMinimal.asyncMsgFromRust;
default:
return dispatchJson.asyncMsgFromRust;
}
}
// TODO(bartlomieju): temporary solution, must be fixed when moving
// dispatches to separate crates
export function initOps(): void {
OPS_CACHE = core.ops();
for (const [name, opId] of Object.entries(OPS_CACHE)) {
core.setAsyncHandler(opId, getAsyncHandler(name));
}
}
/**
* This function bootstraps JS runtime, unfortunately some of runtime
* code depends on information like "os" and thus getting this information
* is required at startup.
*/
export function start(preserveDenoNamespace = true, source?: string): Start {
initOps();
// First we send an empty `Start` message to let the privileged side know we
// are ready. The response should be a `StartRes` message containing the CLI
// args and other info.
const s = startOp();
setVersions(s.denoVersion, s.v8Version, s.tsVersion);
setBuildInfo(s.os, s.arch);
util.setLogDebug(s.debugFlag, source);
// TODO(bartlomieju): this field should always be set
assert(s.location.length > 0);
setLocation(s.location);
setPrepareStackTrace(Error);
// TODO(bartlomieju): I don't like that it's mixed in here, when
// compiler and worker runtimes call this funtion and they don't use
// Deno namespace (sans shared queue - Deno.core)
// pid and noColor need to be set in the Deno module before it's set to be
// frozen.
util.immutableDefine(globalThis.Deno, "pid", s.pid);
util.immutableDefine(globalThis.Deno, "noColor", s.noColor);
Object.freeze(globalThis.Deno);
if (preserveDenoNamespace) {
util.immutableDefine(globalThis, "Deno", globalThis.Deno);
// Deno.core could ONLY be safely frozen here (not in globals.ts)
// since shared_queue.js will modify core properties.
Object.freeze(globalThis.Deno.core);
// core.sharedQueue is an object so we should also freeze it.
Object.freeze(globalThis.Deno.core.sharedQueue);
} else {
// Remove globalThis.Deno
delete globalThis.Deno;
assert(globalThis.Deno === undefined);
}
return s;
}