Lightpanda: the headless browser designed for AI and automation
idb: expose request.source, and improve cursor state check
Karl Seguinb1453f12a3c2
diff --git a/src/browser/webapi/storage/idb/IDBCursor.zig b/src/browser/webapi/storage/idb/IDBCursor.zigindex 21081de40..2254f8296 100644--- a/src/browser/webapi/storage/idb/IDBCursor.zig+++ b/src/browser/webapi/storage/idb/IDBCursor.zig@@ -174,17 +174,21 @@ pub fn beforeDeliver(self: *IDBCursor) void {}pub fn @"continue"(self: *IDBCursor, key_arg: ?js.Value, exec: *Execution) !void {- try self.prepareIterate();+ try self.assertIterable();if (key_arg) |k| {+ // Key conversion (DataError) must run before the got-value flag is+ // cleared, so a failing continue() leaves the cursor re-iterable.const encoded = try Key.encodeValue(self._txn._arena, k);// The target must move past the current key in the iteration direction.const order = std.mem.order(u8, encoded, self._key.?);if (if (self._direction.reverse()) order != .lt else order != .gt) {return error.DataError;}+ self._got_value = false;try self.reiterate(.{ .to = encoded }, 0, exec);} else {+ self._got_value = false;try self.reiterate(.next, 0, exec);}}@@ -194,9 +198,10 @@ pub fn continuePrimaryKey(self: *IDBCursor, key_arg: js.Value, primary_key_arg:if (self._index_id == null or self._direction == .nextunique or self._direction == .prevunique) {return error.InvalidAccessError;}- try self.prepareIterate();+ try self.assertIterable();const reverse = self._direction.reverse();+ // Key conversion (DataError) precedes clearing the got-value flag; see continue().const key = try Key.encodeValue(self._txn._arena, key_arg);const primary_key = try Key.encodeValue(self._txn._arena, primary_key_arg);@@ -211,6 +216,7 @@ pub fn continuePrimaryKey(self: *IDBCursor, key_arg: js.Value, primary_key_arg:};if (!ok) return error.DataError;+ self._got_value = false;try self.reiterate(.{ .to_primary = .{ .key = key, .primary_key = primary_key } }, 0, exec);}@@ -219,7 +225,8 @@ pub fn advance(self: *IDBCursor, count: u32, exec: *Execution) !void {return error.TypeError;}- try self.prepareIterate();+ try self.assertIterable();+ self._got_value = false;// `count` records forward = skip count-1 past the immediate next.try self.reiterate(.next, count - 1, exec);}@@ -395,8 +402,7 @@ pub fn getValueJs(self: *IDBCursor, exec: *Execution) !?js.Value {return value;}-// validate the state before we can advance/continue-fn prepareIterate(self: *IDBCursor) !void {+fn assertIterable(self: *IDBCursor) !void {if (self._txn._settled == true) {return error.TransactionInactiveError;}@@ -404,8 +410,6 @@ fn prepareIterate(self: *IDBCursor) !void {if (self._got_value == false) {return error.InvalidStateError;}-- self._got_value = false;}fn assertCanUpdate(self: *IDBCursor) !void {diff --git a/src/browser/webapi/storage/idb/IDBRequest.zig b/src/browser/webapi/storage/idb/IDBRequest.zigindex e6e7ae5ca..7be3f39a1 100644--- a/src/browser/webapi/storage/idb/IDBRequest.zig+++ b/src/browser/webapi/storage/idb/IDBRequest.zig@@ -50,6 +50,7 @@ _txn: Txn = .none,// be executed/fired in its last append (to preserve ordering)._txn_index: usize = 0,_cursor: ?*IDBCursor = null,+_source: Source = .{ .none = null },_ready_state: ReadyState = .pending,_result: Result = .{ .none = js.Undefined{} },// whether or not we own th result value, if we do, we can free it once we know@@ -75,6 +76,13 @@ const Result = union(enum) {database: *IDBDatabase, // the result of an open};+const Source = union(enum) {+ none: ?js.Undefined,+ store: *IDBObjectStore,+ index: *IDBIndex,+ cursor: *IDBCursor,+};+const Txn = union(enum) {// a page-scoped open/deleteDatabase request, outside an upgradenone,@@ -286,6 +294,12 @@ pub fn getResult(self: *const IDBRequest, exec: *Execution) JsResult {};}+// The bridge converts the active union variant (the store/index/cursor, or JS+// null for an open/delete request).+pub fn getSource(self: *const IDBRequest) Source {+ return self._source;+}+pub fn getTransaction(self: *const IDBRequest) ?*IDBTransaction {return switch (self._txn) {.none => null,@@ -363,12 +377,31 @@ pub const Operation = union(enum) {const CursorIterate = struct { cursor: *IDBCursor, seek: IDBCursor.Seek, offset: u32 };const CursorUpdate = struct { cursor: *IDBCursor, key: []const u8, value: []const u8 };const CursorDelete = struct { cursor: *IDBCursor, key: []const u8 };++ fn source(op: Operation) Source {+ return switch (op) {+ .none => .{ .none = null },+ .store_get, .store_get_key, .store_count, .store_delete => |o| .{ .store = o.store },+ .store_get_all => |o| .{ .store = o.store },+ .store_clear => |store| .{ .store = store },+ .store_write => |o| .{ .store = o.store },+ .index_get, .index_get_key, .index_count => |o| .{ .index = o.index },+ .index_get_all => |o| .{ .index = o.index },+ .cursor_iterate => |o| switch (o.cursor._source) {+ .store => |s| .{ .store = s },+ .index => |x| .{ .index = x },+ },+ .cursor_update => |o| .{ .cursor = o.cursor },+ .cursor_delete => |o| .{ .cursor = o.cursor },+ };+ }};// Commit this request's operation: record it and queue the request on its// transaction's drain.pub fn submit(self: *IDBRequest, op: Operation, exec: *Execution) !*IDBRequest {self._op = op;+ self._source = op.source();const txn = self._txn.owned;try txn.enqueue(self);@@ -424,6 +457,7 @@ pub const JsApi = struct {pub const readyState = bridge.accessor(IDBRequest.getReadyState, null, .{});pub const result = bridge.accessor(IDBRequest.getResult, null, .{});+ pub const source = bridge.accessor(IDBRequest.getSource, null, .{});pub const transaction = bridge.accessor(IDBRequest.getTransaction, null, .{ .null_as_undefined = true });pub const @"error" = bridge.accessor(IDBRequest.getError, null, .{ .null_as_undefined = true });pub const onsuccess = bridge.accessor(IDBRequest.getOnSuccess, IDBRequest.setOnSuccess, .{});diff --git a/src/browser/webapi/storage/idb/Key.zig b/src/browser/webapi/storage/idb/Key.zigindex 85e3551f2..4e4e492a5 100644--- a/src/browser/webapi/storage/idb/Key.zig+++ b/src/browser/webapi/storage/idb/Key.zig@@ -193,7 +193,7 @@ fn fromJsDepth(value: js.Value, allocator: Allocator, depth: usize) !Value {const len = arr.len();const items = try allocator.alloc(Value, len);for (0..len) |i| {- const element = try arr.get(@intCast(i));+ const element = arr.get(@intCast(i)) catch return error.TryCatchRethrow;items[i] = try fromJsDepth(element, allocator, depth + 1);}return .{ .array = items };