Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reorg: cli/js/compiler/, move more API to cli/js/web/ #4310

Merged
merged 13 commits into from
Mar 11, 2020
Prev Previous commit
Next Next commit
revert globals and dispatch changes
bartlomieju committed Mar 11, 2020
commit d245bb123b90f9ece83c29ca590b0b4ada958f19
19 changes: 7 additions & 12 deletions cli/js/deno.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

// Deno namespace
// Public deno module.
export {
Buffer,
readAll,
@@ -114,25 +114,20 @@ 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";

// These are internal Deno APIs. We are marking them as internal so they do not
// appear in the runtime type library.
/** @internal */
export { core } from "./core.ts";

export { symbols } from "./symbols.ts";

/** The current process id of the runtime. */
export let pid: number;

/** Reflects the NO_COLOR environment variable: https://no-color.org/ */
export let noColor: boolean;

export let args: string[];

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

export let version: Version;
export { symbols } from "./symbols.ts";
23 changes: 23 additions & 0 deletions cli/js/dispatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as minimal from "./ops/dispatch_minimal.ts";
import * as json from "./ops/dispatch_json.ts";
import { AsyncHandler } from "./plugins.ts";

const PLUGIN_ASYNC_HANDLER_MAP: Map<number, AsyncHandler> = new Map();

export function setPluginAsyncHandler(
opId: number,
handler: AsyncHandler
): void {
PLUGIN_ASYNC_HANDLER_MAP.set(opId, handler);
}

export function getAsyncHandler(opName: string): (msg: Uint8Array) => void {
switch (opName) {
case "op_write":
case "op_read":
return minimal.asyncMsgFromRust;
default:
return json.asyncMsgFromRust;
}
}
74 changes: 25 additions & 49 deletions cli/js/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
// 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 * as dispatch from "./dispatch.ts";
import { assert } from "./util.ts";
import * as util from "./util.ts";
import { setBuildInfo } from "./build.ts";
import { LocationImpl } from "./web/location.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";
import { setSignals } from "./process.ts";
import { symbols } from "./symbols.ts";
import { internalObject } from "./internals.ts";

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

export 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));
core.setAsyncHandler(opId, dispatch.getAsyncHandler(name));
}
}

@@ -46,47 +32,37 @@ export function start(preserveDenoNamespace = true, source?: string): Start {
// args and other info.
const s = startOp();

// TODO(bartlomieju): Deno[symbols.internal]
// is still exposed to end users, not only for tests.
// 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.
setVersions(s.denoVersion, s.v8Version, s.tsVersion);
setBuildInfo(s.os, s.arch);

util.setLogDebug(s.debugFlag, source);
util.immutableDefine(
globalThis,
"location",
Object.freeze(new LocationImpl(s.location))
);

// TODO(bartlomieju): this field should always be set
assert(s.location.length > 0);
setLocation(s.location);
setPrepareStackTrace(Error);
setSignals();

// 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(
Deno,
"version",
Object.freeze({
deno: s.denoVersion,
v8: s.v8Version,
typescript: s.tsVersion
})
);
util.immutableDefine(Deno, "pid", s.pid);
util.immutableDefine(Deno, "noColor", s.noColor);
util.immutableDefine(Deno, "args", Object.freeze([...s.args]));
Object.freeze(Deno.core);
Object.freeze(Deno.core.sharedQueue);
util.immutableDefine(globalThis, "Deno", Object.freeze(Deno));
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);
}

util.log("cwd", s.cwd);
util.log("args", s.args);
return s;
}
20 changes: 19 additions & 1 deletion cli/js/runtime_main.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
// - `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 {
@@ -16,11 +17,19 @@ 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;

export const mainRuntimeGlobalProperties = {
window: readOnly(globalThis),
self: readOnly(globalThis),
@@ -61,6 +70,15 @@ 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();
}
28 changes: 28 additions & 0 deletions cli/js/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
interface Version {
deno: string;
v8: string;
typescript: string;
}

export const version: Version = {
deno: "",
v8: "",
typescript: ""
};

/**
* Sets the deno, v8, and typescript versions and freezes the version object.
* @internal
*/
export function setVersions(
denoVersion: string,
v8Version: string,
tsVersion: string
): void {
version.deno = denoVersion;
version.v8 = v8Version;
version.typescript = tsVersion;

Object.freeze(version);
}
5 changes: 5 additions & 0 deletions cli/js/web/location.ts
Original file line number Diff line number Diff line change
@@ -44,3 +44,8 @@ export class LocationImpl implements Location {
throw notImplemented();
}
}

export function setLocation(url: string): void {
globalThis.location = new LocationImpl(url);
Object.freeze(globalThis.location);
}