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