Develop wasm support further.

This commit is contained in:
Oleksandr Kozachuk
2023-01-06 16:16:04 +01:00
parent 3416ea96ae
commit e9dba54750
5 changed files with 49 additions and 16 deletions
+14 -11
View File
@@ -47,18 +47,21 @@ impl LKRead {
() ()
} }
} }
self.cmd = match self.rl.readline(&*self.prompt) { if self.cmd != "" {
Ok(str) => str, self.cmd = match self.rl.readline(&*self.prompt) {
Err(LKErr::EOF) => "quit".to_string(), Ok(str) => str,
Err(err) => { Err(LKErr::EOF) => "quit".to_string(),
return LKEval::new( Err(err) => {
Command::Error(LKErr::ReadError(err.to_string())), return LKEval::new(
self.state.clone(), Command::Error(LKErr::ReadError(err.to_string())),
self.read_password, self.state.clone(),
) self.read_password,
} )
}; }
};
}
self.rl.add_history_entry(self.cmd.as_str()); self.rl.add_history_entry(self.cmd.as_str());
self.cmd = "".to_string();
match self.rl.save_history(&history_file) { match self.rl.save_history(&history_file) {
Ok(_) => (), Ok(_) => (),
Err(_) => (), Err(_) => (),
+13
View File
@@ -228,6 +228,19 @@ impl LKOut {
} }
} }
pub fn output(&self) -> Vec<String> {
let mut out: Vec<String> = 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 { pub fn active(&self) -> bool {
self.out.is_some() self.out.is_some()
} }
+1
View File
@@ -15,6 +15,7 @@ crate-type = ["cdylib"]
hel = { version = "0.1.0", path = "../hel" } hel = { version = "0.1.0", path = "../hel" }
lazy_static = "1.4.0" lazy_static = "1.4.0"
wasm-bindgen = "0.2.83" wasm-bindgen = "0.2.83"
parking_lot = "0.12.1"
[dependencies.web-sys] [dependencies.web-sys]
version = "0.3.4" version = "0.3.4"
+3 -2
View File
@@ -76,8 +76,6 @@
<body> <body>
<div id="root"></div> <div id="root"></div>
<script type="module"> <script type="module">
import init, { ok_add } from "./pkg/helwasm.js";
function read_line(prompt) { function read_line(prompt) {
console.log("called read_line: " + prompt); console.log("called read_line: " + prompt);
return "line"; return "line";
@@ -88,9 +86,12 @@
return "password"; return "password";
} }
import init, { ok_add, hel_init, hel_command } from "./pkg/helwasm.js";
init().then(() => { init().then(() => {
window.hel = { window.hel = {
ok_add: ok_add, ok_add: ok_add,
hel_init: hel_init,
hel_command: hel_command,
} }
}); });
</script> </script>
+18 -3
View File
@@ -1,6 +1,21 @@
use hel::lk::LK; use hel::lk::{LK, LKRef};
use std::sync::{Arc, Mutex}; 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! { lazy_static! {
static ref STATE: Arc<Mutex<LK>> = 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")
} }