@@ -158,9 +158,6 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
158158 // the global isolate
159159 isolate : v8.Isolate ,
160160
161- // this is the global scope that all our classes are defined in
162- global_scope : v8.HandleScope ,
163-
164161 // just kept around because we need to free it on deinit
165162 isolate_params : * v8.CreateParams ,
166163
@@ -205,9 +202,9 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
205202 isolate .enter ();
206203 errdefer isolate .exit ();
207204
208- var global_scope : v8.HandleScope = undefined ;
209- v8 .HandleScope .init (& global_scope , isolate );
210- errdefer global_scope .deinit ();
205+ var temp_scope : v8.HandleScope = undefined ;
206+ v8 .HandleScope .init (& temp_scope , isolate );
207+ defer temp_scope .deinit ();
211208
212209 const env = try allocator .create (Self );
213210 errdefer allocator .destroy (env );
@@ -218,7 +215,6 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
218215 .allocator = allocator ,
219216 .isolate_params = params ,
220217 .gc_hints = opts .gc_hints ,
221- .global_scope = global_scope ,
222218 .prototype_lookup = undefined ,
223219 };
224220
@@ -228,7 +224,8 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
228224 // we can get its index via: @field(TYPE_LOOKUP, type_name).index
229225 const templates = & env .templates ;
230226 inline for (Types , 0.. ) | s , i | {
231- templates [i ] = generateClass (@field (types , s .name ), isolate );
227+ @setEvalBranchQuota (10_000 );
228+ templates [i ] = v8 .Persistent (v8 .FunctionTemplate ).init (isolate , generateClass (@field (types , s .name ), isolate )).castToFunctionTemplate ();
232229 }
233230
234231 // 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 {
254251 }
255252
256253 pub fn deinit (self : * Self ) void {
257- self .global_scope .deinit ();
258254 self .isolate .exit ();
259255 self .isolate .deinit ();
260256 v8 .destroyArrayBufferAllocator (self .isolate_params .array_buffer_allocator .? );
@@ -305,20 +301,25 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
305301 // no init, must be initialized via env.newExecutor()
306302
307303 pub fn deinit (self : * Executor ) void {
308- if (self .scope != null ) {
304+ if (self .scope ) | scope | {
305+ const isolate = scope .isolate ;
309306 self .endScope ();
307+
308+ // V8 doesn't immediately free memory associated with
309+ // a Context, it's managed by the garbage collector. So, when the
310+ // `gc_hints` option is enabled, we'll use the `lowMemoryNotification`
311+ // call on the isolate to encourage v8 to free any contexts which
312+ // have been freed.
313+ if (self .env .gc_hints ) {
314+ var handle_scope : v8.HandleScope = undefined ;
315+ v8 .HandleScope .init (& handle_scope , isolate );
316+ defer handle_scope .deinit ();
317+
318+ self .env .isolate .lowMemoryNotification ();
319+ }
310320 }
311321 self .call_arena .deinit ();
312322 self .scope_arena .deinit ();
313-
314- // V8 doesn't immediately free memory associated with
315- // a Context, it's managed by the garbage collector. So, when the
316- // `gc_hints` option is enabled, we'll use the `lowMemoryNotification`
317- // call on the isolate to encourage v8 to free any contexts which
318- // have been freed.
319- if (self .env .gc_hints ) {
320- self .env .isolate .lowMemoryNotification ();
321- }
322323 }
323324
324325 // Our scope maps to a "browser.Page".
@@ -344,6 +345,10 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
344345 const isolate = env .isolate ;
345346 const Global = @TypeOf (global .* );
346347
348+ var handle_scope : v8.HandleScope = undefined ;
349+ v8 .HandleScope .init (& handle_scope , isolate );
350+ errdefer handle_scope .deinit ();
351+
347352 const js_global = v8 .FunctionTemplate .initDefault (isolate );
348353 attachClass (Global , isolate , js_global );
349354
@@ -371,10 +376,6 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
371376 js_global .inherit (templates [proto_index ]);
372377 }
373378
374- var handle_scope : v8.HandleScope = undefined ;
375- v8 .HandleScope .init (& handle_scope , isolate );
376- errdefer handle_scope .deinit ();
377-
378379 const context = v8 .Context .init (isolate , global_template , null );
379380 context .enter ();
380381 errdefer context .exit ();
0 commit comments