From 1a80bcb250c81a8959b8af816f499c22bca0db51 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 14 May 2018 01:30:56 -0400 Subject: [PATCH] Add console.log --- main.ts | 17 +++++++++++++++-- tsconfig.json | 5 +++-- util.ts | 23 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 util.ts diff --git a/main.ts b/main.ts index 44519489ab..153414a195 100644 --- a/main.ts +++ b/main.ts @@ -1,9 +1,22 @@ //import * as ts from "typescript"; import { main as pb } from "./msg.pb" +import "./util"; + + +function load(argv: string[]): void { + console.log("Load argv", argv); +} V8Worker2.recv((ab: ArrayBuffer) => { - let msg = pb.Msg.decode(new Uint8Array(ab)); - V8Worker2.print("msg.argv", msg.argv); + const msg = pb.Msg.decode(new Uint8Array(ab)); + switch (msg.kind) { + case pb.Msg.MsgKind.LOAD: + load(msg.argv); + break; + default: + console.log("Unknown message", msg); + break; + } }); V8Worker2.print("Hello"); diff --git a/tsconfig.json b/tsconfig.json index aa81406c3f..d0810f8301 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "allowJs": true, "module": "commonjs", "noImplicitAny": true, "sourceMap": true, @@ -7,7 +8,7 @@ "preserveConstEnums": true, "declaration": true, "target": "es5", - "lib": ["es2015", "dom"], + "lib": ["es2015"], "noEmit": true, "noUnusedLocals": true, "noImplicitReturns": true, @@ -19,5 +20,5 @@ "allowUnreachableCode": false, "experimentalDecorators": true }, - "include": ["*.ts"] + "include": ["*.ts", "*.js"] } diff --git a/util.ts b/util.ts new file mode 100644 index 0000000000..4675152255 --- /dev/null +++ b/util.ts @@ -0,0 +1,23 @@ +// 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. +const globalEval = eval; + +// A reference to the global object. +const _global = globalEval("this"); + +_global["console"] = { + log(...args: any[]): void { + const out: string[] = []; + for (let a of args) { + if (typeof(a) === "string") { + out.push(a); + } else { + out.push(JSON.stringify(a)); + } + } + V8Worker2.print(out.join(" ")); + } +};