Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: promise-returning operations reject, don't throw, on bad receiver

Karl Seguin289620fbf887
diff --git a/src/browser/js/Snapshot.zig b/src/browser/js/Snapshot.zig
index 537136c5a..2d95273cf 100644
--- a/src/browser/js/Snapshot.zig
+++ b/src/browser/js/Snapshot.zig
@@ -796,8 +796,9 @@ fn attachClass(comptime JsApi: type, comptime flatten: bool, isolate: *v8.Isolat
continue;
}
- // For non-static functions, use the signature to validate the receiver
- const func_signature = if (value.static) null else signature;
+ // For non-static functions, use the signature to validate the
+ // receiver, unless the operation has to reject rather than throw.
+ const func_signature = if (value.static or value.rejects_bad_receiver) null else signature;
const function_template = v8.v8__FunctionTemplate__New__Config(isolate, &.{
.callback = value.func,
.length = value.arity,
diff --git a/src/browser/js/bridge.zig b/src/browser/js/bridge.zig
index e2857fb3a..d74723ee5 100644
--- a/src/browser/js/bridge.zig
+++ b/src/browser/js/bridge.zig
@@ -170,6 +170,7 @@ pub const Function = struct {
js_name: ?[:0]const u8 = null,
exposed: Caller.Function.Opts.Exposed = .both,
cache: ?Caller.Function.Opts.Caching = null,
+ rejects_bad_receiver: bool,
func: *const fn (?*const v8.FunctionCallbackInfo) callconv(.c) void,
fn init(comptime T: type, comptime func: anytype, comptime opts: Caller.Function.Opts) Function {
@@ -179,8 +180,7 @@ pub const Function = struct {
.wpt_only = opts.wpt_only,
.js_name = opts.js_name,
.exposed = opts.exposed,
- // Non-static methods receive `self` as their first param; static
- // methods don't, so don't skip the first param for them.
+ .rejects_bad_receiver = returnsPromise(@TypeOf(func)),
.arity = getArity(@TypeOf(func), if (opts.static) 0 else 1),
.func = if (opts.noop) noopFunction else struct {
fn wrap(handle: ?*const v8.FunctionCallbackInfo) callconv(.c) void {
@@ -192,6 +192,14 @@ pub const Function = struct {
pub fn noopFunction(_: ?*const v8.FunctionCallbackInfo) callconv(.c) void {}
+ fn returnsPromise(comptime F: type) bool {
+ const R = @typeInfo(F).@"fn".return_type orelse return false;
+ return switch (@typeInfo(R)) {
+ .error_union => |eu| eu.payload,
+ else => R,
+ } == js.Promise;
+ }
+
fn getArity(comptime T: type, comptime start: usize) usize {
const Execution = js.Execution;