Skip to content

Commit

Permalink
fix console instanceof Console (denoland#2073)
Browse files Browse the repository at this point in the history
  • Loading branch information
justjavac authored and ry committed Apr 8, 2019
1 parent 1746a3a commit cdb72af
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions js/console.ts
Expand Up @@ -483,14 +483,18 @@ type PrintFunc = (x: string, isErr?: boolean) => void;

const countMap = new Map<string, number>();
const timerMap = new Map<string, number>();
export const isConsoleInstance = Symbol("isConsoleInstance");

export class Console {
indentLevel: number;
collapsedAt: number | null;
[isConsoleInstance]: boolean = false;

/** @internal */
constructor(private printFunc: PrintFunc) {
this.indentLevel = 0;
this.collapsedAt = null;
this[isConsoleInstance] = true;
}

/** Writes the arguments to stdout */
Expand Down Expand Up @@ -730,6 +734,10 @@ export class Console {
cursorTo(stdout, 0, 0);
clearScreenDown(stdout);
};

static [Symbol.hasInstance](instance: Console): boolean {
return instance[isConsoleInstance];
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions js/console_test.ts
Expand Up @@ -20,6 +20,11 @@ test(function consoleShouldBeANamespace() {
assertEquals(prototype2, Object.prototype);
});

test(function consoleHasRightInstance() {
assert(console instanceof Console);
assertEquals({} instanceof Console, false);
});

test(function consoleTestAssertShouldNotThrowError() {
console.assert(true);

Expand Down
1 change: 1 addition & 0 deletions js/globals.ts
Expand Up @@ -52,6 +52,7 @@ Object.freeze(window.Deno);
// by ObjectCreate(%ObjectPrototype%), instead of %ObjectPrototype%.
let console = Object.create({}) as consoleTypes.Console;
Object.assign(console, new consoleTypes.Console(core.print));
console[consoleTypes.isConsoleInstance] = true;

// Globally available functions and object instances.
window.atob = textEncoding.atob;
Expand Down

0 comments on commit cdb72af

Please sign in to comment.