Store snapshot templates in isolate, not context.

This lets us load the isolate without having to create a temp/dummy context
just to get the templates.

Call ContextDisposedNotification when a context is removed. Supposedly this can
help/hint to the isolate about memory management.
This commit is contained in:
Karl Seguin
2026-01-05 19:05:44 +08:00
parent 2322cb9b83
commit 86f4ea108d
5 changed files with 8 additions and 7 deletions

View File

@@ -166,8 +166,8 @@ pub fn deinit(self: *Context) void {
}
if (self.handle_scope) |*scope| {
scope.deinit();
v8.v8__Context__Exit(self.handle);
scope.deinit();
}
}

View File

@@ -93,14 +93,10 @@ pub fn init(allocator: Allocator, platform: *const Platform, snapshot: *Snapshot
var temp_scope: js.HandleScope = undefined;
temp_scope.init(isolate);
defer temp_scope.deinit();
const context_handle = isolate.createContextHandle(null, null);
v8.v8__Context__Enter(context_handle);
defer v8.v8__Context__Exit(context_handle);
inline for (JsApis, 0..) |JsApi, i| {
JsApi.Meta.class_id = i;
const data = v8.v8__Context__GetDataFromSnapshotOnce(context_handle, snapshot.data_start + i);
const data = v8.v8__Isolate__GetDataFromSnapshotOnce(isolate.handle, snapshot.data_start + i);
const function_handle: *const v8.FunctionTemplate = @ptrCast(data);
// Make function template global/persistent
v8.v8__Global__New(isolate.handle, @ptrCast(function_handle), &globals[i]);

View File

@@ -158,6 +158,7 @@ pub fn removeContext(self: *ExecutionWorld) void {
self.persisted_context.?.deinit();
self.persisted_context = null;
self.env.isolate.notifyContextDisposed();
_ = self.context_arena.reset(.{ .retain_with_limit = CONTEXT_ARENA_RETAIN });
}

View File

@@ -57,6 +57,10 @@ pub fn lowMemoryNotification(self: Isolate) void {
v8.v8__Isolate__LowMemoryNotification(self.handle);
}
pub fn notifyContextDisposed(self: Isolate) void {
_ = v8.v8__Isolate__ContextDisposedNotification(self.handle);
}
pub fn getHeapStatistics(self: Isolate) v8.HeapStatistics {
var res: v8.HeapStatistics = undefined;
v8.v8__Isolate__GetHeapStatistics(self.handle, &res);

View File

@@ -179,7 +179,7 @@ pub fn create() !Snapshot {
var last_data_index: usize = 0;
inline for (JsApis, 0..) |_, i| {
@setEvalBranchQuota(10_000);
const data_index = v8.v8__SnapshotCreator__AddData2(snapshot_creator, context, @ptrCast(templates[i]));
const data_index = v8.v8__SnapshotCreator__AddData(snapshot_creator, @ptrCast(templates[i]));
if (i == 0) {
data_start = data_index;
last_data_index = data_index;