Prefer js.Value over js.Object in History/Navigation

Persist function callback in PerformanceObserver
This commit is contained in:
Karl Seguin
2026-01-05 10:29:00 +08:00
parent f2a9125b99
commit b4f134bff6
6 changed files with 15 additions and 14 deletions

View File

@@ -114,11 +114,6 @@ pub fn format(self: Object, writer: *std.Io.Writer) !void {
return writer.writeAll(str); return writer.writeAll(str);
} }
pub fn toJson(self: Object, allocator: Allocator) ![]u8 {
const json_str_handle = v8.v8__JSON__Stringify(self.ctx.handle, @ptrCast(self.handle), null) orelse return error.JsException;
return self.ctx.jsStringToZig(json_str_handle, .{ .allocator = allocator });
}
pub fn persist(self: Object) !Object { pub fn persist(self: Object) !Object {
var ctx = self.ctx; var ctx = self.ctx;

View File

@@ -219,6 +219,11 @@ pub fn toStringZ(self: Value, opts: js.String.ToZigOpts) ![:0]u8 {
return self._toString(true, opts); return self._toString(true, opts);
} }
pub fn toJson(self: Value, allocator: Allocator) ![]u8 {
const json_str_handle = v8.v8__JSON__Stringify(self.ctx.handle, self.handle, null) orelse return error.JsException;
return self.ctx.jsStringToZig(json_str_handle, .{ .allocator = allocator });
}
fn _toString(self: Value, comptime null_terminate: bool, opts: js.String.ToZigOpts) !(if (null_terminate) [:0]u8 else []u8) { fn _toString(self: Value, comptime null_terminate: bool, opts: js.String.ToZigOpts) !(if (null_terminate) [:0]u8 else []u8) {
const ctx: *js.Context = @constCast(self.ctx); const ctx: *js.Context = @constCast(self.ctx);

View File

@@ -35,3 +35,4 @@
history.back(); history.back();
</script> </script>

View File

@@ -49,7 +49,7 @@ pub fn setScrollRestoration(self: *History, str: []const u8) void {
} }
} }
pub fn pushState(_: *History, state: js.Object, _: []const u8, _url: ?[]const u8, page: *Page) !void { pub fn pushState(_: *History, state: js.Value, _: ?[]const u8, _url: ?[]const u8, page: *Page) !void {
const arena = page._session.arena; const arena = page._session.arena;
const url = if (_url) |u| try arena.dupeZ(u8, u) else try arena.dupeZ(u8, page.url); const url = if (_url) |u| try arena.dupeZ(u8, u) else try arena.dupeZ(u8, page.url);
@@ -57,7 +57,7 @@ pub fn pushState(_: *History, state: js.Object, _: []const u8, _url: ?[]const u8
_ = try page._session.navigation.pushEntry(url, .{ .source = .history, .value = json }, page, true); _ = try page._session.navigation.pushEntry(url, .{ .source = .history, .value = json }, page, true);
} }
pub fn replaceState(_: *History, state: js.Object, _: []const u8, _url: ?[]const u8, page: *Page) !void { pub fn replaceState(_: *History, state: js.Value, _: ?[]const u8, _url: ?[]const u8, page: *Page) !void {
const arena = page._session.arena; const arena = page._session.arena;
const url = if (_url) |u| try arena.dupeZ(u8, u) else try arena.dupeZ(u8, page.url); const url = if (_url) |u| try arena.dupeZ(u8, u) else try arena.dupeZ(u8, page.url);

View File

@@ -44,7 +44,7 @@ const DefaultDurationThreshold: f64 = 104;
/// Creates a new PerformanceObserver object with the given observer callback. /// Creates a new PerformanceObserver object with the given observer callback.
pub fn init(callback: js.Function, page: *Page) !*PerformanceObserver { pub fn init(callback: js.Function, page: *Page) !*PerformanceObserver {
return page._factory.create(PerformanceObserver{ return page._factory.create(PerformanceObserver{
._callback = callback, ._callback = try callback.persist(),
._duration_threshold = DefaultDurationThreshold, ._duration_threshold = DefaultDurationThreshold,
._interests = 0, ._interests = 0,
._entries = .{}, ._entries = .{},

View File

@@ -247,8 +247,8 @@ pub fn replaceEntry(
} }
const NavigateOptions = struct { const NavigateOptions = struct {
state: ?js.Object = null, state: ?js.Value = null,
info: ?js.Object = null, info: ?js.Value = null,
history: ?[]const u8 = null, history: ?[]const u8 = null,
}; };
@@ -346,8 +346,8 @@ pub fn navigate(self: *Navigation, _url: [:0]const u8, _opts: ?NavigateOptions,
} }
pub const ReloadOptions = struct { pub const ReloadOptions = struct {
state: ?js.Object = null, state: ?js.Value = null,
info: ?js.Object = null, info: ?js.Value = null,
}; };
pub fn reload(self: *Navigation, _opts: ?ReloadOptions, page: *Page) !NavigationReturn { pub fn reload(self: *Navigation, _opts: ?ReloadOptions, page: *Page) !NavigationReturn {
@@ -371,7 +371,7 @@ pub fn reload(self: *Navigation, _opts: ?ReloadOptions, page: *Page) !Navigation
} }
pub const TraverseToOptions = struct { pub const TraverseToOptions = struct {
info: ?js.Object = null, info: ?js.Value = null,
}; };
pub fn traverseTo(self: *Navigation, key: []const u8, _opts: ?TraverseToOptions, page: *Page) !NavigationReturn { pub fn traverseTo(self: *Navigation, key: []const u8, _opts: ?TraverseToOptions, page: *Page) !NavigationReturn {
@@ -389,7 +389,7 @@ pub fn traverseTo(self: *Navigation, key: []const u8, _opts: ?TraverseToOptions,
} }
pub const UpdateCurrentEntryOptions = struct { pub const UpdateCurrentEntryOptions = struct {
state: js.Object, state: js.Value,
}; };
pub fn updateCurrentEntry(self: *Navigation, options: UpdateCurrentEntryOptions, page: *Page) !void { pub fn updateCurrentEntry(self: *Navigation, options: UpdateCurrentEntryOptions, page: *Page) !void {