Well, it compiles...

This commit is contained in:
Brooks J Rady 2020-11-25 19:39:45 +00:00
parent 489ade6eb8
commit 06b5ef8c26
3 changed files with 1062 additions and 12 deletions

973
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -19,6 +19,9 @@ serde = { version = "1.0", features = ["derive"] }
bincode = "1.3.1" bincode = "1.3.1"
structopt = "0.3" structopt = "0.3"
serde_yaml = "0.8" serde_yaml = "0.8"
serde_json = "1.0"
wasmer = { git = "https://github.com/wasmerio/wasmer.git" }
wasmer-wasi = { git = "https://github.com/wasmerio/wasmer.git" }
[dependencies.async-std] [dependencies.async-std]
version = "1.3.0" version = "1.3.0"

View file

@ -11,15 +11,19 @@ mod screen;
mod terminal_pane; mod terminal_pane;
mod utils; mod utils;
use std::io::{Read, Write}; use std::io::{self, Read, Write};
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::mpsc::{channel, Receiver, Sender}; use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::thread; use std::thread;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use structopt::StructOpt; use structopt::StructOpt;
use wasmer::{Exports, Function, Instance, Module, Store, Value};
use wasmer_wasi::WasiState;
use crate::command_is_executing::CommandIsExecuting; use crate::command_is_executing::CommandIsExecuting;
use crate::input::input_loop; use crate::input::input_loop;
use crate::layout::Layout; use crate::layout::Layout;
@ -253,6 +257,96 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
.unwrap(), .unwrap(),
); );
active_threads.push(
thread::Builder::new()
.name("wasm".to_string())
.spawn(move || {
// TODO: Clone shared state here
move || -> Result<(), Box<dyn std::error::Error>> {
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
let module = Module::from_file(&store, "target/wasm32-wasi/debug/module.wasm")?;
// FIXME: Upstream the `Pipe` struct
//let output = fluff::Pipe::new();
//let input = fluff::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
.alias(".")
.read(true)
.write(true)
.create(true)
})?
//.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_open_file",
Function::new_native_with_env(&store, Arc::clone(&wasi_env.state), 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")?;
// This eventually calls the `.init()` method
start.call(&[])?;
loop {
break;
//let (cols, rows) = terminal::size()?;
//draw.call(&[Value::I32(rows as i32), Value::I32(cols as i32)])?;
// 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")
)?;
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()? {
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!
handle_key.call(&[])?;
}
_ => (),
} */
}
Ok(())
}().unwrap()
})
.unwrap(),
);
// TODO: currently we don't push this into active_threads // TODO: currently we don't push this into active_threads
// because otherwise the app will hang. Need to fix this so it both // because otherwise the app will hang. Need to fix this so it both
// listens to the ipc-bus and is able to quit cleanly // listens to the ipc-bus and is able to quit cleanly