@@ -265,10 +265,12 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
265265 self .isolate .performMicrotasksCheckpoint ();
266266 }
267267
268- pub fn startExecutor (self : * Self , comptime Global : type , state : State , module_loader : anytype ) ! * Executor {
268+ pub fn startExecutor (self : * Self , comptime Global : type , state : State , module_loader : anytype , kind : WorldKind ) ! * Executor {
269269 if (comptime builtin .mode == .Debug ) {
270- std .debug .assert (self .has_executor == false );
271- self .has_executor = true ;
270+ if (kind == .main ) {
271+ std .debug .assert (self .has_executor == false );
272+ self .has_executor = true ;
273+ }
272274 }
273275 const isolate = self .isolate ;
274276 const templates = & self .templates ;
@@ -307,8 +309,10 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
307309 }
308310
309311 const context = v8 .Context .init (isolate , global_template , null );
310- context .enter ();
311- errdefer context .exit ();
312+ if (kind == .main ) {
313+ context .enter ();
314+ errdefer context .exit ();
315+ }
312316
313317 // This shouldn't be necessary, but it is:
314318 // https://groups.google.com/g/v8-users/c/qAQQBmbi--8
@@ -344,8 +348,9 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
344348
345349 executor .* = .{
346350 .state = state ,
347- .context = context ,
348351 .isolate = isolate ,
352+ .kind = kind ,
353+ .context = context ,
349354 .templates = templates ,
350355 .handle_scope = handle_scope ,
351356 .call_arena = undefined ,
@@ -364,7 +369,7 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
364369 executor .call_arena = executor ._call_arena_instance .allocator ();
365370 executor .scope_arena = executor ._scope_arena_instance .allocator ();
366371
367- errdefer self .stopExecutor (executor , false ); // Note: This likely has issues as context.exit() is errdefered as well
372+ errdefer self .stopExecutor (executor ); // Note: This likely has issues as context.exit() is errdefered as well
368373
369374 // Custom exception
370375 // NOTE: there is no way in v8 to subclass the Error built-in type
@@ -385,16 +390,18 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
385390 // a Context, it's managed by the garbage collector. So, when the
386391 // `gc_hints` option is enabled, we'll use the `lowMemoryNotification`
387392 // call on the isolate to encourage v8 to free the context.
388- pub fn stopExecutor (self : * Self , executor : * Executor , exit_context : bool ) void {
389- executor .deinit (exit_context );
393+ pub fn stopExecutor (self : * Self , executor : * Executor ) void {
394+ executor .deinit ();
390395 self .executor_pool .destroy (executor );
391396 if (self .gc_hints ) {
392397 self .isolate .lowMemoryNotification ();
393398 }
394399
395400 if (comptime builtin .mode == .Debug ) {
396- std .debug .assert (self .has_executor == true );
397- self .has_executor = false ;
401+ if (executor .kind == .main ) {
402+ std .debug .assert (self .has_executor == true );
403+ self .has_executor = false ;
404+ }
398405 }
399406 }
400407
@@ -415,7 +422,7 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
415422 // object (i.e. the Window), which gets attached not only to the Window
416423 // constructor/FunctionTemplate as normal, but also through the default
417424 // FunctionTemplate of the isolate (in startExecutor)
418- fn attachClass (self : * Self , comptime Struct : type , template : v8.FunctionTemplate ) void {
425+ fn attachClass (self : * const Self , comptime Struct : type , template : v8.FunctionTemplate ) void {
419426 const template_proto = template .getPrototypeTemplate ();
420427 inline for (@typeInfo (Struct ).@"struct" .decls ) | declaration | {
421428 const name = declaration .name ;
@@ -491,7 +498,7 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
491498 return template ;
492499 }
493500
494- fn generateMethod (self : * Self , comptime Struct : type , comptime name : []const u8 , template_proto : v8.ObjectTemplate ) void {
501+ fn generateMethod (self : * const Self , comptime Struct : type , comptime name : []const u8 , template_proto : v8.ObjectTemplate ) void {
495502 var js_name : v8.Name = undefined ;
496503 if (comptime std .mem .eql (u8 , name , "_symbol_iterator" )) {
497504 js_name = v8 .Symbol .getIterator (self .isolate ).toName ();
@@ -513,7 +520,7 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
513520 template_proto .set (js_name , function_template , v8 .PropertyAttribute .None );
514521 }
515522
516- fn generateAttribute (self : * Self , comptime Struct : type , comptime name : []const u8 , template : v8.FunctionTemplate , template_proto : v8.ObjectTemplate ) void {
523+ fn generateAttribute (self : * const Self , comptime Struct : type , comptime name : []const u8 , template : v8.FunctionTemplate , template_proto : v8.ObjectTemplate ) void {
517524 const zig_value = @field (Struct , name );
518525 const js_value = simpleZigValueToJs (self .isolate , zig_value , true );
519526
@@ -526,7 +533,7 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
526533 template_proto .set (js_name , js_value , v8 .PropertyAttribute .ReadOnly + v8 .PropertyAttribute .DontDelete );
527534 }
528535
529- fn generateProperty (self : * Self , comptime Struct : type , comptime name : []const u8 , template_proto : v8.ObjectTemplate ) void {
536+ fn generateProperty (self : * const Self , comptime Struct : type , comptime name : []const u8 , template_proto : v8.ObjectTemplate ) void {
530537 const getter = @field (Struct , "get_" ++ name );
531538 const param_count = @typeInfo (@TypeOf (getter )).@"fn" .params .len ;
532539
@@ -576,7 +583,7 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
576583 template_proto .setGetterAndSetter (js_name , getter_callback , setter_callback );
577584 }
578585
579- fn generateIndexer (_ : * Self , comptime Struct : type , template_proto : v8.ObjectTemplate ) void {
586+ fn generateIndexer (_ : * const Self , comptime Struct : type , template_proto : v8.ObjectTemplate ) void {
580587 if (@hasDecl (Struct , "indexed_get" ) == false ) {
581588 return ;
582589 }
@@ -605,7 +612,7 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
605612 template_proto .setIndexedProperty (configuration , null );
606613 }
607614
608- fn generateNamedIndexer (_ : * Self , comptime Struct : type , template_proto : v8.ObjectTemplate ) void {
615+ fn generateNamedIndexer (_ : * const Self , comptime Struct : type , template_proto : v8.ObjectTemplate ) void {
609616 if (@hasDecl (Struct , "named_get" ) == false ) {
610617 return ;
611618 }
@@ -765,10 +772,17 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
765772 const PersistentObject = v8 .Persistent (v8 .Object );
766773 const PersistentFunction = v8 .Persistent (v8 .Function );
767774
775+ const WorldKind = enum {
776+ main ,
777+ isolated ,
778+ worker ,
779+ };
780+
768781 // This is capable of executing JavaScript.
769782 pub const Executor = struct {
770783 state : State ,
771784 isolate : v8.Isolate ,
785+ kind : WorldKind ,
772786
773787 handle_scope : v8.HandleScope ,
774788
@@ -819,9 +833,9 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
819833
820834 // not public, must be destroyed via env.stopExecutor()
821835
822- fn deinit (self : * Executor , exit_context : bool ) void {
836+ fn deinit (self : * Executor ) void {
823837 if (self .scope != null ) self .endScope ();
824- if (exit_context ) self .context .exit ();
838+ if (self . kind == .main ) self .context .exit ();
825839 self .handle_scope .deinit ();
826840
827841 self ._call_arena_instance .deinit ();
0 commit comments