summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-05-21 18:53:53 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-05-21 18:53:53 -0400
commit9a6621659937c55c6005a1fa6ce9641a4ceff385 (patch)
tree723763263c610069635db80252d9e638a28052b0
parent8e2e17cdbe02847b19d8bc2002ba713d18e291b9 (diff)
Add globals.ts
-rw-r--r--globals.ts44
-rw-r--r--os.ts8
-rw-r--r--runtime.ts8
-rw-r--r--timers.ts2
-rw-r--r--util.ts46
5 files changed, 56 insertions, 52 deletions
diff --git a/globals.ts b/globals.ts
new file mode 100644
index 000000000..f25cc710b
--- /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 3d3639dc3..82baac25d 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 57b556c21..046a1c605 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 ed84c00e9..6603b3d16 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 0b4b9adbc..c4fa03255 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;
+}