Remove global scope

This commit is contained in:
sjorsdonkers
2025-04-29 11:59:14 +02:00
parent 7309fec51d
commit c31e2d91dd
2 changed files with 26 additions and 25 deletions

View File

@@ -13,8 +13,8 @@
.hash = "tigerbeetle_io-0.0.0-ViLgxpyRBAB5BMfIcj3KMXfbJzwARs9uSl8aRy2OXULd",
},
.v8 = .{
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/5d46f159ca44535cfb4fccd9d46f719eb7eac5fc.tar.gz",
.hash = "v8-0.0.0-xddH66zuIADu8FcQx2kkczC0yhqBY7LoA08-GRWF_zMA",
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/363e2899e6d782ad999edbfae048228871230467.tar.gz",
.hash = "v8-0.0.0-xddH6wHzIAARDy1uFvPqqBpTXzhlnEGDTuX9IAUQz3oU",
},
//.v8 = .{ .path = "../zig-v8-fork" },
//.tigerbeetle_io = .{ .path = "../tigerbeetle-io" },

View File

@@ -158,9 +158,6 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
// the global isolate
isolate: v8.Isolate,
// this is the global scope that all our classes are defined in
global_scope: v8.HandleScope,
// just kept around because we need to free it on deinit
isolate_params: *v8.CreateParams,
@@ -205,9 +202,9 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
isolate.enter();
errdefer isolate.exit();
var global_scope: v8.HandleScope = undefined;
v8.HandleScope.init(&global_scope, isolate);
errdefer global_scope.deinit();
var temp_scope: v8.HandleScope = undefined;
v8.HandleScope.init(&temp_scope, isolate);
defer temp_scope.deinit();
const env = try allocator.create(Self);
errdefer allocator.destroy(env);
@@ -218,7 +215,6 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
.allocator = allocator,
.isolate_params = params,
.gc_hints = opts.gc_hints,
.global_scope = global_scope,
.prototype_lookup = undefined,
};
@@ -228,7 +224,8 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
// we can get its index via: @field(TYPE_LOOKUP, type_name).index
const templates = &env.templates;
inline for (Types, 0..) |s, i| {
templates[i] = generateClass(@field(types, s.name), isolate);
@setEvalBranchQuota(10_000);
templates[i] = v8.Persistent(v8.FunctionTemplate).init(isolate, generateClass(@field(types, s.name), isolate)).castToFunctionTemplate();
}
// Above, we've created all our our FunctionTemplates. Now that we
@@ -254,7 +251,6 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
}
pub fn deinit(self: *Self) void {
self.global_scope.deinit();
self.isolate.exit();
self.isolate.deinit();
v8.destroyArrayBufferAllocator(self.isolate_params.array_buffer_allocator.?);
@@ -305,11 +301,9 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
// no init, must be initialized via env.newExecutor()
pub fn deinit(self: *Executor) void {
if (self.scope != null) {
if (self.scope) |scope| {
const isolate = scope.isolate;
self.endScope();
}
self.call_arena.deinit();
self.scope_arena.deinit();
// V8 doesn't immediately free memory associated with
// a Context, it's managed by the garbage collector. So, when the
@@ -317,9 +311,16 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
// call on the isolate to encourage v8 to free any contexts which
// have been freed.
if (self.env.gc_hints) {
var handle_scope: v8.HandleScope = undefined;
v8.HandleScope.init(&handle_scope, isolate);
defer handle_scope.deinit();
self.env.isolate.lowMemoryNotification();
}
}
self.call_arena.deinit();
self.scope_arena.deinit();
}
// Our scope maps to a "browser.Page".
// A v8.HandleScope is like an arena. Once created, any "Local" that
@@ -344,6 +345,10 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
const isolate = env.isolate;
const Global = @TypeOf(global.*);
var handle_scope: v8.HandleScope = undefined;
v8.HandleScope.init(&handle_scope, isolate);
errdefer handle_scope.deinit();
const js_global = v8.FunctionTemplate.initDefault(isolate);
attachClass(Global, isolate, js_global);
@@ -371,10 +376,6 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
js_global.inherit(templates[proto_index]);
}
var handle_scope: v8.HandleScope = undefined;
v8.HandleScope.init(&handle_scope, isolate);
errdefer handle_scope.deinit();
const context = v8.Context.init(isolate, global_template, null);
context.enter();
errdefer context.exit();