Repository: denoland/deno_core
Pull request #572 (2024-02-12 16:30:43)

fix(core): fix logging of Uint8Array


Patch:
diff --git a/core/01_core.js b/core/01_core.js
index c1b2a89..7772148 100644
--- a/core/01_core.js
+++ b/core/01_core.js
@@ -19,7 +19,7 @@
     TypedArrayPrototypeGetLength,
     TypedArrayPrototypeJoin,
     TypedArrayPrototypeSlice,
-    TypedArrayPrototypeSymbolToStringTag,
+    TypedArrayPrototypeGetSymbolToStringTag,
     TypeError,
   } = window.__bootstrap.primordials;
   const {
@@ -356,41 +356,45 @@
   // It's not fully fledged and is meant to make debugging slightly easier when working with
   // only `deno_core` crate.
   class CoreConsole {
-    #stringify = (arg) => {
-      if (
-        typeof arg === "string" || typeof arg === "boolean" ||
-        typeof arg === "number" || arg === null || arg === undefined
-      ) {
-        return arg;
-      } else if (TypedArrayPrototypeSymbolToStringTag(arg) === "Uint8Array") {
-        `Uint8Array(${TypedArrayPrototypeGetLength(arg)}) [${
-          TypedArrayPrototypeJoin(TypedArrayPrototypeSlice(arg, 0, 10), ", ")
-        }]`;
-      }
-      return JSON.stringify(arg, undefined, 2);
-    };
-
     log = (...args) => {
-      const stringifiedArgs = args.map(this.#stringify);
-      op_print(`${stringifiedArgs.join(" ")}\n`, false);
+      op_print(`${consoleStringify(...args)}\n`, false);
     };
 
     debug = (...args) => {
-      const stringifiedArgs = args.map(this.#stringify);
-      op_print(`${stringifiedArgs.join(" ")}\n`, false);
+      op_print(`${consoleStringify(...args)}\n`, false);
     };
 
     warn = (...args) => {
-      const stringifiedArgs = args.map(this.#stringify);
-      op_print(`${stringifiedArgs.join(" ")}\n`, false);
+      op_print(`${consoleStringify(...args)}\n`, false);
     };
 
     error = (...args) => {
-      const stringifiedArgs = args.map(this.#stringify);
-      op_print(`${stringifiedArgs.join(" ")}\n`, true);
+      op_print(`${consoleStringify(...args)}\n`, false);
     };
   }
 
+  const consoleStringify = (...args) => args.map(consoleStringifyArg).join(" ");
+
+  const consoleStringifyArg = (arg) => {
+    if (
+      typeof arg === "string" || typeof arg === "boolean" ||
+      typeof arg === "number" || arg === null || arg === undefined
+    ) {
+      return arg;
+    }
+    const tag = TypedArrayPrototypeGetSymbolToStringTag(arg);
+    if (op_is_typed_array(arg)) {
+      return `${tag}(${TypedArrayPrototypeGetLength(arg)}) [${
+        TypedArrayPrototypeJoin(TypedArrayPrototypeSlice(arg, 0, 10), ", ")
+      }]`;
+    }
+    if (tag !== undefined) {
+      tag + " " + JSON.stringify(arg, undefined, 2);
+    } else {
+      return JSON.stringify(arg, undefined, 2);
+    }
+  };
+
   const v8Console = globalThis.console;
   const coreConsole = new CoreConsole();
   globalThis.console = coreConsole;
@@ -570,6 +574,7 @@
     unrefOpPromise,
     setReportExceptionCallback,
     setPromiseHooks,
+    consoleStringify,
     close,
     tryClose,
     read,


Test patch:
diff --git a/testing/checkin/runtime/console.ts b/testing/checkin/runtime/console.ts
index cabf3db..0df4701 100644
--- a/testing/checkin/runtime/console.ts
+++ b/testing/checkin/runtime/console.ts
@@ -1,13 +1,14 @@
 // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
 
 import { op_log_debug, op_log_info } from "ext:core/ops";
+import { core } from "ext:core/mod.js";
 
 export const console = {
   debug(...args: string[]) {
-    op_log_debug(args.join(","));
+    op_log_debug(core.consoleStringify(...args));
   },
 
   log(...args: string[]) {
-    op_log_info(args.join(","));
+    op_log_info(core.consoleStringify(...args));
   },
 };
diff --git a/testing/integration/builtin_console_test/builtin_console_test.out b/testing/integration/builtin_console_test/builtin_console_test.out
new file mode 100644
index 0000000..67b45bf
--- /dev/null
+++ b/testing/integration/builtin_console_test/builtin_console_test.out
@@ -0,0 +1,12 @@
+Hello, world
+[
+  1,
+  2,
+  3
+]
+{
+  "foo": "bar"
+}
+Uint8Array(3) [1, 2, 3]
+Uint8Array(1024) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
+Uint32Array(3) [1, 2, 3]
diff --git a/testing/integration/builtin_console_test/builtin_console_test.ts b/testing/integration/builtin_console_test/builtin_console_test.ts
new file mode 100644
index 0000000..702cdeb
--- /dev/null
+++ b/testing/integration/builtin_console_test/builtin_console_test.ts
@@ -0,0 +1,7 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+console.log("Hello, world");
+console.log([1, 2, 3]);
+console.log({ "foo": "bar" });
+console.log(new Uint8Array([1, 2, 3]));
+console.log(new Uint8Array(1024));
+console.log(new Uint32Array([1, 2, 3]));
diff --git a/testing/lib.rs b/testing/lib.rs
index f0c95f4..a591593 100644
--- a/testing/lib.rs
+++ b/testing/lib.rs
@@ -49,6 +49,7 @@ unit_test!(
 // Test the load and run of an entire file within the `checkin` infrastructure.
 // These files are loaded from the system/ dir.
 integration_test!(
+  builtin_console_test,
   dyn_import_circular,
   dyn_import_op,
   error_async_stack,