From bc114fb9d3cceeabc02c5cffa17bb3821b115c3f Mon Sep 17 00:00:00 2001 From: liqiang <974923609@qq.com> Date: Thu, 30 Nov 2023 12:07:03 +0800 Subject: [PATCH] fix console.timeLog (#7089) * fix console.timeLog * fix console.timeLog can log arguments * console timeLog test use bunEnv Co-authored-by: Ashcon Partovi * fix console timeLog test * Update console.timeLog Co-authored-by: dave caruso * Update test/js/web/console/console-timeLog.js Co-authored-by: Radhi Rasho <54078496+RadhiRasho@users.noreply.github.com> * Update console-timeLog.js * fix console-timeLog.js test * fix timeLog tests due to trailing comma PR --------- Co-authored-by: Ashcon Partovi Co-authored-by: dave caruso Co-authored-by: Radhi Rasho <54078496+RadhiRasho@users.noreply.github.com> --- src/bun.js/WebKit | 2 +- src/bun.js/bindings/ZigConsoleClient.cpp | 13 ++++++-- src/bun.js/bindings/exports.zig | 32 +++++++++++++++---- src/bun.js/bindings/headers.h | 2 +- .../web/console/console-timeLog.expected.txt | 18 +++++++++++ test/js/web/console/console-timeLog.js | 13 ++++++++ test/js/web/console/console-timeLog.test.ts | 17 ++++++++++ 7 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 test/js/web/console/console-timeLog.expected.txt create mode 100644 test/js/web/console/console-timeLog.js create mode 100644 test/js/web/console/console-timeLog.test.ts diff --git a/src/bun.js/WebKit b/src/bun.js/WebKit index 63d0e18c06..0aa1f6dfc9 160000 --- a/src/bun.js/WebKit +++ b/src/bun.js/WebKit @@ -1 +1 @@ -Subproject commit 63d0e18c06399180e9b3fb63d2b9132af5e79ed4 +Subproject commit 0aa1f6dfc9b48be1e89b8f709fd44e3c88feaf33 diff --git a/src/bun.js/bindings/ZigConsoleClient.cpp b/src/bun.js/bindings/ZigConsoleClient.cpp index 2cca989d92..d151d6fced 100644 --- a/src/bun.js/bindings/ZigConsoleClient.cpp +++ b/src/bun.js/bindings/ZigConsoleClient.cpp @@ -86,7 +86,16 @@ void Zig::ConsoleClient::timeLog(JSGlobalObject* globalObject, const String& lab Ref&& arguments) { auto input = label.tryGetUTF8().value(); - Zig__ConsoleClient__timeLog(this->m_client, globalObject, reinterpret_cast(input.data()), input.length(), arguments.ptr()); + + auto args = arguments.ptr(); + JSC__JSValue jsArgs[255]; + auto count = std::min(args->argumentCount(), (size_t)255); + for (size_t i = 0; i < count; i++) { + auto val = args->argumentAt(i); + jsArgs[i] = JSC::JSValue::encode(val); + } + + Zig__ConsoleClient__timeLog(this->m_client, globalObject, reinterpret_cast(input.data()), input.length(), jsArgs, count); } void Zig::ConsoleClient::timeEnd(JSGlobalObject* globalObject, const String& label) { @@ -100,4 +109,4 @@ void Zig::ConsoleClient::timeStamp(JSGlobalObject* globalObject, Ref&&) {} void Zig::ConsoleClient::recordEnd(JSGlobalObject*, Ref&&) {} void Zig::ConsoleClient::screenshot(JSGlobalObject*, Ref&&) {} -void Zig::ConsoleClient::warnUnimplemented(const String& method) {} \ No newline at end of file +void Zig::ConsoleClient::warnUnimplemented(const String& method) {} diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index 1ec3be516c..5db1382286 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -3264,13 +3264,14 @@ pub const ZigConsoleClient = struct { // console _: ZigConsoleClient.Type, // global - _: *JSGlobalObject, + global: *JSGlobalObject, // chars chars: [*]const u8, // len len: usize, // args - _: *ScriptArguments, + args: [*]JSValue, + args_len: usize, ) callconv(.C) void { if (!pending_time_logs_loaded) { return; @@ -3282,13 +3283,32 @@ pub const ZigConsoleClient = struct { // then display it in milliseconds Output.printElapsed(@as(f64, @floatFromInt(value.read() / std.time.ns_per_us)) / std.time.us_per_ms); switch (len) { - 0 => Output.printErrorln("\n", .{}), - else => Output.printErrorln(" {s}", .{chars[0..len]}), + 0 => {}, + else => Output.printError(" {s}", .{chars[0..len]}), } - Output.flush(); - // TODO: print the arguments + // print the arguments + var fmt = ZigConsoleClient.Formatter{ + .remaining_values = &[_]JSValue{}, + .globalThis = global, + .ordered_properties = false, + .quote_strings = false, + }; + var console = global.bunVM().console; + var writer = console.error_writer.writer(); + const Writer = @TypeOf(writer); + for (args[0..args_len]) |arg| { + const tag = ZigConsoleClient.Formatter.Tag.get(arg, global); + _ = writer.write(" ") catch 0; + if (Output.enable_ansi_colors_stderr) { + fmt.format(tag, Writer, writer, arg, global, true); + } else { + fmt.format(tag, Writer, writer, arg, global, false); + } + } + _ = writer.write("\n") catch 0; + writer.context.flush() catch {}; } pub fn profile( // console diff --git a/src/bun.js/bindings/headers.h b/src/bun.js/bindings/headers.h index a4c91cd968..944996a9ee 100644 --- a/src/bun.js/bindings/headers.h +++ b/src/bun.js/bindings/headers.h @@ -764,7 +764,7 @@ ZIG_DECL void Zig__ConsoleClient__screenshot(void* arg0, JSC__JSGlobalObject* ar ZIG_DECL void Zig__ConsoleClient__takeHeapSnapshot(void* arg0, JSC__JSGlobalObject* arg1, const unsigned char* arg2, size_t arg3); ZIG_DECL void Zig__ConsoleClient__time(void* arg0, JSC__JSGlobalObject* arg1, const unsigned char* arg2, size_t arg3); ZIG_DECL void Zig__ConsoleClient__timeEnd(void* arg0, JSC__JSGlobalObject* arg1, const unsigned char* arg2, size_t arg3); -ZIG_DECL void Zig__ConsoleClient__timeLog(void* arg0, JSC__JSGlobalObject* arg1, const unsigned char* arg2, size_t arg3, ScriptArguments* arg4); +ZIG_DECL void Zig__ConsoleClient__timeLog(void* arg0, JSC__JSGlobalObject* arg1, const unsigned char* arg2, size_t arg3, JSC__JSValue* arg4, size_t arg5); ZIG_DECL void Zig__ConsoleClient__timeStamp(void* arg0, JSC__JSGlobalObject* arg1, ScriptArguments* arg2); #endif diff --git a/test/js/web/console/console-timeLog.expected.txt b/test/js/web/console/console-timeLog.expected.txt new file mode 100644 index 0000000000..464475e83a --- /dev/null +++ b/test/js/web/console/console-timeLog.expected.txt @@ -0,0 +1,18 @@ +[0.00ms] label +[0.06ms] label Hello World! +[0.09ms] label a %s b c d +[0.11ms] label 0 -0 123 -123 123.567 -123.567 Infinity -Infinity +[0.14ms] label true false +[0.15ms] label null undefined +[0.17ms] label Symbol(Symbol Description) +[0.22ms] label 2000-06-27T02:24:34.304Z +[0.29ms] label [ 123, 456, 789 ] +[0.34ms] label { + name: "foo", +} +[0.37ms] label { + a: 123, + b: 456, + c: 789, +} +[0.39ms] label diff --git a/test/js/web/console/console-timeLog.js b/test/js/web/console/console-timeLog.js new file mode 100644 index 0000000000..9c2bac340a --- /dev/null +++ b/test/js/web/console/console-timeLog.js @@ -0,0 +1,13 @@ +console.time("label"); +console.timeLog("label"); +console.timeLog("label", "Hello World!"); +console.timeLog("label", "a %s b", "c", "d"); +console.timeLog("label", 0, -0, 123, -123, 123.567, -123.567, Infinity, -Infinity); +console.timeLog("label", true, false); +console.timeLog("label", null, undefined); +console.timeLog("label", Symbol("Symbol Description")); +console.timeLog("label", new Date(Math.pow(2, 34) * 56)); +console.timeLog("label", [123, 456, 789]); +console.timeLog("label", { name: "foo" }); +console.timeLog("label", { a: 123, b: 456, c: 789 }); +console.timeEnd("label"); diff --git a/test/js/web/console/console-timeLog.test.ts b/test/js/web/console/console-timeLog.test.ts new file mode 100644 index 0000000000..81513ed112 --- /dev/null +++ b/test/js/web/console/console-timeLog.test.ts @@ -0,0 +1,17 @@ +import { file, spawn } from "bun"; +import { expect, it } from "bun:test"; +import { bunExe, bunEnv } from "harness"; + +it("should log to console correctly", async () => { + const { stderr, exited } = spawn({ + cmd: [bunExe(), import.meta.dir + "/console-timeLog.js"], + stdin: null, + stdout: "pipe", + stderr: "pipe", + env: bunEnv, + }); + expect(await exited).toBe(0); + const outText = await new Response(stderr).text(); + const expectedText = await new Response(file(import.meta.dir + "/console-timeLog.expected.txt")).text(); + expect(outText.replace(/^\[.+?s\] /gm, "")).toBe(expectedText.replace(/^\[.+?s\] /gm, "")); +});