Skip to content

Commit 9c913b2

Browse files
Move loop outside Browser
Signed-off-by: Francis Bouvier <[email protected]>
1 parent 5ab1d2a commit 9c913b2

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

src/browser/browser.zig

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ const log = std.log.scoped(.browser);
5151
pub const Browser = struct {
5252
session: *Session,
5353

54-
pub fn init(alloc: std.mem.Allocator) !Browser {
54+
pub fn init(alloc: std.mem.Allocator, loop: *Loop) !Browser {
5555
// We want to ensure the caller initialised a VM, but the browser
5656
// doesn't use it directly...
5757

5858
return Browser{
59-
.session = try Session.init(alloc, "about:blank"),
59+
.session = try Session.init(alloc, loop, "about:blank"),
6060
};
6161
}
6262

@@ -89,7 +89,6 @@ pub const Session = struct {
8989
// TODO handle proxy
9090
loader: Loader,
9191
env: Env = undefined,
92-
loop: Loop,
9392
inspector: ?jsruntime.Inspector = null,
9493
window: Window,
9594
// TODO move the shed to the browser?
@@ -99,21 +98,20 @@ pub const Session = struct {
9998

10099
jstypes: [Types.len]usize = undefined,
101100

102-
fn init(alloc: std.mem.Allocator, uri: []const u8) !*Session {
101+
fn init(alloc: std.mem.Allocator, loop: *Loop, uri: []const u8) !*Session {
103102
var self = try alloc.create(Session);
104103
self.* = Session{
105104
.uri = uri,
106105
.alloc = alloc,
107106
.arena = std.heap.ArenaAllocator.init(alloc),
108107
.window = Window.create(null),
109108
.loader = Loader.init(alloc),
110-
.loop = try Loop.init(alloc),
111109
.storageShed = storage.Shed.init(alloc),
112110
.httpClient = undefined,
113111
};
114112

115-
self.env = try Env.init(self.arena.allocator(), &self.loop, null);
116-
self.httpClient = .{ .allocator = alloc, .loop = &self.loop };
113+
self.env = try Env.init(self.arena.allocator(), loop, null);
114+
self.httpClient = .{ .allocator = alloc, .loop = loop };
117115
try self.env.load(&self.jstypes);
118116

119117
return self;
@@ -132,7 +130,6 @@ pub const Session = struct {
132130
self.httpClient.deinit();
133131
self.loader.deinit();
134132
self.storageShed.deinit();
135-
self.loop.deinit();
136133
self.alloc.destroy(self);
137134
}
138135

src/main.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ pub fn main() !void {
161161
defer srv.close();
162162
std.debug.print("Listening on: {s}...\n", .{socket_path});
163163

164-
var browser = try Browser.init(arena.allocator());
164+
var loop = try jsruntime.Loop.init(arena.allocator());
165+
defer loop.deinit();
166+
167+
var browser = try Browser.init(arena.allocator(), &loop);
165168
defer browser.deinit();
166169

167-
try server.listen(&browser, srv.sockfd.?);
170+
try server.listen(&browser, &loop, srv.sockfd.?);
168171
}

src/main_get.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ pub fn main() !void {
8080
const vm = jsruntime.VM.init();
8181
defer vm.deinit();
8282

83-
var browser = try Browser.init(allocator);
83+
var loop = try jsruntime.Loop.init(allocator);
84+
defer loop.deinit();
85+
86+
var browser = try Browser.init(allocator, &loop);
8487
defer browser.deinit();
8588

8689
var page = try browser.currentSession().createPage();

src/server.zig

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const BufReadSize = 1024; // 1KB
4040
const MaxStdOutSize = 512; // ensure debug msg are not too long
4141

4242
pub const Cmd = struct {
43+
loop: *public.Loop,
4344

4445
// internal fields
4546
socket: std.posix.socket_t,
@@ -63,7 +64,7 @@ pub const Cmd = struct {
6364

6465
if (size == 0) {
6566
// continue receving incomming messages asynchronously
66-
self.loop().io.recv(*Cmd, self, cbk, completion, self.socket, self.buf);
67+
self.loop.io.recv(*Cmd, self, cbk, completion, self.socket, self.buf);
6768
return;
6869
}
6970

@@ -84,7 +85,7 @@ pub const Cmd = struct {
8485
self.msg_buf.read(self.alloc(), input, self, Cmd.do) catch unreachable;
8586

8687
// continue receving incomming messages asynchronously
87-
self.loop().io.recv(*Cmd, self, cbk, completion, self.socket, self.buf);
88+
self.loop.io.recv(*Cmd, self, cbk, completion, self.socket, self.buf);
8889
}
8990

9091
// shortcuts
@@ -93,11 +94,6 @@ pub const Cmd = struct {
9394
return self.browser.currentSession().alloc;
9495
}
9596

96-
inline fn loop(self: *Cmd) public.Loop {
97-
// TODO: pointer instead?
98-
return self.browser.currentSession().loop;
99-
}
100-
10197
inline fn env(self: Cmd) public.Env {
10298
return self.browser.currentSession().env;
10399
}
@@ -193,7 +189,7 @@ const Send = struct {
193189
return;
194190
};
195191

196-
self.cmd.loop().io.send(*Send, self, Send.asyncCbk, completion, self.cmd.socket, self.buf);
192+
self.cmd.loop.io.send(*Send, self, Send.asyncCbk, completion, self.cmd.socket, self.buf);
197193
}
198194

199195
fn asyncCbk(self: *Send, completion: *Completion, result: SendError!usize) void {
@@ -209,12 +205,12 @@ const Send = struct {
209205

210206
pub fn sendLater(ctx: *Cmd, msg: []const u8, ns: u63) !void {
211207
const sd = try Send.init(ctx, msg);
212-
ctx.loop().io.timeout(*Send, sd.ctx, Send.laterCbk, sd.completion, ns);
208+
ctx.loop.io.timeout(*Send, sd.ctx, Send.laterCbk, sd.completion, ns);
213209
}
214210

215211
pub fn sendAsync(ctx: *Cmd, msg: []const u8) !void {
216212
const sd = try Send.init(ctx, msg);
217-
ctx.loop().io.send(*Send, sd.ctx, Send.asyncCbk, sd.completion, ctx.socket, msg);
213+
ctx.loop.io.send(*Send, sd.ctx, Send.asyncCbk, sd.completion, ctx.socket, msg);
218214
}
219215

220216
pub fn sendSync(ctx: *Cmd, msg: []const u8) !void {
@@ -237,15 +233,14 @@ const Accept = struct {
237233
};
238234

239235
// receving incomming messages asynchronously
240-
self.cmd.loop().io.recv(*Cmd, self.cmd, Cmd.cbk, completion, self.cmd.socket, self.cmd.buf);
236+
self.cmd.loop.io.recv(*Cmd, self.cmd, Cmd.cbk, completion, self.cmd.socket, self.cmd.buf);
241237
}
242238
};
243239

244240
// Listen
245241
// ------
246242

247-
pub fn listen(browser: *Browser, socket: std.posix.socket_t) anyerror!void {
248-
const loop = browser.currentSession().loop;
243+
pub fn listen(browser: *Browser, loop: *public.Loop, socket: std.posix.socket_t) anyerror!void {
249244

250245
// MsgBuffer
251246
var msg_buf = try MsgBuffer.init(loop.alloc, BufReadSize * 256); // 256KB
@@ -255,6 +250,7 @@ pub fn listen(browser: *Browser, socket: std.posix.socket_t) anyerror!void {
255250
// for accepting connections and receving messages
256251
var ctxInput: [BufReadSize]u8 = undefined;
257252
var cmd = Cmd{
253+
.loop = loop,
258254
.browser = browser,
259255
.socket = undefined,
260256
.buf = &ctxInput,

0 commit comments

Comments
 (0)