Skip to content

Commit 7a0e7ff

Browse files
committed
Improve JS value printing
Don't error on JSON.stringify failure (likely caused by circular reference). In debug mode, try to print [slightly] more meaningful value representation when default serialization results in [object Object].
1 parent 81fb71b commit 7a0e7ff

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
.hash = "tigerbeetle_io-0.0.0-ViLgxpyRBAB5BMfIcj3KMXfbJzwARs9uSl8aRy2OXULd",
1414
},
1515
.v8 = .{
16-
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/46ddf8c0a2861a4e5717e6f8d0d81a17e42fa0c9.tar.gz",
17-
.hash = "v8-0.0.0-xddH6865AwDiDnu-HjMsqm9wXvP9OZOh_4clh_67iQsq",
16+
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/dd087771378ea854452bcb010309fa9ffe5a9cac.tar.gz",
17+
.hash = "v8-0.0.0-xddH66e8AwBL3O_A8yWQYQIyfMbKHFNVQr_NqM6YjU11",
1818
},
1919
//.v8 = .{ .path = "../zig-v8-fork" },
2020
//.tigerbeetle_io = .{ .path = "../tigerbeetle-io" },

src/runtime/js.zig

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3392,8 +3392,8 @@ const TaggedAnyOpaque = struct {
33923392

33933393
fn valueToDetailString(arena: Allocator, value: v8.Value, isolate: v8.Isolate, v8_context: v8.Context) ![]u8 {
33943394
var str: ?v8.String = null;
3395-
if (value.isObject() and !value.isFunction()) {
3396-
str = try v8.Json.stringify(v8_context, value, null);
3395+
if (value.isObject() and !value.isFunction()) blk: {
3396+
str = v8.Json.stringify(v8_context, value, null) catch break :blk;
33973397

33983398
if (str.?.lenUtf8(isolate) == 2) {
33993399
// {} isn't useful, null this so that we can get the toDetailString
@@ -3406,10 +3406,24 @@ fn valueToDetailString(arena: Allocator, value: v8.Value, isolate: v8.Isolate, v
34063406
str = try value.toDetailString(v8_context);
34073407
}
34083408

3409-
return jsStringToZig(arena, str.?, isolate);
3409+
const s = try jsStringToZig(arena, str.?, isolate);
3410+
if (comptime builtin.mode == .Debug) {
3411+
if (std.mem.eql(u8, s, "[object Object]")) {
3412+
if (debugValueToString(arena, value.castTo(v8.Object), isolate, v8_context)) |ds| {
3413+
return ds;
3414+
} else |err| {
3415+
log.err(.js, "debug serialize value", .{.err = err});
3416+
}
3417+
}
3418+
}
3419+
return s;
34103420
}
34113421

34123422
fn valueToString(allocator: Allocator, value: v8.Value, isolate: v8.Isolate, v8_context: v8.Context) ![]u8 {
3423+
if (value.isSymbol()) {
3424+
// symbol's can't be converted to a string
3425+
return allocator.dupe(u8, "$Symbol");
3426+
}
34133427
const str = try value.toString(v8_context);
34143428
return jsStringToZig(allocator, str, isolate);
34153429
}
@@ -3431,6 +3445,38 @@ fn jsStringToZig(allocator: Allocator, str: v8.String, isolate: v8.Isolate) ![]u
34313445
return buf;
34323446
}
34333447

3448+
fn debugValueToString(arena: Allocator, js_obj: v8.Object, isolate: v8.Isolate, v8_context: v8.Context) ![]u8 {
3449+
if (comptime builtin.mode != .Debug) {
3450+
@compileError("debugValue can only be called in debug mode");
3451+
}
3452+
3453+
var arr: std.ArrayListUnmanaged(u8) = .empty;
3454+
var writer = arr.writer(arena);
3455+
3456+
const names_arr = js_obj.getOwnPropertyNames(v8_context);
3457+
const names_obj = names_arr.castTo(v8.Object);
3458+
const len = names_arr.length();
3459+
3460+
try writer.writeAll("(JSON.stringify failed, dumping top-level fields)\n");
3461+
for (0..len) |i| {
3462+
const field_name = try names_obj.getAtIndex(v8_context, @intCast(i));
3463+
const field_value = try js_obj.getValue(v8_context, field_name);
3464+
const name = try valueToString(arena, field_name, isolate, v8_context);
3465+
const value = try valueToString(arena, field_value, isolate, v8_context);
3466+
try writer.writeAll(name);
3467+
try writer.writeAll(": ");
3468+
if (std.mem.indexOfAny(u8, value, &std.ascii.whitespace) == null) {
3469+
try writer.writeAll(value);
3470+
} else {
3471+
try writer.writeByte('"');
3472+
try writer.writeAll(value);
3473+
try writer.writeByte('"');
3474+
}
3475+
try writer.writeByte(' ');
3476+
}
3477+
return arr.items;
3478+
}
3479+
34343480
fn stackForLogs(arena: Allocator, isolate: v8.Isolate) !?[]const u8 {
34353481
std.debug.assert(builtin.mode == .Debug);
34363482

0 commit comments

Comments
 (0)