Skip to content

Commit d85b763

Browse files
committed
Executor World kind
1 parent bd1e823 commit d85b763

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

src/browser/browser.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ pub const Session = struct {
192192
self.state.cookie_jar = &self.cookie_jar;
193193
errdefer self.arena.deinit();
194194

195-
self.executor = try browser.env.startExecutor(Window, &self.state, self);
196-
errdefer browser.env.stopExecutor(self.executor, true);
195+
self.executor = try browser.env.startExecutor(Window, &self.state, self, .main);
196+
errdefer browser.env.stopExecutor(self.executor);
197197
self.inspector = try Env.Inspector.init(self.arena.allocator(), self.executor, ctx);
198198

199199
self.microtaskLoop();
@@ -208,7 +208,7 @@ pub const Session = struct {
208208
self.arena.deinit();
209209
self.cookie_jar.deinit();
210210
self.storage_shed.deinit();
211-
self.browser.env.stopExecutor(self.executor, true);
211+
self.browser.env.stopExecutor(self.executor);
212212
}
213213

214214
fn microtaskLoop(self: *Session) void {

src/cdp/cdp.zig

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
341341
pub fn deinit(self: *Self) void {
342342
if (self.isolated_world) |isolated_world| {
343343
isolated_world.executor.endScope();
344-
self.cdp.browser.env.stopExecutor(isolated_world.executor, false);
344+
self.cdp.browser.env.stopExecutor(isolated_world.executor);
345345
self.isolated_world = null;
346346
}
347347
self.node_registry.deinit();
@@ -360,15 +360,13 @@ pub fn BrowserContext(comptime CDP_T: type) type {
360360
) !void {
361361
if (self.isolated_world != null) return error.CurrentlyOnly1IsolatedWorldSupported;
362362

363-
const executor = try self.cdp.browser.env.startExecutor(@import("../browser/html/window.zig").Window, &self.session.state, self.session);
364-
errdefer self.cdp.browser.env.stopExecutor(executor, true);
363+
const executor = try self.cdp.browser.env.startExecutor(@import("../browser/html/window.zig").Window, &self.session.state, self.session, .isolated);
364+
errdefer self.cdp.browser.env.stopExecutor(executor);
365365

366366
// TBD should we endScope on removePage and re-startScope on createPage?
367367
// Window will be refactored into the executor so we leave it ugly here for now as a reminder.
368368
try executor.startScope(@import("../browser/html/window.zig").Window{});
369369

370-
executor.context.exit(); // The default context should remain open
371-
372370
self.isolated_world = .{
373371
.name = try self.arena.dupe(u8, world_name),
374372
.grant_universal_access = grant_universal_access,

src/runtime/js.zig

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

src/testing.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ pub const JsRunner = struct {
433433
.tls_verify_host = false,
434434
});
435435

436-
runner.executor = try runner.env.startExecutor(Window, &runner.state, runner);
436+
runner.executor = try runner.env.startExecutor(Window, &runner.state, runner, .main);
437437
errdefer runner.env.stopExecutor(runner.executor);
438438

439439
try runner.executor.startScope(&runner.window);

0 commit comments

Comments
 (0)