Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
47 changes: 24 additions & 23 deletions src/runtime/js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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);
Expand All @@ -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,
};

Expand All @@ -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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you not freeing these in deinit because you figure the isolate is going away anyways?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct

}

// Above, we've created all our our FunctionTemplates. Now that we
Expand All @@ -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.?);
Expand Down Expand Up @@ -305,20 +301,25 @@ 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();

// 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to create a HandleScope to free memory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently it crashes otherwise with the HandleScope message. Anything that creates a local requires a scope to be active. Before this was leaking the object into the global scope.

v8.HandleScope.init(&handle_scope, isolate);
defer handle_scope.deinit();

self.env.isolate.lowMemoryNotification();
}
}
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
// `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".
Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
Loading