Adopt global Types changes in jsruntime

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-01-10 11:56:53 +01:00
parent c812ff300a
commit a5dfa6cb44
20 changed files with 49 additions and 69 deletions

View File

@@ -86,6 +86,7 @@ pub fn build(b: *std.build.Builder) !void {
const tests = b.addTest(.{ .root_source_file = .{ .path = "src/run_tests.zig" } }); const tests = b.addTest(.{ .root_source_file = .{ .path = "src/run_tests.zig" } });
try common(tests, options); try common(tests, options);
tests.single_threaded = true; tests.single_threaded = true;
tests.test_runner = "src/test_runner.zig";
const run_tests = b.addRunArtifact(tests); const run_tests = b.addRunArtifact(tests);
// step // step

View File

@@ -55,8 +55,7 @@ pub const Attr = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
var getters = [_]Case{ var getters = [_]Case{
.{ .src = "let a = document.createAttributeNS('foo', 'bar')", .ex = "undefined" }, .{ .src = "let a = document.createAttributeNS('foo', 'bar')", .ex = "undefined" },
.{ .src = "a.namespaceURI", .ex = "foo" }, .{ .src = "a.namespaceURI", .ex = "foo" },

View File

@@ -92,8 +92,7 @@ pub const CharacterData = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
var get_data = [_]Case{ var get_data = [_]Case{
.{ .src = "let link = document.getElementById('link')", .ex = "undefined" }, .{ .src = "let link = document.getElementById('link')", .ex = "undefined" },
.{ .src = "let cdata = link.firstChild", .ex = "undefined" }, .{ .src = "let cdata = link.firstChild", .ex = "undefined" },

View File

@@ -251,8 +251,7 @@ pub const Document = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
var constructor = [_]Case{ var constructor = [_]Case{
.{ .src = "document.__proto__.__proto__.constructor.name", .ex = "Document" }, .{ .src = "document.__proto__.__proto__.constructor.name", .ex = "Document" },
.{ .src = "document.__proto__.__proto__.__proto__.constructor.name", .ex = "Node" }, .{ .src = "document.__proto__.__proto__.__proto__.constructor.name", .ex = "Node" },

View File

@@ -290,8 +290,7 @@ pub const Element = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
var getters = [_]Case{ var getters = [_]Case{
.{ .src = "let g = document.getElementById('content')", .ex = "undefined" }, .{ .src = "let g = document.getElementById('content')", .ex = "undefined" },
.{ .src = "g.namespaceURI", .ex = "http://www.w3.org/1999/xhtml" }, .{ .src = "g.namespaceURI", .ex = "http://www.w3.org/1999/xhtml" },

View File

@@ -147,8 +147,7 @@ pub const DOMException = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
const err = "Failed to execute 'appendChild' on 'Node': The new child element contains the parent."; const err = "Failed to execute 'appendChild' on 'Node': The new child element contains the parent.";
var cases = [_]Case{ var cases = [_]Case{
.{ .src = "let content = document.getElementById('content')", .ex = "undefined" }, .{ .src = "let content = document.getElementById('content')", .ex = "undefined" },

View File

@@ -338,8 +338,7 @@ pub const HTMLCollection = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
var getElementsByTagName = [_]Case{ var getElementsByTagName = [_]Case{
.{ .src = "let getElementsByTagName = document.getElementsByTagName('p')", .ex = "undefined" }, .{ .src = "let getElementsByTagName = document.getElementsByTagName('p')", .ex = "undefined" },
.{ .src = "getElementsByTagName.length", .ex = "2" }, .{ .src = "getElementsByTagName.length", .ex = "2" },

View File

@@ -74,8 +74,7 @@ pub const DOMImplementation = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
var getImplementation = [_]Case{ var getImplementation = [_]Case{
.{ .src = "let impl = document.implementation", .ex = "undefined" }, .{ .src = "let impl = document.implementation", .ex = "undefined" },
.{ .src = "impl.createHTMLDocument();", .ex = "[object Document]" }, .{ .src = "impl.createHTMLDocument();", .ex = "[object Document]" },

View File

@@ -65,8 +65,7 @@ pub const NamedNodeMap = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
var setItem = [_]Case{ var setItem = [_]Case{
.{ .src = "let a = document.getElementById('content').attributes", .ex = "undefined" }, .{ .src = "let a = document.getElementById('content').attributes", .ex = "undefined" },
.{ .src = "a.length", .ex = "1" }, .{ .src = "a.length", .ex = "1" },

View File

@@ -264,8 +264,7 @@ pub const Node = struct {
pub fn testExecFn( pub fn testExecFn(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
// helper functions // helper functions
const trim_and_replace = const trim_and_replace =

View File

@@ -61,8 +61,7 @@ pub const NodeList = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
var childnodes = [_]Case{ var childnodes = [_]Case{
.{ .src = "let list = document.getElementById('content').childNodes", .ex = "undefined" }, .{ .src = "let list = document.getElementById('content').childNodes", .ex = "undefined" },
.{ .src = "list.length", .ex = "9" }, .{ .src = "list.length", .ex = "9" },

View File

@@ -43,8 +43,7 @@ pub const Text = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
var get_whole_text = [_]Case{ var get_whole_text = [_]Case{
.{ .src = "let text = document.getElementById('link').firstChild", .ex = "undefined" }, .{ .src = "let text = document.getElementById('link').firstChild", .ex = "undefined" },
.{ .src = "text.wholeText === 'OK'", .ex = "true" }, .{ .src = "text.wholeText === 'OK'", .ex = "true" },

View File

@@ -98,8 +98,7 @@ pub const DOMTokenList = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
var dynamiclist = [_]Case{ var dynamiclist = [_]Case{
.{ .src = "let gs = document.getElementById('para-empty')", .ex = "undefined" }, .{ .src = "let gs = document.getElementById('para-empty')", .ex = "undefined" },
.{ .src = "let cl = gs.classList", .ex = "undefined" }, .{ .src = "let cl = gs.classList", .ex = "undefined" },

View File

@@ -29,8 +29,7 @@ pub const HTMLDocument = struct {
pub fn testExecFn( pub fn testExecFn(
_: std.mem.Allocator, _: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime _: []jsruntime.API, ) anyerror!void {
) !void {
var constructor = [_]Case{ var constructor = [_]Case{
.{ .src = "document.__proto__.constructor.name", .ex = "HTMLDocument" }, .{ .src = "document.__proto__.constructor.name", .ex = "HTMLDocument" },
.{ .src = "document.__proto__.__proto__.constructor.name", .ex = "Document" }, .{ .src = "document.__proto__.__proto__.constructor.name", .ex = "Document" },

View File

@@ -5,6 +5,8 @@ const jsruntime = @import("jsruntime");
const parser = @import("netsurf.zig"); const parser = @import("netsurf.zig");
const DOM = @import("dom.zig"); const DOM = @import("dom.zig");
pub const Types = jsruntime.reflect(DOM.Interfaces);
const socket_path = "/tmp/browsercore-server.sock"; const socket_path = "/tmp/browsercore-server.sock";
var doc: *parser.DocumentHTML = undefined; var doc: *parser.DocumentHTML = undefined;
@@ -13,11 +15,10 @@ var server: std.net.StreamServer = undefined;
fn execJS( fn execJS(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime apis: []jsruntime.API, ) anyerror!void {
) !void {
// start JS env // start JS env
try js_env.start(alloc, apis); try js_env.start(alloc);
defer js_env.stop(); defer js_env.stop();
// alias global as self and window // alias global as self and window
@@ -25,7 +26,7 @@ fn execJS(
try js_env.attachObject(try js_env.getGlobal(), "window", null); try js_env.attachObject(try js_env.getGlobal(), "window", null);
// add document object // add document object
try js_env.addObject(apis, doc, "document"); try js_env.addObject(doc, "document");
while (true) { while (true) {
@@ -49,9 +50,6 @@ fn execJS(
pub fn main() !void { pub fn main() !void {
// generate APIs
const apis = comptime jsruntime.compile(DOM.Interfaces);
// create v8 vm // create v8 vm
const vm = jsruntime.VM.init(); const vm = jsruntime.VM.init();
defer vm.deinit(); defer vm.deinit();
@@ -86,5 +84,5 @@ pub fn main() !void {
try server.listen(addr); try server.listen(addr);
std.debug.print("Listening on: {s}...\n", .{socket_path}); std.debug.print("Listening on: {s}...\n", .{socket_path});
try jsruntime.loadEnv(&arena, execJS, apis); try jsruntime.loadEnv(&arena, execJS);
} }

View File

@@ -7,16 +7,17 @@ const DOM = @import("dom.zig");
const html_test = @import("html_test.zig").html; const html_test = @import("html_test.zig").html;
pub const Types = jsruntime.reflect(DOM.Interfaces);
var doc: *parser.DocumentHTML = undefined; var doc: *parser.DocumentHTML = undefined;
fn execJS( fn execJS(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime apis: []jsruntime.API, ) anyerror!void {
) !void {
// start JS env // start JS env
try js_env.start(alloc, apis); try js_env.start(alloc);
defer js_env.stop(); defer js_env.stop();
// alias global as self and window // alias global as self and window
@@ -24,17 +25,14 @@ fn execJS(
try js_env.attachObject(try js_env.getGlobal(), "window", null); try js_env.attachObject(try js_env.getGlobal(), "window", null);
// add document object // add document object
try js_env.addObject(apis, doc, "document"); try js_env.addObject(doc, "document");
// launch shellExec // launch shellExec
try jsruntime.shellExec(alloc, js_env, apis); try jsruntime.shellExec(alloc, js_env);
} }
pub fn main() !void { pub fn main() !void {
// generate APIs
const apis = comptime jsruntime.compile(DOM.Interfaces);
// allocator // allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
@@ -55,5 +53,5 @@ pub fn main() !void {
defer vm.deinit(); defer vm.deinit();
// launch shell // launch shell
try jsruntime.shell(&arena, apis, execJS, .{ .app_name = "browsercore" }); try jsruntime.shell(&arena, execJS, .{ .app_name = "browsercore" });
} }

View File

@@ -29,16 +29,14 @@ const Out = enum {
text, text,
}; };
pub const Types = jsruntime.reflect(DOM.Interfaces);
// TODO For now the WPT tests run is specific to WPT. // 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 // It manually load js framwork libs, and run the first script w/ js content in
// the HTML page. // the HTML page.
// Once browsercore will have the html loader, it would be useful to refacto // Once browsercore will have the html loader, it would be useful to refacto
// this test to use it. // this test to use it.
pub fn main() !void { pub fn main() !void {
// generate APIs
const apis = comptime jsruntime.compile(DOM.Interfaces);
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const alloc = gpa.allocator(); const alloc = gpa.allocator();
@@ -123,7 +121,7 @@ pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(alloc); var arena = std.heap.ArenaAllocator.init(alloc);
defer arena.deinit(); defer arena.deinit();
const res = wpt.run(&arena, apis, wpt_dir, tc, &loader) catch |err| { const res = wpt.run(&arena, wpt_dir, tc, &loader) catch |err| {
const suite = try Suite.init(alloc, tc, false, @errorName(err), null); const suite = try Suite.init(alloc, tc, false, @errorName(err), null);
try results.append(suite); try results.append(suite);

View File

@@ -20,17 +20,18 @@ const DOMTokenListExecFn = @import("dom/token_list.zig").testExecFn;
const NodeListTestExecFn = @import("dom/nodelist.zig").testExecFn; const NodeListTestExecFn = @import("dom/nodelist.zig").testExecFn;
const AttrTestExecFn = @import("dom/attribute.zig").testExecFn; const AttrTestExecFn = @import("dom/attribute.zig").testExecFn;
pub const Types = jsruntime.reflect(DOM.Interfaces);
var doc: *parser.DocumentHTML = undefined; var doc: *parser.DocumentHTML = undefined;
fn testExecFn( fn testExecFn(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime apis: []jsruntime.API,
comptime execFn: jsruntime.ContextExecFn, comptime execFn: jsruntime.ContextExecFn,
) !void { ) anyerror!void {
// start JS env // start JS env
try js_env.start(alloc, apis); try js_env.start(alloc);
defer js_env.stop(); defer js_env.stop();
// alias global as self and window // alias global as self and window
@@ -47,17 +48,16 @@ fn testExecFn(
}; };
// add document object // add document object
try js_env.addObject(apis, doc, "document"); try js_env.addObject(doc, "document");
// run test // run test
try execFn(alloc, js_env, apis); try execFn(alloc, js_env);
} }
fn testsAllExecFn( fn testsAllExecFn(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
js_env: *jsruntime.Env, js_env: *jsruntime.Env,
comptime apis: []jsruntime.API, ) anyerror!void {
) !void {
const testFns = [_]jsruntime.ContextExecFn{ const testFns = [_]jsruntime.ContextExecFn{
documentTestExecFn, documentTestExecFn,
HTMLDocumentTestExecFn, HTMLDocumentTestExecFn,
@@ -75,19 +75,16 @@ fn testsAllExecFn(
}; };
inline for (testFns) |testFn| { inline for (testFns) |testFn| {
try testExecFn(alloc, js_env, apis, testFn); try testExecFn(alloc, js_env, testFn);
} }
} }
test { pub fn main() !void {
std.debug.print("\n \n", .{}); std.debug.print("\n \n", .{});
// generate tests // generate tests
try generate.tests(); try generate.tests();
// generate APIs
const apis = comptime jsruntime.compile(DOM.Interfaces);
// create JS vm // create JS vm
const vm = jsruntime.VM.init(); const vm = jsruntime.VM.init();
defer vm.deinit(); defer vm.deinit();
@@ -96,7 +93,7 @@ test {
var arena_alloc = std.heap.ArenaAllocator.init(bench_alloc.allocator()); var arena_alloc = std.heap.ArenaAllocator.init(bench_alloc.allocator());
defer arena_alloc.deinit(); defer arena_alloc.deinit();
try jsruntime.loadEnv(&arena_alloc, testsAllExecFn, apis); try jsruntime.loadEnv(&arena_alloc, testsAllExecFn);
} }
test "DocumentHTMLParseFromStr" { test "DocumentHTMLParseFromStr" {

View File

@@ -8,12 +8,13 @@ const parser = @import("../netsurf.zig");
const jsruntime = @import("jsruntime"); const jsruntime = @import("jsruntime");
const Loop = jsruntime.Loop; const Loop = jsruntime.Loop;
const Env = jsruntime.Env; const Env = jsruntime.Env;
const TPL = jsruntime.TPL;
const Types = @import("../main_wpt.zig").Types;
// runWPT parses the given HTML file, starts a js env and run the first script // runWPT parses the given HTML file, starts a js env and run the first script
// tags containing javascript sources. // tags containing javascript sources.
// It loads first the js libs files. // It loads first the js libs files.
pub fn run(arena: *std.heap.ArenaAllocator, comptime apis: []jsruntime.API, comptime dir: []const u8, f: []const u8, loader: *FileLoader) !jsruntime.JSResult { pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const u8, loader: *FileLoader) !jsruntime.JSResult {
const alloc = arena.allocator(); const alloc = arena.allocator();
// document // document
@@ -31,16 +32,16 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime apis: []jsruntime.API, comp
var js_env = try Env.init(alloc, &loop); var js_env = try Env.init(alloc, &loop);
defer js_env.deinit(); defer js_env.deinit();
// load APIs in JS env // load user-defined types in JS env
var tpls: [apis.len]TPL = undefined; var js_types: [Types.len]usize = undefined;
try js_env.load(apis, &tpls); try js_env.load(&js_types);
// start JS env // start JS env
try js_env.start(alloc, apis); try js_env.start(alloc);
defer js_env.stop(); defer js_env.stop();
// add document object // add document object
try js_env.addObject(apis, html_doc, "document"); try js_env.addObject(html_doc, "document");
// alias global as self and window // alias global as self and window
try js_env.attachObject(try js_env.getGlobal(), "self", null); try js_env.attachObject(try js_env.getGlobal(), "self", null);