From e9dba54750469c7aba836d081d2284d1595aa4c7 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Fri, 6 Jan 2023 16:16:04 +0100 Subject: [PATCH] Develop wasm support further. --- hel/src/repl.rs | 25 ++++++++++++++----------- hel/src/structs.rs | 13 +++++++++++++ helwasm/Cargo.toml | 1 + helwasm/index.html | 5 +++-- helwasm/src/hel_state.rs | 21 ++++++++++++++++++--- 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/hel/src/repl.rs b/hel/src/repl.rs index d7140a2..d5c8b1f 100644 --- a/hel/src/repl.rs +++ b/hel/src/repl.rs @@ -47,18 +47,21 @@ impl LKRead { () } } - self.cmd = match self.rl.readline(&*self.prompt) { - Ok(str) => str, - Err(LKErr::EOF) => "quit".to_string(), - Err(err) => { - return LKEval::new( - Command::Error(LKErr::ReadError(err.to_string())), - self.state.clone(), - self.read_password, - ) - } - }; + if self.cmd != "" { + self.cmd = match self.rl.readline(&*self.prompt) { + Ok(str) => str, + Err(LKErr::EOF) => "quit".to_string(), + Err(err) => { + return LKEval::new( + Command::Error(LKErr::ReadError(err.to_string())), + self.state.clone(), + self.read_password, + ) + } + }; + } self.rl.add_history_entry(self.cmd.as_str()); + self.cmd = "".to_string(); match self.rl.save_history(&history_file) { Ok(_) => (), Err(_) => (), diff --git a/hel/src/structs.rs b/hel/src/structs.rs index 7ba4cbb..8e46352 100644 --- a/hel/src/structs.rs +++ b/hel/src/structs.rs @@ -228,6 +228,19 @@ impl LKOut { } } + pub fn output(&self) -> Vec { + let mut out: Vec = vec![]; + match &self.err { + Some(o) => for l in &*o.lock() { out.push(l.to_string()); }, + _ => (), + } + match &self.out { + Some(o) => for l in &*o.lock() { out.push(l.to_string()); }, + _ => (), + } + out + } + pub fn active(&self) -> bool { self.out.is_some() } diff --git a/helwasm/Cargo.toml b/helwasm/Cargo.toml index 50dbe43..e9ef650 100644 --- a/helwasm/Cargo.toml +++ b/helwasm/Cargo.toml @@ -15,6 +15,7 @@ crate-type = ["cdylib"] hel = { version = "0.1.0", path = "../hel" } lazy_static = "1.4.0" wasm-bindgen = "0.2.83" +parking_lot = "0.12.1" [dependencies.web-sys] version = "0.3.4" diff --git a/helwasm/index.html b/helwasm/index.html index 5073fde..0b90faa 100644 --- a/helwasm/index.html +++ b/helwasm/index.html @@ -76,8 +76,6 @@
diff --git a/helwasm/src/hel_state.rs b/helwasm/src/hel_state.rs index 7c38924..e9e6687 100644 --- a/helwasm/src/hel_state.rs +++ b/helwasm/src/hel_state.rs @@ -1,6 +1,21 @@ -use hel::lk::LK; -use std::sync::{Arc, Mutex}; +use hel::lk::{LK, LKRef}; +use hel::repl::LKRead; +use hel::utils::editor::Editor; +use std::sync::Arc; +use parking_lot::ReentrantMutex; +use std::cell::RefCell; +use wasm_bindgen::prelude::*; lazy_static! { - static ref STATE: Arc> = Arc::new(Mutex::new(LK::new())); + static ref STATE: LKRef = Arc::new(ReentrantMutex::new(RefCell::new(LK::new()))); +} + +#[wasm_bindgen] +pub fn hel_command(cmd: String) -> String { + let editor = Editor::new(); + let mut lkread = LKRead::new(editor, "> ".to_string(), STATE.clone()); + lkread.cmd = cmd.to_string(); + let lkeval = lkread.read(); + let lkprint = lkeval.eval(); + lkprint.out.output().join("\n") }