@@ -3392,8 +3392,8 @@ const TaggedAnyOpaque = struct {
33923392
33933393fn 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
34123422fn 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+
34343480fn stackForLogs (arena : Allocator , isolate : v8.Isolate ) ! ? []const u8 {
34353481 std .debug .assert (builtin .mode == .Debug );
34363482
0 commit comments