Browse Source

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>
pull/235/head
Nathaniel 4 years ago committed by GitHub
parent
commit
6947122815
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 46
      src/lib/lib.rs
  2. 47
      src/lib/wasm.rs

46
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()),
}
}

47
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()),
}
}
Loading…
Cancel
Save