event: log listeners errors and continue execution

This commit is contained in:
Pierre Tachoire
2024-04-30 17:31:23 +02:00
parent 23dafb0f12
commit d50f761c8f

View File

@@ -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