@@ -870,7 +870,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
870870 }
871871
872872 // debug/helper to print the source of the JS callback
873- fn printFunc (self : Callback ) ! void {
873+ pub fn printFunc (self : Callback ) ! void {
874874 const scope = self .scope ;
875875 const value = self .func .castToFunction ().toValue ();
876876 const src = try valueToString (scope .call_arena , value , scope .isolate , scope .context );
@@ -2064,6 +2064,11 @@ fn Caller(comptime E: type, comptime State: type) type {
20642064 const last_js_parameter = params_to_map .len - 1 ;
20652065 var is_variadic = false ;
20662066
2067+ errdefer | err | if (std .log .logEnabled (.debug , .js )) {
2068+ const args_dump = self .dumpFunctionArgs (info ) catch "failed to serialize args" ;
2069+ log .debug ("Failed to call '{s}'. Error: {}.\n Args:\n {s}" , .{ named_function .full_name , err , args_dump });
2070+ };
2071+
20672072 {
20682073 // This is going to get complicated. If the last Zig paremeter
20692074 // is a slice AND the corresponding javascript parameter is
@@ -2582,6 +2587,22 @@ fn Caller(comptime E: type, comptime State: type) type {
25822587 fn isJsThis (comptime T : type ) bool {
25832588 return @typeInfo (T ) == .@"struct" and @hasDecl (T , "_JSTHIS_ID_KLUDGE" );
25842589 }
2590+
2591+ fn dumpFunctionArgs (self : * const Self , info : anytype ) ! []const u8 {
2592+ const isolate = self .isolate ;
2593+ const context = self .context ;
2594+ const arena = self .call_arena ;
2595+ const js_parameter_count = info .length ();
2596+
2597+ var arr : std .ArrayListUnmanaged (u8 ) = .{};
2598+ for (0.. js_parameter_count ) | i | {
2599+ const js_value = info .getArg (@intCast (i ));
2600+ const value_string = try valueToDetailString (arena , js_value , isolate , context );
2601+ const value_type = try jsStringToZig (arena , try js_value .typeOf (isolate ), isolate );
2602+ try std .fmt .format (arr .writer (arena ), "{d}: {s} ({s})\n " , .{ i + 1 , value_string , value_type });
2603+ }
2604+ return arr .items ;
2605+ }
25852606 };
25862607}
25872608
@@ -2858,19 +2879,28 @@ const TaggedAnyOpaque = struct {
28582879 subtype : ? SubType ,
28592880};
28602881
2882+ fn valueToDetailString (allocator : Allocator , value : v8.Value , isolate : v8.Isolate , context : v8.Context ) ! []u8 {
2883+ const str = try value .toDetailString (context );
2884+ return jsStringToZig (allocator , str , isolate );
2885+ }
2886+
28612887fn valueToString (allocator : Allocator , value : v8.Value , isolate : v8.Isolate , context : v8.Context ) ! []u8 {
2888+ const str = try value .toString (context );
2889+ return jsStringToZig (allocator , str , isolate );
2890+ }
2891+
2892+ fn valueToStringZ (allocator : Allocator , value : v8.Value , isolate : v8.Isolate , context : v8.Context ) ! [:0 ]u8 {
28622893 const str = try value .toString (context );
28632894 const len = str .lenUtf8 (isolate );
2864- const buf = try allocator .alloc (u8 , len );
2895+ const buf = try allocator .allocSentinel (u8 , len , 0 );
28652896 const n = str .writeUtf8 (isolate , buf );
28662897 std .debug .assert (n == len );
28672898 return buf ;
28682899}
28692900
2870- fn valueToStringZ (allocator : Allocator , value : v8.Value , isolate : v8.Isolate , context : v8.Context ) ! [:0 ]u8 {
2871- const str = try value .toString (context );
2901+ fn jsStringToZig (allocator : Allocator , str : v8.String , isolate : v8.Isolate ) ! []u8 {
28722902 const len = str .lenUtf8 (isolate );
2873- const buf = try allocator .allocSentinel (u8 , len , 0 );
2903+ const buf = try allocator .alloc (u8 , len );
28742904 const n = str .writeUtf8 (isolate , buf );
28752905 std .debug .assert (n == len );
28762906 return buf ;
0 commit comments