mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 00:08:59 +00:00
browser: rename allocator to alloc
This commit is contained in:
@@ -25,23 +25,23 @@ const log = std.log.scoped(.browser);
|
|||||||
// A browser contains only one session.
|
// A browser contains only one session.
|
||||||
// TODO allow multiple sessions per browser.
|
// TODO allow multiple sessions per browser.
|
||||||
pub const Browser = struct {
|
pub const Browser = struct {
|
||||||
allocator: std.mem.Allocator,
|
alloc: std.mem.Allocator,
|
||||||
session: *Session = undefined,
|
session: *Session = undefined,
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator, vm: jsruntime.VM) !Browser {
|
pub fn init(alloc: std.mem.Allocator, vm: jsruntime.VM) !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...
|
||||||
_ = vm;
|
_ = vm;
|
||||||
|
|
||||||
return Browser{
|
return Browser{
|
||||||
.allocator = allocator,
|
.alloc = alloc,
|
||||||
.session = try Session.init(allocator, "about:blank"),
|
.session = try Session.init(alloc, "about:blank"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Browser) void {
|
pub fn deinit(self: *Browser) void {
|
||||||
self.session.deinit();
|
self.session.deinit();
|
||||||
self.allocator.destroy(self.session);
|
self.alloc.destroy(self.session);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn currentSession(self: *Browser) *Session {
|
pub fn currentSession(self: *Browser) *Session {
|
||||||
@@ -66,11 +66,11 @@ pub const Session = struct {
|
|||||||
|
|
||||||
window: Window,
|
window: Window,
|
||||||
|
|
||||||
fn init(allocator: std.mem.Allocator, uri: []const u8) !*Session {
|
fn init(alloc: std.mem.Allocator, uri: []const u8) !*Session {
|
||||||
var self = try allocator.create(Session);
|
var self = try alloc.create(Session);
|
||||||
self.* = Session{
|
self.* = Session{
|
||||||
.uri = uri,
|
.uri = uri,
|
||||||
.arena = std.heap.ArenaAllocator.init(allocator),
|
.arena = std.heap.ArenaAllocator.init(alloc),
|
||||||
.window = Window.create(null),
|
.window = Window.create(null),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ pub const Session = struct {
|
|||||||
// You can navigates multiple urls with the same page, but you have to call
|
// You can navigates multiple urls with the same page, but you have to call
|
||||||
// end() to stop the previous navigation before starting a new one.
|
// end() to stop the previous navigation before starting a new one.
|
||||||
pub const Page = struct {
|
pub const Page = struct {
|
||||||
allocator: std.mem.Allocator,
|
alloc: std.mem.Allocator,
|
||||||
loader: *Loader,
|
loader: *Loader,
|
||||||
env: *Env,
|
env: *Env,
|
||||||
window: *Window,
|
window: *Window,
|
||||||
@@ -119,13 +119,13 @@ pub const Page = struct {
|
|||||||
raw_data: ?[]const u8 = null,
|
raw_data: ?[]const u8 = null,
|
||||||
|
|
||||||
fn init(
|
fn init(
|
||||||
allocator: std.mem.Allocator,
|
alloc: std.mem.Allocator,
|
||||||
loader: *Loader,
|
loader: *Loader,
|
||||||
env: *Env,
|
env: *Env,
|
||||||
window: *Window,
|
window: *Window,
|
||||||
) Page {
|
) Page {
|
||||||
return Page{
|
return Page{
|
||||||
.allocator = allocator,
|
.alloc = alloc,
|
||||||
.loader = loader,
|
.loader = loader,
|
||||||
.env = env,
|
.env = env,
|
||||||
.window = window,
|
.window = window,
|
||||||
@@ -139,10 +139,10 @@ pub const Page = struct {
|
|||||||
|
|
||||||
pub fn deinit(self: *Page) void {
|
pub fn deinit(self: *Page) void {
|
||||||
if (self.raw_data) |s| {
|
if (self.raw_data) |s| {
|
||||||
self.allocator.free(s);
|
self.alloc.free(s);
|
||||||
}
|
}
|
||||||
if (self.raw_data) |s| {
|
if (self.raw_data) |s| {
|
||||||
self.allocator.free(s);
|
self.alloc.free(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,14 +165,14 @@ pub const Page = struct {
|
|||||||
log.debug("starting GET {s}", .{uri});
|
log.debug("starting GET {s}", .{uri});
|
||||||
|
|
||||||
// own the url
|
// own the url
|
||||||
if (self.rawuri) |prev| self.allocator.free(prev);
|
if (self.rawuri) |prev| self.alloc.free(prev);
|
||||||
self.rawuri = try self.allocator.dupe(u8, uri);
|
self.rawuri = try self.alloc.dupe(u8, uri);
|
||||||
self.uri = std.Uri.parse(self.rawuri.?) catch try std.Uri.parseWithoutScheme(self.rawuri.?);
|
self.uri = std.Uri.parse(self.rawuri.?) catch try std.Uri.parseWithoutScheme(self.rawuri.?);
|
||||||
|
|
||||||
// TODO handle fragment in url.
|
// TODO handle fragment in url.
|
||||||
|
|
||||||
// load the data
|
// load the data
|
||||||
var resp = try self.loader.get(self.allocator, self.uri);
|
var resp = try self.loader.get(self.alloc, self.uri);
|
||||||
defer resp.deinit();
|
defer resp.deinit();
|
||||||
|
|
||||||
const req = resp.req;
|
const req = resp.req;
|
||||||
@@ -198,7 +198,7 @@ pub const Page = struct {
|
|||||||
log.info("non-HTML document: {s}", .{ct});
|
log.info("non-HTML document: {s}", .{ct});
|
||||||
|
|
||||||
// save the body into the page.
|
// save the body into the page.
|
||||||
self.raw_data = try req.reader().readAllAlloc(self.allocator, 16 * 1024 * 1024);
|
self.raw_data = try req.reader().readAllAlloc(self.alloc, 16 * 1024 * 1024);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,7 +224,7 @@ pub const Page = struct {
|
|||||||
|
|
||||||
// start JS env
|
// start JS env
|
||||||
log.debug("start js env", .{});
|
log.debug("start js env", .{});
|
||||||
try self.env.start(self.allocator);
|
try self.env.start(self.alloc);
|
||||||
|
|
||||||
// add global objects
|
// add global objects
|
||||||
log.debug("setup global env", .{});
|
log.debug("setup global env", .{});
|
||||||
@@ -237,7 +237,7 @@ pub const Page = struct {
|
|||||||
// sasync stores scripts which can be run asynchronously.
|
// sasync stores scripts which can be run asynchronously.
|
||||||
// for now they are just run after the non-async one in order to
|
// for now they are just run after the non-async one in order to
|
||||||
// dispatch DOMContentLoaded the sooner as possible.
|
// dispatch DOMContentLoaded the sooner as possible.
|
||||||
var sasync = std.ArrayList(*parser.Element).init(self.allocator);
|
var sasync = std.ArrayList(*parser.Element).init(self.alloc);
|
||||||
defer sasync.deinit();
|
defer sasync.deinit();
|
||||||
|
|
||||||
const root = parser.documentToNode(doc);
|
const root = parser.documentToNode(doc);
|
||||||
@@ -333,9 +333,9 @@ pub const Page = struct {
|
|||||||
log.debug("starting GET {s}", .{src});
|
log.debug("starting GET {s}", .{src});
|
||||||
|
|
||||||
const u = std.Uri.parse(src) catch try std.Uri.parseWithoutScheme(src);
|
const u = std.Uri.parse(src) catch try std.Uri.parseWithoutScheme(src);
|
||||||
const ru = try std.Uri.resolve(self.uri, u, false, self.allocator);
|
const ru = try std.Uri.resolve(self.uri, u, false, self.alloc);
|
||||||
|
|
||||||
var fetchres = try self.loader.fetch(self.allocator, ru);
|
var fetchres = try self.loader.fetch(self.alloc, ru);
|
||||||
defer fetchres.deinit();
|
defer fetchres.deinit();
|
||||||
|
|
||||||
log.info("GET {any}: {d}", .{ ru, fetchres.status });
|
log.info("GET {any}: {d}", .{ ru, fetchres.status });
|
||||||
@@ -352,8 +352,8 @@ pub const Page = struct {
|
|||||||
if (fetchres.body == null) return;
|
if (fetchres.body == null) return;
|
||||||
|
|
||||||
var res = jsruntime.JSResult{};
|
var res = jsruntime.JSResult{};
|
||||||
try self.env.run(self.allocator, fetchres.body.?, src, &res, null);
|
try self.env.run(self.alloc, fetchres.body.?, src, &res, null);
|
||||||
defer res.deinit(self.allocator);
|
defer res.deinit(self.alloc);
|
||||||
|
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
log.debug("eval remote {s}: {s}", .{ src, res.result });
|
log.debug("eval remote {s}: {s}", .{ src, res.result });
|
||||||
@@ -371,8 +371,8 @@ pub const Page = struct {
|
|||||||
if (opt_text) |text| {
|
if (opt_text) |text| {
|
||||||
// TODO handle charset attribute
|
// TODO handle charset attribute
|
||||||
var res = jsruntime.JSResult{};
|
var res = jsruntime.JSResult{};
|
||||||
try self.env.run(self.allocator, text, "", &res, null);
|
try self.env.run(self.alloc, text, "", &res, null);
|
||||||
defer res.deinit(self.allocator);
|
defer res.deinit(self.alloc);
|
||||||
|
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
log.debug("eval inline: {s}", .{res.result});
|
log.debug("eval inline: {s}", .{res.result});
|
||||||
|
|||||||
@@ -6,19 +6,19 @@ pub const Loader = struct {
|
|||||||
client: std.http.Client,
|
client: std.http.Client,
|
||||||
|
|
||||||
pub const Response = struct {
|
pub const Response = struct {
|
||||||
allocator: std.mem.Allocator,
|
alloc: std.mem.Allocator,
|
||||||
req: *std.http.Client.Request,
|
req: *std.http.Client.Request,
|
||||||
|
|
||||||
pub fn deinit(self: *Response) void {
|
pub fn deinit(self: *Response) void {
|
||||||
self.req.deinit();
|
self.req.deinit();
|
||||||
self.allocator.destroy(self.req);
|
self.alloc.destroy(self.req);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator) Loader {
|
pub fn init(alloc: std.mem.Allocator) Loader {
|
||||||
return Loader{
|
return Loader{
|
||||||
.client = std.http.Client{
|
.client = std.http.Client{
|
||||||
.allocator = allocator,
|
.allocator = alloc,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -28,15 +28,15 @@ pub const Loader = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// the caller must deinit the FetchResult.
|
// the caller must deinit the FetchResult.
|
||||||
pub fn fetch(self: *Loader, allocator: std.mem.Allocator, uri: std.Uri) !std.http.Client.FetchResult {
|
pub fn fetch(self: *Loader, alloc: std.mem.Allocator, uri: std.Uri) !std.http.Client.FetchResult {
|
||||||
var headers = try std.http.Headers.initList(allocator, &[_]std.http.Field{
|
var headers = try std.http.Headers.initList(alloc, &[_]std.http.Field{
|
||||||
.{ .name = "User-Agent", .value = user_agent },
|
.{ .name = "User-Agent", .value = user_agent },
|
||||||
.{ .name = "Accept", .value = "*/*" },
|
.{ .name = "Accept", .value = "*/*" },
|
||||||
.{ .name = "Accept-Language", .value = "en-US,en;q=0.5" },
|
.{ .name = "Accept-Language", .value = "en-US,en;q=0.5" },
|
||||||
});
|
});
|
||||||
defer headers.deinit();
|
defer headers.deinit();
|
||||||
|
|
||||||
return try self.client.fetch(allocator, .{
|
return try self.client.fetch(alloc, .{
|
||||||
.location = .{ .uri = uri },
|
.location = .{ .uri = uri },
|
||||||
.headers = headers,
|
.headers = headers,
|
||||||
.payload = .none,
|
.payload = .none,
|
||||||
@@ -47,8 +47,8 @@ pub const Loader = struct {
|
|||||||
// https://ziglang.org/documentation/master/std/#A;std:http.Client.fetch
|
// https://ziglang.org/documentation/master/std/#A;std:http.Client.fetch
|
||||||
// for reference.
|
// for reference.
|
||||||
// The caller is responsible for calling `deinit()` on the `Response`.
|
// The caller is responsible for calling `deinit()` on the `Response`.
|
||||||
pub fn get(self: *Loader, allocator: std.mem.Allocator, uri: std.Uri) !Response {
|
pub fn get(self: *Loader, alloc: std.mem.Allocator, uri: std.Uri) !Response {
|
||||||
var headers = try std.http.Headers.initList(allocator, &[_]std.http.Field{
|
var headers = try std.http.Headers.initList(alloc, &[_]std.http.Field{
|
||||||
.{ .name = "User-Agent", .value = user_agent },
|
.{ .name = "User-Agent", .value = user_agent },
|
||||||
.{ .name = "Accept", .value = "*/*" },
|
.{ .name = "Accept", .value = "*/*" },
|
||||||
.{ .name = "Accept-Language", .value = "en-US,en;q=0.5" },
|
.{ .name = "Accept-Language", .value = "en-US,en;q=0.5" },
|
||||||
@@ -56,10 +56,10 @@ pub const Loader = struct {
|
|||||||
defer headers.deinit();
|
defer headers.deinit();
|
||||||
|
|
||||||
var resp = Response{
|
var resp = Response{
|
||||||
.allocator = allocator,
|
.alloc = alloc,
|
||||||
.req = try allocator.create(std.http.Client.Request),
|
.req = try alloc.create(std.http.Client.Request),
|
||||||
};
|
};
|
||||||
errdefer allocator.destroy(resp.req);
|
errdefer alloc.destroy(resp.req);
|
||||||
|
|
||||||
resp.req.* = try self.client.open(.GET, uri, headers, .{
|
resp.req.* = try self.client.open(.GET, uri, headers, .{
|
||||||
.handle_redirects = true, // TODO handle redirects manually
|
.handle_redirects = true, // TODO handle redirects manually
|
||||||
|
|||||||
Reference in New Issue
Block a user