diff options
| author | Brooks J Rady <b.j.rady@gmail.com> | 2020-12-29 10:24:22 +0000 |
|---|---|---|
| committer | Brooks J Rady <b.j.rady@gmail.com> | 2020-12-29 10:24:22 +0000 |
| commit | db4d6b6eb58ae3e509695b596bdbee2bb2ba3de4 (patch) | |
| tree | f2821319a68a463d790fec2a5f171b7be941c560 /src | |
| parent | Update GOVERNANCE.md (diff) | |
| download | sl0ppij-db4d6b6eb58ae3e509695b596bdbee2bb2ba3de4.tar.gz sl0ppij-db4d6b6eb58ae3e509695b596bdbee2bb2ba3de4.tar.xz sl0ppij-db4d6b6eb58ae3e509695b596bdbee2bb2ba3de4.zip | |
Fully Working WebAssembly Loading!
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 87 |
1 files changed, 51 insertions, 36 deletions
diff --git a/src/main.rs b/src/main.rs index 82ce1a75..787a670a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -387,52 +387,48 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) { .spawn(move || { // TODO: Clone shared state here move || -> Result<(), Box<dyn std::error::Error>> { - use std::io; - use std::sync::{Arc, Mutex}; + use std::{ + io, + process::{Command, Stdio}, + }; use wasmer::{Exports, Function, Instance, Module, Store, Value}; - use wasmer_wasi::WasiState; + use wasmer_wasi::{Pipe, WasiEnv, WasiState}; + let store = Store::default(); println!("Compiling module..."); - // FIXME: Switch to a higher performance compiler (`Store::default()`) and cache this on disk - // I could use `(de)serialize_to_file()` for that + // FIXME: Cache this compiled module on disk. I could use `(de)serialize_to_file()` for that let module = if let Ok(m) = Module::from_file(&store, "strider.wasm") { m } else { return Ok(()); // Just abort this thread quietly if the WASM isn't found }; - // FIXME: Upstream the `Pipe` struct - //let output = fluff::Pipe::new(); - //let input = fluff::Pipe::new(); + let output = Pipe::new(); + let input = Pipe::new(); let mut wasi_env = WasiState::new("mosaic") .env("CLICOLOR_FORCE", "1") .preopen(|p| { - p.directory(".") // TODO: Change this to a more meaningful dir + p.directory(".") // FIXME: Change this to a more meaningful dir .alias(".") .read(true) .write(true) .create(true) })? - //.stdin(Box::new(input)) - //.stdout(Box::new(output)) + .stdin(Box::new(input)) + .stdout(Box::new(output)) .finalize()?; let mut import_object = wasi_env.import_object(&module)?; // FIXME: Upstream an `ImportObject` merge method let mut host_exports = Exports::new(); - /* host_exports.insert( + host_exports.insert( "host_open_file", - Function::new_native_with_env(&store, Arc::clone(&wasi_env.state), host_open_file), - ); */ - fn noop() {} - host_exports.insert("host_open_file", Function::new_native(&store, noop)); + Function::new_native_with_env(&store, wasi_env.clone(), host_open_file), + ); import_object.register("mosaic", host_exports); let instance = Instance::new(&module, &import_object)?; - // WASI requires to explicitly set the memory for the `WasiEnv` - wasi_env.set_memory(instance.exports.get_memory("memory")?.clone()); - let start = instance.exports.get_function("_start")?; let handle_key = instance.exports.get_function("handle_key")?; let draw = instance.exports.get_function("draw")?; @@ -442,40 +438,59 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) { #[warn(clippy::never_loop)] loop { - break; - //let (cols, rows) = terminal::size()?; - //draw.call(&[Value::I32(rows as i32), Value::I32(cols as i32)])?; + let (cols, rows) = (80, 24); //terminal::size()?; + draw.call(&[Value::I32(rows), Value::I32(cols)])?; - // FIXME: This downcasting mess needs to be abstracted away - /* let mut state = wasi_env.state(); - let wasi_file = state.fs.stdout_mut()?.as_mut().unwrap(); - let output: &mut fluff::Pipe = wasi_file.downcast_mut().unwrap(); // Needed because raw mode doesn't implicitly return to the start of the line write!( io::stdout(), "{}\n\r", - output.to_string().lines().collect::<Vec<_>>().join("\n\r") + wasi_stdout(&wasi_env) + .lines() + .collect::<Vec<_>>() + .join("\n\r") )?; - output.clear(); - let wasi_file = state.fs.stdin_mut()?.as_mut().unwrap(); - let input: &mut fluff::Pipe = wasi_file.downcast_mut().unwrap(); - input.clear(); */ - - /* match event::read()? { + /* match event::read().unwrap() { Event::Key(KeyEvent { code: KeyCode::Char('q'), .. }) => break, Event::Key(e) => { - writeln!(input, "{}\r", serde_json::to_string(&e)?)?; - drop(state); - // Need to release the implicit `state` mutex or I deadlock! + wasi_write_string(&wasi_env, serde_json::to_string(&e).unwrap()); handle_key.call(&[])?; } _ => (), } */ + break; + } + + fn host_open_file(wasi_env: &WasiEnv) { + Command::new("xdg-open") + .arg(format!( + "./{}", + wasi_stdout(wasi_env).lines().next().unwrap() + )) + .stderr(Stdio::null()) + .spawn() + .unwrap(); + } + + // FIXME: Unwrap city + fn wasi_stdout(wasi_env: &WasiEnv) -> String { + let mut state = wasi_env.state(); + let wasi_file = state.fs.stdout_mut().unwrap().as_mut().unwrap(); + let mut buf = String::new(); + wasi_file.read_to_string(&mut buf).unwrap(); + buf } + + fn _wasi_write_string(wasi_env: &WasiEnv, buf: &str) { + let mut state = wasi_env.state(); + let wasi_file = state.fs.stdin_mut().unwrap().as_mut().unwrap(); + writeln!(wasi_file, "{}\r", buf).unwrap(); + } + debug_log_to_file("WASM module loaded and exited cleanly :)".to_string())?; Ok(()) }() |
