Lightpanda: the headless browser designed for AI and automation
update v8 dep
Karl Seguin5c0ed733d251
diff --git a/.github/actions/install/action.yml b/.github/actions/install/action.ymlindex 96fc529a4..2266cec33 100644--- a/.github/actions/install/action.yml+++ b/.github/actions/install/action.yml@@ -13,7 +13,7 @@ inputs:zig-v8:description: 'zig v8 version to install'required: false- default: 'v0.5.3'+ default: 'v0.5.4'v8:description: 'v8 version to install'required: falsediff --git a/.github/actions/v8-snapshot/action.yml b/.github/actions/v8-snapshot/action.ymlindex 87b5b6a58..20f2830e8 100644--- a/.github/actions/v8-snapshot/action.yml+++ b/.github/actions/v8-snapshot/action.yml@@ -13,7 +13,7 @@ inputs:zig-v8:description: 'zig-v8 release tag the prebuilt lib came from'required: false- default: 'v0.5.3'+ default: 'v0.5.4'runs:using: "composite"diff --git a/Dockerfile b/Dockerfileindex 919695860..efd297324 100644--- a/Dockerfile+++ b/Dockerfile@@ -4,7 +4,7 @@ FROM debian:stable-slimARG MINISIG=0.12ARG ZIG_MINISIG=RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/UARG V8=14.9.207.35-ARG ZIG_V8=v0.5.3+ARG ZIG_V8=v0.5.4ARG TARGETPLATFORMRUN apt-get update -yq && \diff --git a/src/browser/tests/bidi/input.html b/src/browser/tests/bidi/input.htmlindex 6cbf255c9..f369cebd6 100644--- a/src/browser/tests/bidi/input.html+++ b/src/browser/tests/bidi/input.html@@ -4,6 +4,7 @@<input id=inp><script>window.events = [];+window.codes = [];for (const t of ['mousemove', 'mousedown', 'mouseup', 'click', 'dblclick', 'keydown', 'keyup', 'wheel']) {document.addEventListener(t, (e) => {let s = t;@@ -11,6 +12,7 @@ for (const t of ['mousemove', 'mousedown', 'mouseup', 'click', 'dblclick', 'keydif (e.button) s += ':b' + e.button;if (e.target && e.target.id) s += '@' + e.target.id;window.events.push(s);+ if (e.code !== undefined) window.codes.push(e.code);});}</script>diff --git a/src/server/Server.zig b/src/server/Server.zigindex 6463852a9..460eeba51 100644--- a/src/server/Server.zig+++ b/src/server/Server.zig@@ -77,6 +77,10 @@ json_version_response: []const u8,driver_mutex: std.Io.Mutex = .init,drivers: std.ArrayList(Driver) = .empty,+// list of sockets that haven't yet (and might never) be updated to a driver+// (needed for a clean shutdown).+handshakes: std.ArrayList(posix.socket_t) = .empty,+// Number of active conns, used to enforce the cdp-max-connections limit.active_conns: std.atomic.Value(u32) = .init(0),// Number of existing threads, used to deinit correctly.@@ -198,6 +202,9 @@ pub fn shutdown(self: *Server) void {for (self.drivers.items) |*driver| {driver.shutdown();}+ for (self.handshakes.items) |socket| {+ sys_net.shutdown(socket, .recv) catch {};+ }}pub fn deinit(self: *Server) void {@@ -209,6 +216,7 @@ pub fn deinit(self: *Server) void {const allocator = self.app.allocator;self.drivers.deinit(allocator);+ self.handshakes.deinit(allocator);self.cdp_pool.deinit(allocator);allocator.free(self.json_version_response);allocator.free(self.pollfds);@@ -623,13 +631,32 @@ fn handleConnection(self: *Server, socket: posix.socket_t) void {defer _ = self.active_threads.fetchSub(1, .monotonic);defer _ = std.c.close(socket);- const route = Handshake.run(self.app, socket, self.json_version_response) orelse return;+ const route = self.handshake(socket) orelse return;switch (route) {.cdp => self.serveCDP(socket, &active_conns_early_release),.bidi => self.serveBiDi(socket, &active_conns_early_release),}}+fn handshake(self: *Server, socket: posix.socket_t) ?Handshake.Driver {+ {+ self.driver_mutex.lockUncancelable(lp.io);+ defer self.driver_mutex.unlock(lp.io);+ self.handshakes.append(self.app.allocator, socket) catch return null;+ }+ defer {+ self.driver_mutex.lockUncancelable(lp.io);+ defer self.driver_mutex.unlock(lp.io);+ for (self.handshakes.items, 0..) |s, i| {+ if (s == socket) {+ _ = self.handshakes.swapRemove(i);+ break;+ }+ }+ }+ return Handshake.run(self.app, socket, self.json_version_response);+}+// The socket is an upgraded websocket speaking CDP.fn serveCDP(self: *Server, socket: posix.socket_t, active_conns_early_release: *bool) void {const cdp = blk: {diff --git a/src/server/bidi/input.zig b/src/server/bidi/input.zigindex 0eadcb1ec..b63cfef0a 100644--- a/src/server/bidi/input.zig+++ b/src/server/bidi/input.zig@@ -55,17 +55,21 @@ fn performActions(cmd: *const BiDi.Command) !void {const bidi = cmd.bidi;const arena = try bidi.app.arena_pool.acquire(.small, "bidi input.Pending");- errdefer arena.release();+ // run takes onwership of arena, so errdefer can lead to a double-free.+ // explicit release on error instead, then transfer to run.const ticks = parseTicks(bidi, arena.allocator(), p.actions) catch |err| {+ arena.release();if (err == error.OutOfMemory) {return err;}- arena.release();return cmd.sendError("invalid argument", errorMessage(err));};- const pending = try arena.allocator().create(Pending);+ const pending = arena.allocator().create(Pending) catch |err| {+ arena.release();+ return err;+ };pending.* = .{.bidi = bidi,.arena = arena,@@ -91,24 +95,34 @@ fn releaseActions(cmd: *const BiDi.Command) !void {return cmd.sendError("no such frame", "no frame");};- // dispatch pops the released key/button off `pressed` itself+ // dispatch pops the released key/button off `pressed` itself. The state+ // is reset even if a dispatch fails (e.g. ExecutionTerminated), so nothing+ // stays held into the next performActions, and the client gets a reply.const state = &bidi.input_state;+ defer state.reset(bidi.app.allocator);+for (state.sources.items) |*source| {switch (source.kind) {.key => while (source.key.pressed.getLastOrNull()) |cp| {- try dispatch(bidi, frame, source, &.{ .key_up = cp });+ dispatch(bidi, frame, source, &.{ .key_up = cp }) catch |err| return dispatchFailed(cmd, err);},.pointer => while (source.pointer.pressed.getLastOrNull()) |button| {- try dispatch(bidi, frame, source, &.{ .pointer_up = button });+ dispatch(bidi, frame, source, &.{ .pointer_up = button }) catch |err| return dispatchFailed(cmd, err);},.none, .wheel => {},}}- state.reset(bidi.app.allocator);return cmd.sendResult(struct {}{});}+fn dispatchFailed(cmd: *const BiDi.Command, err: DispatchError) !void {+ if (err == error.OutOfMemory) {+ return err;+ }+ return cmd.sendError(errorCode(err), errorMessage(err));+}+// Spec: the input state map, keyed by top-level browsing context. We have one// browsing context, so this lives on the BiDi and survives navigations until// input.releaseActions or the browsing context closes.@@ -238,17 +252,27 @@ const Pending = struct {self.arena.release();}- // Runs ticks until one has a durationfn run(self: *Pending) !void {+ const parked = self.step() catch |err| {+ self.deinit();+ return err;+ };+ if (!parked) {+ self.deinit();+ }+ }++ // Runs ticks until one has a duration. True if parked in the scheduler.+ fn step(self: *Pending) !bool {const bidi = self.bidi;while (self.next < self.ticks.len) {if (bidi.input_state.generation != self.generation) {- defer self.deinit();- return bidi.sendError(self.command_id, "unknown error", "input state was released while actions were pending");+ try bidi.sendError(self.command_id, "unknown error", "input state was released while actions were pending");+ return false;}const frame = bidi.user_context.session.currentFrame() orelse {- defer self.deinit();- return bidi.sendError(self.command_id, "no such frame", "no frame");+ try bidi.sendError(self.command_id, "no such frame", "no frame");+ return false;};const next = self.next;@@ -259,28 +283,32 @@ const Pending = struct {for (tick) |ta| {wait = @max(wait, ta.action.duration());dispatch(bidi, frame, &bidi.input_state.sources.items[ta.source], &ta.action) catch |err| {- defer self.deinit();if (err == error.OutOfMemory) {return err;}- return bidi.sendError(self.command_id, errorCode(err), errorMessage(err));+ try bidi.sendError(self.command_id, errorCode(err), errorMessage(err));+ return false;};}if (wait > 0) {- return frame.js.scheduler.add(self, resumeFromScheduler, wait, .{ .name = "bidi.performActions", .finalizer = cancelled });+ try frame.js.scheduler.add(self, resumeFromScheduler, wait, .{ .name = "bidi.performActions", .finalizer = cancelled });+ return true;}}- defer self.deinit();try bidi.sendResult(self.command_id, struct {}{});+ return false;}fn resumeFromScheduler(ctx: *anyopaque) !?u32 {const self: *Pending = @ptrCast(@alignCast(ctx));++ // need to capture it here, run can free self+ const command_id = self.command_id;+self.run() catch |err| {- log.err(.bidi, "performActions", .{ .err = err, .id = self.command_id });- self.deinit();+ log.err(.bidi, "performActions", .{ .err = err, .id = command_id });};return null;}@@ -718,6 +746,10 @@ fn specialKey(cp: u21) KeyInfo {};}+// slices into this literal are always valid+const key_codes = "KeyAKeyBKeyCKeyDKeyEKeyFKeyGKeyHKeyIKeyJKeyKKeyLKeyMKeyNKeyOKeyPKeyQKeyRKeySKeyTKeyUKeyVKeyWKeyXKeyYKeyZ";+const digit_codes = "Digit0Digit1Digit2Digit3Digit4Digit5Digit6Digit7Digit8Digit9";+// The `code` of a printable character on a US layout.fn asciiCode(cp: u21) []const u8 {if (cp >= 128) {@@ -725,9 +757,9 @@ fn asciiCode(cp: u21) []const u8 {}const c: u8 = @intCast(cp);return switch (c) {- 'a'...'z' => &[_]u8{ 'K', 'e', 'y', std.ascii.toUpper(c) },- 'A'...'Z' => &[_]u8{ 'K', 'e', 'y', c },- '0'...'9' => &[_]u8{ 'D', 'i', 'g', 'i', 't', c },+ 'a'...'z' => key_codes[(c - 'a') * 4 ..][0..4],+ 'A'...'Z' => key_codes[(c - 'A') * 4 ..][0..4],+ '0'...'9' => digit_codes[(c - '0') * 6 ..][0..6],' ' => "Space",'\n', '\r' => "Enter",'\t' => "Tab",@@ -924,6 +956,12 @@ test "bidi.input: keys and modifiers" {try evaluate(&ctx, 4, context_id, "document.getElementById('inp').value");try ctx.expectSentResult(.{ .type = "success", .result = .{ .type = "string", .value = "aB" } }, .{ .id = 4 });++ try evaluate(&ctx, 5, context_id, "window.codes.join(' ')");+ try ctx.expectSentResult(.{ .type = "success", .result = .{+ .type = "string",+ .value = "KeyA KeyA ShiftLeft KeyB KeyB ShiftLeft NumpadEnter NumpadEnter",+ } }, .{ .id = 5 });}test "bidi.input: pause parks the command, releaseActions undoes held input" {diff --git a/src/server/bidi/remote_value.zig b/src/server/bidi/remote_value.zigindex 1d30de3b7..5167104e5 100644--- a/src/server/bidi/remote_value.zig+++ b/src/server/bidi/remote_value.zig@@ -333,8 +333,16 @@ pub const Serializer = struct {};fn remote(self: *Serializer, value: js.Value, depth: u32, own: bool) Error!Remote {- const handle: ?[]const u8 = if (own and value.isObject()) blk: {+ var handle_id: ?u32 = null;+ // The reply carries the handle; if serializing the body throws (a+ // throwing accessor, a Proxy trap) the client never learns it, so+ // don't keep the global alive.+ errdefer if (handle_id) |id| self.handles.release(id);++ // Symbols have identity, so the spec lets them carry a handle too.+ const handle: ?[]const u8 = if (own and (value.isObject() or value.isSymbol())) blk: {const id = try self.handles.add(value);+ handle_id = id;break :blk try std.fmt.allocPrint(self.arena, "{d}", .{id});} else null;@@ -363,8 +371,14 @@ pub const Serializer = struct {if (value.isBigInt()) {return .{ .bigint = try value.toStringSliceWithAlloc(self.arena) };}+ if (value.isSymbol()) {+ return .{ .other = "symbol" };+ }+if (value.isObject() == false) {- // A symbol. BiDi has no type for one.+ // Should not be possible, we've exhausted every primitive. But,+ // let's be defensive here.+ log.warn(.bidi, "unexpected value type", .{});return .{ .other = "object" };}diff --git a/src/server/bidi/script.zig b/src/server/bidi/script.zigindex a8faa04a3..69f86c7ab 100644--- a/src/server/bidi/script.zig+++ b/src/server/bidi/script.zig@@ -1171,6 +1171,7 @@ test "bidi.script: callFunction arguments" {}test "bidi.script: handles" {+ testing.expectLog(&.{.bidi});var ctx = try testing.context();defer ctx.deinit();@@ -1206,6 +1207,34 @@ test "bidi.script: handles" {.result = .{ .type = "number", .value = 7 },}, .{ .id = 2 });+ // A symbol is a primitive with identity: it gets a handle and round-trips.+ try ctx.processMessage(.{+ .id = 10,+ .method = "script.evaluate",+ .params = .{+ .expression = "window.sym = Symbol('s')",+ .target = .{ .context = context_id },+ .resultOwnership = "root",+ },+ });+ try ctx.expectSentResult(.{ .type = "success", .result = .{ .type = "symbol", .handle = "2" } }, .{ .id = 10 });+ try ctx.processMessage(.{+ .id = 11,+ .method = "script.callFunction",+ .params = .{+ .functionDeclaration = "(s) => s === window.sym",+ .target = .{ .context = context_id },+ .arguments = .{.{ .handle = "2" }},+ },+ });+ try ctx.expectSentResult(.{ .type = "success", .result = .{ .type = "boolean", .value = true } }, .{ .id = 11 });+ try ctx.processMessage(.{+ .id = 12,+ .method = "script.disown",+ .params = .{ .handles = .{"2"}, .target = .{ .context = context_id } },+ });+ try ctx.expectSentResult(null, .{ .id = 12 });+// A primitive has nothing to hold on to, so it gets no handle.try ctx.processMessage(.{.id = 3,@@ -1244,6 +1273,43 @@ test "bidi.script: handles" {.params = .{ .handles = .{"1"}, .target = .{ .context = context_id } },});try ctx.expectSentResult(null, .{ .id = 6 });++ // A value whose serialization throws gets its handle released again,+ // since the client never learns the id. Ids aren't reused, so the next+ // owned result proves 3 was minted then released: it takes 4, and 3 is+ // unknown.+ try ctx.processMessage(.{+ .id = 7,+ .method = "script.evaluate",+ .params = .{+ .expression = "({ get boom() { throw new Error('nope') } })",+ .target = .{ .context = context_id },+ .resultOwnership = "root",+ },+ });+ try ctx.expectSentError("unknown error", "cannot serialize result", .{ .id = 7 });++ try ctx.processMessage(.{+ .id = 8,+ .method = "script.evaluate",+ .params = .{+ .expression = "({n: 8})",+ .target = .{ .context = context_id },+ .resultOwnership = "root",+ },+ });+ try ctx.expectSentResult(.{ .type = "success", .result = .{ .type = "object", .handle = "4" } }, .{ .id = 8 });++ try ctx.processMessage(.{+ .id = 9,+ .method = "script.callFunction",+ .params = .{+ .functionDeclaration = "(o) => o.n",+ .target = .{ .context = context_id },+ .arguments = .{.{ .handle = "3" }},+ },+ });+ try ctx.expectSentError("no such handle", null, .{ .id = 9 });}test "bidi.script: target validation" {