Rust编写的JavaScript引擎,该项目是一个试验性质的项目。
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
use boa::{parse, Executable, Interpreter, Realm};
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
pub fn evaluate(src: &str) -> Result<String, JsValue> {
|
|
|
|
// Setup executor
|
|
|
|
let realm = Realm::create();
|
|
|
|
let mut engine = Interpreter::new(realm);
|
|
|
|
|
|
|
|
let expr = match parse(src) {
|
|
|
|
Ok(res) => res,
|
|
|
|
Err(e) => {
|
|
|
|
return Err(format!(
|
|
|
|
"Uncaught {}",
|
|
|
|
engine
|
|
|
|
.throw_syntax_error(e.to_string())
|
|
|
|
.expect_err("interpreter.throw_syntax_error() did not return an error")
|
|
|
|
.display()
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
expr.run(&mut engine)
|
|
|
|
.map_err(|e| JsValue::from(format!("Uncaught {}", e.display())))
|
|
|
|
.map(|v| v.display().to_string())
|
|
|
|
}
|