@@ -29,8 +29,11 @@ const c = @cImport({
2929const mimalloc = @import ("mimalloc.zig" );
3030
3131const Callback = @import ("jsruntime" ).Callback ;
32+ const CallbackResult = @import ("jsruntime" ).CallbackResult ;
3233const EventToInterface = @import ("events/event.zig" ).Event .toInterface ;
3334
35+ const log = std .log .scoped (.netsurf );
36+
3437// init initializes netsurf lib.
3538// init starts a mimalloc heap arena for the netsurf session. The caller must
3639// call deinit() to free the arena memory.
@@ -534,13 +537,24 @@ const event_handler = struct {
534537 if (data ) | d | {
535538 const func = event_handler_cbk (d );
536539
540+ // TODO get the allocator by another way?
541+ var res = CallbackResult .init (func .nat_ctx .alloc );
542+ defer res .deinit ();
543+
537544 if (event ) | evt | {
538- func .call (.{
545+ func .trycall (.{
539546 EventToInterface (evt ) catch unreachable ,
540- }) catch unreachable ;
547+ }, & res ) catch {} ;
541548 } else {
542- func .call (.{event }) catch unreachable ;
549+ func .trycall (.{event }, & res ) catch {} ;
543550 }
551+
552+ // in case of function error, we log the result and the trace.
553+ if (! res .success ) {
554+ log .info ("event handler error: {s}" , .{res .result orelse "unknown" });
555+ log .debug ("{s}" , .{res .stack orelse "no stack trace" });
556+ }
557+
544558 // NOTE: we can not call func.deinit here
545559 // b/c the handler can be called several times
546560 // either on this dispatch event or in anoter one
0 commit comments