mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
Adapt to refacto in js_exec from zig-js-runtime
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const builtin = @import("builtin");
|
||||||
|
|
||||||
const jsruntime = @import("jsruntime");
|
const jsruntime = @import("jsruntime");
|
||||||
|
|
||||||
@@ -143,22 +144,11 @@ fn evaluate(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// evaluate the script in the context of the current page
|
// evaluate the script in the context of the current page
|
||||||
|
const session = ctx.browser.currentSession();
|
||||||
// TODO: should we use instead the allocator of the page?
|
// TODO: should we use instead the allocator of the page?
|
||||||
// the following code does not work
|
// the following code does not work with session.page.?.arena.allocator() as alloc
|
||||||
// const page_alloc = ctx.browser.currentSession().page.?.arena.allocator();
|
|
||||||
const session_alloc = ctx.browser.currentSession().alloc;
|
|
||||||
var res = jsruntime.JSResult{};
|
|
||||||
try ctx.browser.currentSession().env.run(session_alloc, params.expression, "cdp", &res, null);
|
|
||||||
defer res.deinit(session_alloc);
|
|
||||||
|
|
||||||
if (!res.success) {
|
_ = try runtimeEvaluate(session.alloc, id, session.env, params.expression, "cdp");
|
||||||
std.log.err("script {d} result: {s}", .{ id, res.result });
|
|
||||||
if (res.stack) |stack| {
|
|
||||||
std.log.err("script {d} stack: {s}", .{ id, stack });
|
|
||||||
}
|
|
||||||
return error.CDPRuntimeEvaluate;
|
|
||||||
}
|
|
||||||
std.log.debug("script {d} result: {s}", .{ id, res.result });
|
|
||||||
|
|
||||||
// TODO: Resp should depends on JS result returned by the JS engine
|
// TODO: Resp should depends on JS result returned by the JS engine
|
||||||
const Resp = struct {
|
const Resp = struct {
|
||||||
@@ -193,8 +183,7 @@ fn addBinding(
|
|||||||
defer alloc.free(script);
|
defer alloc.free(script);
|
||||||
|
|
||||||
const session = ctx.browser.currentSession();
|
const session = ctx.browser.currentSession();
|
||||||
const res = try runtimeEvaluate(session.alloc, id, session.env, script, "addBinding");
|
_ = try runtimeEvaluate(session.alloc, id, session.env, script, "addBinding");
|
||||||
defer res.deinit(session.alloc);
|
|
||||||
|
|
||||||
return result(alloc, id, null, null, msg.sessionID);
|
return result(alloc, id, null, null, msg.sessionID);
|
||||||
}
|
}
|
||||||
@@ -259,16 +248,13 @@ fn callFunctionOn(
|
|||||||
|
|
||||||
const session = ctx.browser.currentSession();
|
const session = ctx.browser.currentSession();
|
||||||
// TODO: should we use the page's allocator instead of the session's allocator?
|
// TODO: should we use the page's allocator instead of the session's allocator?
|
||||||
// the following code does not work:
|
// the following code does not work with session.page.?.arena.allocator() as alloc
|
||||||
// const page_alloc = ctx.browser.currentSession().page.?.arena.allocator();
|
|
||||||
|
|
||||||
// first evaluate the function declaration
|
// first evaluate the function declaration
|
||||||
const decl = try runtimeEvaluate(session.alloc, id, session.env, params.functionDeclaration, name);
|
_ = try runtimeEvaluate(session.alloc, id, session.env, params.functionDeclaration, name);
|
||||||
defer decl.deinit(session.alloc);
|
|
||||||
|
|
||||||
// then call the function on the arguments
|
// then call the function on the arguments
|
||||||
const res = try runtimeEvaluate(session.alloc, id, session.env, function, name);
|
_ = try runtimeEvaluate(session.alloc, id, session.env, function, name);
|
||||||
defer res.deinit(session.alloc);
|
|
||||||
|
|
||||||
return result(alloc, id, null, "{\"type\":\"undefined\"}", msg.sessionID);
|
return result(alloc, id, null, "{\"type\":\"undefined\"}", msg.sessionID);
|
||||||
}
|
}
|
||||||
@@ -280,17 +266,26 @@ fn runtimeEvaluate(
|
|||||||
env: jsruntime.Env,
|
env: jsruntime.Env,
|
||||||
script: []const u8,
|
script: []const u8,
|
||||||
comptime name: []const u8,
|
comptime name: []const u8,
|
||||||
) !jsruntime.JSResult {
|
) !jsruntime.JSValue {
|
||||||
var res = jsruntime.JSResult{};
|
|
||||||
try env.run(alloc, script, "cdp.Runtime." ++ name, &res, null);
|
|
||||||
|
|
||||||
if (!res.success) {
|
// try catch
|
||||||
std.log.err("'{s}' id {d}, result: {s}", .{ name, id, res.result });
|
var try_catch: jsruntime.TryCatch = undefined;
|
||||||
if (res.stack) |stack| {
|
try_catch.init(env);
|
||||||
std.log.err("'{s}' id {d}, stack: {s}", .{ name, id, stack });
|
defer try_catch.deinit();
|
||||||
|
|
||||||
|
// script exec
|
||||||
|
const res = env.execWait(script, name) catch {
|
||||||
|
if (try try_catch.err(alloc, env)) |err_msg| {
|
||||||
|
defer alloc.free(err_msg);
|
||||||
|
std.log.err("'{s}' id {d}, result: {s}", .{ name, id, err_msg });
|
||||||
}
|
}
|
||||||
return error.CDPRuntimeEvaluate;
|
return error.CDPRuntimeEvaluate;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (builtin.mode == .Debug) {
|
||||||
|
const res_msg = try res.toString(alloc, env);
|
||||||
|
defer alloc.free(res_msg);
|
||||||
|
std.log.debug("'{s}' id {d}, result: {s}", .{ name, id, res_msg });
|
||||||
}
|
}
|
||||||
std.log.debug("'{s}' id {d}, result: {s}", .{ name, id, res.result });
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user