From 72e7dade097eca82f4976ddfbbc8b9f184ca3bf1 Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Tue, 5 Jun 2018 00:27:18 -0700 Subject: [PATCH] Move console to console.ts and better stringify --- console.ts | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ globals.ts | 28 ++----------------- tests.ts | 43 ++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 26 deletions(-) create mode 100644 console.ts diff --git a/console.ts b/console.ts new file mode 100644 index 00000000000000..647e7e287b41a8 --- /dev/null +++ b/console.ts @@ -0,0 +1,82 @@ +class ConsoleContext { + seen = new Set<{}>(); +} + +// tslint:disable-next-line:no-any +function stringify(ctx: ConsoleContext, value: any): string { + switch (typeof value) { + case "string": + return `"${value}"`; + case "number": + case "boolean": + case "undefined": + return String(value); + case "function": + return "[Function]"; + case "object": + if (value === null) { + return "null"; + } + if (ctx.seen.has(value)) { + return "[Circular]"; + } + + ctx.seen.add(value); + + const keys = Object.keys(value); + const keyStrings = []; + for (const key of keys) { + keyStrings.push(`${key}: ${stringify(ctx, value[key])}`); + } + + ctx.seen.delete(value); + + if (keyStrings.length === 0) { + return "{}"; + } + + return `{ ${keyStrings.join(", ")} }`; + default: + return "[Not Implemented]"; + } +} + +// 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(stringify(new ConsoleContext(), a)); + } + } + return out.join(" "); +} + +const _print = V8Worker2.print; + +export class DConsole { + // tslint:disable-next-line:no-any + log(...args: any[]): void { + _print(stringifyArgs(args)); + } + + debug = this.log; + info = this.log; + dirxml = this.log; + + // tslint:disable-next-line:no-any + warn(...args: any[]): void { + _print("ERROR: " + stringifyArgs(args)); + } + + error = this.warn; + + // tslint:disable-next-line:no-any + assert(condition: boolean, ...args: any[]): void { + if (!condition) { + throw new Error("Assertion failed: " + stringifyArgs(args)); + } + } +} \ No newline at end of file diff --git a/globals.ts b/globals.ts index ed60bc46a74512..96d879c5477dcd 100644 --- a/globals.ts +++ b/globals.ts @@ -21,32 +21,8 @@ _global["setInterval"] = timer.setInterval; _global["clearTimeout"] = timer.clearTimer; _global["clearInterval"] = timer.clearTimer; -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(" "); -} +import { DConsole } from "./console"; +_global["console"] = new DConsole(); import { fetch } from "./fetch"; _global["fetch"] = fetch; diff --git a/tests.ts b/tests.ts index d4b188fdaff119..ada00aa8a5ac62 100644 --- a/tests.ts +++ b/tests.ts @@ -43,3 +43,46 @@ test(async function tests_writeFileSync() { const actual = dec.decode(dataRead); assertEqual("Hello", actual); }); + +test(function tests_console_assert() { + console.assert(true); + + let hasThrown = false; + try { + console.assert(false); + } catch { + hasThrown = true; + } + assertEqual(hasThrown, true); +}); + +test(function tests_console_stringify_circular() { + // tslint:disable-next-line:no-any + const nestedObj: any = { + num: 1, + bool: true, + method() {}, + un: undefined, + nu: null, + }; + + const circularObj = { + num: 2, + bool: false, + method() {}, + un: undefined, + nu: null, + nested: nestedObj, + emptyObj: {}, + }; + + nestedObj.o = circularObj; + + try { + console.log(nestedObj); + } catch { + throw new Error( + "Expected no crash on circular object" + ); + } +});