From cdb72afd8d91978573f0fa897844aee853983b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=B7=E6=B8=A1?= Date: Mon, 8 Apr 2019 21:25:01 +0800 Subject: [PATCH] fix `console instanceof Console` (#2073) --- js/console.ts | 8 ++++++++ js/console_test.ts | 5 +++++ js/globals.ts | 1 + 3 files changed, 14 insertions(+) diff --git a/js/console.ts b/js/console.ts index 10be30aceb..c16ea0302f 100644 --- a/js/console.ts +++ b/js/console.ts @@ -483,14 +483,18 @@ type PrintFunc = (x: string, isErr?: boolean) => void; const countMap = new Map(); const timerMap = new Map(); +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 */ @@ -730,6 +734,10 @@ export class Console { cursorTo(stdout, 0, 0); clearScreenDown(stdout); }; + + static [Symbol.hasInstance](instance: Console): boolean { + return instance[isConsoleInstance]; + } } /** diff --git a/js/console_test.ts b/js/console_test.ts index 7bedcf973c..a4ff8a4daa 100644 --- a/js/console_test.ts +++ b/js/console_test.ts @@ -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); diff --git a/js/globals.ts b/js/globals.ts index 93fe6dbe73..4bb40fdb53 100644 --- a/js/globals.ts +++ b/js/globals.ts @@ -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;