mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
Move loop outside Browser
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
@@ -51,12 +51,12 @@ const log = std.log.scoped(.browser);
|
||||
pub const Browser = struct {
|
||||
session: *Session,
|
||||
|
||||
pub fn init(alloc: std.mem.Allocator) !Browser {
|
||||
pub fn init(alloc: std.mem.Allocator, loop: *Loop) !Browser {
|
||||
// We want to ensure the caller initialised a VM, but the browser
|
||||
// doesn't use it directly...
|
||||
|
||||
return Browser{
|
||||
.session = try Session.init(alloc, "about:blank"),
|
||||
.session = try Session.init(alloc, loop, "about:blank"),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -89,7 +89,6 @@ pub const Session = struct {
|
||||
// TODO handle proxy
|
||||
loader: Loader,
|
||||
env: Env = undefined,
|
||||
loop: Loop,
|
||||
inspector: ?jsruntime.Inspector = null,
|
||||
window: Window,
|
||||
// TODO move the shed to the browser?
|
||||
@@ -99,7 +98,7 @@ pub const Session = struct {
|
||||
|
||||
jstypes: [Types.len]usize = undefined,
|
||||
|
||||
fn init(alloc: std.mem.Allocator, uri: []const u8) !*Session {
|
||||
fn init(alloc: std.mem.Allocator, loop: *Loop, uri: []const u8) !*Session {
|
||||
var self = try alloc.create(Session);
|
||||
self.* = Session{
|
||||
.uri = uri,
|
||||
@@ -107,13 +106,12 @@ pub const Session = struct {
|
||||
.arena = std.heap.ArenaAllocator.init(alloc),
|
||||
.window = Window.create(null),
|
||||
.loader = Loader.init(alloc),
|
||||
.loop = try Loop.init(alloc),
|
||||
.storageShed = storage.Shed.init(alloc),
|
||||
.httpClient = undefined,
|
||||
};
|
||||
|
||||
self.env = try Env.init(self.arena.allocator(), &self.loop, null);
|
||||
self.httpClient = .{ .allocator = alloc, .loop = &self.loop };
|
||||
self.env = try Env.init(self.arena.allocator(), loop, null);
|
||||
self.httpClient = .{ .allocator = alloc, .loop = loop };
|
||||
try self.env.load(&self.jstypes);
|
||||
|
||||
return self;
|
||||
@@ -132,7 +130,6 @@ pub const Session = struct {
|
||||
self.httpClient.deinit();
|
||||
self.loader.deinit();
|
||||
self.storageShed.deinit();
|
||||
self.loop.deinit();
|
||||
self.alloc.destroy(self);
|
||||
}
|
||||
|
||||
|
||||
@@ -161,8 +161,11 @@ pub fn main() !void {
|
||||
defer srv.close();
|
||||
std.debug.print("Listening on: {s}...\n", .{socket_path});
|
||||
|
||||
var browser = try Browser.init(arena.allocator());
|
||||
var loop = try jsruntime.Loop.init(arena.allocator());
|
||||
defer loop.deinit();
|
||||
|
||||
var browser = try Browser.init(arena.allocator(), &loop);
|
||||
defer browser.deinit();
|
||||
|
||||
try server.listen(&browser, srv.sockfd.?);
|
||||
try server.listen(&browser, &loop, srv.sockfd.?);
|
||||
}
|
||||
|
||||
@@ -80,7 +80,10 @@ pub fn main() !void {
|
||||
const vm = jsruntime.VM.init();
|
||||
defer vm.deinit();
|
||||
|
||||
var browser = try Browser.init(allocator);
|
||||
var loop = try jsruntime.Loop.init(allocator);
|
||||
defer loop.deinit();
|
||||
|
||||
var browser = try Browser.init(allocator, &loop);
|
||||
defer browser.deinit();
|
||||
|
||||
var page = try browser.currentSession().createPage();
|
||||
|
||||
@@ -40,6 +40,7 @@ const BufReadSize = 1024; // 1KB
|
||||
const MaxStdOutSize = 512; // ensure debug msg are not too long
|
||||
|
||||
pub const Cmd = struct {
|
||||
loop: *public.Loop,
|
||||
|
||||
// internal fields
|
||||
socket: std.posix.socket_t,
|
||||
@@ -63,7 +64,7 @@ pub const Cmd = struct {
|
||||
|
||||
if (size == 0) {
|
||||
// continue receving incomming messages asynchronously
|
||||
self.loop().io.recv(*Cmd, self, cbk, completion, self.socket, self.buf);
|
||||
self.loop.io.recv(*Cmd, self, cbk, completion, self.socket, self.buf);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -84,7 +85,7 @@ pub const Cmd = struct {
|
||||
self.msg_buf.read(self.alloc(), input, self, Cmd.do) catch unreachable;
|
||||
|
||||
// continue receving incomming messages asynchronously
|
||||
self.loop().io.recv(*Cmd, self, cbk, completion, self.socket, self.buf);
|
||||
self.loop.io.recv(*Cmd, self, cbk, completion, self.socket, self.buf);
|
||||
}
|
||||
|
||||
// shortcuts
|
||||
@@ -93,11 +94,6 @@ pub const Cmd = struct {
|
||||
return self.browser.currentSession().alloc;
|
||||
}
|
||||
|
||||
inline fn loop(self: *Cmd) public.Loop {
|
||||
// TODO: pointer instead?
|
||||
return self.browser.currentSession().loop;
|
||||
}
|
||||
|
||||
inline fn env(self: Cmd) public.Env {
|
||||
return self.browser.currentSession().env;
|
||||
}
|
||||
@@ -193,7 +189,7 @@ const Send = struct {
|
||||
return;
|
||||
};
|
||||
|
||||
self.cmd.loop().io.send(*Send, self, Send.asyncCbk, completion, self.cmd.socket, self.buf);
|
||||
self.cmd.loop.io.send(*Send, self, Send.asyncCbk, completion, self.cmd.socket, self.buf);
|
||||
}
|
||||
|
||||
fn asyncCbk(self: *Send, completion: *Completion, result: SendError!usize) void {
|
||||
@@ -209,12 +205,12 @@ const Send = struct {
|
||||
|
||||
pub fn sendLater(ctx: *Cmd, msg: []const u8, ns: u63) !void {
|
||||
const sd = try Send.init(ctx, msg);
|
||||
ctx.loop().io.timeout(*Send, sd.ctx, Send.laterCbk, sd.completion, ns);
|
||||
ctx.loop.io.timeout(*Send, sd.ctx, Send.laterCbk, sd.completion, ns);
|
||||
}
|
||||
|
||||
pub fn sendAsync(ctx: *Cmd, msg: []const u8) !void {
|
||||
const sd = try Send.init(ctx, msg);
|
||||
ctx.loop().io.send(*Send, sd.ctx, Send.asyncCbk, sd.completion, ctx.socket, msg);
|
||||
ctx.loop.io.send(*Send, sd.ctx, Send.asyncCbk, sd.completion, ctx.socket, msg);
|
||||
}
|
||||
|
||||
pub fn sendSync(ctx: *Cmd, msg: []const u8) !void {
|
||||
@@ -237,15 +233,14 @@ const Accept = struct {
|
||||
};
|
||||
|
||||
// receving incomming messages asynchronously
|
||||
self.cmd.loop().io.recv(*Cmd, self.cmd, Cmd.cbk, completion, self.cmd.socket, self.cmd.buf);
|
||||
self.cmd.loop.io.recv(*Cmd, self.cmd, Cmd.cbk, completion, self.cmd.socket, self.cmd.buf);
|
||||
}
|
||||
};
|
||||
|
||||
// Listen
|
||||
// ------
|
||||
|
||||
pub fn listen(browser: *Browser, socket: std.posix.socket_t) anyerror!void {
|
||||
const loop = browser.currentSession().loop;
|
||||
pub fn listen(browser: *Browser, loop: *public.Loop, socket: std.posix.socket_t) anyerror!void {
|
||||
|
||||
// MsgBuffer
|
||||
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 {
|
||||
// for accepting connections and receving messages
|
||||
var ctxInput: [BufReadSize]u8 = undefined;
|
||||
var cmd = Cmd{
|
||||
.loop = loop,
|
||||
.browser = browser,
|
||||
.socket = undefined,
|
||||
.buf = &ctxInput,
|
||||
|
||||
Reference in New Issue
Block a user