use deno_core::v8;
use deno_core::JsRuntime;
use deno_core::RuntimeOptions;
fn main() {
let mut runtime = JsRuntime::new(RuntimeOptions::default());
let code = "let a = 1+4; a*2";
let output: serde_json::Value =
eval(&mut runtime, code).expect("Eval failed");
println!("Output: {output:?}");
let expected_output = serde_json::json!(10);
assert_eq!(expected_output, output);
}
fn eval(
context: &mut JsRuntime,
code: &'static str,
) -> Result<serde_json::Value, String> {
let res = context.execute_script_static("<anon>", code);
match res {
Ok(global) => {
let scope = &mut context.handle_scope();
let local = v8::Local::new(scope, global);
let deserialized_value =
serde_v8::from_v8::<serde_json::Value>(scope, local);
match deserialized_value {
Ok(value) => Ok(value),
Err(err) => Err(format!("Cannot deserialize value: {err:?}")),
}
}
Err(err) => Err(format!("Evaling error: {err:?}")),
}
}