Lightpanda: the headless browser designed for AI and automation
Revert "refactor: Rework how v8::Globals are managed"
Karl Seguin15d6b1626860
diff --git a/src/browser/EventManager.zig b/src/browser/EventManager.zigindex dada38b5c..863923d66 100644--- a/src/browser/EventManager.zig+++ b/src/browser/EventManager.zig@@ -124,7 +124,7 @@ pub const DispatchDirectOptions = EventManagerBase.DispatchDirectOptions;// Direct dispatch for non-DOM targets (Window, XHR, AbortSignal) or DOM nodes with// property handlers. No propagation - just calls the handler and registered listeners.-// Handler can be: null, ?js.Function.Global or js.Function+// Handler can be: null, ?js.Function.Global, ?js.Function.Temp, or js.Functionpub fn dispatchDirect(self: *EventManager, target: *EventTarget, event: *Event, handler: anytype, comptime opts: DispatchDirectOptions) !void {const frame = self.frame;diff --git a/src/browser/EventManagerBase.zig b/src/browser/EventManagerBase.zigindex 335a98dc0..c18253426 100644--- a/src/browser/EventManagerBase.zig+++ b/src/browser/EventManagerBase.zig@@ -223,7 +223,7 @@ pub const DispatchDirectOptions = struct {/// Direct dispatch for non-DOM targets. No propagation - just calls the property/// handler and registered listeners. Caller is responsible for event ref counting.-/// Handler can be: null, ?js.Function.Global or js.Function+/// Handler can be: null, ?js.Function.Global, ?js.Function.Temp, or js.Functionpub fn dispatchDirect(self: *EventManagerBase,arena: Allocator,@@ -359,6 +359,7 @@ fn getFunction(handler: anytype, local: *const js.Local) ?js.Function {}return switch (T) {js.Function => handler,+ js.Function.Temp => local.toLocal(handler),js.Function.Global => local.toLocal(handler),else => @compileError("handler must be null or \\??js.Function(\\.(Temp|Global))?"),};diff --git a/src/browser/Page.zig b/src/browser/Page.zigindex 793090f83..3336e4111 100644--- a/src/browser/Page.zig+++ b/src/browser/Page.zig@@ -20,6 +20,7 @@ const std = @import("std");const builtin = @import("builtin");const js = @import("js/js.zig");+const v8 = js.v8;const Frame = @import("Frame.zig");const Session = @import("Session.zig");@@ -80,10 +81,11 @@ identity: js.Identity = .{},// weak-callback safety.finalizer_callbacks: std.AutoHashMapUnmanaged(usize, *js.FinalizerCallback) = .empty,-// Persisted v8 handles owned by this Page. Handles that outlive the Page are-// reset on teardown; handles that can be released early are dropped-// individually. See js.GlobalTracker.-globals: js.GlobalTracker,+// Tracked global v8 objects that need to be released when the Page tears down.+globals: std.ArrayList(v8.Global) = .empty,++// Temporary v8 globals that can be released early. Key is global.data_ptr.+temps: std.AutoHashMapUnmanaged(usize, v8.Global) = .empty,// Double buffered so that, as we process one list of queued navigations, new// entries are added to the separate buffer. Prevents endless navigation loops@@ -142,7 +144,6 @@ pub fn init(self: *Page, session: *Session, frame_id: u32) !void {.frame = undefined,.frame_arena = frame_arena,.factory = Factory.init(frame_arena),- .globals = .init(session.browser.app.allocator),};self.queued_navigation = &self.queued_navigation_1;@@ -179,7 +180,20 @@ pub fn deinit(self: *Page) void {self.finalizer_callbacks = .empty;}- self.globals.deinit();+ {+ for (self.globals.items) |*global| {+ v8.v8__Global__Reset(global);+ }+ self.globals = .empty;+ }++ {+ var it = self.temps.valueIterator();+ while (it.next()) |global| {+ v8.v8__Global__Reset(global);+ }+ self.temps = .empty;+ }if (comptime IS_DEBUG) {std.debug.assert(self.origins.count() == 0);diff --git a/src/browser/js/Context.zig b/src/browser/js/Context.zigindex ae7da2545..085649ba3 100644--- a/src/browser/js/Context.zig+++ b/src/browser/js/Context.zig@@ -277,6 +277,14 @@ pub fn setOrigin(self: *Context, key: ?[]const u8) !void {}}+pub fn trackGlobal(self: *Context, global: v8.Global) !void {+ return self.page.globals.append(self.page.frame_arena, global);+}++pub fn trackTemp(self: *Context, global: v8.Global) !void {+ return self.page.temps.put(self.page.frame_arena, global.data_ptr, global);+}+pub const IdentityResult = struct {value_ptr: *v8.Global,found_existing: bool,@@ -1126,7 +1134,7 @@ fn enqueueMicrotask(self: *Context, callback: anytype) void {// this should be safe (I think). In whatever HandleScope a microtask is enqueued,// PerformCheckpoint should be run. So the v8::Local<v8::Function> should remain// valid. If we have problems with this, a simple solution is to provide a Zig-// wrapper for these callbacks which references a js.Function, on callback+// wrapper for these callbacks which references a js.Function.Temp, on callback// it executes the function and then releases the global.pub fn queueMicrotaskFunc(self: *Context, cb: js.Function) void {// Use context-specific microtask queue instead of isolate queuediff --git a/src/browser/js/Function.zig b/src/browser/js/Function.zigindex 318e45f0f..7ab174835 100644--- a/src/browser/js/Function.zig+++ b/src/browser/js/Function.zig@@ -227,7 +227,29 @@ pub fn getPropertyValue(self: *const Function, name: []const u8) !?js.Value {}pub fn persist(self: *const Function) !Global {- return .{ .slot = try js.newTrackedSlot(self.local.ctx, self.handle) };+ return self._persist(true);+}++pub fn temp(self: *const Function) !Temp {+ return self._persist(false);+}++fn _persist(self: *const Function, comptime is_global: bool) !(if (is_global) Global else Temp) {+ var ctx = self.local.ctx;++ var global: v8.Global = undefined;+ v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);+ if (comptime is_global) {+ try ctx.trackGlobal(global);+ return .{ .handle = global, .temps = {} };+ }+ try ctx.trackTemp(global);+ return .{ .handle = global, .temps = &ctx.page.temps };+}++pub fn tempWithThis(self: *const Function, value: anytype) !Temp {+ const with_this = try self.withThis(value);+ return with_this.temp();}pub fn persistWithThis(self: *const Function, value: anytype) !Global {@@ -235,23 +257,41 @@ pub fn persistWithThis(self: *const Function, value: anytype) !Global {return with_this.persist();}-// A cheap, copyable handle to a persisted function. See js.GlobalSlot.-pub const Global = struct {- slot: *js.GlobalSlot,+pub const Temp = G(.temp);+pub const Global = G(.global);- pub fn deinit(self: Global) void {- self.slot.release();- }- pub const release = deinit;+const GlobalType = enum(u8) {+ temp,+ global,+};- pub fn local(self: Global, l: *const js.Local) Function {- return .{- .local = l,- .handle = @ptrCast(v8.v8__Global__Get(&self.slot.handle, l.isolate.handle)),- };- }+fn G(comptime global_type: GlobalType) type {+ return struct {+ handle: v8.Global,+ temps: if (global_type == .temp) *std.AutoHashMapUnmanaged(usize, v8.Global) else void,- pub fn isEqual(self: Global, other: Function) bool {- return v8.v8__Global__IsEqual(&self.slot.handle, other.handle);- }-};+ const Self = @This();++ pub fn deinit(self: *Self) void {+ v8.v8__Global__Reset(&self.handle);+ }++ pub fn local(self: *const Self, l: *const js.Local) Function {+ return .{+ .local = l,+ .handle = @ptrCast(v8.v8__Global__Get(&self.handle, l.isolate.handle)),+ };+ }++ pub fn isEqual(self: *const Self, other: Function) bool {+ return v8.v8__Global__IsEqual(&self.handle, other.handle);+ }++ pub fn release(self: *const Self) void {+ if (self.temps.fetchRemove(self.handle.data_ptr)) |kv| {+ var g = kv.value;+ v8.v8__Global__Reset(&g);+ }+ }+ };+}diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zigindex b24340c9b..ad4ab25db 100644--- a/src/browser/js/Local.zig+++ b/src/browser/js/Local.zig@@ -275,10 +275,7 @@ pub fn mapZigInstanceToJs(self: *const Local, js_obj_handle: ?*const v8.Object,const gop = try ctx.addIdentity(resolved_ptr_id);if (gop.found_existing) {// we've seen this instance before, return the same object- return .{- .local = self,- .handle = @ptrCast(v8.v8__Global__Get(gop.value_ptr, self.isolate.handle)),- };+ return (js.Object.Global{ .handle = gop.value_ptr.* }).local(self);}const isolate = self.isolate;@@ -471,9 +468,12 @@ pub fn zigValueToJs(self: *const Local, value: anytype, comptime opts: CallOpts)inlinejs.Function.Global,+ js.Function.Temp,js.Value.Global,+ js.Value.Temp,js.Object.Global,js.Promise.Global,+ js.Promise.Temp,js.PromiseResolver.Global,js.Module.Global => return .{ .local = self, .handle = @ptrCast(value.local(self).handle) },@@ -743,13 +743,14 @@ pub fn jsValueToZig(self: *const Local, comptime T: type, js_val: js.Value) !T {// probeJsValueToZig. Avoids having to duplicate this logic when probing.fn jsValueToStruct(self: *const Local, comptime T: type, js_val: js.Value) !?T {return switch (T) {- js.Function, js.Function.Global => {+ js.Function, js.Function.Global, js.Function.Temp => {if (!js_val.isFunction()) {return null;}const js_func = js.Function{ .local = self, .handle = @ptrCast(js_val.handle) };return switch (T) {js.Function => js_func,+ js.Function.Temp => try js_func.temp(),js.Function.Global => try js_func.persist(),else => unreachable,};@@ -766,6 +767,7 @@ fn jsValueToStruct(self: *const Local, comptime T: type, js_val: js.Value) !?T {},js.Value => js_val,js.Value.Global => return try js_val.persist(),+ js.Value.Temp => return try js_val.temp(),js.Object => {if (!js_val.isObject()) {return null;@@ -786,7 +788,7 @@ fn jsValueToStruct(self: *const Local, comptime T: type, js_val: js.Value) !?T {return try obj.persist();},- js.Promise.Global => {+ js.Promise.Global, js.Promise.Temp => {if (!js_val.isPromise()) {return null;}@@ -794,7 +796,11 @@ fn jsValueToStruct(self: *const Local, comptime T: type, js_val: js.Value) !?T {.local = self,.handle = @ptrCast(js_val.handle),};- return try js_promise.persist();+ return switch (T) {+ js.Promise.Temp => try js_promise.temp(),+ js.Promise.Global => try js_promise.persist(),+ else => unreachable,+ };},js.String => return js_val.isString(),js.String.OneByte => {diff --git a/src/browser/js/Object.zig b/src/browser/js/Object.zigindex 8869a9060..138997820 100644--- a/src/browser/js/Object.zig+++ b/src/browser/js/Object.zig@@ -92,7 +92,14 @@ pub fn format(self: Object, writer: *std.Io.Writer) !void {}pub fn persist(self: Object) !Global {- return .{ .slot = try js.newTrackedSlot(self.local.ctx, self.handle) };+ var ctx = self.local.ctx;++ var global: v8.Global = undefined;+ v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);++ try ctx.trackGlobal(global);++ return .{ .handle = global };}pub fn getFunction(self: Object, name: []const u8) !?js.Function {@@ -164,24 +171,21 @@ pub fn toZig(self: Object, comptime T: type) !T {}pub const Global = struct {- slot: *js.GlobalSlot,+ handle: v8.Global,- pub fn deinit(self: Global) void {- self.slot.release();+ pub fn deinit(self: *Global) void {+ v8.v8__Global__Reset(&self.handle);}- // TODO: deprecated. @GlobalSlot- pub const release = deinit;-- pub fn local(self: Global, l: *const js.Local) Object {+ pub fn local(self: *const Global, l: *const js.Local) Object {return .{.local = l,- .handle = @ptrCast(v8.v8__Global__Get(&self.slot.handle, l.isolate.handle)),+ .handle = @ptrCast(v8.v8__Global__Get(&self.handle, l.isolate.handle)),};}- pub fn isEqual(self: Global, other: Object) bool {- return v8.v8__Global__IsEqual(&self.slot.handle, other.handle);+ pub fn isEqual(self: *const Global, other: Object) bool {+ return v8.v8__Global__IsEqual(&self.handle, other.handle);}};diff --git a/src/browser/js/Promise.zig b/src/browser/js/Promise.zigindex 78a509f9b..20edc6516 100644--- a/src/browser/js/Promise.zig+++ b/src/browser/js/Promise.zig@@ -74,21 +74,57 @@ pub fn markAsHandled(self: Promise) void {}pub fn persist(self: Promise) !Global {- return .{ .slot = try js.newTrackedSlot(self.local.ctx, self.handle) };+ return self._persist(true);}-pub const Global = struct {- slot: *js.GlobalSlot,+pub fn temp(self: Promise) !Temp {+ return self._persist(false);+}- pub fn deinit(self: Global) void {- self.slot.release();- }- pub const release = deinit;+fn _persist(self: *const Promise, comptime is_global: bool) !(if (is_global) Global else Temp) {+ var ctx = self.local.ctx;- pub fn local(self: Global, l: *const js.Local) Promise {- return .{- .local = l,- .handle = @ptrCast(v8.v8__Global__Get(&self.slot.handle, l.isolate.handle)),- };+ var global: v8.Global = undefined;+ v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);+ if (comptime is_global) {+ try ctx.trackGlobal(global);+ return .{ .handle = global, .temps = {} };}+ try ctx.trackTemp(global);+ return .{ .handle = global, .temps = &ctx.page.temps };+}++pub const Temp = G(.temp);+pub const Global = G(.global);++const GlobalType = enum(u8) {+ temp,+ global,};++fn G(comptime global_type: GlobalType) type {+ return struct {+ handle: v8.Global,+ temps: if (global_type == .temp) *std.AutoHashMapUnmanaged(usize, v8.Global) else void,++ const Self = @This();++ pub fn deinit(self: *Self) void {+ v8.v8__Global__Reset(&self.handle);+ }++ pub fn local(self: *const Self, l: *const js.Local) Promise {+ return .{+ .local = l,+ .handle = @ptrCast(v8.v8__Global__Get(&self.handle, l.isolate.handle)),+ };+ }++ pub fn release(self: *const Self) void {+ if (self.temps.fetchRemove(self.handle.data_ptr)) |kv| {+ var g = kv.value;+ v8.v8__Global__Reset(&g);+ }+ }+ };+}diff --git a/src/browser/js/PromiseResolver.zig b/src/browser/js/PromiseResolver.zigindex 287ddd221..e99a3d0b7 100644--- a/src/browser/js/PromiseResolver.zig+++ b/src/browser/js/PromiseResolver.zig@@ -118,22 +118,24 @@ fn _reject(self: PromiseResolver, value: anytype) !void {}pub fn persist(self: PromiseResolver) !Global {- return .{ .slot = try js.newTrackedSlot(self.local.ctx, self.handle) };+ var ctx = self.local.ctx;+ var global: v8.Global = undefined;+ v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);+ try ctx.trackGlobal(global);+ return .{ .handle = global };}pub const Global = struct {- slot: *js.GlobalSlot,+ handle: v8.Global,- pub fn deinit(self: Global) void {- self.slot.release();+ pub fn deinit(self: *Global) void {+ v8.v8__Global__Reset(&self.handle);}- pub const release = deinit;-- pub fn local(self: Global, l: *const js.Local) PromiseResolver {+ pub fn local(self: *const Global, l: *const js.Local) PromiseResolver {return .{.local = l,- .handle = @ptrCast(v8.v8__Global__Get(&self.slot.handle, l.isolate.handle)),+ .handle = @ptrCast(v8.v8__Global__Get(&self.handle, l.isolate.handle)),};}};diff --git a/src/browser/js/Value.zig b/src/browser/js/Value.zigindex 08bb1b8b4..3ac76aa43 100644--- a/src/browser/js/Value.zig+++ b/src/browser/js/Value.zig@@ -622,17 +622,34 @@ const CloneDelegate = struct {};pub fn persist(self: Value) !Global {- return .{ .slot = try js.newTrackedSlot(self.local.ctx, self.handle) };+ return self._persist(true);}-// like persist, but not tracked by the page. Caller takes responsibility for-// resetting and freeing the allocation.-pub fn persistBare(self: Value, arena: std.mem.Allocator) !*js.GlobalSlot {- const slot = try arena.create(js.GlobalSlot);+pub fn temp(self: Value) !Temp {+ return self._persist(false);+}++// Like persist(), but not tracked on the context: the caller owns the handle+// and must deinit (Reset) it. A reset is only idempotent through the same+// instance — copies alias one v8 slot, so keep a single canonical instance and+// reset through it.+pub fn bare(self: Value) BareGlobal {var global: v8.Global = undefined;v8.v8__Global__New(self.local.ctx.isolate.handle, self.handle, &global);- slot.* = .{ .handle = global, .tracker = null, .gindex = undefined };- return slot;+ return .{ .handle = global, .temps = {} };+}++fn _persist(self: *const Value, comptime is_global: bool) !(if (is_global) Global else Temp) {+ var ctx = self.local.ctx;++ var global: v8.Global = undefined;+ v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);+ if (comptime is_global) {+ try ctx.trackGlobal(global);+ return .{ .handle = global, .temps = {} };+ }+ try ctx.trackTemp(global);+ return .{ .handle = global, .temps = &ctx.page.temps };}pub fn toZig(self: Value, comptime T: type) !T {@@ -679,71 +696,48 @@ pub fn format(self: Value, writer: *std.Io.Writer) !void {return js_str.format(writer);}-// Copyable handle to our v8::Global wrapper so that releasing a copy resets-// the underlying v8::Global-pub const Global = struct {- slot: *js.GlobalSlot,+pub const Temp = G(.temp);+pub const Global = G(.global);+pub const BareGlobal = G(.bare);- pub fn deinit(self: Global) void {- self.slot.release();- }- pub const release = deinit;-- pub fn local(self: Global, l: *const js.Local) Value {- return .{- .local = l,- .handle = @ptrCast(v8.v8__Global__Get(&self.slot.handle, l.isolate.handle)),- };- }-- pub fn isEqual(self: Global, other: Value) bool {- return v8.v8__Global__IsEqual(&self.slot.handle, other.handle);- }+const GlobalType = enum(u8) {+ temp,+ global,+ bare,};-const testing = @import("../../testing.zig");-test "Value: persisted handle early-release swap-removes and fixes up indices" {- const frame = try testing.createFrame();- defer testing.test_session.closeAllPages();-- var ls: js.Local.Scope = undefined;- frame.js.localScope(&ls);- defer ls.deinit();-- const tracker = &frame.js.page.globals;- const base = tracker.list.items.len;-- var a = try (try ls.local.exec("({a:1})", null)).persist();- var b = try (try ls.local.exec("({b:2})", null)).persist();- var c = try (try ls.local.exec("({c:3})", null)).persist();+fn G(comptime global_type: GlobalType) type {+ return struct {+ handle: v8.Global,+ temps: if (global_type == .temp) *std.AutoHashMapUnmanaged(usize, v8.Global) else void,- try testing.expectEqual(base + 3, tracker.list.items.len);- try testing.expectEqual(base + 0, a.slot.gindex);- try testing.expectEqual(base + 1, b.slot.gindex);- try testing.expectEqual(base + 2, c.slot.gindex);+ const Self = @This();- // Release the middle one: the last live slot (c) must move into b's spot and- // have its stored index rewritten to match, or a later release corrupts.- b.deinit();- try testing.expectEqual(base + 2, tracker.list.items.len);- try testing.expectEqual(base + 1, c.slot.gindex);- try testing.expectEqual(c.slot, tracker.list.items[base + 1]);- try testing.expectEqual(a.slot, tracker.list.items[base + 0]);+ pub fn deinit(self: *Self) void {+ v8.v8__Global__Reset(&self.handle);+ }- // a and c are still usable (right handle, not b's).- try testing.expect(a.local(&ls.local).isObject());- try testing.expect(c.local(&ls.local).isObject());+ pub fn local(self: *const Self, l: *const js.Local) Value {+ return .{+ .local = l,+ .handle = @ptrCast(v8.v8__Global__Get(&self.handle, l.isolate.handle)),+ };+ }- // Release the remaining two via the moved indices — must not corrupt.- a.deinit();- try testing.expectEqual(base + 1, tracker.list.items.len);- try testing.expectEqual(base + 0, c.slot.gindex);- try testing.expectEqual(c.slot, tracker.list.items[base + 0]);+ pub fn isEqual(self: *const Self, other: Value) bool {+ return v8.v8__Global__IsEqual(&self.handle, other.handle);+ }- c.deinit();- try testing.expectEqual(base, tracker.list.items.len);+ pub fn release(self: *const Self) void {+ if (self.temps.fetchRemove(self.handle.data_ptr)) |kv| {+ var g = kv.value;+ v8.v8__Global__Reset(&g);+ }+ }+ };}+const testing = @import("../../testing.zig");test "Value: jsonStringify maps unserializable JS values to null" {const frame = try testing.createFrame();defer testing.test_session.closeAllPages();diff --git a/src/browser/js/js.zig b/src/browser/js/js.zigindex 675f6955b..952c61f6d 100644--- a/src/browser/js/js.zig+++ b/src/browser/js/js.zig@@ -67,84 +67,6 @@ pub fn Bridge(comptime T: type) type {return bridge.Builder(T);}-// Our wrapper around a v8::Global designed to be tracked (in a GlobalTracker).-pub const GlobalSlot = struct {- handle: v8.Global,- tracker: ?*GlobalTracker, // null for Bare globals (see IndexedDB)- gindex: u32, // position in GlobalTracker, used to efficiently remove + reuse-- pub fn reset(self: *GlobalSlot) void {- v8.v8__Global__Reset(&self.handle);- }-- // Eager free: reset the handle and, if page-tracked, drop the slot from the- // tracker and return it to the pool. Idempotent for bare slots.- pub fn release(self: *GlobalSlot) void {- self.reset();- if (self.tracker) |t| {- t.untrack(self);- }- }-- pub fn local(self: *const GlobalSlot, l: *const Local) Value {- return .{- .local = l,- .handle = @ptrCast(v8.v8__Global__Get(&self.handle, l.isolate.handle)),- };- }-};--// Per-page owner of persisted v8 handles (v8::Global). Teardown resets all globals-pub const GlobalTracker = struct {- allocator: Allocator,- list: std.ArrayList(*GlobalSlot) = .empty,- pool: std.heap.MemoryPool(GlobalSlot),-- pub fn init(allocator: Allocator) GlobalTracker {- return .{ .allocator = allocator, .pool = std.heap.MemoryPool(GlobalSlot).init(allocator) };- }-- pub fn deinit(self: *GlobalTracker) void {- for (self.list.items) |slot| {- slot.reset();- }- self.list.deinit(self.allocator);- self.pool.deinit();- }-- pub fn track(self: *GlobalTracker, handle: v8.Global) !*GlobalSlot {- const slot = try self.pool.create();- errdefer self.pool.destroy(slot);- slot.* = .{- .handle = handle,- .tracker = self,- .gindex = @intCast(self.list.items.len),- };- try self.list.append(self.allocator, slot);- return slot;- }-- // swapRemove + updating the moved's index- fn untrack(self: *GlobalTracker, slot: *GlobalSlot) void {- const idx = slot.gindex;- const moved = self.list.pop().?;- if (moved != slot) {- self.list.items[idx] = moved;- // moved has..well...moved, we need to update its gindex- moved.gindex = idx;- }- self.pool.destroy(slot);- }-};--// Build a v8.Global from a live handle and track it on the context's page.-pub fn newTrackedSlot(ctx: *Context, handle: anytype) !*GlobalSlot {- var global: v8.Global = undefined;- v8.v8__Global__New(ctx.isolate.handle, handle, &global);- errdefer v8.v8__Global__Reset(&global);- return ctx.page.globals.track(global);-}-// If a function returns a []i32, should that map to a plain-old// JavaScript array, or a Int32Array? It's ambiguous. By default, we'll// map arrays/slices to the JavaScript arrays. If you want a TypedArray@@ -204,16 +126,14 @@ pub fn ArrayBufferRef(comptime kind: ArrayType) type {/// Persisted typed array.pub const Global = struct {- slot: *GlobalSlot,+ handle: v8.Global,- pub fn deinit(self: Global) void {- self.slot.release();+ pub fn deinit(self: *Global) void {+ v8.v8__Global__Reset(&self.handle);}- pub const release = deinit;-- pub fn local(self: Global, l: *const Local) Self {- return .{ .local = l, .handle = v8.v8__Global__Get(&self.slot.handle, l.isolate.handle).? };+ pub fn local(self: *const Global, l: *const Local) Self {+ return .{ .local = l, .handle = v8.v8__Global__Get(&self.handle, l.isolate.handle).? };}};@@ -253,7 +173,12 @@ pub fn ArrayBufferRef(comptime kind: ArrayType) type {}pub fn persist(self: *const Self) !Global {- return .{ .slot = try js.newTrackedSlot(self.local.ctx, self.handle) };+ var ctx = self.local.ctx;+ var global: v8.Global = undefined;+ v8.v8__Global__New(ctx.isolate.handle, self.handle, &global);+ try ctx.trackGlobal(global);++ return .{ .handle = global };}// Direct view into the typed array's backing memory.diff --git a/src/browser/webapi/BroadcastChannel.zig b/src/browser/webapi/BroadcastChannel.zigindex 457c5323b..b85b9f9a0 100644--- a/src/browser/webapi/BroadcastChannel.zig+++ b/src/browser/webapi/BroadcastChannel.zig@@ -88,7 +88,7 @@ pub fn postMessage(self: *BroadcastChannel, message: js.Value, exec: *Execution)const cloned = message.structuredCloneTo(&ls.local) catch {return error.DataClone;};- break :blk try cloned.persist();+ break :blk try cloned.temp();};errdefer snapshot.release();@@ -134,7 +134,7 @@ const PostMessageCallback = struct {sender: *BroadcastChannel,// A self-owned structured-clone snapshot of the posted message. Re-cloned// (never shared) into each receiver's MessageEvent, then released.- message: js.Value.Global,+ message: js.Value.Temp,exec: *Execution,post_sequence: u64,@@ -221,7 +221,7 @@ const PostMessageCallback = struct {continue;};- const cloned_temp = cloned.persist() catch |err| {+ const cloned_temp = cloned.temp() catch |err| {log.err(.dom, "BroadcastChannel.postMessage", .{ .err = err });continue;};diff --git a/src/browser/webapi/DedicatedWorkerGlobalScope.zig b/src/browser/webapi/DedicatedWorkerGlobalScope.zigindex d34c20645..311306d94 100644--- a/src/browser/webapi/DedicatedWorkerGlobalScope.zig+++ b/src/browser/webapi/DedicatedWorkerGlobalScope.zig@@ -48,7 +48,7 @@ _on_messageerror: ?js.Function.Global = null,// and delivered once the worker is ready (i.e. once onmessage can be set).// Drained by drainPendingMessages, called from Worker.loadInitialScript// after the initial script has been evaluated.-_pending_messages: std.ArrayList(?js.Value.Global) = .empty,+_pending_messages: std.ArrayList(?js.Value.Temp) = .empty,pub fn init(worker: *Worker, url: [:0]const u8) !*DedicatedWorkerGlobalScope {const self = try worker._arena.create(DedicatedWorkerGlobalScope);@@ -104,7 +104,7 @@ pub fn setOnMessageError(self: *DedicatedWorkerGlobalScope, setter: ?WorkerGlobaself._on_messageerror = WorkerGlobalScope.getFunctionFromSetter(setter);}-pub fn requestAnimationFrame(self: *DedicatedWorkerGlobalScope, cb: js.Function.Global, exec: *js.Execution) !u32 {+pub fn requestAnimationFrame(self: *DedicatedWorkerGlobalScope, cb: js.Function.Temp, exec: *js.Execution) !u32 {return self._proto._timers.schedule(exec, cb, 5, .{.repeat = false,.params = &.{},@@ -123,7 +123,7 @@ pub fn receiveMessage(self: *DedicatedWorkerGlobalScope, data: js.Value) !void {return;}- const cloned_data: ?js.Value.Global = blk: {+ const cloned_data: ?js.Value.Temp = blk: {// Enter our context to clone the messagevar ls: js.Local.Scope = undefined;self._proto.js.localScope(&ls);@@ -131,7 +131,7 @@ pub fn receiveMessage(self: *DedicatedWorkerGlobalScope, data: js.Value) !void {// clones from where it currently is (the Worker's Page context) to our Contextconst cloned = data.structuredCloneTo(&ls.local) catch break :blk null;- break :blk cloned.persist() catch break :blk null;+ break :blk cloned.temp() catch break :blk null;};if (!self._worker._script_loaded) {@@ -147,7 +147,7 @@ pub fn receiveMessage(self: *DedicatedWorkerGlobalScope, data: js.Value) !void {try self.scheduleMessage(cloned_data);}-fn scheduleMessage(self: *DedicatedWorkerGlobalScope, cloned_data: ?js.Value.Global) !void {+fn scheduleMessage(self: *DedicatedWorkerGlobalScope, cloned_data: ?js.Value.Temp) !void {const wgs = self._proto;const session = wgs._session;@@ -183,7 +183,7 @@ pub fn drainPendingMessages(self: *DedicatedWorkerGlobalScope) void {}const ReceiveMessageCallback = struct {- data: ?js.Value.Global,+ data: ?js.Value.Temp,arena: Allocator,worker_scope: *DedicatedWorkerGlobalScope,diff --git a/src/browser/webapi/FileReader.zig b/src/browser/webapi/FileReader.zigindex 0c7896331..4b2803a39 100644--- a/src/browser/webapi/FileReader.zig+++ b/src/browser/webapi/FileReader.zig@@ -42,12 +42,12 @@ _ready_state: ReadyState = .empty,_result: ?Result = null,_error: ?[]const u8 = null,-_on_abort: ?js.Function.Global = null,-_on_error: ?js.Function.Global = null,-_on_load: ?js.Function.Global = null,-_on_load_end: ?js.Function.Global = null,-_on_load_start: ?js.Function.Global = null,-_on_progress: ?js.Function.Global = null,+_on_abort: ?js.Function.Temp = null,+_on_error: ?js.Function.Temp = null,+_on_load: ?js.Function.Temp = null,+_on_load_end: ?js.Function.Temp = null,+_on_load_start: ?js.Function.Temp = null,+_on_progress: ?js.Function.Temp = null,_aborted: bool = false,@@ -97,51 +97,51 @@ fn asEventTarget(self: *FileReader) *EventTarget {return self._proto;}-pub fn getOnAbort(self: *const FileReader) ?js.Function.Global {+pub fn getOnAbort(self: *const FileReader) ?js.Function.Temp {return self._on_abort;}-pub fn setOnAbort(self: *FileReader, cb: ?js.Function.Global) !void {+pub fn setOnAbort(self: *FileReader, cb: ?js.Function.Temp) !void {self._on_abort = cb;}-pub fn getOnError(self: *const FileReader) ?js.Function.Global {+pub fn getOnError(self: *const FileReader) ?js.Function.Temp {return self._on_error;}-pub fn setOnError(self: *FileReader, cb: ?js.Function.Global) !void {+pub fn setOnError(self: *FileReader, cb: ?js.Function.Temp) !void {self._on_error = cb;}-pub fn getOnLoad(self: *const FileReader) ?js.Function.Global {+pub fn getOnLoad(self: *const FileReader) ?js.Function.Temp {return self._on_load;}-pub fn setOnLoad(self: *FileReader, cb: ?js.Function.Global) !void {+pub fn setOnLoad(self: *FileReader, cb: ?js.Function.Temp) !void {self._on_load = cb;}-pub fn getOnLoadEnd(self: *const FileReader) ?js.Function.Global {+pub fn getOnLoadEnd(self: *const FileReader) ?js.Function.Temp {return self._on_load_end;}-pub fn setOnLoadEnd(self: *FileReader, cb: ?js.Function.Global) !void {+pub fn setOnLoadEnd(self: *FileReader, cb: ?js.Function.Temp) !void {self._on_load_end = cb;}-pub fn getOnLoadStart(self: *const FileReader) ?js.Function.Global {+pub fn getOnLoadStart(self: *const FileReader) ?js.Function.Temp {return self._on_load_start;}-pub fn setOnLoadStart(self: *FileReader, cb: ?js.Function.Global) !void {+pub fn setOnLoadStart(self: *FileReader, cb: ?js.Function.Temp) !void {self._on_load_start = cb;}-pub fn getOnProgress(self: *const FileReader) ?js.Function.Global {+pub fn getOnProgress(self: *const FileReader) ?js.Function.Temp {return self._on_progress;}-pub fn setOnProgress(self: *FileReader, cb: ?js.Function.Global) !void {+pub fn setOnProgress(self: *FileReader, cb: ?js.Function.Temp) !void {self._on_progress = cb;}diff --git a/src/browser/webapi/IntersectionObserver.zig b/src/browser/webapi/IntersectionObserver.zigindex 8a70a7436..69bdfcd64 100644--- a/src/browser/webapi/IntersectionObserver.zig+++ b/src/browser/webapi/IntersectionObserver.zig@@ -41,7 +41,7 @@ const IntersectionObserver = @This();_rc: lp.RC(u8) = .{},_arena: Allocator,-_callback: js.Function.Global,+_callback: js.Function.Temp,_observing: std.ArrayList(*Element) = .{},_root: ?*Element = null,_root_margin: []const u8 = "0px",@@ -69,7 +69,7 @@ pub const ObserverInit = struct {};};-pub fn init(callback: js.Function.Global, options: ?ObserverInit, frame: *Frame) !*IntersectionObserver {+pub fn init(callback: js.Function.Temp, options: ?ObserverInit, frame: *Frame) !*IntersectionObserver {const arena = try frame.getArena(.small, "IntersectionObserver");errdefer frame.releaseArena(arena);diff --git a/src/browser/webapi/MessagePort.zig b/src/browser/webapi/MessagePort.zigindex b9cb57539..b82f8ed3f 100644--- a/src/browser/webapi/MessagePort.zig+++ b/src/browser/webapi/MessagePort.zig@@ -78,7 +78,7 @@ pub fn postMessage(self: *MessagePort, message: js.Value, exec: *Execution) !voiconst c = message.structuredCloneTo(&ls.local) catch {return error.DataClone;};- break :blk try c.persist();+ break :blk try c.temp();};errdefer cloned.release();diff --git a/src/browser/webapi/MutationObserver.zig b/src/browser/webapi/MutationObserver.zigindex 719f09567..6d3dab74e 100644--- a/src/browser/webapi/MutationObserver.zig+++ b/src/browser/webapi/MutationObserver.zig@@ -41,7 +41,7 @@ const MutationObserver = @This();_rc: lp.RC(u8) = .{},_arena: Allocator,-_callback: js.Function.Global,+_callback: js.Function.Temp,_observing: std.ArrayList(Observing) = .{},_pending_records: std.ArrayList(*MutationRecord) = .{},@@ -74,7 +74,7 @@ pub const ObserveOptions = struct {attributeFilter: ?[]const []const u8 = null,};-pub fn init(callback: js.Function.Global, frame: *Frame) !*MutationObserver {+pub fn init(callback: js.Function.Temp, frame: *Frame) !*MutationObserver {const arena = try frame.getArena(.small, "MutationObserver");errdefer frame.releaseArena(arena);const self = try arena.create(MutationObserver);diff --git a/src/browser/webapi/Timers.zig b/src/browser/webapi/Timers.zigindex 771935b6b..ac1c3c169 100644--- a/src/browser/webapi/Timers.zig+++ b/src/browser/webapi/Timers.zig@@ -68,7 +68,7 @@ pub const Mode = enum {pub const ScheduleOpts = struct {repeat: bool,- params: []js.Value.Global,+ params: []js.Value.Temp,name: []const u8,low_priority: bool = false,mode: Mode = .normal,@@ -77,7 +77,7 @@ pub const ScheduleOpts = struct {pub fn schedule(self: *Timers,exec: *js.Execution,- cb: js.Function.Global,+ cb: js.Function.Temp,delay_ms: u32,opts: ScheduleOpts,) !u32 {@@ -97,7 +97,7 @@ pub fn schedule(var persisted_params: []js.Value.Temp = &.{};if (opts.params.len > 0) {- persisted_params = try arena.dupe(js.Value.Global, opts.params);+ persisted_params = try arena.dupe(js.Value.Temp, opts.params);}const gop = try self._callbacks.getOrPut(exec.arena, timer_id);@@ -142,15 +142,15 @@ pub fn clear(self: *Timers, id: u32) void {// compiled into an anonymous function body, matching how legacy browsers// (and all current UAs) interpret `setTimeout("foo()", 100)`.pub const LegacyHandler = union(enum) {- function: js.Function.Global,+ function: js.Function.Temp,string: js.String,- pub fn resolve(handler: LegacyHandler, exec: *js.Execution) !js.Function.Global {+ pub fn resolve(handler: LegacyHandler, exec: *js.Execution) !js.Function.Temp {switch (handler) {.function => |fun| return fun,.string => |str| {const fun = try exec.js.local.?.compileFunction(str, &.{}, &.{});- return fun.persist();+ return fun.temp();},}}@@ -177,7 +177,7 @@ const ScheduleCallback = struct {timers: *Timers,arena: Allocator,removed: bool = false,- params: []const js.Value.Global,+ params: []const js.Value.Temp,fn cancelled(ptr: *anyopaque) void {var self: *ScheduleCallback = @ptrCast(@alignCast(ptr));diff --git a/src/browser/webapi/WebDriver.zig b/src/browser/webapi/WebDriver.zigindex c91d268f5..c87d0cfa9 100644--- a/src/browser/webapi/WebDriver.zig+++ b/src/browser/webapi/WebDriver.zig@@ -64,7 +64,7 @@ pub fn actionSequence(_: *const WebDriver, sources: js.Value, frame: *Frame) !voconst arena = try frame.getArena(.tiny, "WebDriver.actionSequence");errdefer frame.releaseArena(arena);- const persisted = try sources.persist();+ const persisted = try sources.temp();errdefer persisted.release();const action_sequence = try arena.create(ActionSequence);@@ -85,7 +85,7 @@ pub fn actionSequence(_: *const WebDriver, sources: js.Value, frame: *Frame) !voconst ActionSequence = struct {frame: *Frame,arena: Allocator,- sources: js.Value.Global,+ sources: js.Value.Temp,fn run(ptr: *anyopaque) !?u32 {const self: *ActionSequence = @ptrCast(@alignCast(ptr));diff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zigindex ecbb3e182..bfc7dd851 100644--- a/src/browser/webapi/Window.zig+++ b/src/browser/webapi/Window.zig@@ -416,7 +416,7 @@ pub fn fetch(_: *const Window, input: Fetch.Input, options: ?Fetch.InitOpts, exereturn Fetch.init(input, options, exec);}-pub fn setTimeout(self: *Window, handler: Timers.LegacyHandler, delay_ms: ?u32, params: []js.Value.Global, exec: *js.Execution) !u32 {+pub fn setTimeout(self: *Window, handler: Timers.LegacyHandler, delay_ms: ?u32, params: []js.Value.Temp, exec: *js.Execution) !u32 {const cb = try handler.resolve(exec);return self._timers.schedule(exec, cb, delay_ms orelse 0, .{.repeat = false,@@ -425,7 +425,7 @@ pub fn setTimeout(self: *Window, handler: Timers.LegacyHandler, delay_ms: ?u32,});}-pub fn setInterval(self: *Window, handler: Timers.LegacyHandler, delay_ms: ?u32, params: []js.Value.Global, exec: *js.Execution) !u32 {+pub fn setInterval(self: *Window, handler: Timers.LegacyHandler, delay_ms: ?u32, params: []js.Value.Temp, exec: *js.Execution) !u32 {const cb = try handler.resolve(exec);return self._timers.schedule(exec, cb, delay_ms orelse 0, .{.repeat = true,@@ -434,7 +434,7 @@ pub fn setInterval(self: *Window, handler: Timers.LegacyHandler, delay_ms: ?u32,});}-pub fn setImmediate(self: *Window, cb: js.Function.Global, params: []js.Value.Global, exec: *js.Execution) !u32 {+pub fn setImmediate(self: *Window, cb: js.Function.Temp, params: []js.Value.Temp, exec: *js.Execution) !u32 {return self._timers.schedule(exec, cb, 0, .{.repeat = false,.params = params,@@ -442,7 +442,7 @@ pub fn setImmediate(self: *Window, cb: js.Function.Global, params: []js.Value.Gl});}-pub fn requestAnimationFrame(self: *Window, cb: js.Function.Global, exec: *js.Execution) !u32 {+pub fn requestAnimationFrame(self: *Window, cb: js.Function.Temp, exec: *js.Execution) !u32 {return self._timers.schedule(exec, cb, 5, .{.repeat = false,.params = &.{},@@ -474,7 +474,7 @@ pub fn cancelAnimationFrame(self: *Window, id: u32) void {const RequestIdleCallbackOpts = struct {timeout: ?u32 = null,};-pub fn requestIdleCallback(self: *Window, cb: js.Function.Global, opts_: ?RequestIdleCallbackOpts, exec: *js.Execution) !u32 {+pub fn requestIdleCallback(self: *Window, cb: js.Function.Temp, opts_: ?RequestIdleCallbackOpts, exec: *js.Execution) !u32 {const opts = opts_ orelse RequestIdleCallbackOpts{};return self._timers.schedule(exec, cb, opts.timeout orelse 50, .{.mode = .idle,@@ -491,7 +491,7 @@ pub fn cancelIdleCallback(self: *Window, id: u32) void {pub fn reportError(self: *Window, err: js.Value, frame: *Frame) !void {const error_event = try ErrorEvent.initTrusted(comptime .wrap("error"), .{- .@"error" = try err.persist(),+ .@"error" = try err.temp(),.message = err.toStringSlice() catch "Unknown error",.bubbles = false,.cancelable = true,@@ -725,7 +725,7 @@ pub fn postMessage(self: *Window, message: js.Value, target_origin: ?[]const u8,const c = message.structuredCloneTo(&ls.local) catch {return error.DataClone;};- break :blk try c.persist();+ break :blk try c.temp();};errdefer cloned.release();@@ -926,8 +926,8 @@ pub fn unhandledPromiseRejection(self: *Window, no_handler: bool, rejection: js.const target = self.asEventTarget();if (frame._event_manager.hasDirectListeners(target, event_name, attribute_callback)) {const event = (try @import("event/PromiseRejectionEvent.zig").init(event_name, .{- .reason = if (rejection.reason()) |r| try r.persist() else null,- .promise = try rejection.promise().persist(),+ .reason = if (rejection.reason()) |r| try r.temp() else null,+ .promise = try rejection.promise().temp(),}, frame._page)).asEvent();try frame._event_manager.dispatchDirect(target, event, attribute_callback, .{ .context = "window.unhandledrejection" });}@@ -965,7 +965,7 @@ const PostMessageCallback = struct {source: *Window,arena: Allocator,origin: []const u8,- message: js.Value.Global,+ message: js.Value.Temp,ports: []const *MessagePort,fn deinit(self: *PostMessageCallback) void {diff --git a/src/browser/webapi/Worker.zig b/src/browser/webapi/Worker.zigindex bd16987db..9b2659ed7 100644--- a/src/browser/webapi/Worker.zig+++ b/src/browser/webapi/Worker.zig@@ -265,13 +265,13 @@ fn httpErrorCallback(ctx: *anyopaque, err: anyerror) void {}// Fire an error event on the Worker object (parent context)-fn fireErrorEvent(self: *Worker, message: []const u8, error_value: ?js.Value.Global) void {+fn fireErrorEvent(self: *Worker, message: []const u8, error_value: ?js.Value.Temp) void {self._fireErrorEvent(message, error_value) catch |err| {log.warn(.browser, "worker fire error", .{ .err = err, .message = message });};}-fn _fireErrorEvent(self: *Worker, message: []const u8, error_value: ?js.Value.Global) !void {+fn _fireErrorEvent(self: *Worker, message: []const u8, error_value: ?js.Value.Temp) !void {const frame = self._frame;const target = self.asEventTarget();const on_error = self._on_error;@@ -318,7 +318,7 @@ pub fn receiveMessage(self: *Worker, data: js.Value) !void {// clones from where it currently is (the Worker context) to our Page's contextconst cloned = data.structuredCloneTo(&ls.local) catch |err| break :blk err;- break :blk cloned.persist();+ break :blk cloned.temp();};const message_arena = try frame.getArena(.tiny, "Worker.receiveMessage");@@ -376,7 +376,7 @@ fn getFunctionFromSetter(setter_: ?FunctionSetter) ?js.Function.Global {}const ReceiveMessageCallback = struct {- data: anyerror!js.Value.Global,+ data: anyerror!js.Value.Temp,arena: Allocator,worker: *Worker,diff --git a/src/browser/webapi/WorkerGlobalScope.zig b/src/browser/webapi/WorkerGlobalScope.zigindex 7b3871bf5..92df5137f 100644--- a/src/browser/webapi/WorkerGlobalScope.zig+++ b/src/browser/webapi/WorkerGlobalScope.zig@@ -348,8 +348,8 @@ pub fn unhandledPromiseRejection(self: *WorkerGlobalScope, no_handler: bool, rejconst target = self.asEventTarget();if (self._event_manager.hasDirectListeners(target, event_name, attribute_callback)) {const event = (try @import("event/PromiseRejectionEvent.zig").init(event_name, .{- .reason = if (rejection.reason()) |r| try r.persist() else null,- .promise = try rejection.promise().persist(),+ .reason = if (rejection.reason()) |r| try r.temp() else null,+ .promise = try rejection.promise().temp(),}, self._page)).asEvent();try self.dispatch(target, event, attribute_callback, .{});}@@ -423,7 +423,7 @@ fn importScript(self: *WorkerGlobalScope, arena: Allocator, url: [:0]const u8) !pub fn reportError(self: *WorkerGlobalScope, err: JS.Value) !void {const error_event = try ErrorEvent.initTrusted(comptime .wrap("error"), .{- .@"error" = try err.persist(),+ .@"error" = try err.temp(),.message = err.toStringSlice() catch "Unknown error",.bubbles = false,.cancelable = true,@@ -479,7 +479,7 @@ pub fn queueMicrotask(self: *WorkerGlobalScope, cb: JS.Function) void {self.js.queueMicrotaskFunc(cb);}-pub fn setTimeout(self: *WorkerGlobalScope, handler: Timers.LegacyHandler, delay_ms: ?u32, params: []JS.Value.Global, exec: *JS.Execution) !u32 {+pub fn setTimeout(self: *WorkerGlobalScope, handler: Timers.LegacyHandler, delay_ms: ?u32, params: []JS.Value.Temp, exec: *JS.Execution) !u32 {const cb = try handler.resolve(exec);return self._timers.schedule(exec, cb, delay_ms orelse 0, .{.repeat = false,@@ -492,7 +492,7 @@ pub fn clearTimeout(self: *WorkerGlobalScope, id: u32) void {self._timers.clear(id);}-pub fn setInterval(self: *WorkerGlobalScope, handler: Timers.LegacyHandler, delay_ms: ?u32, params: []JS.Value.Global, exec: *JS.Execution) !u32 {+pub fn setInterval(self: *WorkerGlobalScope, handler: Timers.LegacyHandler, delay_ms: ?u32, params: []JS.Value.Temp, exec: *JS.Execution) !u32 {const cb = try handler.resolve(exec);return self._timers.schedule(exec, cb, delay_ms orelse 0, .{.repeat = true,diff --git a/src/browser/webapi/animation/Animation.zig b/src/browser/webapi/animation/Animation.zigindex 5880df9dd..91ddff41e 100644--- a/src/browser/webapi/animation/Animation.zig+++ b/src/browser/webapi/animation/Animation.zig@@ -43,7 +43,7 @@ _timeline: ?js.Object.Global = null,_ready_resolver: ?js.PromiseResolver.Global = null,_finished_resolver: ?js.PromiseResolver.Global = null,_startTime: ?f64 = null,-_onFinish: ?js.Function.Global = null,+_onFinish: ?js.Function.Temp = null,_playState: PlayState = .idle,// Fake the animation by passing the states:@@ -179,7 +179,7 @@ pub fn setStartTime(self: *Animation, value: ?f64, frame: *Frame) !void {return self.play(frame);}-pub fn getOnFinish(self: *const Animation) ?js.Function.Global {+pub fn getOnFinish(self: *const Animation) ?js.Function.Temp {return self._onFinish;}@@ -215,7 +215,7 @@ fn update(ctx: *anyopaque) !?u32 {return null;}-pub fn setOnFinish(self: *Animation, cb: ?js.Function.Global) !void {+pub fn setOnFinish(self: *Animation, cb: ?js.Function.Temp) !void {self._onFinish = cb;}diff --git a/src/browser/webapi/event/CustomEvent.zig b/src/browser/webapi/event/CustomEvent.zigindex 381f0cdaa..338451811 100644--- a/src/browser/webapi/event/CustomEvent.zig+++ b/src/browser/webapi/event/CustomEvent.zig@@ -30,11 +30,11 @@ const Allocator = std.mem.Allocator;const CustomEvent = @This();_proto: *Event,-_detail: ?js.Value.Global = null,+_detail: ?js.Value.Temp = null,_arena: Allocator,const CustomEventOptions = struct {- detail: ?js.Value.Global = null,+ detail: ?js.Value.Temp = null,};const Options = Event.inheritOptions(CustomEvent, CustomEventOptions);@@ -64,7 +64,7 @@ pub fn initCustomEvent(event_string: []const u8,bubbles: ?bool,cancelable: ?bool,- detail_: ?js.Value.Global,+ detail_: ?js.Value.Temp,) !void {// This function can only be called after the constructor has called.// So we assume proto is initialized already by constructor.@@ -94,7 +94,7 @@ pub fn asEvent(self: *CustomEvent) *Event {return self._proto;}-pub fn getDetail(self: *const CustomEvent) ?js.Value.Global {+pub fn getDetail(self: *const CustomEvent) ?js.Value.Temp {return self._detail;}diff --git a/src/browser/webapi/event/ErrorEvent.zig b/src/browser/webapi/event/ErrorEvent.zigindex 672422f10..de7c1d927 100644--- a/src/browser/webapi/event/ErrorEvent.zig+++ b/src/browser/webapi/event/ErrorEvent.zig@@ -34,7 +34,7 @@ _message: []const u8 = "",_filename: []const u8 = "",_line_number: u32 = 0,_column_number: u32 = 0,-_error: ?js.Value.Global = null,+_error: ?js.Value.Temp = null,_arena: Allocator,pub const ErrorEventOptions = struct {@@ -42,7 +42,7 @@ pub const ErrorEventOptions = struct {filename: ?[]const u8 = null,lineno: u32 = 0,colno: u32 = 0,- @"error": ?js.Value.Global = null,+ @"error": ?js.Value.Temp = null,};const Options = Event.inheritOptions(ErrorEvent, ErrorEventOptions);@@ -116,7 +116,7 @@ pub fn getColumnNumber(self: *const ErrorEvent) u32 {return self._column_number;}-pub fn getError(self: *const ErrorEvent) ?js.Value.Global {+pub fn getError(self: *const ErrorEvent) ?js.Value.Temp {return self._error;}diff --git a/src/browser/webapi/event/MessageEvent.zig b/src/browser/webapi/event/MessageEvent.zigindex 9ed49ce1b..6874b5b7e 100644--- a/src/browser/webapi/event/MessageEvent.zig+++ b/src/browser/webapi/event/MessageEvent.zig@@ -46,7 +46,7 @@ const MessageEventOptions = struct {};pub const Data = union(enum) {- value: js.Value.Global,+ value: js.Value.Temp,string: []const u8,arraybuffer: js.ArrayBuffer,blob: *@import("../Blob.zig"),diff --git a/src/browser/webapi/event/PromiseRejectionEvent.zig b/src/browser/webapi/event/PromiseRejectionEvent.zigindex a622a5d49..20ebb0a7c 100644--- a/src/browser/webapi/event/PromiseRejectionEvent.zig+++ b/src/browser/webapi/event/PromiseRejectionEvent.zig@@ -27,12 +27,12 @@ const String = lp.String;const PromiseRejectionEvent = @This();_proto: *Event,-_reason: ?js.Value.Global = null,-_promise: ?js.Promise.Global = null,+_reason: ?js.Value.Temp = null,+_promise: ?js.Promise.Temp = null,const PromiseRejectionEventOptions = struct {- reason: ?js.Value.Global = null,- promise: ?js.Promise.Global = null,+ reason: ?js.Value.Temp = null,+ promise: ?js.Promise.Temp = null,};const Options = Event.inheritOptions(PromiseRejectionEvent, PromiseRejectionEventOptions);@@ -79,11 +79,11 @@ pub fn asEvent(self: *PromiseRejectionEvent) *Event {return self._proto;}-pub fn getReason(self: *const PromiseRejectionEvent) ?js.Value.Global {+pub fn getReason(self: *const PromiseRejectionEvent) ?js.Value.Temp {return self._reason;}-pub fn getPromise(self: *const PromiseRejectionEvent) ?js.Promise.Global {+pub fn getPromise(self: *const PromiseRejectionEvent) ?js.Promise.Temp {return self._promise;}diff --git a/src/browser/webapi/net/WebSocket.zig b/src/browser/webapi/net/WebSocket.zigindex 8db34e857..4d4268a88 100644--- a/src/browser/webapi/net/WebSocket.zig+++ b/src/browser/webapi/net/WebSocket.zig@@ -75,10 +75,10 @@ _close_reason: []const u8 = "",_protocol: []const u8 = "",// Event handlers-_on_open: ?js.Function.Global = null,-_on_message: ?js.Function.Global = null,-_on_error: ?js.Function.Global = null,-_on_close: ?js.Function.Global = null,+_on_open: ?js.Function.Temp = null,+_on_message: ?js.Function.Temp = null,+_on_error: ?js.Function.Temp = null,+_on_close: ?js.Function.Temp = null,pub const ReadyState = enum(u8) {connecting = 0,@@ -434,53 +434,53 @@ pub fn setBinaryType(self: *WebSocket, value: []const u8) void {}}-pub fn getOnOpen(self: *const WebSocket) ?js.Function.Global {+pub fn getOnOpen(self: *const WebSocket) ?js.Function.Temp {return self._on_open;}pub fn setOnOpen(self: *WebSocket, cb_: ?js.Function) !void {if (self._on_open) |old| old.release();if (cb_) |cb| {- self._on_open = try cb.persistWithThis(self);+ self._on_open = try cb.tempWithThis(self);} else {self._on_open = null;}}-pub fn getOnMessage(self: *const WebSocket) ?js.Function.Global {+pub fn getOnMessage(self: *const WebSocket) ?js.Function.Temp {return self._on_message;}pub fn setOnMessage(self: *WebSocket, cb_: ?js.Function) !void {if (self._on_message) |old| old.release();if (cb_) |cb| {- self._on_message = try cb.persistWithThis(self);+ self._on_message = try cb.tempWithThis(self);} else {self._on_message = null;}}-pub fn getOnError(self: *const WebSocket) ?js.Function.Global {+pub fn getOnError(self: *const WebSocket) ?js.Function.Temp {return self._on_error;}pub fn setOnError(self: *WebSocket, cb_: ?js.Function) !void {if (self._on_error) |old| old.release();if (cb_) |cb| {- self._on_error = try cb.persistWithThis(self);+ self._on_error = try cb.tempWithThis(self);} else {self._on_error = null;}}-pub fn getOnClose(self: *const WebSocket) ?js.Function.Global {+pub fn getOnClose(self: *const WebSocket) ?js.Function.Temp {return self._on_close;}pub fn setOnClose(self: *WebSocket, cb_: ?js.Function) !void {if (self._on_close) |old| old.release();if (cb_) |cb| {- self._on_close = try cb.persistWithThis(self);+ self._on_close = try cb.tempWithThis(self);} else {self._on_close = null;}diff --git a/src/browser/webapi/net/XMLHttpRequest.zig b/src/browser/webapi/net/XMLHttpRequest.zigindex 5d17730de..278658efc 100644--- a/src/browser/webapi/net/XMLHttpRequest.zig+++ b/src/browser/webapi/net/XMLHttpRequest.zig@@ -71,7 +71,7 @@ _response_headers: std.ArrayList([]const u8) = .empty,_response_type: ResponseType = .text,_ready_state: ReadyState = .unsent,-_on_ready_state_change: ?js.Function.Global = null,+_on_ready_state_change: ?js.Function.Temp = null,_with_credentials: bool = false,_timeout: u32 = 0,@@ -147,13 +147,13 @@ fn asEventTarget(self: *XMLHttpRequest) *EventTarget {return self._proto._proto;}-pub fn getOnReadyStateChange(self: *const XMLHttpRequest) ?js.Function.Global {+pub fn getOnReadyStateChange(self: *const XMLHttpRequest) ?js.Function.Temp {return self._on_ready_state_change;}pub fn setOnReadyStateChange(self: *XMLHttpRequest, cb_: ?js.Function) !void {if (cb_) |cb| {- self._on_ready_state_change = try cb.persistWithThis(self);+ self._on_ready_state_change = try cb.tempWithThis(self);} else {self._on_ready_state_change = null;}diff --git a/src/browser/webapi/net/XMLHttpRequestEventTarget.zig b/src/browser/webapi/net/XMLHttpRequestEventTarget.zigindex cfa258fc0..a5b64dc0b 100644--- a/src/browser/webapi/net/XMLHttpRequestEventTarget.zig+++ b/src/browser/webapi/net/XMLHttpRequestEventTarget.zig@@ -27,13 +27,13 @@ const XMLHttpRequestEventTarget = @This();_type: Type,_proto: *EventTarget,-_on_abort: ?js.Function.Global = null,-_on_error: ?js.Function.Global = null,-_on_load: ?js.Function.Global = null,-_on_load_end: ?js.Function.Global = null,-_on_load_start: ?js.Function.Global = null,-_on_progress: ?js.Function.Global = null,-_on_timeout: ?js.Function.Global = null,+_on_abort: ?js.Function.Temp = null,+_on_error: ?js.Function.Temp = null,+_on_load: ?js.Function.Temp = null,+_on_load_end: ?js.Function.Temp = null,+_on_load_start: ?js.Function.Temp = null,+_on_progress: ?js.Function.Temp = null,+_on_timeout: ?js.Function.Temp = null,pub const Type = union(enum) {request: *@import("XMLHttpRequest.zig"),@@ -83,61 +83,61 @@ pub fn dispatch(self: *XMLHttpRequestEventTarget, comptime event_type: DispatchT);}-pub fn getOnAbort(self: *const XMLHttpRequestEventTarget) ?js.Function.Global {+pub fn getOnAbort(self: *const XMLHttpRequestEventTarget) ?js.Function.Temp {return self._on_abort;}-pub fn setOnAbort(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Global) !void {+pub fn setOnAbort(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Temp) !void {self._on_abort = cb;}-pub fn getOnError(self: *const XMLHttpRequestEventTarget) ?js.Function.Global {+pub fn getOnError(self: *const XMLHttpRequestEventTarget) ?js.Function.Temp {return self._on_error;}-pub fn setOnError(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Global) !void {+pub fn setOnError(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Temp) !void {self._on_error = cb;}-pub fn getOnLoad(self: *const XMLHttpRequestEventTarget) ?js.Function.Global {+pub fn getOnLoad(self: *const XMLHttpRequestEventTarget) ?js.Function.Temp {return self._on_load;}-pub fn setOnLoad(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Global) !void {+pub fn setOnLoad(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Temp) !void {self._on_load = cb;}-pub fn getOnLoadEnd(self: *const XMLHttpRequestEventTarget) ?js.Function.Global {+pub fn getOnLoadEnd(self: *const XMLHttpRequestEventTarget) ?js.Function.Temp {return self._on_load_end;}-pub fn setOnLoadEnd(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Global) !void {+pub fn setOnLoadEnd(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Temp) !void {self._on_load_end = cb;}-pub fn getOnLoadStart(self: *const XMLHttpRequestEventTarget) ?js.Function.Global {+pub fn getOnLoadStart(self: *const XMLHttpRequestEventTarget) ?js.Function.Temp {return self._on_load_start;}-pub fn setOnLoadStart(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Global) !void {+pub fn setOnLoadStart(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Temp) !void {self._on_load_start = cb;}-pub fn getOnProgress(self: *const XMLHttpRequestEventTarget) ?js.Function.Global {+pub fn getOnProgress(self: *const XMLHttpRequestEventTarget) ?js.Function.Temp {return self._on_progress;}-pub fn setOnProgress(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Global) !void {+pub fn setOnProgress(self: *XMLHttpRequestEventTarget, cb: ?js.Function.Temp) !void {self._on_progress = cb;}-pub fn getOnTimeout(self: *const XMLHttpRequestEventTarget) ?js.Function.Global {+pub fn getOnTimeout(self: *const XMLHttpRequestEventTarget) ?js.Function.Temp {return self._on_timeout;}pub fn setOnTimeout(self: *XMLHttpRequestEventTarget, cb_: ?js.Function) !void {if (cb_) |cb| {- self._on_timeout = try cb.persistWithThis(self);+ self._on_timeout = try cb.tempWithThis(self);} else {self._on_timeout = null;}diff --git a/src/browser/webapi/storage/idb/IDBCursor.zig b/src/browser/webapi/storage/idb/IDBCursor.zigindex 809474be4..e347f0557 100644--- a/src/browser/webapi/storage/idb/IDBCursor.zig+++ b/src/browser/webapi/storage/idb/IDBCursor.zig@@ -50,7 +50,7 @@ _index_id: ?i64 = null,// the JS value of this Cursor, pre-converted and cached as an optimization// since this cursor will be the request value on every iteration.-_js: *js.GlobalSlot,+_js: *js.Value.BareGlobal,// Encoded current key; null before iteration and at the end. For an index cursor// this is the index key; for an object store it equals the primary key.@@ -62,7 +62,7 @@ _primary_key: ?[]const u8 = null,_value: ?[]const u8 = null,// The deserialized JS value, cached so repeated `.value` reads return the same// object (and observe mutations to it). Reset whenever the cursor repositions.-_value_js: ?*js.GlobalSlot = null,+_value_js: ?*js.Value.BareGlobal = null,// Backing storage for _key/_primary_key/_value, reused across positions so a// long scan holds one record's worth of memory, not the whole traversal.@@ -384,7 +384,7 @@ fn exhaust(self: *IDBCursor) void {// value until transaction teardown.fn invalidateValue(self: *IDBCursor) void {if (self._value_js) |slot| {- slot.reset();+ slot.deinit();self._value_js = null;}}diff --git a/src/browser/webapi/storage/idb/IDBIndex.zig b/src/browser/webapi/storage/idb/IDBIndex.zigindex 88aa4670c..9aa342120 100644--- a/src/browser/webapi/storage/idb/IDBIndex.zig+++ b/src/browser/webapi/storage/idb/IDBIndex.zig@@ -49,7 +49,7 @@ _deleted: bool = false,_created: bool = false,// not just for efficiency, we must return the same v8::Array every time the// compound key is accessed.-_key_path_js: ?*js.GlobalSlot = null,+_key_path_js: ?*js.Value.BareGlobal = null,pub fn init(obj_store: *IDBObjectStore, info: Engine.IndexInfo, name: []const u8) !*IDBIndex {const self = try obj_store._txn._arena.create(IDBIndex);diff --git a/src/browser/webapi/storage/idb/IDBObjectStore.zig b/src/browser/webapi/storage/idb/IDBObjectStore.zigindex dc8b155aa..31034f08e 100644--- a/src/browser/webapi/storage/idb/IDBObjectStore.zig+++ b/src/browser/webapi/storage/idb/IDBObjectStore.zig@@ -52,7 +52,7 @@ _created: bool = false,_indexes: std.ArrayList(*IDBIndex) = .empty,// not just for efficiency, we must return the same v8::Array every time the// compound key is accessed.-_key_path_js: ?*js.GlobalSlot = null,+_key_path_js: ?*js.Value.BareGlobal = null,pub fn init(txn: *IDBTransaction,@@ -358,10 +358,10 @@ fn write(self: *IDBObjectStore, value: js.Value, key_arg: ?js.Value, kind: Write} }, exec);}-pub fn runWrite(self: *IDBObjectStore, request: *IDBRequest, kind: WriteKind, value_global: *js.GlobalSlot, prepared: PreparedKey, exec: *Execution) !void {+pub fn runWrite(self: *IDBObjectStore, request: *IDBRequest, kind: WriteKind, value_global: *js.Value.BareGlobal, prepared: PreparedKey, exec: *Execution) !void {// Written (or failed) is written: the pinned value is dead once this op// ran, so release its handle now instead of at transaction teardown.- defer value_global.reset();+ defer value_global.deinit();self.writeInner(request, kind, value_global, prepared, exec) catch |err| {if (err != error.Constraint) {log.warn(.storage, "idb write", .{ .err = err, .kind = kind, .sqlite = self._engine.lastError() });@@ -370,7 +370,7 @@ pub fn runWrite(self: *IDBObjectStore, request: *IDBRequest, kind: WriteKind, va};}-fn writeInner(self: *IDBObjectStore, request: *IDBRequest, kind: WriteKind, value_global: *js.GlobalSlot, prepared: PreparedKey, exec: *Execution) !void {+fn writeInner(self: *IDBObjectStore, request: *IDBRequest, kind: WriteKind, value_global: *js.Value.BareGlobal, prepared: PreparedKey, exec: *Execution) !void {const local = exec.js.local.?;const value = value_global.local(local);diff --git a/src/browser/webapi/storage/idb/IDBRequest.zig b/src/browser/webapi/storage/idb/IDBRequest.zigindex 7c18f3838..7be3f39a1 100644--- a/src/browser/webapi/storage/idb/IDBRequest.zig+++ b/src/browser/webapi/storage/idb/IDBRequest.zig@@ -72,7 +72,7 @@ const ReadyState = enum {const Result = union(enum) {none: ?js.Undefined, // null or undefined (different APIs return different values)- value: *js.GlobalSlot, // the result of a get/add/put, or a positioned cursor+ value: *js.Value.BareGlobal, // the result of a get/add/put, or a positioned cursordatabase: *IDBDatabase, // the result of an open};@@ -139,8 +139,8 @@ fn clearOwnedResult(self: *IDBRequest) void {self._result_owned = false;switch (self._result) {.value => |global| {- // It's ok to keep this in txn._globals, reset can be called multiple times- global.reset();+ // It's ok to keep this in txn._globals, deinit can be called multiple times+ global.deinit();self._result = .{ .none = js.Undefined{} };},.none, .database => {},@@ -159,7 +159,7 @@ pub fn setValue(self: *IDBRequest, value: js.Value) !void {// Not exposed to JS, called internally. The handle is borrowed (a cursor's// transaction-owned _js), not owned by this request.-pub fn setValueGlobal(self: *IDBRequest, global: *js.GlobalSlot) void {+pub fn setValueGlobal(self: *IDBRequest, global: *js.Value.BareGlobal) void {self.clearOwnedResult();self._result = .{ .value = global };}@@ -371,7 +371,7 @@ pub const Operation = union(enum) {const StoreQuery = struct { store: *IDBObjectStore, bounds: Engine.Bounds };const StoreGetAll = struct { store: *IDBObjectStore, args: IDBKeyRange.GetAllArgs, mode: IDBObjectStore.GetAllMode };- const StoreWrite = struct { store: *IDBObjectStore, kind: IDBObjectStore.WriteKind, value: *js.GlobalSlot, key: IDBObjectStore.PreparedKey };+ const StoreWrite = struct { store: *IDBObjectStore, kind: IDBObjectStore.WriteKind, value: *js.Value.BareGlobal, key: IDBObjectStore.PreparedKey };const IndexQuery = struct { index: *IDBIndex, bounds: Engine.Bounds };const IndexGetAll = struct { index: *IDBIndex, args: IDBKeyRange.GetAllArgs, mode: IDBObjectStore.GetAllMode };const CursorIterate = struct { cursor: *IDBCursor, seek: IDBCursor.Seek, offset: u32 };diff --git a/src/browser/webapi/storage/idb/IDBTransaction.zig b/src/browser/webapi/storage/idb/IDBTransaction.zigindex 6b57ac96b..dc70727c9 100644--- a/src/browser/webapi/storage/idb/IDBTransaction.zig+++ b/src/browser/webapi/storage/idb/IDBTransaction.zig@@ -65,7 +65,7 @@ _arena: Allocator,// v8 handles owned by the transaction, swept (reset) in deinit. Slots are// arena-allocated so an early release and the sweep hit the same instance —// a v8 Global reset is only idempotent through a single instance.-_globals: std.ArrayList(*js.GlobalSlot) = .empty,+_globals: std.ArrayList(*js.Value.BareGlobal) = .empty,// objectStore() must return the same object for a given name within one// transaction (per spec); this also keeps repeated lookups off sqlite.@@ -180,7 +180,7 @@ pub fn deinit(self: *IDBTransaction, page: *Page) void {std.debug.assert(self._parked == false);}for (self._globals.items) |slot| {- slot.reset();+ slot.deinit();}page.releaseArena(self._arena);}@@ -194,12 +194,12 @@ pub fn releaseRef(self: *IDBTransaction, page: *Page) void {}// Persist a JS value with the transaction's lifetime: the handle is reset when the-// transaction's memory is released — or earlier, by calling reset() on the+// transaction's memory is released — or earlier, by calling deinit() on the// returned slot (the sweep's second reset is then a no-op).-pub fn persist(self: *IDBTransaction, value: js.Value) !*js.GlobalSlot {- const slot = try value.persistBare(self._arena);- errdefer slot.reset();+pub fn persist(self: *IDBTransaction, value: js.Value) !*js.Value.BareGlobal {+ const slot = try self._arena.create(js.Value.BareGlobal);try self._globals.append(self._arena, slot);+ slot.* = value.bare();return slot;}diff --git a/src/browser/webapi/storage/idb/idb.zig b/src/browser/webapi/storage/idb/idb.zigindex 33d7da71a..8b196c996 100644--- a/src/browser/webapi/storage/idb/idb.zig+++ b/src/browser/webapi/storage/idb/idb.zig@@ -54,7 +54,7 @@ pub fn registerTypes() []const type {// the keyPath attribute need identity equality. For a compound key (an array)// we have to return the same v8::rray on every reach. So users of KeyPath will// cache it locally, and store it in the Transaction's globals list for cleanup.-pub fn cachedKeyPathJs(cache: *?*js.GlobalSlot, txn: *IDBTransaction, kp: ?Key.KeyPath, exec: *js.Execution) !js.Value {+pub fn cachedKeyPathJs(cache: *?*js.Value.BareGlobal, txn: *IDBTransaction, kp: ?Key.KeyPath, exec: *js.Execution) !js.Value {const local = exec.js.local.?;if (kp) |path| {if (std.meta.activeTag(path) == .list) {