mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-02-04 14:33:47 +00:00
js: persist value returned by v8 JSON parser
This commit is contained in:
@@ -14,6 +14,7 @@ const types = @import("types.zig");
|
|||||||
const Caller = @import("Caller.zig");
|
const Caller = @import("Caller.zig");
|
||||||
const NamedFunction = Caller.NamedFunction;
|
const NamedFunction = Caller.NamedFunction;
|
||||||
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);
|
||||||
@@ -70,6 +71,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.
|
||||||
@@ -149,6 +153,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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -148,6 +148,8 @@ pub const Exception = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub const Value = struct {
|
pub const Value = struct {
|
||||||
|
const PersistentValue = v8.Persistent(v8.Value);
|
||||||
|
|
||||||
value: v8.Value,
|
value: v8.Value,
|
||||||
context: *const Context,
|
context: *const Context,
|
||||||
|
|
||||||
@@ -159,7 +161,11 @@ pub const Value = struct {
|
|||||||
pub fn fromJson(ctx: *Context, json: []const u8) !Value {
|
pub fn fromJson(ctx: *Context, json: []const u8) !Value {
|
||||||
const json_string = v8.String.initUtf8(ctx.isolate, json);
|
const json_string = v8.String.initUtf8(ctx.isolate, json);
|
||||||
const value = try v8.Json.parse(ctx.v8_context, json_string);
|
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() };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user