Skip to content
Closed
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
22 changes: 11 additions & 11 deletions src/browser/page.zig
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub const Page = struct {

// Our JavaScript context for this specific page. This is what we use to
// execute any JavaScript
scope: *Env.Scope,
main_context: *Env.Context,

// For a Page we only create one HandleScope and keep it alive for the duration of the page.
// If needed JS Locals' lifetimes can be reduced by layering on additional Scopes.
Expand Down Expand Up @@ -112,22 +112,22 @@ pub const Page = struct {
.microtask_node = .{ .func = microtaskCallback },
.window_clicked_event_node = .{ .func = windowClicked },
.request_factory = browser.http_client.requestFactory(browser.notification),
.scope = undefined,
.main_context = undefined,
.handle_scope = undefined,
.module_map = .empty,
};

self.scope = try session.executor.startScope(&self.window, self, self);
self.main_context = try session.executor.createContext(&self.window, self, self);

// Start a scope (stackframe) for JS Local variables.
Env.HandleScope.init(&self.handle_scope, browser.env.isolate);
errdefer self.handle_scope.deinit();

self.scope.enter();
errdefer self.scope.exit();
self.main_context.enter();
errdefer self.main_context.exit();

// load polyfills
try polyfill.load(self.arena, self.scope);
try polyfill.load(self.arena, self.main_context);

_ = try session.browser.app.loop.timeout(1 * std.time.ns_per_ms, &self.microtask_node);
}
Expand Down Expand Up @@ -169,7 +169,7 @@ pub const Page = struct {

pub fn wait(self: *Page) !void {
var try_catch: Env.TryCatch = undefined;
try_catch.init(self.scope);
try_catch.init(self.main_context);
defer try_catch.deinit();

try self.session.browser.app.loop.run();
Expand Down Expand Up @@ -664,14 +664,14 @@ const Script = struct {

fn eval(self: *const Script, page: *Page, body: []const u8) !void {
var try_catch: Env.TryCatch = undefined;
try_catch.init(page.scope);
try_catch.init(page.main_context); // Sjors: Is this always the main context?
defer try_catch.deinit();

const src = self.src orelse "inline";
const res = switch (self.kind) {
.javascript => page.scope.exec(body, src),
.javascript => page.main_context.exec(body, src),
.module => blk: {
switch (try page.scope.module(body, src)) {
switch (try page.main_context.module(body, src)) {
.value => |v| break :blk v,
.exception => |e| {
log.warn(.page, "eval module", .{ .src = src, .err = try e.exception(page.arena) });
Expand All @@ -688,7 +688,7 @@ const Script = struct {
_ = res;

if (self.onload) |onload| {
_ = page.scope.exec(onload, "script_on_load") catch {
_ = page.main_context.exec(onload, "script_on_load") catch {
if (try try_catch.err(page.arena)) |msg| {
log.warn(.page, "eval onload", .{ .src = src, .err = msg });
}
Expand Down
2 changes: 1 addition & 1 deletion src/browser/polyfill/fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test "Browser.fetch" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
defer runner.deinit();

try @import("polyfill.zig").load(testing.allocator, runner.page.scope);
try @import("polyfill.zig").load(testing.allocator, runner.page.main_context);

try runner.testCases(&.{
.{
Expand Down
6 changes: 3 additions & 3 deletions src/browser/polyfill/polyfill.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ const modules = [_]struct {
.{ .name = "polyfill-fetch", .source = @import("fetch.zig").source },
};

pub fn load(allocator: Allocator, scope: *Env.Scope) !void {
pub fn load(allocator: Allocator, context: *Env.Context) !void {
var try_catch: Env.TryCatch = undefined;
try_catch.init(scope);
try_catch.init(context);
defer try_catch.deinit();

for (modules) |m| {
_ = scope.exec(m.source, m.name) catch |err| {
_ = context.exec(m.source, m.name) catch |err| {
if (try try_catch.err(allocator)) |msg| {
defer allocator.free(msg);
log.err(.polyfill, "exec error", .{ .name = m.name, .err = msg });
Expand Down
4 changes: 2 additions & 2 deletions src/browser/session.zig
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ pub const Session = struct {
// Reset all existing callbacks.
self.browser.app.loop.reset();

self.executor.scope.?.exit();
self.executor.endScope();
self.executor.context.?.exit();
self.executor.destroyContext();

self.page.?.handle_scope.deinit();
self.page = null;
Expand Down
8 changes: 4 additions & 4 deletions src/cdp/cdp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,8 @@ const IsolatedWorld = struct {
self.executor.deinit();
}
pub fn removeContext(self: *IsolatedWorld) !void {
if (self.executor.scope == null) return error.NoIsolatedContextToRemove;
self.executor.endScope();
if (self.executor.context == null) return error.NoIsolatedContextToRemove;
self.executor.destroyContext();
}

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

Expand Down
10 changes: 5 additions & 5 deletions src/cdp/domains/dom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,13 @@ fn resolveNode(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const page = bc.session.currentPage() orelse return error.PageNotLoaded;

var scope = page.scope;
var context = page.main_context;
if (params.executionContextId) |context_id| {
if (scope.context.debugContextId() != context_id) {
if (context.v8_context.debugContextId() != context_id) {
var isolated_world = bc.isolated_world orelse return error.ContextNotFound;
scope = &(isolated_world.executor.scope orelse return error.ContextNotFound);
context = &(isolated_world.executor.context orelse return error.ContextNotFound);

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

Expand All @@ -275,7 +275,7 @@ fn resolveNode(cmd: anytype) !void {
// node._node is a *parser.Node we need this to be able to find its most derived type e.g. Node -> Element -> HTMLElement
// So we use the Node.Union when retrieve the value from the environment
const remote_object = try bc.inspector.getRemoteObject(
scope,
context,
params.objectGroup orelse "",
try dom_node.Node.toInterface(node._node),
);
Expand Down
12 changes: 6 additions & 6 deletions src/cdp/domains/page.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ fn createIsolatedWorld(cmd: anytype) !void {
const world = try bc.createIsolatedWorld(params.worldName, params.grantUniveralAccess);
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
try pageCreated(bc, page);
const scope = &world.executor.scope.?;
const context = &world.executor.context.?;

// Create the auxdata json for the contextCreated event
// Calling contextCreated will assign a Id to the context and send the contextCreated event
const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{params.frameId});
bc.inspector.contextCreated(scope, world.name, "", aux_data, false);
bc.inspector.contextCreated(context, world.name, "", aux_data, false);

return cmd.sendResult(.{ .executionContextId = scope.context.debugContextId() }, .{});
return cmd.sendResult(.{ .executionContextId = context.v8_context.debugContextId() }, .{});
}

fn navigate(cmd: anytype) !void {
Expand Down Expand Up @@ -239,7 +239,7 @@ pub fn pageNavigate(arena: Allocator, bc: anytype, event: *const Notification.Pa
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
const aux_data = try std.fmt.allocPrint(arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
bc.inspector.contextCreated(
page.scope,
page.main_context,
"",
try page.origin(arena),
aux_data,
Expand All @@ -250,7 +250,7 @@ pub fn pageNavigate(arena: Allocator, bc: anytype, event: *const Notification.Pa
const aux_json = try std.fmt.allocPrint(arena, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{target_id});
// Calling contextCreated will assign a new Id to the context and send the contextCreated event
bc.inspector.contextCreated(
&isolated_world.executor.scope.?,
&isolated_world.executor.context.?,
isolated_world.name,
"://",
aux_json,
Expand All @@ -272,7 +272,7 @@ pub fn pageCreated(bc: anytype, page: *Page) !void {
try isolated_world.createContext(page);

const polyfill = @import("../../browser/polyfill/polyfill.zig");
try polyfill.load(bc.arena, &isolated_world.executor.scope.?);
try polyfill.load(bc.arena, &isolated_world.executor.context.?);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cdp/domains/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn createTarget(cmd: anytype) !void {
{
const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
bc.inspector.contextCreated(
page.scope,
page.main_context,
"",
try page.origin(cmd.arena),
aux_data,
Expand Down
4 changes: 2 additions & 2 deletions src/main_wpt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn run(arena: Allocator, test_file: []const u8, loader: *FileLoader, err_out: *?
});
defer runner.deinit();

try polyfill.load(arena, runner.page.scope);
try polyfill.load(arena, runner.page.main_context);

// loop over the scripts.
const doc = parser.documentHTMLToDocument(runner.page.window.document);
Expand Down Expand Up @@ -155,7 +155,7 @@ fn run(arena: Allocator, test_file: []const u8, loader: *FileLoader, err_out: *?
{
// wait for all async executions
var try_catch: Env.TryCatch = undefined;
try_catch.init(runner.page.scope);
try_catch.init(runner.page.main_context);
defer try_catch.deinit();
try runner.page.loop.run();

Expand Down
Loading