From d50f761c8f7907439b099157f4c6c3ea324c8bbd Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 30 Apr 2024 17:31:23 +0200 Subject: [PATCH] event: log listeners errors and continue execution --- src/netsurf.zig | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/netsurf.zig b/src/netsurf.zig index 0c4f3b8a5..aff0e1401 100644 --- a/src/netsurf.zig +++ b/src/netsurf.zig @@ -29,8 +29,11 @@ const c = @cImport({ const mimalloc = @import("mimalloc.zig"); const Callback = @import("jsruntime").Callback; +const CallbackResult = @import("jsruntime").CallbackResult; const EventToInterface = @import("events/event.zig").Event.toInterface; +const log = std.log.scoped(.netsurf); + // init initializes netsurf lib. // init starts a mimalloc heap arena for the netsurf session. The caller must // call deinit() to free the arena memory. @@ -534,13 +537,24 @@ const event_handler = struct { if (data) |d| { const func = event_handler_cbk(d); + // TODO get the allocator by another way? + var res = CallbackResult.init(func.nat_ctx.alloc); + defer res.deinit(); + if (event) |evt| { - func.call(.{ + func.trycall(.{ EventToInterface(evt) catch unreachable, - }) catch unreachable; + }, &res) catch {}; } else { - func.call(.{event}) catch unreachable; + func.trycall(.{event}, &res) catch {}; } + + // in case of function error, we log the result and the trace. + if (!res.success) { + log.info("event handler error: {s}", .{res.result orelse "unknown"}); + log.debug("{s}", .{res.stack orelse "no stack trace"}); + } + // NOTE: we can not call func.deinit here // b/c the handler can be called several times // either on this dispatch event or in anoter one