Skip to content

Commit

Permalink
centralize setup of Deno namespace 1/2
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Mar 10, 2020
1 parent 91e5e22 commit f9ec21e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 39 deletions.
1 change: 0 additions & 1 deletion cli/js/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export { connectTLS, listenTLS } from "./tls.ts";
export { truncateSync, truncate } from "./ops/fs/truncate.ts";
export { isatty, setRaw } from "./ops/tty.ts";
export { utimeSync, utime } from "./ops/fs/utime.ts";
export { version } from "./version.ts";
export { writeFileSync, writeFile, WriteFileOptions } from "./write_file.ts";
export const args: string[] = [];
export { test, runTests } from "./testing.ts";
Expand Down
47 changes: 33 additions & 14 deletions cli/js/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { core } from "./core.ts";
import * as Deno from "./deno.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 { LocationImpl } from "./web/location.ts";
import { setPrepareStackTrace } from "./error_stack.ts";
import { Start, start as startOp } from "./ops/runtime.ts";
import { setSignals } from "./process.ts";
import { symbols } from "./symbols.ts";
import { internalObject } from "./internals.ts";

interface Version {
deno: string;
v8: string;
typescript: string;
}

export let OPS_CACHE: { [name: string]: number };

Expand Down Expand Up @@ -43,26 +52,34 @@ export function start(preserveDenoNamespace = true, source?: string): Start {
// args and other info.
const s = startOp();

setVersions(s.denoVersion, s.v8Version, s.tsVersion);
// Add internal object to Deno object.
// This is not exposed as part of the Deno types.
// @ts-ignore
Deno[symbols.internal] = internalObject;
// Build info is used by internal code, so setting it first.
setBuildInfo(s.os, s.arch);

util.setLogDebug(s.debugFlag, source);
util.immutableDefine(globalThis, "location", new LocationImpl(s.location));
Object.freeze(globalThis.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);
setSignals();

if (preserveDenoNamespace) {
util.immutableDefine(globalThis, "Deno", globalThis.Deno);
const version: Version = {
deno: s.denoVersion,
v8: s.v8Version,
typescript: s.tsVersion
};
Object.freeze(version);
util.immutableDefine(Deno, "version", version);
util.immutableDefine(Deno, "pid", s.pid);
util.immutableDefine(Deno, "noColor", s.noColor);
util.immutableDefine(Deno, "args", [...s.args]);
// TODO(bartlomieju): Object.freeze should be called recursively on
// all properties of `globalThis.Deno`
Object.freeze(Deno);
util.immutableDefine(globalThis, "Deno", 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);
Expand All @@ -74,5 +91,7 @@ export function start(preserveDenoNamespace = true, source?: string): Start {
assert(globalThis.Deno === undefined);
}

util.log("cwd", s.cwd);
util.log("args", Deno.args);
return s;
}
20 changes: 1 addition & 19 deletions cli/js/runtime_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// - `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 domTypes from "./web/dom_types.ts";
import * as csprng from "./ops/get_random_values.ts";
import {
Expand All @@ -17,18 +16,10 @@ import {
windowOrWorkerGlobalScopeProperties,
eventTargetProperties
} from "./globals.ts";
import { internalObject } from "./internals.ts";
import { setSignals } from "./process.ts";
import { replLoop } from "./repl.ts";
import * as runtime from "./runtime.ts";
import { symbols } from "./symbols.ts";
import { log } 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;
import { log } from "./util.ts";

export const mainRuntimeGlobalProperties = {
window: readOnly(globalThis),
Expand Down Expand Up @@ -70,15 +61,6 @@ export function bootstrapMainRuntime(): void {
});

const s = runtime.start(true);
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();
}
Expand Down
5 changes: 0 additions & 5 deletions cli/js/version.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
interface Version {
deno: string;
v8: string;
typescript: string;
}

export const version: Version = {
deno: "",
Expand Down

0 comments on commit f9ec21e

Please sign in to comment.