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", .hash = "tigerbeetle_io-0.0.0-ViLgxpyRBAB5BMfIcj3KMXfbJzwARs9uSl8aRy2OXULd",
}, },
.v8 = .{ .v8 = .{
.url = "https://github.com/lightpanda-io/zig-v8-fork/archive/5d46f159ca44535cfb4fccd9d46f719eb7eac5fc.tar.gz", .url = "https://github.com/lightpanda-io/zig-v8-fork/archive/363e2899e6d782ad999edbfae048228871230467.tar.gz",
.hash = "v8-0.0.0-xddH66zuIADu8FcQx2kkczC0yhqBY7LoA08-GRWF_zMA", .hash = "v8-0.0.0-xddH6wHzIAARDy1uFvPqqBpTXzhlnEGDTuX9IAUQz3oU",
}, },
//.v8 = .{ .path = "../zig-v8-fork" }, //.v8 = .{ .path = "../zig-v8-fork" },
//.tigerbeetle_io = .{ .path = "../tigerbeetle-io" }, //.tigerbeetle_io = .{ .path = "../tigerbeetle-io" },

View File

@@ -158,9 +158,6 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
// the global isolate // the global isolate
isolate: v8.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 // just kept around because we need to free it on deinit
isolate_params: *v8.CreateParams, isolate_params: *v8.CreateParams,
@@ -205,9 +202,9 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
isolate.enter(); isolate.enter();
errdefer isolate.exit(); errdefer isolate.exit();
var global_scope: v8.HandleScope = undefined; var temp_scope: v8.HandleScope = undefined;
v8.HandleScope.init(&global_scope, isolate); v8.HandleScope.init(&temp_scope, isolate);
errdefer global_scope.deinit(); defer temp_scope.deinit();
const env = try allocator.create(Self); const env = try allocator.create(Self);
errdefer allocator.destroy(env); errdefer allocator.destroy(env);
@@ -218,7 +215,6 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
.allocator = allocator, .allocator = allocator,
.isolate_params = params, .isolate_params = params,
.gc_hints = opts.gc_hints, .gc_hints = opts.gc_hints,
.global_scope = global_scope,
.prototype_lookup = undefined, .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 // we can get its index via: @field(TYPE_LOOKUP, type_name).index
const templates = &env.templates; const templates = &env.templates;
inline for (Types, 0..) |s, i| { 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 // 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 { pub fn deinit(self: *Self) void {
self.global_scope.deinit();
self.isolate.exit(); self.isolate.exit();
self.isolate.deinit(); self.isolate.deinit();
v8.destroyArrayBufferAllocator(self.isolate_params.array_buffer_allocator.?); v8.destroyArrayBufferAllocator(self.isolate_params.array_buffer_allocator.?);
@@ -305,20 +301,25 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
// no init, must be initialized via env.newExecutor() // no init, must be initialized via env.newExecutor()
pub fn deinit(self: *Executor) void { pub fn deinit(self: *Executor) void {
if (self.scope != null) { if (self.scope) |scope| {
const isolate = scope.isolate;
self.endScope(); self.endScope();
// V8 doesn't immediately free memory associated with
// a Context, it's managed by the garbage collector. So, when the
// `gc_hints` option is enabled, we'll use the `lowMemoryNotification`
// 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.call_arena.deinit();
self.scope_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
// `gc_hints` option is enabled, we'll use the `lowMemoryNotification`
// call on the isolate to encourage v8 to free any contexts which
// have been freed.
if (self.env.gc_hints) {
self.env.isolate.lowMemoryNotification();
}
} }
// Our scope maps to a "browser.Page". // Our scope maps to a "browser.Page".
@@ -344,6 +345,10 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
const isolate = env.isolate; const isolate = env.isolate;
const Global = @TypeOf(global.*); 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); const js_global = v8.FunctionTemplate.initDefault(isolate);
attachClass(Global, isolate, js_global); 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]); 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); const context = v8.Context.init(isolate, global_template, null);
context.enter(); context.enter();
errdefer context.exit(); errdefer context.exit();