summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author迷渡 <justjavac@gmail.com>2019-04-03 20:37:01 +0800
committerRyan Dahl <ry@tinyclouds.org>2019-04-03 08:37:01 -0400
commit6463a75b44a4f7aad6ffe04d0c5ae073fb5ca137 (patch)
tree97e573ca8485863bcdb30de5636b0aa27e61fe37
parent917e68f30f25c22a87d0f8b8a2f39d27bc8ca906 (diff)
web-compatibility console (#2042)
-rw-r--r--js/console_test.ts12
-rw-r--r--js/globals.ts9
2 files changed, 19 insertions, 2 deletions
diff --git a/js/console_test.ts b/js/console_test.ts
index 39ccad843..ed2f37548 100644
--- a/js/console_test.ts
+++ b/js/console_test.ts
@@ -10,6 +10,16 @@ function stringify(...args: unknown[]): string {
return stringifyArgs(args).replace(/\n$/, "");
}
+// test cases from web-platform-tests
+// via https://github.com/web-platform-tests/wpt/blob/master/console/console-is-a-namespace.any.js
+test(function consoleShouldBeANamespace() {
+ const prototype1 = Object.getPrototypeOf(console);
+ const prototype2 = Object.getPrototypeOf(prototype1);
+
+ assertEquals(Object.getOwnPropertyNames(prototype1).length, 0);
+ assertEquals(prototype2, Object.prototype);
+});
+
test(function consoleTestAssertShouldNotThrowError() {
console.assert(true);
@@ -123,7 +133,7 @@ test(function consoleTestStringifyCircular() {
assertEquals(stringify(JSON), "{}");
assertEquals(
stringify(console),
- "Console { printFunc, log, debug, info, dir, warn, error, assert, count, countReset, table, time, timeLog, timeEnd, group, groupCollapsed, groupEnd, clear, indentLevel, collapsedAt }"
+ "{ printFunc, log, debug, info, dir, warn, error, assert, count, countReset, table, time, timeLog, timeEnd, group, groupCollapsed, groupEnd, clear, indentLevel, collapsedAt }"
);
// test inspect is working the same
assertEquals(inspect(nestedObj), nestedObjExpected);
diff --git a/js/globals.ts b/js/globals.ts
index 56956b4ad..93fe6dbe7 100644
--- a/js/globals.ts
+++ b/js/globals.ts
@@ -46,13 +46,20 @@ window.window = window;
window.Deno = deno;
Object.freeze(window.Deno);
+// ref https://console.spec.whatwg.org/#console-namespace
+// For historical web-compatibility reasons, the namespace object for
+// console must have as its [[Prototype]] an empty object, created as if
+// by ObjectCreate(%ObjectPrototype%), instead of %ObjectPrototype%.
+let console = Object.create({}) as consoleTypes.Console;
+Object.assign(console, new consoleTypes.Console(core.print));
+
// Globally available functions and object instances.
window.atob = textEncoding.atob;
window.btoa = textEncoding.btoa;
window.fetch = fetchTypes.fetch;
window.clearTimeout = timers.clearTimer;
window.clearInterval = timers.clearTimer;
-window.console = new consoleTypes.Console(core.print);
+window.console = console;
window.setTimeout = timers.setTimeout;
window.setInterval = timers.setInterval;
window.location = (undefined as unknown) as domTypes.Location;