-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move console to console.ts and better stringify
- Loading branch information
1 parent
71d7891
commit 72e7dad
Showing
3 changed files
with
127 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters