js: persist value returned by v8 JSON parser

This commit is contained in:
Pierre Tachoire
2025-12-24 16:36:24 +01:00
parent 1dcccef080
commit beef458c3c
2 changed files with 15 additions and 1 deletions

View File

@@ -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();
}

View File

@@ -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() };
}
};