From 69f65a5be08b19aee6a6dd75073bca3fd1b09a86 Mon Sep 17 00:00:00 2001 From: Oleksandr Kozachuk Date: Sun, 1 Jan 2023 19:46:14 +0100 Subject: [PATCH] Make helwasm compilable for wasm32-unknown-unknown target. --- hel/Cargo.toml | 6 +++- hel/src/utils.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++-- helwasm/Cargo.toml | 2 +- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/hel/Cargo.toml b/hel/Cargo.toml index b6f2f7a..177232f 100644 --- a/hel/Cargo.toml +++ b/hel/Cargo.toml @@ -23,9 +23,13 @@ shlex = "1.1.0" shellexpand = "3.0.0" scopeguard = "1.1.0" -[target.'cfg(not(wasm))'.dependencies] +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] chrono = "0.4.23" rand = "0.8.5" home = "0.5.4" rustyline = "10.0.0" rpassword = "7.2.0" + +[target.'cfg(target_arch = "wasm32")'.dependencies] +wasm-bindgen = "0.2.83" +chrono = { version = "0.4.23", features = ["wasmbind"] } diff --git a/hel/src/utils.rs b/hel/src/utils.rs index eb2d1a8..144382c 100644 --- a/hel/src/utils.rs +++ b/hel/src/utils.rs @@ -5,7 +5,6 @@ use std::io; use std::io::{Read, Write}; use std::process::{Command, Stdio}; -#[cfg(not(wasm))] pub mod date { use chrono::naive::NaiveDate; use chrono::Local; @@ -43,7 +42,22 @@ pub mod date { } } -#[cfg(not(wasm))] +#[cfg(target_arch = "wasm32")] +pub mod rnd { + use wasm_bindgen::prelude::*; + + #[wasm_bindgen] + extern "C" { + #[wasm_bindgen(js_name = rnd_range)] + fn extern_rnd_range(start: u32, end: u32) -> u32; + } + + pub fn range(start: u32, end: u32) -> u32 { + extern_rnd_range(start, end) + } +} + +#[cfg(not(target_arch = "wasm32"))] pub mod rnd { use rand::{thread_rng, Rng}; @@ -52,6 +66,13 @@ pub mod rnd { } } +#[cfg(target_arch = "wasm32")] +pub mod home { + pub fn dir() -> std::path::PathBuf { + std::path::PathBuf::new() + } +} + #[cfg(unix)] pub mod home { use home::home_dir; @@ -113,6 +134,56 @@ pub mod editor { } } +#[cfg(target_arch = "wasm32")] +pub mod editor { + use crate::structs::LKErr; + use wasm_bindgen::prelude::*; + + #[wasm_bindgen] + extern "C" { + #[wasm_bindgen(js_name = read_line)] + fn extern_readline(prompt: &str) -> String; + + #[wasm_bindgen(js_name = read_password)] + fn extern_password(prompt: &str) -> String; + } + + #[derive(Debug)] + pub struct Editor { + history: Vec, + } + + impl Editor { + pub fn new() -> Self { + Self { history: vec![] } + } + + pub fn clear_history(&mut self) { + self.history.clear(); + } + + pub fn add_history_entry(&mut self, entry: &str) { + self.history.push(entry.to_string()); + } + + pub fn load_history<'a>(&mut self, _fname: &str) -> Result<(), LKErr<'a>> { + Ok(()) + } + + pub fn save_history<'a>(&mut self, _fname: &str) -> Result<(), LKErr<'a>> { + Ok(()) + } + + pub fn readline<'a>(&mut self, prompt: &str) -> Result> { + Ok(extern_readline(&prompt)) + } + } + + pub fn password(prompt: String) -> std::io::Result { + Ok(extern_password(&prompt)) + } +} + pub fn call_cmd_with_input(cmd: &str, args: &Vec, input: &str) -> io::Result { let mut cmd = Command::new(cmd).args(args).stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?; let mut stdin = cmd.stdin.take().unwrap(); diff --git a/helwasm/Cargo.toml b/helwasm/Cargo.toml index ef00f95..ac69c18 100644 --- a/helwasm/Cargo.toml +++ b/helwasm/Cargo.toml @@ -4,10 +4,10 @@ version = "0.1.0" authors = ["ok2"] edition = "2021" -[target.'cfg(wasm32)'] [lib] name = "helwasm" path = "src/lib.rs" +crate-type = ["cdylib"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html