mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
userctx: inject user context
This commit is contained in:
@@ -39,3 +39,5 @@ pub const Interfaces = generate.Tuple(.{
|
||||
Storage.Interfaces,
|
||||
URL.Interfaces,
|
||||
});
|
||||
|
||||
pub const UserContext = @import("user_context.zig").UserContext;
|
||||
|
||||
@@ -107,7 +107,7 @@ pub const Session = struct {
|
||||
.storageShed = storage.Shed.init(alloc),
|
||||
};
|
||||
|
||||
self.env = try Env.init(self.arena.allocator(), &self.loop);
|
||||
self.env = try Env.init(self.arena.allocator(), &self.loop, null);
|
||||
try self.env.load(&self.jstypes);
|
||||
|
||||
return self;
|
||||
|
||||
@@ -25,6 +25,7 @@ const apiweb = @import("apiweb.zig");
|
||||
const Window = @import("html/window.zig").Window;
|
||||
|
||||
pub const Types = jsruntime.reflect(apiweb.Interfaces);
|
||||
pub const UserContext = apiweb.UserContext;
|
||||
|
||||
const socket_path = "/tmp/browsercore-server.sock";
|
||||
|
||||
@@ -103,5 +104,5 @@ pub fn main() !void {
|
||||
try server.listen(addr);
|
||||
std.debug.print("Listening on: {s}...\n", .{socket_path});
|
||||
|
||||
try jsruntime.loadEnv(&arena, execJS);
|
||||
try jsruntime.loadEnv(&arena, null, execJS);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ const jsruntime = @import("jsruntime");
|
||||
const apiweb = @import("apiweb.zig");
|
||||
|
||||
pub const Types = jsruntime.reflect(apiweb.Interfaces);
|
||||
pub const UserContext = apiweb.UserContext;
|
||||
|
||||
pub const std_options = struct {
|
||||
pub const log_level = .debug;
|
||||
|
||||
@@ -28,6 +28,7 @@ const storage = @import("storage/storage.zig");
|
||||
const html_test = @import("html_test.zig").html;
|
||||
|
||||
pub const Types = jsruntime.reflect(apiweb.Interfaces);
|
||||
pub const UserContext = apiweb.UserContext;
|
||||
|
||||
var doc: *parser.DocumentHTML = undefined;
|
||||
|
||||
@@ -39,6 +40,10 @@ fn execJS(
|
||||
try js_env.start(alloc);
|
||||
defer js_env.stop();
|
||||
|
||||
js_env.setUserContext(UserContext{
|
||||
.document = doc,
|
||||
});
|
||||
|
||||
var storageShelf = storage.Shelf.init(alloc);
|
||||
defer storageShelf.deinit();
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ const Out = enum {
|
||||
|
||||
pub const Types = jsruntime.reflect(apiweb.Interfaces);
|
||||
pub const GlobalType = apiweb.GlobalType;
|
||||
pub const UserContext = apiweb.UserContext;
|
||||
|
||||
// TODO For now the WPT tests run is specific to WPT.
|
||||
// It manually load js framwork libs, and run the first script w/ js content in
|
||||
|
||||
@@ -54,6 +54,7 @@ const URLTestExecFn = url.testExecFn;
|
||||
const HTMLElementTestExecFn = @import("html/elements.zig").testExecFn;
|
||||
|
||||
pub const Types = jsruntime.reflect(apiweb.Interfaces);
|
||||
pub const UserContext = @import("user_context.zig").UserContext;
|
||||
|
||||
var doc: *parser.DocumentHTML = undefined;
|
||||
|
||||
@@ -81,6 +82,8 @@ fn testExecFn(
|
||||
std.debug.print("documentHTMLClose error: {s}\n", .{@errorName(err)});
|
||||
};
|
||||
|
||||
js_env.getUserContext().?.document = doc;
|
||||
|
||||
// alias global as self and window
|
||||
var window = Window.create(null);
|
||||
|
||||
@@ -315,7 +318,11 @@ fn testJSRuntime(alloc: std.mem.Allocator) !void {
|
||||
var arena_alloc = std.heap.ArenaAllocator.init(alloc);
|
||||
defer arena_alloc.deinit();
|
||||
|
||||
try jsruntime.loadEnv(&arena_alloc, testsAllExecFn);
|
||||
const userctx = UserContext{
|
||||
.document = null,
|
||||
};
|
||||
|
||||
try jsruntime.loadEnv(&arena_alloc, userctx, testsAllExecFn);
|
||||
}
|
||||
|
||||
test "DocumentHTMLParseFromStr" {
|
||||
|
||||
@@ -21,6 +21,7 @@ const std = @import("std");
|
||||
const tests = @import("run_tests.zig");
|
||||
|
||||
pub const Types = tests.Types;
|
||||
pub const UserContext = tests.UserContext;
|
||||
|
||||
pub fn main() !void {
|
||||
try tests.main();
|
||||
|
||||
6
src/user_context.zig
Normal file
6
src/user_context.zig
Normal file
@@ -0,0 +1,6 @@
|
||||
const std = @import("std");
|
||||
const parser = @import("netsurf.zig");
|
||||
|
||||
pub const UserContext = struct {
|
||||
document: ?*parser.DocumentHTML,
|
||||
};
|
||||
@@ -30,6 +30,7 @@ const Window = @import("../html/window.zig").Window;
|
||||
const storage = @import("../storage/storage.zig");
|
||||
|
||||
const Types = @import("../main_wpt.zig").Types;
|
||||
const UserContext = @import("../main_wpt.zig").UserContext;
|
||||
|
||||
// runWPT parses the given HTML file, starts a js env and run the first script
|
||||
// tags containing javascript sources.
|
||||
@@ -50,7 +51,9 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
|
||||
// create JS env
|
||||
var loop = try Loop.init(alloc);
|
||||
defer loop.deinit();
|
||||
var js_env = try Env.init(alloc, &loop);
|
||||
var js_env = try Env.init(alloc, &loop, UserContext{
|
||||
.document = html_doc,
|
||||
});
|
||||
defer js_env.deinit();
|
||||
|
||||
var storageShelf = storage.Shelf.init(alloc);
|
||||
|
||||
Reference in New Issue
Block a user