Make helwasm compilable for wasm32-unknown-unknown target.

This commit is contained in:
Oleksandr Kozachuk
2023-01-01 19:46:14 +01:00
parent 7563645518
commit 69f65a5be0
3 changed files with 79 additions and 4 deletions
+73 -2
View File
@@ -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<String>,
}
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<String, LKErr<'a>> {
Ok(extern_readline(&prompt))
}
}
pub fn password(prompt: String) -> std::io::Result<String> {
Ok(extern_password(&prompt))
}
}
pub fn call_cmd_with_input(cmd: &str, args: &Vec<String>, input: &str) -> io::Result<String> {
let mut cmd = Command::new(cmd).args(args).stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?;
let mut stdin = cmd.stdin.take().unwrap();