From 6947122815f33b57b51062720380ca9ae68b47ad Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sun, 2 Feb 2020 05:40:08 -0800 Subject: [PATCH] Fixed compilation without "wasm-bindgen" feature (#236) * 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> --- src/lib/lib.rs | 46 ++++------------------------------------------ src/lib/wasm.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 42 deletions(-) create mode 100644 src/lib/wasm.rs diff --git a/src/lib/lib.rs b/src/lib/lib.rs index b1575d828b..90472e142b 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -7,6 +7,8 @@ pub mod environment; pub mod exec; pub mod realm; pub mod syntax; +#[cfg(feature = "wasm-bindgen")] +mod wasm; use crate::{ builtins::value::ResultValue, @@ -14,7 +16,8 @@ use crate::{ realm::Realm, syntax::{ast::expr::Expr, lexer::Lexer, parser::Parser}, }; -use wasm_bindgen::prelude::*; +#[cfg(feature = "wasm-bindgen")] +pub use wasm::*; fn parser_expr(src: &str) -> Expr { let mut lexer = Lexer::new(src); @@ -52,44 +55,3 @@ pub fn exec(src: &str) -> String { let mut engine: Interpreter = Executor::new(realm); forward(&mut engine, src) } - -// 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()), - } -} diff --git a/src/lib/wasm.rs b/src/lib/wasm.rs new file mode 100644 index 0000000000..c062279cf4 --- /dev/null +++ b/src/lib/wasm.rs @@ -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()), + } +}