Skip to content

Commit a3b576a

Browse files
Merge pull request #656 from lightpanda-io/module-exception
module: report module's evaluation error
2 parents 9366729 + 2261eac commit a3b576a

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
.hash = "tigerbeetle_io-0.0.0-ViLgxpyRBAB5BMfIcj3KMXfbJzwARs9uSl8aRy2OXULd",
1414
},
1515
.v8 = .{
16-
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/6f1ee74a0e7002ea3568e337ab716c1e75c53769.tar.gz",
17-
.hash = "v8-0.0.0-xddH6z2yAwCOPUGmy1IgXysI1yWt8ftd2Z3D5zp0I9tV",
16+
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/240140e5b3e5a8e5e51cbdd36bc120bf28ae5c31.tar.gz",
17+
.hash = "v8-0.0.0-xddH64eyAwBcX7e2x5tv9MhT0MgQbshP2rb19blo06Db",
1818
},
1919
//.v8 = .{ .path = "../zig-v8-fork" },
2020
//.tigerbeetle_io = .{ .path = "../tigerbeetle-io" },

src/browser/page.zig

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,18 @@ pub const Page = struct {
638638
const src = self.src orelse "inline";
639639
const res = switch (self.kind) {
640640
.javascript => page.scope.exec(body, src),
641-
.module => page.scope.module(body, src),
641+
.module => blk: {
642+
switch (try page.scope.module(body, src)) {
643+
.value => |v| break :blk v,
644+
.exception => |e| {
645+
log.info("eval module {s}: {s}", .{
646+
src,
647+
try e.exception(page.arena),
648+
});
649+
return error.JsErr;
650+
},
651+
}
652+
},
642653
} catch {
643654
if (try try_catch.err(page.arena)) |msg| {
644655
log.info("eval script {s}: {s}", .{ src, msg });

src/runtime/js.zig

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,12 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
610610

611611
// compile and eval a JS module
612612
// It doesn't wait for callbacks execution
613-
pub fn module(self: *Scope, src: []const u8, name: []const u8) !Value {
613+
pub fn module(self: *Scope, src: []const u8, name: []const u8) !union(enum) { value: Value, exception: Exception } {
614614
const context = self.context;
615615
const m = try compileModule(self.isolate, src, name);
616616

617617
// instantiate
618-
// TODO handle ResolveModuleCallback parameters to load module's
619-
// dependencies.
618+
// resolveModuleCallback loads module's dependencies.
620619
const ok = m.instantiate(context, resolveModuleCallback) catch {
621620
return error.ExecutionError;
622621
};
@@ -626,8 +625,18 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
626625
}
627626

628627
// evaluate
629-
const value = m.evaluate(context) catch return error.ExecutionError;
630-
return self.createValue(value);
628+
const value = m.evaluate(context) catch {
629+
return .{ .exception = self.createException(m.getException()) };
630+
};
631+
return .{ .value = self.createValue(value) };
632+
}
633+
634+
// Wrap a v8.Exception
635+
fn createException(self: *const Scope, e: v8.Value) Exception {
636+
return .{
637+
.inner = e,
638+
.scope = self,
639+
};
631640
}
632641

633642
// 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 {
11331142

11341143
pub const RemoteObject = v8.RemoteObject;
11351144

1145+
pub const Exception = struct {
1146+
inner: v8.Value,
1147+
scope: *const Scope,
1148+
1149+
// the caller needs to deinit the string returned
1150+
pub fn exception(self: Exception, allocator: Allocator) ![]const u8 {
1151+
const scope = self.scope;
1152+
return try valueToString(allocator, self.inner, scope.isolate, scope.context);
1153+
}
1154+
};
1155+
11361156
pub const Value = struct {
11371157
value: v8.Value,
11381158
scope: *const Scope,

0 commit comments

Comments
 (0)