1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! scriptit is a simple way to run JavaScript code in Rust
//!
//! scriptit will run your JS differently depending on your platform:
//!
//! - Run in a V8 interpreter for "native" targets
//! - Run in the WASM host interpreter for "wasm32" targets
//!
//! ## Example
//!
//! ```
//! use scriptit::{
//! core::{ value::ScriptValue, ScriptingEnvironment },
//! platform::PlatformScriptingEnvironment,
//! };
//!
//! let mut s_env = PlatformScriptingEnvironment::new();
//!
//! s_env.register_func("greet", Box::new(|args| {
//! let name = args.get(0).unwrap().as_str().unwrap();
//! return ScriptValue::String(format!("Hello {}!", name));
//! }));
//!
//! let src = "(function() {
//! const greeter = 'JS';
//! const greeted = 'Rust';
//! return `${ScriptIt.funcs.greet(greeted)} (from ${greeter}...)`;
//! })()";
//! let res = s_env.eval_expression(src).unwrap();
//!
//! assert_eq!(res, ScriptValue::String("Hello Rust! (from JS...)".to_string()));
//! ```