add Value.persist

This commit is contained in:
Pierre Tachoire
2025-12-25 12:39:09 +01:00
parent 8f2921f61f
commit d50f6b830a
2 changed files with 20 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ const ScriptManager = @import("../ScriptManager.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const PersistentObject = v8.Persistent(v8.Object); const PersistentObject = v8.Persistent(v8.Object);
const PersistentValue = v8.Persistent(v8.Value);
const PersistentModule = v8.Persistent(v8.Module); const PersistentModule = v8.Persistent(v8.Module);
const PersistentPromise = v8.Persistent(v8.Promise); const PersistentPromise = v8.Persistent(v8.Promise);
const PersistentFunction = v8.Persistent(v8.Function); const PersistentFunction = v8.Persistent(v8.Function);
@@ -85,6 +86,9 @@ identity_map: std.AutoHashMapUnmanaged(usize, PersistentObject) = .empty,
// we now simply persist every time persist() is called. // we now simply persist every time persist() is called.
js_object_list: std.ArrayListUnmanaged(PersistentObject) = .empty, 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 // Various web APIs depend on having a persistent promise resolver. They
// require for this PromiseResolver to be valid for a lifetime longer than // require for this PromiseResolver to be valid for a lifetime longer than
// the function that resolves/rejects them. // the function that resolves/rejects them.
@@ -161,6 +165,10 @@ pub fn deinit(self: *Context) void {
p.deinit(); p.deinit();
} }
for (self.js_value_list.items) |*p| {
p.deinit();
}
for (self.persisted_promise_resolvers.items) |*p| { for (self.persisted_promise_resolvers.items) |*p| {
p.deinit(); p.deinit();
} }

View File

@@ -23,6 +23,8 @@ const v8 = js.v8;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const PersistentValue = v8.Persistent(v8.Value);
const Value = @This(); const Value = @This();
js_val: v8.Value, js_val: v8.Value,
context: *js.Context, context: *js.Context,
@@ -49,6 +51,16 @@ pub fn fromJson(ctx: *js.Context, json: []const u8) !Value {
return Value{ .context = ctx, .js_val = value }; return Value{ .context = ctx, .js_val = value };
} }
pub fn persist(self: Value) !Value {
const js_val = self.js_val;
var context = self.context;
const persisted = PersistentValue.init(context.isolate, js_val);
try context.js_value_list.append(context.arena, persisted);
return Value{ .context = context, .js_val = persisted.toValue() };
}
pub fn toObject(self: Value) js.Object { pub fn toObject(self: Value) js.Object {
return .{ return .{
.context = self.context, .context = self.context,