mirror of https://github.com/boa-dev/boa.git
Browse Source
* Fixed compilation without "wasm-bindgen" feature * updating clippy rules on all files (#238) * Fixed compilation without "wasm-bindgen" feature Co-authored-by: Jason Williams <936006+jasonwilliams@users.noreply.github.com>pull/235/head
Nathaniel
4 years ago
committed by
GitHub
2 changed files with 51 additions and 42 deletions
@ -0,0 +1,47 @@ |
|||||||
|
use crate::{ |
||||||
|
exec::{Executor, Interpreter}, |
||||||
|
realm::Realm, |
||||||
|
syntax::{ast::expr::Expr, lexer::Lexer, parser::Parser}, |
||||||
|
}; |
||||||
|
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
|
||||||
|
let expr: Expr; |
||||||
|
|
||||||
|
match Parser::new(tokens).parse_all() { |
||||||
|
Ok(v) => { |
||||||
|
expr = v; |
||||||
|
} |
||||||
|
Err(_v) => { |
||||||
|
log("parsing fail"); |
||||||
|
return String::from("parsing failed"); |
||||||
|
} |
||||||
|
} |
||||||
|
// Create new Realm
|
||||||
|
let realm = Realm::create(); |
||||||
|
let mut engine: Interpreter = Executor::new(realm); |
||||||
|
let result = engine.run(&expr); |
||||||
|
match result { |
||||||
|
Ok(v) => v.to_string(), |
||||||
|
Err(v) => format!("{}: {}", "error", v.to_string()), |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue