Skip to content

Commit

Permalink
Fixed compilation without "wasm-bindgen" feature (#236)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
adumbidiot and jasonwilliams authored Feb 2, 2020
1 parent 33490e1 commit 6947122
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 42 deletions.
46 changes: 4 additions & 42 deletions src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ pub mod environment;
pub mod exec;
pub mod realm;
pub mod syntax;
#[cfg(feature = "wasm-bindgen")]
mod wasm;

use crate::{
builtins::value::ResultValue,
exec::{Executor, Interpreter},
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);
Expand Down Expand Up @@ -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()),
}
}
47 changes: 47 additions & 0 deletions src/lib/wasm.rs
Original file line number Diff line number Diff line change
@@ -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()),
}
}

0 comments on commit 6947122

Please sign in to comment.