11const std = @import ("std" );
2+ const builtin = @import ("builtin" );
23
34const jsruntime = @import ("jsruntime" );
45
@@ -143,22 +144,11 @@ fn evaluate(
143144 }
144145
145146 // evaluate the script in the context of the current page
147+ const session = ctx .browser .currentSession ();
146148 // TODO: should we use instead the allocator of the page?
147- // the following code does not work
148- // const page_alloc = ctx.browser.currentSession().page.?.arena.allocator();
149- const session_alloc = ctx .browser .currentSession ().alloc ;
150- var res = jsruntime.JSResult {};
151- try ctx .browser .currentSession ().env .run (session_alloc , params .expression , "cdp" , & res , null );
152- defer res .deinit (session_alloc );
153-
154- if (! res .success ) {
155- std .log .err ("script {d} result: {s}" , .{ id , res .result });
156- if (res .stack ) | stack | {
157- std .log .err ("script {d} stack: {s}" , .{ id , stack });
158- }
159- return error .CDPRuntimeEvaluate ;
160- }
161- std .log .debug ("script {d} result: {s}" , .{ id , res .result });
149+ // the following code does not work with session.page.?.arena.allocator() as alloc
150+
151+ _ = try runtimeEvaluate (session .alloc , id , session .env , params .expression , "cdp" );
162152
163153 // TODO: Resp should depends on JS result returned by the JS engine
164154 const Resp = struct {
@@ -193,8 +183,7 @@ fn addBinding(
193183 defer alloc .free (script );
194184
195185 const session = ctx .browser .currentSession ();
196- const res = try runtimeEvaluate (session .alloc , id , session .env , script , "addBinding" );
197- defer res .deinit (session .alloc );
186+ _ = try runtimeEvaluate (session .alloc , id , session .env , script , "addBinding" );
198187
199188 return result (alloc , id , null , null , msg .sessionID );
200189}
@@ -259,16 +248,13 @@ fn callFunctionOn(
259248
260249 const session = ctx .browser .currentSession ();
261250 // TODO: should we use the page's allocator instead of the session's allocator?
262- // the following code does not work:
263- // const page_alloc = ctx.browser.currentSession().page.?.arena.allocator();
251+ // the following code does not work with session.page.?.arena.allocator() as alloc
264252
265253 // first evaluate the function declaration
266- const decl = try runtimeEvaluate (session .alloc , id , session .env , params .functionDeclaration , name );
267- defer decl .deinit (session .alloc );
254+ _ = try runtimeEvaluate (session .alloc , id , session .env , params .functionDeclaration , name );
268255
269256 // then call the function on the arguments
270- const res = try runtimeEvaluate (session .alloc , id , session .env , function , name );
271- defer res .deinit (session .alloc );
257+ _ = try runtimeEvaluate (session .alloc , id , session .env , function , name );
272258
273259 return result (alloc , id , null , "{\" type\" :\" undefined\" }" , msg .sessionID );
274260}
@@ -280,17 +266,26 @@ fn runtimeEvaluate(
280266 env : jsruntime.Env ,
281267 script : []const u8 ,
282268 comptime name : []const u8 ,
283- ) ! jsruntime.JSResult {
284- var res = jsruntime.JSResult {};
285- try env .run (alloc , script , "cdp.Runtime." ++ name , & res , null );
286-
287- if (! res .success ) {
288- std .log .err ("'{s}' id {d}, result: {s}" , .{ name , id , res .result });
289- if (res .stack ) | stack | {
290- std .log .err ("'{s}' id {d}, stack: {s}" , .{ name , id , stack });
269+ ) ! jsruntime.JSValue {
270+
271+ // try catch
272+ var try_catch : jsruntime.TryCatch = undefined ;
273+ try_catch .init (env );
274+ defer try_catch .deinit ();
275+
276+ // script exec
277+ const res = env .execWait (script , name ) catch {
278+ if (try try_catch .err (alloc , env )) | err_msg | {
279+ defer alloc .free (err_msg );
280+ std .log .err ("'{s}' id {d}, result: {s}" , .{ name , id , err_msg });
291281 }
292282 return error .CDPRuntimeEvaluate ;
283+ };
284+
285+ if (builtin .mode == .Debug ) {
286+ const res_msg = try res .toString (alloc , env );
287+ defer alloc .free (res_msg );
288+ std .log .debug ("'{s}' id {d}, result: {s}" , .{ name , id , res_msg });
293289 }
294- std .log .debug ("'{s}' id {d}, result: {s}" , .{ name , id , res .result });
295290 return res ;
296291}
0 commit comments