Skip to content

Commit

Permalink
Add globals.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 21, 2018
1 parent 8e2e17c commit 9a66216
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 52 deletions.
44 changes: 44 additions & 0 deletions globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { setTimeout } from "./timers";

// If you use the eval function indirectly, by invoking it via a reference
// other than eval, as of ECMAScript 5 it works in the global scope rather than
// the local scope. This means, for instance, that function declarations create
// global functions, and that the code being evaluated doesn't have access to
// local variables within the scope where it's being called.
export const globalEval = eval;

// A reference to the global object.
// TODO The underscore is because it's conflicting with @types/node.
export const _global = globalEval("this");

_global["window"] = _global; // Create a window object.
import "./url";

_global["setTimeout"] = setTimeout;

const print = V8Worker2.print;

_global["console"] = {
// tslint:disable-next-line:no-any
log(...args: any[]): void {
print(stringifyArgs(args));
},

// tslint:disable-next-line:no-any
error(...args: any[]): void {
print("ERROR: " + stringifyArgs(args));
}
};

// tslint:disable-next-line:no-any
function stringifyArgs(args: any[]): string {
const out: string[] = [];
for (const a of args) {
if (typeof a === "string") {
out.push(a);
} else {
out.push(JSON.stringify(a));
}
}
return out.join(" ");
}
8 changes: 2 additions & 6 deletions os.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { main as pb } from "./msg.pb";
import { TypedArray, ModuleInfo } from "./types";
import { ModuleInfo } from "./types";
import { typedArrayToArrayBuffer } from "./util";

export function exit(code = 0): void {
sendMsgFromObject({
Expand Down Expand Up @@ -28,11 +29,6 @@ export function sourceCodeCache(
throwOnError(res);
}

function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
return ab as ArrayBuffer;
}

export function sendMsgFromObject(obj: pb.IMsg): null | pb.Msg {
const msg = pb.Msg.fromObject(obj);
const ui8 = pb.Msg.encode(msg).finish();
Expand Down
8 changes: 4 additions & 4 deletions runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import * as ts from "typescript";
import * as util from "./util";
import { log } from "./util";
import * as os from "./os";
import "./url";
import * as sourceMaps from "./v8_source_maps";
import { _global, globalEval } from "./globals";

const EOL = "\n";

Expand Down Expand Up @@ -141,10 +141,10 @@ function resolveModuleName(

function execute(fileName: string, outputCode: string): void {
util.assert(outputCode && outputCode.length > 0);
util._global["define"] = makeDefine(fileName);
_global["define"] = makeDefine(fileName);
outputCode += "\n//# sourceURL=" + fileName;
util.globalEval(outputCode);
util._global["define"] = null;
globalEval(outputCode);
_global["define"] = null;
}

// This is a singleton class. Use Compiler.instance() to access.
Expand Down
2 changes: 0 additions & 2 deletions timers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { _global } from "./util";
import { sendMsgFromObject } from "./os";

let nextTimerId = 1;
Expand Down Expand Up @@ -32,7 +31,6 @@ export function setTimeout(cb: TimerCallback, duration: number): number {
});
return timer.id;
}
_global["setTimeout"] = setTimeout;

export function timerReady(id: number, done: boolean): void {
const timer = timers.get(id);
Expand Down
46 changes: 6 additions & 40 deletions util.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
// If you use the eval function indirectly, by invoking it via a reference
// other than eval, as of ECMAScript 5 it works in the global scope rather than
// the local scope. This means, for instance, that function declarations create
// global functions, and that the code being evaluated doesn't have access to
// local variables within the scope where it's being called.
export const globalEval = eval;

// A reference to the global object.
// TODO The underscore is because it's conflicting with @types/node.
export const _global = globalEval("this");

_global["window"] = _global; // Create a window object.

const print = V8Worker2.print;

import { debug } from "./main";
import { TypedArray } from "./types";

// Internal logging for deno. Use the "debug" variable above to control
// output.
Expand All @@ -24,33 +10,13 @@ export function log(...args: any[]): void {
}
}

_global["console"] = {
// tslint:disable-next-line:no-any
log(...args: any[]): void {
print(stringifyArgs(args));
},

// tslint:disable-next-line:no-any
error(...args: any[]): void {
print("ERROR: " + stringifyArgs(args));
}
};

// tslint:disable-next-line:no-any
function stringifyArgs(args: any[]): string {
const out: string[] = [];
for (const a of args) {
if (typeof a === "string") {
out.push(a);
} else {
out.push(JSON.stringify(a));
}
}
return out.join(" ");
}

export function assert(cond: boolean, msg = "") {
if (!cond) {
throw Error("Assert fail. " + msg);
}
}

export function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
return ab as ArrayBuffer;
}

0 comments on commit 9a66216

Please sign in to comment.