Skip to content
Merged
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
9 changes: 6 additions & 3 deletions src/app.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Notification = @import("notification.zig").Notification;
pub const App = struct {
http: Http,
config: Config,
platform: ?*const Platform,
platform: Platform,
allocator: Allocator,
telemetry: Telemetry,
app_dir_path: ?[]const u8,
Expand All @@ -29,7 +29,6 @@ pub const App = struct {

pub const Config = struct {
run_mode: RunMode,
platform: ?*const Platform = null,
tls_verify_host: bool = true,
http_proxy: ?[:0]const u8 = null,
proxy_bearer_token: ?[:0]const u8 = null,
Expand Down Expand Up @@ -57,13 +56,16 @@ pub const App = struct {
});
errdefer http.deinit();

const platform = try Platform.init();
errdefer platform.deinit();

const app_dir_path = getAndMakeAppDir(allocator);

app.* = .{
.http = http,
.allocator = allocator,
.telemetry = undefined,
.platform = config.platform,
.platform = platform,
.app_dir_path = app_dir_path,
.notification = notification,
.config = config,
Expand All @@ -85,6 +87,7 @@ pub const App = struct {
self.telemetry.deinit();
self.notification.deinit();
self.http.deinit();
self.platform.deinit();
allocator.destroy(self);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/browser/browser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub const Browser = struct {
pub fn init(app: *App) !Browser {
const allocator = app.allocator;

const env = try Env.init(allocator, app.platform, .{});
const env = try Env.init(allocator, &app.platform, .{});
errdefer env.deinit();

const notification = try Notification.init(allocator, app.notification);
Expand Down
2 changes: 1 addition & 1 deletion src/browser/html/elements.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ test "Browser.HTML.Element.DataSet" {
test "Browser.HTML.HtmlInputElement.properties" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .url = "https://lightpanda.io/noslashattheend" });
defer runner.deinit();
var alloc = std.heap.ArenaAllocator.init(runner.app.allocator);
var alloc = std.heap.ArenaAllocator.init(runner.allocator);
defer alloc.deinit();
const arena = alloc.allocator();

Expand Down
5 changes: 1 addition & 4 deletions src/cdp/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const TestCDP = main.CDPT(struct {
});

const TestContext = struct {
app: *App,
client: ?Client = null,
cdp_: ?TestCDP = null,
arena: ArenaAllocator,
Expand All @@ -80,7 +79,6 @@ const TestContext = struct {
if (self.cdp_) |*c| {
c.deinit();
}
self.app.deinit();
self.arena.deinit();
}

Expand All @@ -89,7 +87,7 @@ const TestContext = struct {
self.client = Client.init(self.arena.allocator());
// Don't use the arena here. We want to detect leaks in CDP.
// The arena is only for test-specific stuff
self.cdp_ = TestCDP.init(self.app, &self.client.?) catch unreachable;
self.cdp_ = TestCDP.init(base.test_app, &self.client.?) catch unreachable;
}
return &self.cdp_.?;
}
Expand Down Expand Up @@ -221,7 +219,6 @@ const TestContext = struct {

pub fn context() TestContext {
return .{
.app = App.init(std.testing.allocator, .{ .run_mode = .serve }) catch unreachable,
.arena = ArenaAllocator.init(std.testing.allocator),
};
}
Expand Down
57 changes: 25 additions & 32 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ const Allocator = std.mem.Allocator;

const log = @import("log.zig");
const App = @import("app.zig").App;
const Server = @import("server.zig").Server;
const Http = @import("http/Http.zig");
const Platform = @import("runtime/js.zig").Platform;
const Server = @import("server.zig").Server;
const Browser = @import("browser/browser.zig").Browser;

const build_config = @import("build_config");
Expand Down Expand Up @@ -109,13 +108,9 @@ fn run(alloc: Allocator) !void {
log.opts.filter_scopes = lfs;
}

const platform = try Platform.init();
defer platform.deinit();

// _app is global to handle graceful shutdown.
_app = try App.init(alloc, .{
.run_mode = args.mode,
.platform = &platform,
.http_proxy = args.httpProxy(),
.proxy_bearer_token = args.proxyBearerToken(),
.tls_verify_host = args.tlsVerifyHost(),
Expand All @@ -124,6 +119,7 @@ fn run(alloc: Allocator) !void {
.http_max_host_open = args.httpMaxHostOpen(),
.http_max_concurrent = args.httpMaxConcurrent(),
});

const app = _app.?;
defer app.deinit();
app.telemetry.record(.{ .run = {} });
Expand Down Expand Up @@ -715,48 +711,50 @@ fn parseCommonArg(
return false;
}

const testing = @import("testing.zig");
test {
std.testing.refAllDecls(@This());
}

var test_wg: std.Thread.WaitGroup = .{};
var test_cdp_server: ?Server = null;
test "tests:beforeAll" {
try parser.init();
log.opts.level = .err;
log.opts.format = .logfmt;

test_wg.startMany(2);
const platform = try Platform.init();
try testing.setup();

var wg: std.Thread.WaitGroup = .{};
wg.startMany(2);

{
const address = try std.net.Address.parseIp("127.0.0.1", 9582);
const thread = try std.Thread.spawn(.{}, serveHTTP, .{address});
const thread = try std.Thread.spawn(.{}, serveHTTP, .{&wg});
thread.detach();
}

{
const address = try std.net.Address.parseIp("127.0.0.1", 9583);
const thread = try std.Thread.spawn(.{}, serveCDP, .{ address, &platform });
const thread = try std.Thread.spawn(.{}, serveCDP, .{&wg});
thread.detach();
}

// need to wait for the servers to be listening, else tests will fail because
// they aren't able to connect.
test_wg.wait();
wg.wait();
}

test "tests:afterAll" {
parser.deinit();
if (test_cdp_server) |*server| {
server.deinit();
}
testing.shutdown();
}

fn serveHTTP(address: std.net.Address) !void {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
fn serveHTTP(wg: *std.Thread.WaitGroup) !void {
const address = try std.net.Address.parseIp("127.0.0.1", 9582);

var listener = try address.listen(.{ .reuse_address = true });
defer listener.deinit();

test_wg.finish();
wg.finish();

var read_buffer: [1024]u8 = undefined;
while (true) {
Expand Down Expand Up @@ -799,20 +797,15 @@ fn serveHTTP(address: std.net.Address) !void {
}
}

fn serveCDP(address: std.net.Address, platform: *const Platform) !void {
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
var app = try App.init(gpa.allocator(), .{
.run_mode = .serve,
.tls_verify_host = false,
.platform = platform,
.http_max_concurrent = 2,
});
defer app.deinit();
fn serveCDP(wg: *std.Thread.WaitGroup) !void {
const address = try std.net.Address.parseIp("127.0.0.1", 9583);
test_cdp_server = try Server.init(testing.test_app, address);

test_wg.finish();
var server = try Server.init(app, address);
var server = try Server.init(testing.test_app, address);
defer server.deinit();
server.run(address, 5) catch |err| {
wg.finish();

test_cdp_server.?.run(address, 5) catch |err| {
std.debug.print("CDP server error: {}", .{err});
return err;
};
Expand Down
17 changes: 10 additions & 7 deletions src/main_wpt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub fn main() !void {

const cmd = try parseArgs(runner_arena);

const platform = try Platform.init();
defer platform.deinit();
try @import("testing.zig").setup();
defer @import("testing.zig").shutdown();

// prepare libraries to load on each test case.
var loader = FileLoader.init(runner_arena, WPT_DIR);
Expand All @@ -69,7 +69,6 @@ pub fn main() !void {
var err_out: ?[]const u8 = null;
const result = run(
test_arena.allocator(),
&platform,
test_file,
&loader,
&err_out,
Expand All @@ -81,20 +80,18 @@ pub fn main() !void {
};

if (result == null and err_out == null) {
// We somtimes pass a non-test to `run` (we don't know it's a non
// We sometimes pass a non-test to `run` (we don't know it's a non
// test, we need to open the contents of the test file to find out
// and that's in run).
continue;
}

try writer.process(test_file, result, err_out);
}
try writer.finalize();
}

fn run(
arena: Allocator,
platform: *const Platform,
test_file: []const u8,
loader: *FileLoader,
err_out: *?[]const u8,
Expand All @@ -119,10 +116,15 @@ fn run(
var runner = try @import("testing.zig").jsRunner(arena, .{
.url = "http://127.0.0.1",
.html = html,
.platform = platform,
});
defer runner.deinit();

defer if (err_out.*) |eo| {
// the error might be owned by the runner, we'll dupe it with our
// own arena so that it can be returned out of this function.
err_out.* = arena.dupe(u8, eo) catch "failed to dupe error";
};

try polyfill.preload(arena, runner.page.main_context);

// loop over the scripts.
Expand Down Expand Up @@ -179,6 +181,7 @@ fn run(

// return the detailed result.
const res = try runner.eval("report.log", "report", err_out);

return try res.toString(arena);
}

Expand Down
10 changes: 4 additions & 6 deletions src/runtime/js.zig
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
return struct {
allocator: Allocator,

platform: ?*const Platform,
platform: *const Platform,

// the global isolate
isolate: v8.Isolate,
Expand Down Expand Up @@ -182,7 +182,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {

const Opts = struct {};

pub fn init(allocator: Allocator, platform: ?*const Platform, _: Opts) !*Self {
pub fn init(allocator: Allocator, platform: *const Platform, _: Opts) !*Self {
// var params = v8.initCreateParams();
var params = try allocator.create(v8.CreateParams);
errdefer allocator.destroy(params);
Expand Down Expand Up @@ -301,13 +301,11 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
}

pub fn pumpMessageLoop(self: *const Self) bool {
// assume it's not-null.
return self.platform.?.inner.pumpMessageLoop(self.isolate, false);
return self.platform.inner.pumpMessageLoop(self.isolate, false);
}

pub fn runIdleTasks(self: *const Self) void {
// assume it's not-null.
return self.platform.?.inner.runIdleTasks(self.isolate, 1);
return self.platform.inner.runIdleTasks(self.isolate, 1);
}

pub fn newExecutionWorld(self: *Self) !ExecutionWorld {
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

const std = @import("std");
const js = @import("js.zig");
const base = @import("../testing.zig");
const generate = @import("generate.zig");

pub const allocator = std.testing.allocator;
Expand All @@ -42,7 +43,7 @@ pub fn Runner(comptime State: type, comptime Global: type, comptime types: anyty
const self = try allocator.create(Self);
errdefer allocator.destroy(self);

self.env = try Env.init(allocator, null, .{});
self.env = try Env.init(allocator, &base.test_app.platform, .{});
errdefer self.env.deinit();

self.executor = try self.env.newExecutionWorld();
Expand Down
5 changes: 1 addition & 4 deletions src/telemetry/telemetry.zig
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ test "telemetry: getOrCreateId" {
}

test "telemetry: sends event to provider" {
var app = testing.createApp(.{});
defer app.deinit();

var telemetry = try TelemetryT(MockProvider).init(app, .serve);
var telemetry = try TelemetryT(MockProvider).init(testing.test_app, .serve);
defer telemetry.deinit();
const mock = &telemetry.provider;

Expand Down
Loading