@@ -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