diff --git a/src/browser/js/Context.zig b/src/browser/js/Context.zig index 6ac0dae0..2e577265 100644 --- a/src/browser/js/Context.zig +++ b/src/browser/js/Context.zig @@ -14,6 +14,7 @@ const types = @import("types.zig"); const Caller = @import("Caller.zig"); const NamedFunction = Caller.NamedFunction; const PersistentObject = v8.Persistent(v8.Object); +const PersistentValue = v8.Persistent(v8.Value); const PersistentModule = v8.Persistent(v8.Module); const PersistentPromise = v8.Persistent(v8.Promise); const PersistentFunction = v8.Persistent(v8.Function); @@ -70,6 +71,9 @@ identity_map: std.AutoHashMapUnmanaged(usize, PersistentObject) = .empty, // we now simply persist every time persist() is called. js_object_list: std.ArrayListUnmanaged(PersistentObject) = .empty, +// js_value_list tracks persisted js values. +js_value_list: std.ArrayListUnmanaged(PersistentValue) = .empty, + // Various web APIs depend on having a persistent promise resolver. They // require for this PromiseResolver to be valid for a lifetime longer than // the function that resolves/rejects them. @@ -149,6 +153,10 @@ pub fn deinit(self: *Context) void { p.deinit(); } + for (self.js_value_list.items) |*p| { + p.deinit(); + } + for (self.persisted_promise_resolvers.items) |*p| { p.deinit(); } diff --git a/src/browser/js/js.zig b/src/browser/js/js.zig index 1504e846..a6af906f 100644 --- a/src/browser/js/js.zig +++ b/src/browser/js/js.zig @@ -148,6 +148,8 @@ pub const Exception = struct { }; pub const Value = struct { + const PersistentValue = v8.Persistent(v8.Value); + value: v8.Value, context: *const Context, @@ -159,7 +161,11 @@ pub const Value = struct { pub fn fromJson(ctx: *Context, json: []const u8) !Value { const json_string = v8.String.initUtf8(ctx.isolate, json); const value = try v8.Json.parse(ctx.v8_context, json_string); - return Value{ .context = ctx, .value = value }; + + const persisted = PersistentValue.init(ctx.isolate, value); + try ctx.js_value_list.append(ctx.arena, persisted); + + return Value{ .context = ctx, .value = persisted.toValue() }; } };