Skip to content

Commit eed3d27

Browse files
authored
Merge pull request #678 from lightpanda-io/ExecutionWorld
Rename to ExecutionWorld
2 parents 6506fa7 + 193e012 commit eed3d27

File tree

7 files changed

+35
-36
lines changed

7 files changed

+35
-36
lines changed

src/browser/session.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ pub const Session = struct {
4949
// page and start another.
5050
transfer_arena: Allocator,
5151

52-
executor: Env.Executor,
52+
executor: Env.ExecutionWorld,
5353
storage_shed: storage.Shed,
5454
cookie_jar: storage.CookieJar,
5555

5656
page: ?Page = null,
5757

5858
pub fn init(self: *Session, browser: *Browser) !void {
59-
var executor = try browser.env.newExecutor();
59+
var executor = try browser.env.newExecutionWorld();
6060
errdefer executor.deinit();
6161

6262
const allocator = browser.app.allocator;

src/cdp/cdp.zig

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,11 @@ pub fn BrowserContext(comptime CDP_T: type) type {
372372
return error.CurrentlyOnly1IsolatedWorldSupported;
373373
}
374374

375-
var executor = try self.cdp.browser.env.newExecutor();
375+
var executor = try self.cdp.browser.env.newExecutionWorld();
376376
errdefer executor.deinit();
377377

378378
self.isolated_world = .{
379379
.name = try self.arena.dupe(u8, world_name),
380-
.scope = null,
381380
.executor = executor,
382381
.grant_universal_access = grant_universal_access,
383382
};
@@ -511,18 +510,15 @@ pub fn BrowserContext(comptime CDP_T: type) type {
511510
/// An object id is unique across all contexts, different object ids can refer to the same Node in different contexts.
512511
const IsolatedWorld = struct {
513512
name: []const u8,
514-
scope: ?*Env.Scope,
515-
executor: Env.Executor,
513+
executor: Env.ExecutionWorld,
516514
grant_universal_access: bool,
517515

518516
pub fn deinit(self: *IsolatedWorld) void {
519517
self.executor.deinit();
520-
self.scope = null;
521518
}
522519
pub fn removeContext(self: *IsolatedWorld) !void {
523-
if (self.scope == null) return error.NoIsolatedContextToRemove;
520+
if (self.executor.scope == null) return error.NoIsolatedContextToRemove;
524521
self.executor.endScope();
525-
self.scope = null;
526522
}
527523

528524
// The isolate world must share at least some of the state with the related page, specifically the DocumentHTML
@@ -531,8 +527,8 @@ const IsolatedWorld = struct {
531527
// This also means this pointer becomes invalid after removePage untill a new page is created.
532528
// Currently we have only 1 page/frame and thus also only 1 state in the isolate world.
533529
pub fn createContext(self: *IsolatedWorld, page: *Page) !void {
534-
if (self.scope != null) return error.Only1IsolatedContextSupported;
535-
self.scope = try self.executor.startScope(&page.window, &page.state, {}, false);
530+
if (self.executor.scope != null) return error.Only1IsolatedContextSupported;
531+
_ = try self.executor.startScope(&page.window, &page.state, {}, false);
536532
}
537533
};
538534

src/cdp/domains/dom.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ fn resolveNode(cmd: anytype) !void {
262262
var scope = page.scope;
263263
if (params.executionContextId) |context_id| {
264264
if (scope.context.debugContextId() != context_id) {
265-
const isolated_world = bc.isolated_world orelse return error.ContextNotFound;
266-
scope = isolated_world.scope orelse return error.ContextNotFound;
265+
var isolated_world = bc.isolated_world orelse return error.ContextNotFound;
266+
scope = &(isolated_world.executor.scope orelse return error.ContextNotFound);
267267

268268
if (scope.context.debugContextId() != context_id) return error.ContextNotFound;
269269
}

src/cdp/domains/page.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn createIsolatedWorld(cmd: anytype) !void {
115115
const world = try bc.createIsolatedWorld(params.worldName, params.grantUniveralAccess);
116116
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
117117
try pageCreated(bc, page);
118-
const scope = world.scope.?;
118+
const scope = &world.executor.scope.?;
119119

120120
// Create the auxdata json for the contextCreated event
121121
// Calling contextCreated will assign a Id to the context and send the contextCreated event
@@ -236,7 +236,7 @@ pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void
236236
const aux_json = try std.fmt.bufPrint(&buffer, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{target_id});
237237
// Calling contextCreated will assign a new Id to the context and send the contextCreated event
238238
bc.inspector.contextCreated(
239-
isolated_world.scope.?,
239+
&isolated_world.executor.scope.?,
240240
isolated_world.name,
241241
"://",
242242
aux_json,
@@ -258,7 +258,7 @@ pub fn pageCreated(bc: anytype, page: *Page) !void {
258258
try isolated_world.createContext(page);
259259

260260
const polyfill = @import("../../browser/polyfill/polyfill.zig");
261-
try polyfill.load(bc.arena, isolated_world.scope.?);
261+
try polyfill.load(bc.arena, &isolated_world.executor.scope.?);
262262
}
263263
}
264264

src/runtime/js.zig

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ pub const Platform = struct {
5353

5454
// The Env maps to a V8 isolate, which represents a isolated sandbox for
5555
// executing JavaScript. The Env is where we'll define our V8 <-> Zig bindings,
56-
// and it's where we'll start Executors, which actually execute JavaScript.
57-
// The `S` parameter is arbitrary state. When we start an Executor, an instance
56+
// and it's where we'll start ExecutionWorlds, which actually execute JavaScript.
57+
// The `S` parameter is arbitrary state. When we start an ExecutionWorld, an instance
5858
// of S must be given. This instance is available to any Zig binding.
5959
// The `types` parameter is a tuple of Zig structures we want to bind to V8.
6060
pub fn Env(comptime State: type, comptime WebApis: type) type {
@@ -259,7 +259,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
259259
self.isolate.performMicrotasksCheckpoint();
260260
}
261261

262-
pub fn newExecutor(self: *Self) !Executor {
262+
pub fn newExecutionWorld(self: *Self) !ExecutionWorld {
263263
return .{
264264
.env = self,
265265
.scope = null,
@@ -280,32 +280,35 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
280280
self.isolate.lowMemoryNotification();
281281
}
282282

283-
pub const Executor = struct {
283+
// ExecutionWorld closely models a JS World.
284+
// https://chromium.googlesource.com/chromium/src/+/master/third_party/blink/renderer/bindings/core/v8/V8BindingDesign.md#World
285+
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/ExecutionWorld
286+
pub const ExecutionWorld = struct {
284287
env: *Self,
285288

286289
// Arena whose lifetime is for a single getter/setter/function/etc.
287290
// Largely used to get strings out of V8, like a stack trace from
288291
// a TryCatch. The allocator will be owned by the Scope, but the
289-
// arena itself is owned by the Executor so that we can re-use it
292+
// arena itself is owned by the ExecutionWorld so that we can re-use it
290293
// from scope to scope.
291294
call_arena: ArenaAllocator,
292295

293296
// Arena whose lifetime is for a single page load, aka a Scope. Where
294297
// the call_arena lives for a single function call, the scope_arena
295298
// lives for the lifetime of the entire page. The allocator will be
296-
// owned by the Scope, but the arena itself is owned by the Executor
299+
// owned by the Scope, but the arena itself is owned by the ExecutionWorld
297300
// so that we can re-use it from scope to scope.
298301
scope_arena: ArenaAllocator,
299302

300303
// A Scope maps to a Browser's Page. Here though, it's only a
301-
// mechanism to organization page-specific memory. The Executor
304+
// mechanism to organization page-specific memory. The ExecutionWorld
302305
// does all the work, but having all page-specific data structures
303306
// grouped together helps keep things clean.
304307
scope: ?Scope = null,
305308

306-
// no init, must be initialized via env.newExecutor()
309+
// no init, must be initialized via env.newExecutionWorld()
307310

308-
pub fn deinit(self: *Executor) void {
311+
pub fn deinit(self: *ExecutionWorld) void {
309312
if (self.scope != null) {
310313
self.endScope();
311314
}
@@ -320,7 +323,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
320323
// when the handle_scope is freed.
321324
// We also maintain our own "scope_arena" which allows us to have
322325
// all page related memory easily managed.
323-
pub fn startScope(self: *Executor, global: anytype, state: State, module_loader: anytype, enter: bool) !*Scope {
326+
pub fn startScope(self: *ExecutionWorld, global: anytype, state: State, module_loader: anytype, enter: bool) !*Scope {
324327
std.debug.assert(self.scope == null);
325328

326329
const ModuleLoader = switch (@typeInfo(@TypeOf(module_loader))) {
@@ -338,9 +341,9 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
338341
const Global = @TypeOf(global.*);
339342

340343
var context: v8.Context = blk: {
341-
var handle_scope: v8.HandleScope = undefined;
342-
v8.HandleScope.init(&handle_scope, isolate);
343-
defer handle_scope.deinit();
344+
var temp_scope: v8.HandleScope = undefined;
345+
v8.HandleScope.init(&temp_scope, isolate);
346+
defer temp_scope.deinit();
344347

345348
const js_global = v8.FunctionTemplate.initDefault(isolate);
346349
attachClass(Global, isolate, js_global);
@@ -466,7 +469,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
466469
return scope;
467470
}
468471

469-
pub fn endScope(self: *Executor) void {
472+
pub fn endScope(self: *ExecutionWorld) void {
470473
self.scope.?.deinit();
471474
self.scope = null;
472475
_ = self.scope_arena.reset(.{ .retain_with_limit = SCOPE_ARENA_RETAIN });
@@ -1517,7 +1520,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
15171520
}
15181521

15191522
// Retrieves the RemoteObject for a given value.
1520-
// The value is loaded through the Executor's mapZigInstanceToJs function,
1523+
// The value is loaded through the ExecutionWorld's mapZigInstanceToJs function,
15211524
// just like a method return value. Therefore, if we've mapped this
15221525
// value before, we'll get the existing JS PersistedObject and if not
15231526
// we'll create it and track it for cleanup when the scope ends.
@@ -2198,7 +2201,7 @@ fn isEmpty(comptime T: type) bool {
21982201
}
21992202

22002203
// Responsible for calling Zig functions from JS invokations. This could
2201-
// probably just contained in Executor, but having this specific logic, which
2204+
// probably just contained in ExecutionWorld, but having this specific logic, which
22022205
// is somewhat repetitive between constructors, functions, getters, etc contained
22032206
// here does feel like it makes it clenaer.
22042207
fn Caller(comptime E: type, comptime State: type) type {

src/runtime/testing.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn Runner(comptime State: type, comptime Global: type, comptime types: anyty
3030
return struct {
3131
env: *Env,
3232
scope: *Env.Scope,
33-
executor: Env.Executor,
33+
executor: Env.ExecutionWorld,
3434

3535
pub const Env = js.Env(State, struct {
3636
pub const Interfaces = AdjustedTypes;
@@ -45,7 +45,7 @@ pub fn Runner(comptime State: type, comptime Global: type, comptime types: anyty
4545
self.env = try Env.init(allocator, .{});
4646
errdefer self.env.deinit();
4747

48-
self.executor = try self.env.newExecutor();
48+
self.executor = try self.env.newExecutionWorld();
4949
errdefer self.executor.deinit();
5050

5151
self.scope = try self.executor.startScope(

src/testing.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ pub const JsRunner = struct {
383383
renderer: Renderer,
384384
http_client: HttpClient,
385385
scope: *Env.Scope,
386-
executor: Env.Executor,
386+
executor: Env.ExecutionWorld,
387387
storage_shelf: storage.Shelf,
388388
cookie_jar: storage.CookieJar,
389389

@@ -435,7 +435,7 @@ pub const JsRunner = struct {
435435
.tls_verify_host = false,
436436
});
437437

438-
self.executor = try self.env.newExecutor();
438+
self.executor = try self.env.newExecutionWorld();
439439
errdefer self.executor.deinit();
440440

441441
self.scope = try self.executor.startScope(&self.window, &self.state, {}, true);

0 commit comments

Comments
 (0)