diff --git a/Cargo.lock b/Cargo.lock index efd7253..f915c85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8fd72866655d1904d6b0997d0b07ba561047d070fbe29de039031c641b61217" +dependencies = [ + "const-random", +] + [[package]] name = "aho-corasick" version = "0.7.18" @@ -41,6 +50,15 @@ version = "1.0.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61604a8f862e1d5c3229fdd78f8b02c68dcf73a4c4b05fd636d12240aaa242c1" +[[package]] +name = "ariadne" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7080ae01b2f0c312065d4914cd0f0de045eb8832e9415b355106a6cff3073cb4" +dependencies = [ + "yansi", +] + [[package]] name = "atty" version = "0.2.14" @@ -162,6 +180,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chumsky" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2d3efff85e8572b1c3fa0127706af58c4fff8458f8d9436d54b1e97573c7a3f" +dependencies = [ + "ahash", +] + [[package]] name = "clang-sys" version = "1.2.2" @@ -197,6 +224,28 @@ dependencies = [ "cc", ] +[[package]] +name = "const-random" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f590d95d011aa80b063ffe3253422ed5aa462af4e9867d43ce8337562bac77c4" +dependencies = [ + "const-random-macro", + "proc-macro-hack", +] + +[[package]] +name = "const-random-macro" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "615f6e27d000a2bffbc7f2f6a8669179378fa27ee4d0a509e985dfc0a7defb40" +dependencies = [ + "getrandom", + "lazy_static", + "proc-macro-hack", + "tiny-keccak", +] + [[package]] name = "cpp_demangle" version = "0.3.3" @@ -301,7 +350,7 @@ dependencies = [ "itertools", "log", "smallvec", - "wasmparser", + "wasmparser 0.80.2", "wasmtime-types", ] @@ -358,6 +407,24 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + +[[package]] +name = "curlywas" +version = "0.1.0" +source = "git+https://github.com/exoticorn/curlywas.git?rev=d62bc8bf86cff65145d5aea045b81122bc15d709#d62bc8bf86cff65145d5aea045b81122bc15d709" +dependencies = [ + "anyhow", + "ariadne", + "chumsky", + "wasm-encoder", + "wasmparser 0.81.0", +] + [[package]] name = "digest" version = "0.9.0" @@ -977,6 +1044,12 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba" +[[package]] +name = "proc-macro-hack" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" + [[package]] name = "proc-macro2" version = "1.0.30" @@ -1350,6 +1423,15 @@ dependencies = [ "syn", ] +[[package]] +name = "tiny-keccak" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] + [[package]] name = "toml" version = "0.5.8" @@ -1393,6 +1475,7 @@ name = "uw8" version = "0.1.0" dependencies = [ "anyhow", + "curlywas", "minifb", "notify", "pico-args", @@ -1488,12 +1571,27 @@ version = "0.2.78" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0237232789cf037d5480773fe568aac745bfe2afbc11a863e97901780a6b47cc" +[[package]] +name = "wasm-encoder" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db0c351632e46cc06a58a696a6c11e4cf90cad4b9f8f07a0b59128d616c29bb0" +dependencies = [ + "leb128", +] + [[package]] name = "wasmparser" version = "0.80.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "449167e2832691a1bff24cde28d2804e90e09586a448c8e76984792c44334a6b" +[[package]] +name = "wasmparser" +version = "0.81.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98930446519f63d00a836efdc22f67766ceae8dbcc1571379f2bcabc6b2b9abc" + [[package]] name = "wasmtime" version = "0.30.0" @@ -1517,7 +1615,7 @@ dependencies = [ "rustc-demangle", "serde", "target-lexicon", - "wasmparser", + "wasmparser 0.80.2", "wasmtime-cache", "wasmtime-cranelift", "wasmtime-environ", @@ -1566,7 +1664,7 @@ dependencies = [ "object 0.26.2", "target-lexicon", "thiserror", - "wasmparser", + "wasmparser 0.80.2", "wasmtime-environ", ] @@ -1587,7 +1685,7 @@ dependencies = [ "serde", "target-lexicon", "thiserror", - "wasmparser", + "wasmparser 0.80.2", "wasmtime-types", ] @@ -1621,7 +1719,7 @@ dependencies = [ "serde", "target-lexicon", "thiserror", - "wasmparser", + "wasmparser 0.80.2", "wasmtime-environ", "wasmtime-runtime", "winapi 0.3.9", @@ -1661,7 +1759,7 @@ dependencies = [ "cranelift-entity", "serde", "thiserror", - "wasmparser", + "wasmparser 0.80.2", ] [[package]] @@ -1881,6 +1979,12 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" +[[package]] +name = "yansi" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fc79f4a1e39857fc00c3f662cbf2651c771f00e9c15fe2abc341806bd46bd71" + [[package]] name = "zstd" version = "0.9.0+zstd.1.5.0" diff --git a/Cargo.toml b/Cargo.toml index be68e29..b11a549 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ wasmtime = "0.30" anyhow = "1" minifb = "0.19" notify = "4" -pico-args = "0.4" \ No newline at end of file +pico-args = "0.4" +curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "d62bc8bf86cff65145d5aea045b81122bc15d709" } \ No newline at end of file diff --git a/src/microw8.rs b/src/lib.rs similarity index 100% rename from src/microw8.rs rename to src/lib.rs diff --git a/src/main.rs b/src/main.rs index 84e60c3..d91906f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,17 @@ -use std::path::PathBuf; +use std::fs::File; use std::sync::mpsc; use std::time::Duration; +use std::{ + path::{Path, PathBuf}, + process::exit, +}; +use std::io::prelude::*; use anyhow::{bail, Result}; -use microw8::MicroW8; +use uw8::MicroW8; use notify::{DebouncedEvent, Watcher}; use pico_args::Arguments; -mod microw8; - fn main() -> Result<()> { let mut args = Arguments::from_env(); @@ -25,18 +28,18 @@ fn main() -> Result<()> { watcher.watch(&filename, notify::RecursiveMode::NonRecursive)?; } - if let Err(err) = uw8.load_from_file(&filename) { - if !watch_mode { - return Err(err); - } + if let Err(err) = load_cart(&filename, &mut uw8) { eprintln!("Load error: {}", err); + if !watch_mode { + exit(1); + } } while uw8.is_open() { match rx.try_recv() { Ok(DebouncedEvent::Create(_) | DebouncedEvent::Write(_)) => { - if let Err(err) = uw8.load_from_file(&filename) { - eprintln!("Load error: {}", err) + if let Err(err) = load_cart(&filename, &mut uw8) { + eprintln!("Load error: {}", err); } } Err(mpsc::TryRecvError::Disconnected) => bail!("File watcher disconnected"), @@ -48,3 +51,20 @@ fn main() -> Result<()> { Ok(()) } + +fn load_cart(filename: &Path, uw8: &mut MicroW8) -> Result<()> { + let mut cart = vec![]; + File::open(filename)?.read_to_end(&mut cart)?; + + if cart[0] >= 10 { + let src = String::from_utf8(cart)?; + cart = curlywas::compile_str(&src)?; + } + + if let Err(err) = uw8.load_from_memory(&cart) { + eprintln!("Load error: {}", err); + Err(err) + } else { + Ok(()) + } +}