From 9f97725894fc1ad2f1f134e87d27f4a8db16a925 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Fri, 16 May 2025 20:23:07 +0200 Subject: [PATCH 1/2] module: report module's evaluation error --- build.zig.zon | 4 ++-- src/browser/page.zig | 12 +++++++++++- src/runtime/js.zig | 30 +++++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index 58f5508f..b3701d90 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -13,8 +13,8 @@ .hash = "tigerbeetle_io-0.0.0-ViLgxpyRBAB5BMfIcj3KMXfbJzwARs9uSl8aRy2OXULd", }, .v8 = .{ - .url = "https://github.com/lightpanda-io/zig-v8-fork/archive/6f1ee74a0e7002ea3568e337ab716c1e75c53769.tar.gz", - .hash = "v8-0.0.0-xddH6z2yAwCOPUGmy1IgXysI1yWt8ftd2Z3D5zp0I9tV", + .url = "https://github.com/lightpanda-io/zig-v8-fork/archive/240140e5b3e5a8e5e51cbdd36bc120bf28ae5c31.tar.gz", + .hash = "v8-0.0.0-xddH64eyAwBcX7e2x5tv9MhT0MgQbshP2rb19blo06Db", }, //.v8 = .{ .path = "../zig-v8-fork" }, //.tigerbeetle_io = .{ .path = "../tigerbeetle-io" }, diff --git a/src/browser/page.zig b/src/browser/page.zig index ffed1cd2..e124a8a4 100644 --- a/src/browser/page.zig +++ b/src/browser/page.zig @@ -638,7 +638,17 @@ pub const Page = struct { const src = self.src orelse "inline"; const res = switch (self.kind) { .javascript => page.scope.exec(body, src), - .module => page.scope.module(body, src), + .module => blk: { + switch (try page.scope.module(body, src)) { + .value => |v| break :blk v, + .exception => |e| { + if (try e.exception(page.arena)) |msg| { + log.info("eval module {s}: {s}", .{ src, msg }); + } + return error.JsErr; + }, + } + }, } catch { if (try try_catch.err(page.arena)) |msg| { log.info("eval script {s}: {s}", .{ src, msg }); diff --git a/src/runtime/js.zig b/src/runtime/js.zig index d3d68873..a2e1fd41 100644 --- a/src/runtime/js.zig +++ b/src/runtime/js.zig @@ -610,13 +610,12 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { // compile and eval a JS module // It doesn't wait for callbacks execution - pub fn module(self: *Scope, src: []const u8, name: []const u8) !Value { + pub fn module(self: *Scope, src: []const u8, name: []const u8) !union(enum) { value: Value, exception: Exception } { const context = self.context; const m = try compileModule(self.isolate, src, name); // instantiate - // TODO handle ResolveModuleCallback parameters to load module's - // dependencies. + // resolveModuleCallback loads module's dependencies. const ok = m.instantiate(context, resolveModuleCallback) catch { return error.ExecutionError; }; @@ -626,8 +625,18 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { } // evaluate - const value = m.evaluate(context) catch return error.ExecutionError; - return self.createValue(value); + const value = m.evaluate(context) catch { + return .{ .exception = self.createException(m.getException()) }; + }; + return .{ .value = self.createValue(value) }; + } + + // Wrap a v8.Exception + fn createException(self: *const Scope, e: v8.Value) Exception { + return .{ + .inner = e, + .scope = self, + }; } // Wrap a v8.Value, largely so that we can provide a convenient @@ -1133,6 +1142,17 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { pub const RemoteObject = v8.RemoteObject; + pub const Exception = struct { + inner: v8.Value, + scope: *const Scope, + + // the caller needs to deinit the string returned + pub fn exception(self: Exception, allocator: Allocator) !?[]const u8 { + const scope = self.scope; + return try valueToString(allocator, self.inner, scope.isolate, scope.context); + } + }; + pub const Value = struct { value: v8.Value, scope: *const Scope, From 2261eac288a8d5fa21db74227526a71ddcd3be51 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Sat, 17 May 2025 10:57:25 +0200 Subject: [PATCH 2/2] expection: fix non-nullable return --- src/browser/page.zig | 7 ++++--- src/runtime/js.zig | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/browser/page.zig b/src/browser/page.zig index e124a8a4..a193d5b0 100644 --- a/src/browser/page.zig +++ b/src/browser/page.zig @@ -642,9 +642,10 @@ pub const Page = struct { switch (try page.scope.module(body, src)) { .value => |v| break :blk v, .exception => |e| { - if (try e.exception(page.arena)) |msg| { - log.info("eval module {s}: {s}", .{ src, msg }); - } + log.info("eval module {s}: {s}", .{ + src, + try e.exception(page.arena), + }); return error.JsErr; }, } diff --git a/src/runtime/js.zig b/src/runtime/js.zig index a2e1fd41..90aeb580 100644 --- a/src/runtime/js.zig +++ b/src/runtime/js.zig @@ -1147,7 +1147,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type { scope: *const Scope, // the caller needs to deinit the string returned - pub fn exception(self: Exception, allocator: Allocator) !?[]const u8 { + pub fn exception(self: Exception, allocator: Allocator) ![]const u8 { const scope = self.scope; return try valueToString(allocator, self.inner, scope.isolate, scope.context); }