wpt: load script soruce file dynamically

This commit is contained in:
Pierre Tachoire
2023-10-27 19:00:26 +02:00
parent 9acb1890b4
commit 89431971ac

View File

@@ -82,7 +82,7 @@ pub fn main() !void {
defer vm.deinit();
// prepare libraries to load on each test case.
var loader = FileLoader.init(alloc, "tests/wpt");
var loader = FileLoader.init(alloc, wpt_dir);
defer loader.deinit();
// browse the dir to get the tests dynamically.
@@ -160,6 +160,8 @@ fn runWPT(arena: *std.heap.ArenaAllocator, comptime apis: []jsruntime.API, f: []
const html_doc = try parser.documentHTMLParseFromFileAlloc(alloc, f);
const doc = parser.documentHTMLToDocument(html_doc);
const dirname = std.fs.path.dirname(f[wpt_dir.len..]) orelse unreachable;
// create JS env
var loop = try Loop.init(alloc);
defer loop.deinit();
@@ -207,23 +209,28 @@ fn runWPT(arena: *std.heap.ArenaAllocator, comptime apis: []jsruntime.API, f: []
return res;
}
// TODO load <script src> attributes instead of the static list.
res = try evalJS(js_env, alloc, try loader.get("/resources/testharness.js"), "testharness.js");
if (!res.success) {
return res;
}
res = try evalJS(js_env, alloc, try loader.get("/resources/testharnessreport.js"), "testharnessreport.js");
if (!res.success) {
return res;
}
// loop hover the scripts.
const scripts = parser.documentGetElementsByTagName(doc, "script");
const slen = parser.nodeListLength(scripts);
for (0..slen) |i| {
const s = parser.nodeListItem(scripts, @intCast(i)).?;
const src = parser.nodeTextContent(s).?;
// If the script contains an src attribute, load it.
if (parser.elementGetAttribute(@as(*parser.Element, @ptrCast(s)), "src")) |src| {
var path = src;
if (!std.mem.startsWith(u8, src, "/")) {
// no need to free path, thanks to the arena.
path = try std.fs.path.join(alloc, &.{ "/", dirname, path });
}
res = try evalJS(js_env, alloc, try loader.get(path), src);
if (!res.success) {
return res;
}
}
// If the script as a source text, execute it.
const src = parser.nodeTextContent(s) orelse continue;
res = try evalJS(js_env, alloc, src, "");
// return the first failure.