mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
Merge pull request #253 from lightpanda-io/refacto_exec
Adapt to js_exec changes in zig-js-runtime
This commit is contained in:
@@ -198,22 +198,22 @@ pub const Page = struct {
|
||||
}
|
||||
|
||||
pub fn wait(self: *Page) !void {
|
||||
|
||||
// try catch
|
||||
var try_catch: jsruntime.TryCatch = undefined;
|
||||
try_catch.init(self.session.env);
|
||||
defer try_catch.deinit();
|
||||
|
||||
self.session.env.wait() catch {
|
||||
const alloc = self.arena.allocator();
|
||||
var res = try self.session.env.waitTryCatch(alloc);
|
||||
defer res.deinit(alloc);
|
||||
|
||||
if (res.success) {
|
||||
log.debug("wait: {s}", .{res.result});
|
||||
} else {
|
||||
if (builtin.mode == .Debug and res.stack != null) {
|
||||
log.info("wait: {s}", .{res.stack.?});
|
||||
} else {
|
||||
log.info("wait: {s}", .{res.result});
|
||||
}
|
||||
}
|
||||
|
||||
if (try try_catch.err(alloc, self.session.env)) |msg| {
|
||||
defer alloc.free(msg);
|
||||
log.info("wait error: {s}", .{msg});
|
||||
return;
|
||||
}
|
||||
};
|
||||
log.debug("wait: OK", .{});
|
||||
}
|
||||
|
||||
// spec reference: https://html.spec.whatwg.org/#document-lifecycle
|
||||
pub fn navigate(self: *Page, uri: []const u8) !void {
|
||||
@@ -322,7 +322,7 @@ pub const Page = struct {
|
||||
// start JS env
|
||||
// TODO load the js env concurrently with the HTML parsing.
|
||||
log.debug("start js env", .{});
|
||||
try self.session.env.start(alloc);
|
||||
try self.session.env.start();
|
||||
|
||||
// replace the user context document with the new one.
|
||||
try self.session.env.setUserContext(.{
|
||||
@@ -473,22 +473,26 @@ pub const Page = struct {
|
||||
return;
|
||||
}
|
||||
|
||||
var try_catch: jsruntime.TryCatch = undefined;
|
||||
try_catch.init(self.session.env);
|
||||
defer try_catch.deinit();
|
||||
|
||||
const opt_text = try parser.nodeTextContent(parser.elementToNode(e));
|
||||
if (opt_text) |text| {
|
||||
// TODO handle charset attribute
|
||||
var res = try self.session.env.execTryCatch(alloc, text, "");
|
||||
defer res.deinit(alloc);
|
||||
|
||||
if (res.success) {
|
||||
log.debug("eval inline: {s}", .{res.result});
|
||||
} else {
|
||||
if (builtin.mode == .Debug and res.stack != null) {
|
||||
log.info("eval inline: {s}", .{res.stack.?});
|
||||
} else {
|
||||
log.info("eval inline: {s}", .{res.result});
|
||||
}
|
||||
const res = self.session.env.exec(text, "") catch {
|
||||
if (try try_catch.err(alloc, self.session.env)) |msg| {
|
||||
defer alloc.free(msg);
|
||||
log.info("eval inline {s}: {s}", .{ text, msg });
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
if (builtin.mode == .Debug) {
|
||||
const msg = try res.toString(alloc, self.session.env);
|
||||
defer alloc.free(msg);
|
||||
log.debug("eval inline {s}", .{msg});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -530,18 +534,22 @@ pub const Page = struct {
|
||||
// check no body
|
||||
if (body.len == 0) return FetchError.NoBody;
|
||||
|
||||
var res = try self.session.env.execTryCatch(alloc, body, src);
|
||||
defer res.deinit(alloc);
|
||||
var try_catch: jsruntime.TryCatch = undefined;
|
||||
try_catch.init(self.session.env);
|
||||
defer try_catch.deinit();
|
||||
|
||||
if (res.success) {
|
||||
log.debug("eval remote {s}: {s}", .{ src, res.result });
|
||||
} else {
|
||||
if (builtin.mode == .Debug and res.stack != null) {
|
||||
log.info("eval remote {s}: {s}", .{ src, res.stack.? });
|
||||
} else {
|
||||
log.info("eval remote {s}: {s}", .{ src, res.result });
|
||||
const res = self.session.env.exec(body, src) catch {
|
||||
if (try try_catch.err(alloc, self.session.env)) |msg| {
|
||||
defer alloc.free(msg);
|
||||
log.info("eval remote {s}: {s}", .{ src, msg });
|
||||
}
|
||||
return FetchError.JsErr;
|
||||
};
|
||||
|
||||
if (builtin.mode == .Debug) {
|
||||
const msg = try res.toString(alloc, self.session.env);
|
||||
defer alloc.free(msg);
|
||||
log.debug("eval remote {s}: {s}", .{ src, msg });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
18
src/main.zig
18
src/main.zig
@@ -37,7 +37,7 @@ fn execJS(
|
||||
js_env: *jsruntime.Env,
|
||||
) anyerror!void {
|
||||
// start JS env
|
||||
try js_env.start(alloc);
|
||||
try js_env.start();
|
||||
defer js_env.stop();
|
||||
|
||||
// alias global as self and window
|
||||
@@ -45,6 +45,11 @@ fn execJS(
|
||||
window.replaceDocument(doc);
|
||||
try js_env.bindGlobal(window);
|
||||
|
||||
// try catch
|
||||
var try_catch: jsruntime.TryCatch = undefined;
|
||||
try_catch.init(js_env.*);
|
||||
defer try_catch.deinit();
|
||||
|
||||
while (true) {
|
||||
|
||||
// read cmd
|
||||
@@ -57,11 +62,12 @@ fn execJS(
|
||||
break;
|
||||
}
|
||||
|
||||
const res = try js_env.execTryCatch(alloc, cmd, "cdp");
|
||||
if (res.success) {
|
||||
std.debug.print("-> {s}\n", .{res.result});
|
||||
}
|
||||
_ = try conn.stream.write(res.result);
|
||||
const res = try js_env.exec(cmd, "cdp");
|
||||
const res_str = try res.toString(alloc, js_env.*);
|
||||
defer alloc.free(res_str);
|
||||
std.debug.print("-> {s}\n", .{res_str});
|
||||
|
||||
_ = try conn.stream.write(res_str);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ fn execJS(
|
||||
js_env: *jsruntime.Env,
|
||||
) anyerror!void {
|
||||
// start JS env
|
||||
try js_env.start(alloc);
|
||||
try js_env.start();
|
||||
defer js_env.stop();
|
||||
|
||||
var cli = Client{ .allocator = alloc, .loop = js_env.nat_ctx.loop };
|
||||
|
||||
@@ -142,7 +142,7 @@ pub fn main() !void {
|
||||
defer arena.deinit();
|
||||
|
||||
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));
|
||||
try results.append(suite);
|
||||
|
||||
if (out == .text) {
|
||||
@@ -151,9 +151,9 @@ pub fn main() !void {
|
||||
failures += 1;
|
||||
continue;
|
||||
};
|
||||
// no need to call res.deinit() thanks to the arena allocator.
|
||||
defer res.deinit(arena.allocator());
|
||||
|
||||
const suite = try Suite.init(alloc, tc, res.success, res.result, res.stack);
|
||||
const suite = try Suite.init(alloc, tc, res.ok, res.msg orelse "");
|
||||
try results.append(suite);
|
||||
|
||||
if (out == .json) {
|
||||
@@ -196,7 +196,7 @@ pub fn main() !void {
|
||||
try cases.append(Case{
|
||||
.pass = suite.pass,
|
||||
.name = suite.name,
|
||||
.message = suite.stack orelse suite.message,
|
||||
.message = suite.message,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ fn testExecFn(
|
||||
defer parser.deinit();
|
||||
|
||||
// start JS env
|
||||
try js_env.start(alloc);
|
||||
try js_env.start();
|
||||
defer js_env.stop();
|
||||
|
||||
var storageShelf = storage.Shelf.init(alloc);
|
||||
|
||||
@@ -36,7 +36,7 @@ const Client = @import("../async/Client.zig");
|
||||
// runWPT parses the given HTML file, starts a js env and run the first script
|
||||
// tags containing javascript sources.
|
||||
// It loads first the js libs files.
|
||||
pub fn run(arena: *std.heap.ArenaAllocator, 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) !Res {
|
||||
const alloc = arena.allocator();
|
||||
try parser.init();
|
||||
defer parser.deinit();
|
||||
@@ -70,15 +70,16 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
|
||||
try js_env.load(&js_types);
|
||||
|
||||
// start JS env
|
||||
try js_env.start(alloc);
|
||||
try js_env.start();
|
||||
defer js_env.stop();
|
||||
|
||||
// display console logs
|
||||
defer {
|
||||
var res = evalJS(js_env, alloc, "console.join('\\n');", "console") catch unreachable;
|
||||
const res = evalJS(js_env, alloc, "console.join('\\n');", "console") catch unreachable;
|
||||
defer res.deinit(alloc);
|
||||
if (res.result.len > 0) {
|
||||
std.debug.print("-- CONSOLE LOG\n{s}\n--\n", .{res.result});
|
||||
|
||||
if (res.msg != null and res.msg.?.len > 0) {
|
||||
std.debug.print("-- CONSOLE LOG\n{s}\n--\n", .{res.msg.?});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,9 +89,6 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
|
||||
window.setStorageShelf(&storageShelf);
|
||||
try js_env.bindGlobal(&window);
|
||||
|
||||
// thanks to the arena, we don't need to deinit res.
|
||||
var res: jsruntime.JSResult = undefined;
|
||||
|
||||
const init =
|
||||
\\console = [];
|
||||
\\console.log = function () {
|
||||
@@ -100,10 +98,8 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
|
||||
\\ console.push("debug", ...arguments);
|
||||
\\};
|
||||
;
|
||||
res = try evalJS(js_env, alloc, init, "init");
|
||||
if (!res.success) {
|
||||
return res;
|
||||
}
|
||||
var res = try evalJS(js_env, alloc, init, "init");
|
||||
if (!res.ok) return res;
|
||||
res.deinit(alloc);
|
||||
|
||||
// loop hover the scripts.
|
||||
@@ -122,20 +118,14 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
|
||||
}
|
||||
|
||||
res = try evalJS(js_env, alloc, try loader.get(path), src);
|
||||
if (!res.success) {
|
||||
return res;
|
||||
}
|
||||
if (!res.ok) return res;
|
||||
res.deinit(alloc);
|
||||
}
|
||||
|
||||
// If the script as a source text, execute it.
|
||||
const src = try parser.nodeTextContent(s) orelse continue;
|
||||
res = try evalJS(js_env, alloc, src, "");
|
||||
|
||||
// return the first failure.
|
||||
if (!res.success) {
|
||||
return res;
|
||||
}
|
||||
if (!res.ok) return res;
|
||||
res.deinit(alloc);
|
||||
}
|
||||
|
||||
@@ -150,25 +140,52 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
|
||||
);
|
||||
|
||||
// wait for all async executions
|
||||
res = try js_env.waitTryCatch(alloc);
|
||||
if (!res.success) {
|
||||
return res;
|
||||
}
|
||||
res.deinit(alloc);
|
||||
var try_catch: jsruntime.TryCatch = undefined;
|
||||
try_catch.init(js_env);
|
||||
defer try_catch.deinit();
|
||||
js_env.wait() catch {
|
||||
return .{
|
||||
.ok = false,
|
||||
.msg = try try_catch.err(alloc, js_env),
|
||||
};
|
||||
};
|
||||
|
||||
// Check the final test status.
|
||||
res = try evalJS(js_env, alloc, "report.status;", "teststatus");
|
||||
if (!res.success) {
|
||||
return res;
|
||||
}
|
||||
if (!res.ok) return res;
|
||||
res.deinit(alloc);
|
||||
|
||||
// return the detailed result.
|
||||
return try evalJS(js_env, alloc, "report.log", "teststatus");
|
||||
}
|
||||
|
||||
fn evalJS(env: jsruntime.Env, alloc: std.mem.Allocator, script: []const u8, name: ?[]const u8) !jsruntime.JSResult {
|
||||
return try env.execTryCatch(alloc, script, name);
|
||||
pub const Res = struct {
|
||||
ok: bool,
|
||||
msg: ?[]const u8,
|
||||
|
||||
pub fn deinit(res: Res, alloc: std.mem.Allocator) void {
|
||||
if (res.msg) |msg| {
|
||||
alloc.free(msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn evalJS(env: jsruntime.Env, alloc: std.mem.Allocator, script: []const u8, name: ?[]const u8) !Res {
|
||||
var try_catch: jsruntime.TryCatch = undefined;
|
||||
try_catch.init(env);
|
||||
defer try_catch.deinit();
|
||||
|
||||
const v = env.exec(script, name) catch {
|
||||
return .{
|
||||
.ok = false,
|
||||
.msg = try try_catch.err(alloc, env),
|
||||
};
|
||||
};
|
||||
|
||||
return .{
|
||||
.ok = true,
|
||||
.msg = try v.toString(alloc, env),
|
||||
};
|
||||
}
|
||||
|
||||
// browse the path to find the tests list.
|
||||
|
||||
@@ -67,28 +67,22 @@ pub const Suite = struct {
|
||||
pass: bool,
|
||||
name: []const u8,
|
||||
message: ?[]const u8,
|
||||
stack: ?[]const u8,
|
||||
cases: ?[]Case,
|
||||
|
||||
// caller owns the wpt.Suite.
|
||||
// owner must call deinit().
|
||||
pub fn init(alloc: std.mem.Allocator, name: []const u8, pass: bool, res: []const u8, stack: ?[]const u8) !Suite {
|
||||
pub fn init(alloc: std.mem.Allocator, name: []const u8, pass: bool, res: []const u8) !Suite {
|
||||
var suite = Suite{
|
||||
.alloc = alloc,
|
||||
.pass = false,
|
||||
.name = try alloc.dupe(u8, name),
|
||||
.message = null,
|
||||
.stack = null,
|
||||
.cases = null,
|
||||
};
|
||||
|
||||
// handle JS error.
|
||||
if (!pass) {
|
||||
suite.message = try alloc.dupe(u8, res);
|
||||
if (stack) |st| {
|
||||
suite.stack = try alloc.dupe(u8, st);
|
||||
}
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
@@ -155,10 +149,6 @@ pub const Suite = struct {
|
||||
pub fn deinit(self: Suite) void {
|
||||
self.alloc.free(self.name);
|
||||
|
||||
if (self.stack) |stack| {
|
||||
self.alloc.free(stack);
|
||||
}
|
||||
|
||||
if (self.message) |res| {
|
||||
self.alloc.free(res);
|
||||
}
|
||||
@@ -175,9 +165,6 @@ pub const Suite = struct {
|
||||
if (self.message) |v| {
|
||||
return v;
|
||||
}
|
||||
if (self.stack) |v| {
|
||||
return v;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
};
|
||||
@@ -199,7 +186,7 @@ test "success test case" {
|
||||
,
|
||||
};
|
||||
|
||||
const suite = Suite.init(alloc, "foo", res.pass, res.result, null) catch unreachable; // TODO
|
||||
const suite = Suite.init(alloc, "foo", res.pass, res.result) catch unreachable; // TODO
|
||||
defer suite.deinit();
|
||||
|
||||
try testing.expect(suite.pass == true);
|
||||
@@ -226,7 +213,7 @@ test "failed test case" {
|
||||
,
|
||||
};
|
||||
|
||||
const suite = Suite.init(alloc, "foo", res.pass, res.result, null) catch unreachable; // TODO
|
||||
const suite = Suite.init(alloc, "foo", res.pass, res.result) catch unreachable; // TODO
|
||||
defer suite.deinit();
|
||||
|
||||
try testing.expect(suite.pass == false);
|
||||
@@ -251,7 +238,7 @@ test "invalid result" {
|
||||
,
|
||||
};
|
||||
|
||||
const suite = Suite.init(alloc, "foo", res.pass, res.result, null) catch unreachable; // TODO
|
||||
const suite = Suite.init(alloc, "foo", res.pass, res.result) catch unreachable; // TODO
|
||||
defer suite.deinit();
|
||||
|
||||
try testing.expect(suite.pass == false);
|
||||
@@ -266,7 +253,7 @@ test "invalid result" {
|
||||
,
|
||||
};
|
||||
|
||||
const suite2 = Suite.init(alloc, "foo", res2.pass, res2.result, null) catch unreachable; // TODO
|
||||
const suite2 = Suite.init(alloc, "foo", res2.pass, res2.result) catch unreachable; // TODO
|
||||
defer suite2.deinit();
|
||||
|
||||
try testing.expect(suite2.pass == false);
|
||||
|
||||
2
vendor/zig-js-runtime
vendored
2
vendor/zig-js-runtime
vendored
Submodule vendor/zig-js-runtime updated: a820cf4387...e7fd4b1e04
Reference in New Issue
Block a user