Skip to content

Commit fae018b

Browse files
committed
Make Callback.printFunc public
When calling a Zig function from JS fails due to a parameter type error, log.debug information about the function and parameters.
1 parent b2605dd commit fae018b

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/runtime/js.zig

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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: {}.\nArgs:\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 valueToString(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

@@ -2859,18 +2880,22 @@ const TaggedAnyOpaque = struct {
28592880
};
28602881

28612882
fn valueToString(allocator: Allocator, value: v8.Value, isolate: v8.Isolate, context: v8.Context) ![]u8 {
2883+
const str = try value.toString(context);
2884+
return jsStringToZig(allocator, str, isolate);
2885+
}
2886+
2887+
fn valueToStringZ(allocator: Allocator, value: v8.Value, isolate: v8.Isolate, context: v8.Context) ![:0]u8 {
28622888
const str = try value.toString(context);
28632889
const len = str.lenUtf8(isolate);
2864-
const buf = try allocator.alloc(u8, len);
2890+
const buf = try allocator.allocSentinel(u8, len, 0);
28652891
const n = str.writeUtf8(isolate, buf);
28662892
std.debug.assert(n == len);
28672893
return buf;
28682894
}
28692895

2870-
fn valueToStringZ(allocator: Allocator, value: v8.Value, isolate: v8.Isolate, context: v8.Context) ![:0]u8 {
2871-
const str = try value.toString(context);
2896+
fn jsStringToZig(allocator: Allocator, str: v8.String, isolate: v8.Isolate) ![]u8 {
28722897
const len = str.lenUtf8(isolate);
2873-
const buf = try allocator.allocSentinel(u8, len, 0);
2898+
const buf = try allocator.alloc(u8, len);
28742899
const n = str.writeUtf8(isolate, buf);
28752900
std.debug.assert(n == len);
28762901
return buf;

0 commit comments

Comments
 (0)