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
10 changes: 3 additions & 7 deletions src/app.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ pub const App = struct {
run_mode: RunMode,
};

pub fn init(allocator: Allocator, config: Config) !*App {
const app = try allocator.create(App);
errdefer allocator.destroy(app);

pub fn init(allocator: Allocator, config: Config) !App {
const loop = try allocator.create(Loop);
errdefer allocator.destroy(loop);

Expand All @@ -40,7 +37,7 @@ pub const App = struct {

const app_dir_path = getAndMakeAppDir(allocator);

app.* = .{
var app: App = .{
.loop = loop,
.allocator = allocator,
.telemetry = undefined,
Expand All @@ -49,7 +46,7 @@ pub const App = struct {
.tls_verify_host = config.tls_verify_host,
}),
};
app.telemetry = Telemetry.init(app, config.run_mode);
app.telemetry = Telemetry.init(&app, config.run_mode);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is taking a stack address. It could be ok, depending on what Telemetry does with the app, but in this case, it isn't.

The alternative to doing what I did is to have init take app *App, which achieves what you want: allowing the caller to decide where the app lives. Just a slightly less friendly API and, in many cases, just puts the heap allocation burden on the caller.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


return app;
}
Expand All @@ -63,7 +60,6 @@ pub const App = struct {
self.loop.deinit();
allocator.destroy(self.loop);
self.http_client.deinit();
allocator.destroy(self);
}
};

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

const TestContext = struct {
app: *App,
app: App,
client: ?Client = null,
cdp_: ?TestCDP = null,
arena: std.heap.ArenaAllocator,
Expand All @@ -156,7 +156,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.?);
self.cdp_ = TestCDP.init(&self.app, &self.client.?);
}
return &self.cdp_.?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn main() !void {
app.telemetry.record(.{ .run = {} });

const timeout = std.time.ns_per_s * @as(u64, opts.timeout);
server.run(app, address, timeout) catch |err| {
server.run(&app, address, timeout) catch |err| {
log.err("Server error", .{});
return err;
};
Expand All @@ -99,7 +99,7 @@ pub fn main() !void {
defer vm.deinit();

// browser
var browser = Browser.init(app);
var browser = Browser.init(&app);
defer browser.deinit();

var session = try browser.newSession({});
Expand Down
2 changes: 1 addition & 1 deletion src/main_unit_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ fn serveCDP(address: std.net.Address) !void {

const server = @import("server.zig");
wg.finish();
server.run(app, address, std.time.ns_per_s * 2) catch |err| {
server.run(&app, address, std.time.ns_per_s * 2) catch |err| {
std.debug.print("CDP server error: {}", .{err});
return err;
};
Expand Down
2 changes: 1 addition & 1 deletion src/telemetry/telemetry.zig
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ test "telemetry: sends event to provider" {
var app = testing.app(.{});
defer app.deinit();

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

Expand Down
2 changes: 1 addition & 1 deletion src/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub fn print(comptime fmt: []const u8, args: anytype) void {
}

// dummy opts incase we want to add something, and not have to break all the callers
pub fn app(_: anytype) *App {
pub fn app(_: anytype) App {
return App.init(allocator, .{ .run_mode = .serve }) catch unreachable;
}

Expand Down
Loading