Skip to content

Commit 5f5e01a

Browse files
Merge pull request #253 from lightpanda-io/refacto_exec
Adapt to js_exec changes in zig-js-runtime
2 parents f8395fe + 8c3939b commit 5f5e01a

File tree

8 files changed

+113
-95
lines changed

8 files changed

+113
-95
lines changed

src/browser/browser.zig

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,21 @@ pub const Page = struct {
198198
}
199199

200200
pub fn wait(self: *Page) !void {
201-
const alloc = self.arena.allocator();
202-
var res = try self.session.env.waitTryCatch(alloc);
203-
defer res.deinit(alloc);
204201

205-
if (res.success) {
206-
log.debug("wait: {s}", .{res.result});
207-
} else {
208-
if (builtin.mode == .Debug and res.stack != null) {
209-
log.info("wait: {s}", .{res.stack.?});
210-
} else {
211-
log.info("wait: {s}", .{res.result});
202+
// try catch
203+
var try_catch: jsruntime.TryCatch = undefined;
204+
try_catch.init(self.session.env);
205+
defer try_catch.deinit();
206+
207+
self.session.env.wait() catch {
208+
const alloc = self.arena.allocator();
209+
if (try try_catch.err(alloc, self.session.env)) |msg| {
210+
defer alloc.free(msg);
211+
log.info("wait error: {s}", .{msg});
212+
return;
212213
}
213-
}
214-
215-
return;
214+
};
215+
log.debug("wait: OK", .{});
216216
}
217217

218218
// spec reference: https://html.spec.whatwg.org/#document-lifecycle
@@ -322,7 +322,7 @@ pub const Page = struct {
322322
// start JS env
323323
// TODO load the js env concurrently with the HTML parsing.
324324
log.debug("start js env", .{});
325-
try self.session.env.start(alloc);
325+
try self.session.env.start();
326326

327327
// replace the user context document with the new one.
328328
try self.session.env.setUserContext(.{
@@ -473,22 +473,26 @@ pub const Page = struct {
473473
return;
474474
}
475475

476+
var try_catch: jsruntime.TryCatch = undefined;
477+
try_catch.init(self.session.env);
478+
defer try_catch.deinit();
479+
476480
const opt_text = try parser.nodeTextContent(parser.elementToNode(e));
477481
if (opt_text) |text| {
478482
// TODO handle charset attribute
479-
var res = try self.session.env.execTryCatch(alloc, text, "");
480-
defer res.deinit(alloc);
481-
482-
if (res.success) {
483-
log.debug("eval inline: {s}", .{res.result});
484-
} else {
485-
if (builtin.mode == .Debug and res.stack != null) {
486-
log.info("eval inline: {s}", .{res.stack.?});
487-
} else {
488-
log.info("eval inline: {s}", .{res.result});
483+
const res = self.session.env.exec(text, "") catch {
484+
if (try try_catch.err(alloc, self.session.env)) |msg| {
485+
defer alloc.free(msg);
486+
log.info("eval inline {s}: {s}", .{ text, msg });
489487
}
490-
}
488+
return;
489+
};
491490

491+
if (builtin.mode == .Debug) {
492+
const msg = try res.toString(alloc, self.session.env);
493+
defer alloc.free(msg);
494+
log.debug("eval inline {s}", .{msg});
495+
}
492496
return;
493497
}
494498

@@ -530,18 +534,22 @@ pub const Page = struct {
530534
// check no body
531535
if (body.len == 0) return FetchError.NoBody;
532536

533-
var res = try self.session.env.execTryCatch(alloc, body, src);
534-
defer res.deinit(alloc);
537+
var try_catch: jsruntime.TryCatch = undefined;
538+
try_catch.init(self.session.env);
539+
defer try_catch.deinit();
535540

536-
if (res.success) {
537-
log.debug("eval remote {s}: {s}", .{ src, res.result });
538-
} else {
539-
if (builtin.mode == .Debug and res.stack != null) {
540-
log.info("eval remote {s}: {s}", .{ src, res.stack.? });
541-
} else {
542-
log.info("eval remote {s}: {s}", .{ src, res.result });
541+
const res = self.session.env.exec(body, src) catch {
542+
if (try try_catch.err(alloc, self.session.env)) |msg| {
543+
defer alloc.free(msg);
544+
log.info("eval remote {s}: {s}", .{ src, msg });
543545
}
544546
return FetchError.JsErr;
547+
};
548+
549+
if (builtin.mode == .Debug) {
550+
const msg = try res.toString(alloc, self.session.env);
551+
defer alloc.free(msg);
552+
log.debug("eval remote {s}: {s}", .{ src, msg });
545553
}
546554
}
547555

src/main.zig

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ fn execJS(
3737
js_env: *jsruntime.Env,
3838
) anyerror!void {
3939
// start JS env
40-
try js_env.start(alloc);
40+
try js_env.start();
4141
defer js_env.stop();
4242

4343
// alias global as self and window
4444
var window = Window.create(null);
4545
window.replaceDocument(doc);
4646
try js_env.bindGlobal(window);
4747

48+
// try catch
49+
var try_catch: jsruntime.TryCatch = undefined;
50+
try_catch.init(js_env.*);
51+
defer try_catch.deinit();
52+
4853
while (true) {
4954

5055
// read cmd
@@ -57,11 +62,12 @@ fn execJS(
5762
break;
5863
}
5964

60-
const res = try js_env.execTryCatch(alloc, cmd, "cdp");
61-
if (res.success) {
62-
std.debug.print("-> {s}\n", .{res.result});
63-
}
64-
_ = try conn.stream.write(res.result);
65+
const res = try js_env.exec(cmd, "cdp");
66+
const res_str = try res.toString(alloc, js_env.*);
67+
defer alloc.free(res_str);
68+
std.debug.print("-> {s}\n", .{res_str});
69+
70+
_ = try conn.stream.write(res_str);
6571
}
6672
}
6773

src/main_shell.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn execJS(
3838
js_env: *jsruntime.Env,
3939
) anyerror!void {
4040
// start JS env
41-
try js_env.start(alloc);
41+
try js_env.start();
4242
defer js_env.stop();
4343

4444
var cli = Client{ .allocator = alloc, .loop = js_env.nat_ctx.loop };

src/main_wpt.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub fn main() !void {
142142
defer arena.deinit();
143143

144144
const res = wpt.run(&arena, wpt_dir, tc, &loader) catch |err| {
145-
const suite = try Suite.init(alloc, tc, false, @errorName(err), null);
145+
const suite = try Suite.init(alloc, tc, false, @errorName(err));
146146
try results.append(suite);
147147

148148
if (out == .text) {
@@ -151,9 +151,9 @@ pub fn main() !void {
151151
failures += 1;
152152
continue;
153153
};
154-
// no need to call res.deinit() thanks to the arena allocator.
154+
defer res.deinit(arena.allocator());
155155

156-
const suite = try Suite.init(alloc, tc, res.success, res.result, res.stack);
156+
const suite = try Suite.init(alloc, tc, res.ok, res.msg orelse "");
157157
try results.append(suite);
158158

159159
if (out == .json) {
@@ -196,7 +196,7 @@ pub fn main() !void {
196196
try cases.append(Case{
197197
.pass = suite.pass,
198198
.name = suite.name,
199-
.message = suite.stack orelse suite.message,
199+
.message = suite.message,
200200
});
201201
}
202202

src/run_tests.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn testExecFn(
7171
defer parser.deinit();
7272

7373
// start JS env
74-
try js_env.start(alloc);
74+
try js_env.start();
7575
defer js_env.stop();
7676

7777
var storageShelf = storage.Shelf.init(alloc);

src/wpt/run.zig

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const Client = @import("../async/Client.zig");
3636
// runWPT parses the given HTML file, starts a js env and run the first script
3737
// tags containing javascript sources.
3838
// It loads first the js libs files.
39-
pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const u8, loader: *FileLoader) !jsruntime.JSResult {
39+
pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const u8, loader: *FileLoader) !Res {
4040
const alloc = arena.allocator();
4141
try parser.init();
4242
defer parser.deinit();
@@ -70,15 +70,16 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
7070
try js_env.load(&js_types);
7171

7272
// start JS env
73-
try js_env.start(alloc);
73+
try js_env.start();
7474
defer js_env.stop();
7575

7676
// display console logs
7777
defer {
78-
var res = evalJS(js_env, alloc, "console.join('\\n');", "console") catch unreachable;
78+
const res = evalJS(js_env, alloc, "console.join('\\n');", "console") catch unreachable;
7979
defer res.deinit(alloc);
80-
if (res.result.len > 0) {
81-
std.debug.print("-- CONSOLE LOG\n{s}\n--\n", .{res.result});
80+
81+
if (res.msg != null and res.msg.?.len > 0) {
82+
std.debug.print("-- CONSOLE LOG\n{s}\n--\n", .{res.msg.?});
8283
}
8384
}
8485

@@ -88,9 +89,6 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
8889
window.setStorageShelf(&storageShelf);
8990
try js_env.bindGlobal(&window);
9091

91-
// thanks to the arena, we don't need to deinit res.
92-
var res: jsruntime.JSResult = undefined;
93-
9492
const init =
9593
\\console = [];
9694
\\console.log = function () {
@@ -100,10 +98,8 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
10098
\\ console.push("debug", ...arguments);
10199
\\};
102100
;
103-
res = try evalJS(js_env, alloc, init, "init");
104-
if (!res.success) {
105-
return res;
106-
}
101+
var res = try evalJS(js_env, alloc, init, "init");
102+
if (!res.ok) return res;
107103
res.deinit(alloc);
108104

109105
// loop hover the scripts.
@@ -122,20 +118,14 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
122118
}
123119

124120
res = try evalJS(js_env, alloc, try loader.get(path), src);
125-
if (!res.success) {
126-
return res;
127-
}
121+
if (!res.ok) return res;
128122
res.deinit(alloc);
129123
}
130124

131125
// If the script as a source text, execute it.
132126
const src = try parser.nodeTextContent(s) orelse continue;
133127
res = try evalJS(js_env, alloc, src, "");
134-
135-
// return the first failure.
136-
if (!res.success) {
137-
return res;
138-
}
128+
if (!res.ok) return res;
139129
res.deinit(alloc);
140130
}
141131

@@ -150,25 +140,52 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
150140
);
151141

152142
// wait for all async executions
153-
res = try js_env.waitTryCatch(alloc);
154-
if (!res.success) {
155-
return res;
156-
}
157-
res.deinit(alloc);
143+
var try_catch: jsruntime.TryCatch = undefined;
144+
try_catch.init(js_env);
145+
defer try_catch.deinit();
146+
js_env.wait() catch {
147+
return .{
148+
.ok = false,
149+
.msg = try try_catch.err(alloc, js_env),
150+
};
151+
};
158152

159153
// Check the final test status.
160154
res = try evalJS(js_env, alloc, "report.status;", "teststatus");
161-
if (!res.success) {
162-
return res;
163-
}
155+
if (!res.ok) return res;
164156
res.deinit(alloc);
165157

166158
// return the detailed result.
167159
return try evalJS(js_env, alloc, "report.log", "teststatus");
168160
}
169161

170-
fn evalJS(env: jsruntime.Env, alloc: std.mem.Allocator, script: []const u8, name: ?[]const u8) !jsruntime.JSResult {
171-
return try env.execTryCatch(alloc, script, name);
162+
pub const Res = struct {
163+
ok: bool,
164+
msg: ?[]const u8,
165+
166+
pub fn deinit(res: Res, alloc: std.mem.Allocator) void {
167+
if (res.msg) |msg| {
168+
alloc.free(msg);
169+
}
170+
}
171+
};
172+
173+
fn evalJS(env: jsruntime.Env, alloc: std.mem.Allocator, script: []const u8, name: ?[]const u8) !Res {
174+
var try_catch: jsruntime.TryCatch = undefined;
175+
try_catch.init(env);
176+
defer try_catch.deinit();
177+
178+
const v = env.exec(script, name) catch {
179+
return .{
180+
.ok = false,
181+
.msg = try try_catch.err(alloc, env),
182+
};
183+
};
184+
185+
return .{
186+
.ok = true,
187+
.msg = try v.toString(alloc, env),
188+
};
172189
}
173190

174191
// browse the path to find the tests list.

0 commit comments

Comments
 (0)