Skip to content

Lightpanda: the headless browser designed for AI and automation

js: create thrown exceptions in the receiver's relevant realm

Francis Bouvier945c6d40cb81
diff --git a/src/browser/js/Caller.zig b/src/browser/js/Caller.zig
index eff8769e0..d5aeb85cf 100644
--- a/src/browser/js/Caller.zig
+++ b/src/browser/js/Caller.zig
@@ -500,6 +500,61 @@ fn nameToString(local: *const Local, comptime T: type, name: *const v8.Name) !T
return try js.String.toSlice(.{ .local = local, .handle = handle });
}
+// Per Web IDL, exceptions belong to the operation's relevant realm — the
+// receiver's — which differs from the calling realm for cross-realm calls
+// (v8 API callbacks run in the caller's context, and our JS wrappers are
+// shared across the page's contexts). For DOM nodes, the relevant realm is
+// the node document's frame; otherwise fall back to the calling realm.
+fn errorLocal(comptime T: type, local: *const Local, info: anytype) Local {
+ if (@TypeOf(info) != FunctionCallbackInfo) {
+ return local.*;
+ }
+
+ const Node = @import("../webapi/Node.zig");
+ const Document = @import("../webapi/Document.zig");
+
+ const is_node_type = comptime blk: {
+ if (@typeInfo(T) != .@"struct") break :blk false;
+ if (T == Node) break :blk true;
+ break :blk @hasDecl(T, "JsApi") and @hasDecl(T, "asNode") and @TypeOf(T.asNode) == fn (*T) *Node;
+ };
+ if (comptime !is_node_type) {
+ return local.*;
+ }
+
+ const node: *Node = blk: {
+ const instance = TaggedOpaque.fromJS(*T, info.getThis()) catch return local.*;
+ if (comptime T == Node) {
+ break :blk instance;
+ } else {
+ break :blk instance.asNode();
+ }
+ };
+
+ const frame = switch (local.ctx.global) {
+ .frame => |f| f,
+ .worker => return local.*,
+ };
+
+ const doc: *Document = node.ownerDocument(frame) orelse switch (node._type) {
+ .document => |d| d,
+ else => return local.*,
+ };
+ const doc_frame = doc._frame orelse return local.*;
+ if (doc_frame == frame) {
+ return local.*;
+ }
+
+ const ctx = doc_frame.js;
+ const local_v8_context: *const v8.Context = @ptrCast(v8.v8__Global__Get(&ctx.handle, ctx.isolate.handle));
+ return .{
+ .ctx = ctx,
+ .handle = local_v8_context,
+ .call_arena = ctx.call_arena,
+ .isolate = ctx.isolate,
+ };
+}
+
fn handleError(comptime T: type, comptime F: type, local: *const Local, err: anyerror, info: anytype) void {
const isolate = local.isolate;
@@ -513,18 +568,28 @@ fn handleError(comptime T: type, comptime F: type, local: *const Local, err: any
}
}
- const js_err: *const v8.Value = switch (err) {
- error.TryCatchRethrow => return,
- // A JS exception is already pending in the isolate (e.g. a value's
- // toString threw during argument conversion); throwing anything here
- // would replace the original exception the script expects to see.
- error.JsException => return,
- error.InvalidArgument => isolate.createTypeError("invalid argument"),
- error.TypeError => isolate.createTypeError(""),
- error.RangeError => isolate.createRangeError(""),
- error.OutOfMemory => isolate.createError("out of memory"),
- error.IllegalConstructor => isolate.createError("Illegal Constructor"),
- else => domExceptionToJs(local, err) orelse isolate.createError(@errorName(err)),
+ const err_local = errorLocal(T, local, info);
+
+ const js_err: *const v8.Value = blk: {
+ // Error constructors use the isolate's current context: enter the
+ // receiver's realm so the exception gets its prototypes.
+ const entered = err_local.handle != local.handle;
+ if (entered) v8.v8__Context__Enter(err_local.handle);
+ defer if (entered) v8.v8__Context__Exit(err_local.handle);
+
+ break :blk switch (err) {
+ error.TryCatchRethrow => return,
+ // A JS exception is already pending in the isolate (e.g. a value's
+ // toString threw during argument conversion); throwing anything here
+ // would replace the original exception the script expects to see.
+ error.JsException => return,
+ error.InvalidArgument => isolate.createTypeError("invalid argument"),
+ error.TypeError => isolate.createTypeError(""),
+ error.RangeError => isolate.createRangeError(""),
+ error.OutOfMemory => isolate.createError("out of memory"),
+ error.IllegalConstructor => isolate.createError("Illegal Constructor"),
+ else => domExceptionToJs(&err_local, err) orelse isolate.createError(@errorName(err)),
+ };
};
const js_exception = isolate.throwException(js_err);
diff --git a/src/browser/js/TaggedOpaque.zig b/src/browser/js/TaggedOpaque.zig
index 3a8250ce9..ca20b3ef5 100644
--- a/src/browser/js/TaggedOpaque.zig
+++ b/src/browser/js/TaggedOpaque.zig
@@ -106,7 +106,14 @@ pub fn fromJS(comptime R: type, js_obj_handle: *const v8.Object) !R {
@compileError("unknown Zig type: " ++ @typeName(R));
}
- const tao_ptr = v8.v8__Object__GetAlignedPointerFromInternalField(js_obj_handle, 0).?;
+ const tao_ptr = v8.v8__Object__GetAlignedPointerFromInternalField(js_obj_handle, 0) orelse return error.InvalidArgument;
+ // A wrapped object always embeds an aligned TaggedOpaque pointer. An
+ // unaligned value is a leftover v8 tagged value: the object was created
+ // from our template but never mapped to a Zig instance (e.g. a custom
+ // element whose constructor threw).
+ if (@intFromPtr(tao_ptr) % @alignOf(TaggedOpaque) != 0) {
+ return error.InvalidArgument;
+ }
const tao: *TaggedOpaque = @ptrCast(@alignCast(tao_ptr));
const expected_type_index = bridge.JsApiLookup.getId(JsApi);