@@ -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 ) ! [] const u8 {
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 ();
@@ -75,10 +75,11 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
7575
7676 // display console logs
7777 defer {
78- const res = evalJS (js_env , "console.join('\\ n');" , "console" ) catch unreachable ;
79- const res_str = res .toString (alloc , js_env ) catch unreachable ;
80- if (res_str .len > 0 ) {
81- std .debug .print ("-- CONSOLE LOG\n {s}\n --\n " , .{res_str });
78+ const res = evalJS (js_env , alloc , "console.join('\\ n');" , "console" ) catch unreachable ;
79+ defer res .deinit (alloc );
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,8 +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-
9392 const init =
9493 \\console = [];
9594 \\console.log = function () {
@@ -99,7 +98,9 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
9998 \\ console.push("debug", ...arguments);
10099 \\};
101100 ;
102- _ = try evalJS (js_env , init , "init" );
101+ var res = try evalJS (js_env , alloc , init , "init" );
102+ if (! res .ok ) return res ;
103+ res .deinit (alloc );
103104
104105 // loop hover the scripts.
105106 const doc = parser .documentHTMLToDocument (html_doc );
@@ -116,12 +117,16 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
116117 path = try fspath .join (alloc , &.{ "/" , dirname , path });
117118 }
118119
119- _ = try evalJS (js_env , try loader .get (path ), src );
120+ res = try evalJS (js_env , alloc , try loader .get (path ), src );
121+ if (! res .ok ) return res ;
122+ res .deinit (alloc );
120123 }
121124
122125 // If the script as a source text, execute it.
123126 const src = try parser .nodeTextContent (s ) orelse continue ;
124- _ = try evalJS (js_env , src , "" );
127+ res = try evalJS (js_env , alloc , src , "" );
128+ if (! res .ok ) return res ;
129+ res .deinit (alloc );
125130 }
126131
127132 // Mark tests as ready to run.
@@ -135,18 +140,52 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
135140 );
136141
137142 // wait for all async executions
138- _ = try js_env .wait ();
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+ };
139152
140153 // Check the final test status.
141- _ = try evalJS (js_env , "report.status;" , "teststatus" );
154+ res = try evalJS (js_env , alloc , "report.status;" , "teststatus" );
155+ if (! res .ok ) return res ;
156+ res .deinit (alloc );
142157
143158 // return the detailed result.
144- const res = try evalJS (js_env , "report.log" , "teststatus" );
145- return try res .toString (alloc , js_env );
159+ return try evalJS (js_env , alloc , "report.log" , "teststatus" );
146160}
147161
148- fn evalJS (env : jsruntime.Env , script : []const u8 , name : ? []const u8 ) ! jsruntime.JSValue {
149- return try env .exec (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+ };
150189}
151190
152191// browse the path to find the tests list.
0 commit comments