1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-22 06:09:25 -05:00

reorg: Deno global initialization (#4317)

This commit is contained in:
Bartek Iwańczuk 2020-03-11 21:57:24 +01:00 committed by GitHub
parent b8fa3fd5e7
commit 810e4a16be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 40 deletions

View file

@ -2,11 +2,9 @@
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";
@ -36,7 +34,7 @@ export function initOps(): void {
* code depends on information like "os" and thus getting this information
* is required at startup.
*/
export function start(preserveDenoNamespace = true, source?: string): Start {
export function start(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
@ -47,33 +45,6 @@ export function start(preserveDenoNamespace = true, source?: string): Start {
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;
}

View file

@ -20,9 +20,10 @@ import {
import { internalObject } from "./internals.ts";
import { setSignals } from "./signals.ts";
import { replLoop } from "./repl.ts";
import { LocationImpl } from "./web/location.ts";
import * as runtime from "./runtime.ts";
import { symbols } from "./symbols.ts";
import { log } from "./util.ts";
import { log, immutableDefine } from "./util.ts";
// TODO: factor out `Deno` global assignment to separate function
// Add internal object to Deno object.
@ -33,8 +34,6 @@ Deno[symbols.internal] = internalObject;
export const mainRuntimeGlobalProperties = {
window: readOnly(globalThis),
self: readOnly(globalThis),
Deno: readOnly(Deno),
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
@ -69,15 +68,27 @@ export function bootstrapMainRuntime(): void {
}
});
const s = runtime.start(true);
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);
for (let i = 0; i < s.args.length; i++) {
Deno.args.push(s.args[i]);
}
log("args", Deno.args);
Object.freeze(Deno.args);
if (s.repl) {
replLoop();

View file

@ -17,7 +17,8 @@ import {
eventTargetProperties
} from "./globals.ts";
import * as webWorkerOps from "./ops/web_worker.ts";
import { log } from "./util.ts";
import { LocationImpl } from "./web/location.ts";
import { log, assert, immutableDefine } from "./util.ts";
import { TextEncoder } from "./web/text_encoding.ts";
import * as runtime from "./runtime.ts";
@ -104,5 +105,13 @@ export function bootstrapWorkerRuntime(name: string): void {
Object.defineProperties(globalThis, workerRuntimeGlobalProperties);
Object.defineProperties(globalThis, eventTargetProperties);
Object.defineProperties(globalThis, { name: readOnly(name) });
runtime.start(false, name);
const s = runtime.start(name);
const location = new LocationImpl(s.location);
immutableDefine(globalThis, "location", location);
Object.freeze(globalThis.location);
// globalThis.Deno is not available in worker scope
delete globalThis.Deno;
assert(globalThis.Deno === undefined);
}