Skip to content

Lightpanda: the headless browser designed for AI and automation

js: required-but-nullable arguments; fix EventTarget/AbortSignal IDL shape

Francis Bouvier0740f7d11333
diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zig
index b24340c9b..192537f32 100644
--- a/src/browser/js/Local.zig
+++ b/src/browser/js/Local.zig
@@ -742,6 +742,14 @@ pub fn jsValueToZig(self: *const Local, comptime T: type, js_val: js.Value) !T {
// Extracted so that it can be used in both jsValueToZig and in
// probeJsValueToZig. Avoids having to duplicate this logic when probing.
fn jsValueToStruct(self: *const Local, comptime T: type, js_val: js.Value) !?T {
+ // js.Nullable(T): a required argument that accepts null.
+ if (@hasDecl(T, "js_nullable")) {
+ if (js_val.isNullOrUndefined()) {
+ return T{ .value = null };
+ }
+ return T{ .value = try self.jsValueToZig(T.js_nullable, js_val) };
+ }
+
return switch (T) {
js.Function, js.Function.Global => {
if (!js_val.isFunction()) {
diff --git a/src/browser/js/js.zig b/src/browser/js/js.zig
index 675f6955b..a8bec4d91 100644
--- a/src/browser/js/js.zig
+++ b/src/browser/js/js.zig
@@ -284,6 +284,18 @@ pub const NullableString = struct {
value: []const u8,
};
+// A required argument that accepts null (Web IDL "T?"): unlike a Zig optional
+// parameter, omitting the argument is a TypeError, while passing null or
+// undefined yields .{ .value = null }. It also counts towards the JS-visible
+// function length, which a plain optional would not.
+pub fn Nullable(comptime T: type) type {
+ return struct {
+ value: ?T,
+
+ pub const js_nullable = T;
+ };
+}
+
pub const Exception = struct {
local: *const Local,
handle: *const v8.Value,
diff --git a/src/browser/webapi/AbortSignal.zig b/src/browser/webapi/AbortSignal.zig
index b9e34faec..9b679cbb2 100644
--- a/src/browser/webapi/AbortSignal.zig
+++ b/src/browser/webapi/AbortSignal.zig
@@ -167,7 +167,12 @@ pub fn createAborted(reason_: ?js.Value, exec: *const Execution) !*AbortSignal {
return signal;
}
-pub fn createAny(signals: []const *AbortSignal, exec: *const Execution) !*AbortSignal {
+pub fn createAny(signals_value: js.Value, exec: *const Execution) !*AbortSignal {
+ // A js.Value parameter (not a slice) so the argument is required — the
+ // bridge would treat a trailing slice as variadic and default it to
+ // empty, but AbortSignal.any() must throw when called with no argument.
+ const signals = try signals_value.toZig([]const *AbortSignal);
+
const result = try init(exec);
for (signals) |source| {
if (source._aborted) {
@@ -279,7 +284,9 @@ pub const JsApi = struct {
pub const Prototype = EventTarget;
- pub const constructor = bridge.constructor(AbortSignal.init, .{});
+ // Per spec AbortSignal has no constructor; instances are created via the
+ // static methods and AbortController. (AbortSignal.init stays for
+ // internal use.)
pub const aborted = bridge.accessor(AbortSignal.getAborted, null, .{});
pub const reason = bridge.accessor(AbortSignal.getReason, null, .{});
pub const onabort = bridge.accessor(AbortSignal.getOnAbort, AbortSignal.setOnAbort, .{});
diff --git a/src/browser/webapi/EventTarget.zig b/src/browser/webapi/EventTarget.zig
index f0c25e1c3..b083969be 100644
--- a/src/browser/webapi/EventTarget.zig
+++ b/src/browser/webapi/EventTarget.zig
@@ -138,7 +138,7 @@ pub const EventListenerCallback = union(enum) {
function: js.Function,
object: js.Object,
};
-pub fn addEventListener(self: *EventTarget, typ: []const u8, callback_: ?EventListenerCallback, opts_: ?AddEventListenerOptions, exec: *js.Execution) !void {
+pub fn addEventListener(self: *EventTarget, typ: []const u8, callback_: js.Nullable(EventListenerCallback), opts_: ?AddEventListenerOptions, exec: *js.Execution) !void {
// Convert the options before the null-callback early return: per spec,
// the dictionary conversion throws even when the callback is null.
const options = blk: {
@@ -160,7 +160,7 @@ pub fn addEventListener(self: *EventTarget, typ: []const u8, callback_: ?EventLi
};
};
- const callback = callback_ orelse return;
+ const callback = callback_.value orelse return;
const em_callback: EventManager.Callback = switch (callback) {
.object => |obj| .{ .object = obj },
@@ -180,8 +180,8 @@ const RemoveEventListenerOptions = union(enum) {
capture: bool = false,
};
};
-pub fn removeEventListener(self: *EventTarget, typ: []const u8, callback_: ?EventListenerCallback, opts_: ?RemoveEventListenerOptions, exec: *js.Execution) !void {
- const callback = callback_ orelse return;
+pub fn removeEventListener(self: *EventTarget, typ: []const u8, callback_: js.Nullable(EventListenerCallback), opts_: ?RemoveEventListenerOptions, exec: *js.Execution) !void {
+ const callback = callback_.value orelse return;
// For object callbacks, check if handleEvent exists
if (callback == .object) {