mirror of https://github.com/boa-dev/boa.git
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.
48 lines
1.1 KiB
48 lines
1.1 KiB
4 years ago
|
use boa::{
|
||
4 years ago
|
exec::{Executor, Interpreter},
|
||
|
realm::Realm,
|
||
4 years ago
|
syntax::{ast::node::Node, lexer::Lexer, parser::Parser},
|
||
4 years ago
|
};
|
||
|
use wasm_bindgen::prelude::*;
|
||
|
|
||
|
// WASM
|
||
|
#[wasm_bindgen]
|
||
|
extern "C" {
|
||
|
// Use `js_namespace` here to bind `console.log(..)` instead of just
|
||
|
// `log(..)`
|
||
|
#[wasm_bindgen(js_namespace = console)]
|
||
|
fn log(s: &str);
|
||
|
}
|
||
|
|
||
|
#[wasm_bindgen]
|
||
|
pub fn evaluate(src: &str) -> String {
|
||
|
let mut lexer = Lexer::new(&src);
|
||
|
match lexer.lex() {
|
||
|
Ok(_v) => (),
|
||
|
Err(v) => log(&v.to_string()),
|
||
|
}
|
||
|
|
||
|
let tokens = lexer.tokens;
|
||
|
|
||
|
// Setup executor
|
||
4 years ago
|
let node: Node;
|
||
4 years ago
|
|
||
4 years ago
|
match Parser::new(&tokens).parse_all() {
|
||
4 years ago
|
Ok(v) => {
|
||
4 years ago
|
node = v;
|
||
4 years ago
|
}
|
||
|
Err(_v) => {
|
||
|
log("parsing fail");
|
||
|
return String::from("parsing failed");
|
||
|
}
|
||
|
}
|
||
|
// Create new Realm
|
||
|
let realm = Realm::create();
|
||
|
let mut engine: Interpreter = Executor::new(realm);
|
||
4 years ago
|
let result = engine.run(&node);
|
||
4 years ago
|
match result {
|
||
|
Ok(v) => v.to_string(),
|
||
|
Err(v) => format!("{}: {}", "error", v.to_string()),
|
||
|
}
|
||
|
}
|