Skip to content

Commit 42e5f49

Browse files
committed
Make the App own the Platform
Removes optional platform, which only existed for tests. There is now a global `@import("testing.zig").test_app` available. This is setup when the test runner starts, and cleaned up at the end of tests. Individual tests don't have to worry about creating app, which I assume was the reason I Platform optional, since that woul dhave been something else that needed to be setup.
1 parent 1a9d4af commit 42e5f49

File tree

10 files changed

+62
-74
lines changed

10 files changed

+62
-74
lines changed

src/app.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub const App = struct {
1616
http: Http,
1717
loop: *Loop,
1818
config: Config,
19-
platform: ?*const Platform,
19+
platform: Platform,
2020
allocator: Allocator,
2121
telemetry: Telemetry,
2222
app_dir_path: ?[]const u8,
@@ -31,7 +31,6 @@ pub const App = struct {
3131

3232
pub const Config = struct {
3333
run_mode: RunMode,
34-
platform: ?*const Platform = null,
3534
tls_verify_host: bool = true,
3635
http_proxy: ?[:0]const u8 = null,
3736
proxy_bearer_token: ?[:0]const u8 = null,
@@ -65,14 +64,17 @@ pub const App = struct {
6564
});
6665
errdefer http.deinit();
6766

67+
const platform = try Platform.init();
68+
errdefer platform.deinit();
69+
6870
const app_dir_path = getAndMakeAppDir(allocator);
6971

7072
app.* = .{
7173
.loop = loop,
7274
.http = http,
7375
.allocator = allocator,
7476
.telemetry = undefined,
75-
.platform = config.platform,
77+
.platform = platform,
7678
.app_dir_path = app_dir_path,
7779
.notification = notification,
7880
.config = config,
@@ -96,6 +98,7 @@ pub const App = struct {
9698
allocator.destroy(self.loop);
9799
self.notification.deinit();
98100
self.http.deinit();
101+
self.platform.deinit();
99102
allocator.destroy(self);
100103
}
101104
};

src/browser/browser.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub const Browser = struct {
4848
pub fn init(app: *App) !Browser {
4949
const allocator = app.allocator;
5050

51-
const env = try Env.init(allocator, app.platform, .{});
51+
const env = try Env.init(allocator, &app.platform, .{});
5252
errdefer env.deinit();
5353

5454
const notification = try Notification.init(allocator, app.notification);

src/browser/html/elements.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ test "Browser.HTML.Element.DataSet" {
13361336
test "Browser.HTML.HtmlInputElement.properties" {
13371337
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .url = "https://lightpanda.io/noslashattheend" });
13381338
defer runner.deinit();
1339-
var alloc = std.heap.ArenaAllocator.init(runner.app.allocator);
1339+
var alloc = std.heap.ArenaAllocator.init(runner.allocator);
13401340
defer alloc.deinit();
13411341
const arena = alloc.allocator();
13421342

src/cdp/testing.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ const TestCDP = main.CDPT(struct {
6969
});
7070

7171
const TestContext = struct {
72-
app: *App,
7372
client: ?Client = null,
7473
cdp_: ?TestCDP = null,
7574
arena: ArenaAllocator,
@@ -78,7 +77,6 @@ const TestContext = struct {
7877
if (self.cdp_) |*c| {
7978
c.deinit();
8079
}
81-
self.app.deinit();
8280
self.arena.deinit();
8381
}
8482

@@ -87,7 +85,7 @@ const TestContext = struct {
8785
self.client = Client.init(self.arena.allocator());
8886
// Don't use the arena here. We want to detect leaks in CDP.
8987
// The arena is only for test-specific stuff
90-
self.cdp_ = TestCDP.init(self.app, &self.client.?) catch unreachable;
88+
self.cdp_ = TestCDP.init(base.test_app, &self.client.?) catch unreachable;
9189
}
9290
return &self.cdp_.?;
9391
}
@@ -220,7 +218,6 @@ const TestContext = struct {
220218

221219
pub fn context() TestContext {
222220
return .{
223-
.app = App.init(std.testing.allocator, .{ .run_mode = .serve }) catch unreachable,
224221
.arena = ArenaAllocator.init(std.testing.allocator),
225222
};
226223
}

src/main.zig

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,8 @@ fn run(alloc: Allocator) !void {
7878
log.opts.filter_scopes = lfs;
7979
}
8080

81-
const platform = try Platform.init();
82-
defer platform.deinit();
83-
8481
var app = try App.init(alloc, .{
8582
.run_mode = args.mode,
86-
.platform = &platform,
8783
.http_proxy = args.httpProxy(),
8884
.proxy_bearer_token = args.proxyBearerToken(),
8985
.tls_verify_host = args.tlsVerifyHost(),
@@ -678,48 +674,46 @@ fn parseCommonArg(
678674
return false;
679675
}
680676

677+
const testing = @import("testing.zig");
681678
test {
682679
std.testing.refAllDecls(@This());
683680
}
684681

685-
var test_wg: std.Thread.WaitGroup = .{};
686682
test "tests:beforeAll" {
687-
try parser.init();
688683
log.opts.level = .err;
689684
log.opts.format = .logfmt;
690685

691-
test_wg.startMany(2);
692-
const platform = try Platform.init();
686+
try testing.setup();
687+
688+
var wg: std.Thread.WaitGroup = .{};
689+
wg.startMany(2);
693690

694691
{
695-
const address = try std.net.Address.parseIp("127.0.0.1", 9582);
696-
const thread = try std.Thread.spawn(.{}, serveHTTP, .{address});
692+
const thread = try std.Thread.spawn(.{}, serveHTTP, .{&wg});
697693
thread.detach();
698694
}
699695

700696
{
701-
const address = try std.net.Address.parseIp("127.0.0.1", 9583);
702-
const thread = try std.Thread.spawn(.{}, serveCDP, .{ address, &platform });
697+
const thread = try std.Thread.spawn(.{}, serveCDP, .{&wg});
703698
thread.detach();
704699
}
705700

706701
// need to wait for the servers to be listening, else tests will fail because
707702
// they aren't able to connect.
708-
test_wg.wait();
703+
wg.wait();
709704
}
710705

711706
test "tests:afterAll" {
712-
parser.deinit();
707+
testing.shutdown();
713708
}
714709

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

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

722-
test_wg.finish();
716+
wg.finish();
723717

724718
var read_buffer: [1024]u8 = undefined;
725719
while (true) {
@@ -762,18 +756,11 @@ fn serveHTTP(address: std.net.Address) !void {
762756
}
763757
}
764758

765-
fn serveCDP(address: std.net.Address, platform: *const Platform) !void {
766-
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
767-
var app = try App.init(gpa.allocator(), .{
768-
.run_mode = .serve,
769-
.tls_verify_host = false,
770-
.platform = platform,
771-
.http_max_concurrent = 2,
772-
});
773-
defer app.deinit();
759+
fn serveCDP(wg: *std.Thread.WaitGroup) !void {
760+
const address = try std.net.Address.parseIp("127.0.0.1", 9583);
774761

775-
test_wg.finish();
776-
server.run(app, address, std.time.ns_per_s * 2) catch |err| {
762+
wg.finish();
763+
server.run(testing.test_app, address, std.time.ns_per_s * 2) catch |err| {
777764
std.debug.print("CDP server error: {}", .{err});
778765
return err;
779766
};

src/main_wpt.zig

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ pub fn main() !void {
4848

4949
const cmd = try parseArgs(runner_arena);
5050

51-
const platform = try Platform.init();
52-
defer platform.deinit();
51+
try @import("testing.zig").setup();
52+
defer @import("testing.zig").shutdown();
5353

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

8382
if (result == null and err_out == null) {
84-
// We somtimes pass a non-test to `run` (we don't know it's a non
83+
// We sometimes pass a non-test to `run` (we don't know it's a non
8584
// test, we need to open the contents of the test file to find out
8685
// and that's in run).
8786
continue;
8887
}
89-
9088
try writer.process(test_file, result, err_out);
9189
}
9290
try writer.finalize();
9391
}
9492

9593
fn run(
9694
arena: Allocator,
97-
platform: *const Platform,
9895
test_file: []const u8,
9996
loader: *FileLoader,
10097
err_out: *?[]const u8,
@@ -119,10 +116,15 @@ fn run(
119116
var runner = try @import("testing.zig").jsRunner(arena, .{
120117
.url = "http://127.0.0.1",
121118
.html = html,
122-
.platform = platform,
123119
});
124120
defer runner.deinit();
125121

122+
defer if (err_out.*) |eo| {
123+
// the error might be owned by the runner, we'll dupe it with our
124+
// own arena so that it can be returned out of this function.
125+
err_out.* = arena.dupe(u8, eo) catch "failed to dupe error";
126+
};
127+
126128
try polyfill.preload(arena, runner.page.main_context);
127129

128130
// loop over the scripts.
@@ -179,6 +181,7 @@ fn run(
179181

180182
// return the detailed result.
181183
const res = try runner.eval("report.log", "report", err_out);
184+
182185
return try res.toString(arena);
183186
}
184187

src/runtime/js.zig

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
153153
return struct {
154154
allocator: Allocator,
155155

156-
platform: ?*const Platform,
156+
platform: *const Platform,
157157

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

183183
const Opts = struct {};
184184

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

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

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

313311
pub fn newExecutionWorld(self: *Self) !ExecutionWorld {

src/runtime/testing.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
const std = @import("std");
2020
const js = @import("js.zig");
21+
const base = @import("../testing.zig");
2122
const generate = @import("generate.zig");
2223

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

45-
self.env = try Env.init(allocator, null, .{});
46+
self.env = try Env.init(allocator, &base.test_app.platform, .{});
4647
errdefer self.env.deinit();
4748

4849
self.executor = try self.env.newExecutionWorld();

src/telemetry/telemetry.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,7 @@ test "telemetry: getOrCreateId" {
186186
}
187187

188188
test "telemetry: sends event to provider" {
189-
var app = testing.createApp(.{});
190-
defer app.deinit();
191-
192-
var telemetry = try TelemetryT(MockProvider).init(app, .serve);
189+
var telemetry = try TelemetryT(MockProvider).init(testing.test_app, .serve);
193190
defer telemetry.deinit();
194191
const mock = &telemetry.provider;
195192

0 commit comments

Comments
 (0)