userctx: inject user context

This commit is contained in:
Pierre Tachoire
2024-04-16 12:06:29 +02:00
parent c1b73dfdc2
commit e18d04a799
10 changed files with 31 additions and 4 deletions

View File

@@ -39,3 +39,5 @@ pub const Interfaces = generate.Tuple(.{
Storage.Interfaces,
URL.Interfaces,
});
pub const UserContext = @import("user_context.zig").UserContext;

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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();

View File

@@ -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

View File

@@ -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" {

View File

@@ -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
View File

@@ -0,0 +1,6 @@
const std = @import("std");
const parser = @import("netsurf.zig");
pub const UserContext = struct {
document: ?*parser.DocumentHTML,
};

View File

@@ -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);