From 9a6621659937c55c6005a1fa6ce9641a4ceff385 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 21 May 2018 18:53:53 -0400 Subject: [PATCH] Add globals.ts --- globals.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ os.ts | 8 ++------ runtime.ts | 8 ++++---- timers.ts | 2 -- util.ts | 46 ++++++---------------------------------------- 5 files changed, 56 insertions(+), 52 deletions(-) create mode 100644 globals.ts diff --git a/globals.ts b/globals.ts new file mode 100644 index 0000000000..f25cc710b1 --- /dev/null +++ b/globals.ts @@ -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(" "); +} diff --git a/os.ts b/os.ts index 3d3639dc32..82baac25df 100644 --- a/os.ts +++ b/os.ts @@ -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({ @@ -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(); diff --git a/runtime.ts b/runtime.ts index 57b556c216..046a1c6057 100644 --- a/runtime.ts +++ b/runtime.ts @@ -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"; @@ -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. diff --git a/timers.ts b/timers.ts index ed84c00e9d..6603b3d161 100644 --- a/timers.ts +++ b/timers.ts @@ -1,4 +1,3 @@ -import { _global } from "./util"; import { sendMsgFromObject } from "./os"; let nextTimerId = 1; @@ -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); diff --git a/util.ts b/util.ts index 0b4b9adbcb..c4fa032558 100644 --- a/util.ts +++ b/util.ts @@ -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. @@ -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; +}