Skip to content

Commit

Permalink
Move console to console.ts and better stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo committed Jun 5, 2018
1 parent 71d7891 commit 72e7dad
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 26 deletions.
82 changes: 82 additions & 0 deletions console.ts
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
28 changes: 2 additions & 26 deletions globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
});

0 comments on commit 72e7dad

Please sign in to comment.