js_sandbox_ios/
util.rs

1// Copyright (c) 2020-2023 js-sandbox contributors. Zlib license.
2
3use crate::{JsError, JsValue, Script};
4
5/// Evaluates a standalone Javascript expression, and returns the result as a JSON value.
6///
7/// If there is an error, Err will be returned.
8/// This function is primarily useful for small standalone experiments. Usually, you would want to use the [`Script`](struct.Script.html) struct
9/// for more sophisticated Rust->JS interaction.
10pub fn eval_json(js_expr: &str) -> Result<JsValue, JsError> {
11	let code = format!(
12		"
13		function __rust_expr() {{
14			return ({js_expr});
15		}}
16	"
17	);
18
19	let mut script = Script::from_string(&code)?;
20	script.call_json("__rust_expr", &JsValue::Null)
21}