watson

a hyper minimalistic no_std
+ alloc
WebAssembly parser/compiler for Rust based off the official specification
- [X] supports all section types
- [X] helper functions for finding things
- [x] support for compilation to wasm
- [ ] .wast parsing and assertion
- [ ] pass core WebAssembly spec tests
- [ ] interpreter
- [ ] [WASI simulator](https://richardanaya.github.io/watson/examples/simulator/index.html)
- [ ] lofi wasm mode (i.e. i32 only)
rust
[dependencies]
watson = "0.9"
Parse a WebAssembly module
```rust
use watson::*;
let program = watons::parse(&bytesofwasm)?;
for s in program.sections.iter() {
match s {
CodeSection(code)=> ...,
...
}
}
...
```
Write an interpreter
this is in progress
```rust
async fn run(program: impl InterpretableProgram) -> Result, &'static str> {
let mut interpreter = Interpreter::new(program)?;
let mut executor = interpreter.call("main", &[])?;
loop {
let executionunit = executor.nextoperation()?;
let response = match executionunit {
// if an import is called, figure out what to do
ExecutionUnit::CallImport(x) => {
if x.name == "print" {
let start = x.params[0].toi32() as usize;
let mem = match executor.memory() {
Some(m) => m,
None => return Err("there should be memory"),
};
let mem = mem.borrow();
let mut chars = vec![];
let mut i = 0;
loop {
if mem[start + i] == 0 {
break;
}
chars.push(mem[start + i]);
i += 1;
}
let text = fromutf8(&chars).unwrap();
println!("{}", text);
ExecutionResponse::DoNothing
} else if x.name == "sleep" {
let millis = x.params[0].toi32();
task::sleep(Duration::from_millis(millis as u64)).await;
ExecutionResponse::DoNothing
} else {
panic!("unknown import call")
}
}
// if there's nothing left to do, break out of loop
ExecutionUnit::Complete(v) => break Ok(v),
// handle other execution units with default behavior
mut x @ _ => x.evaluate()?,
};
executor.execute(response)?;
}
}
fn main() -> Result<(), Box> {
let args: Vec = env::args().collect();
if args.len() == 2 {
let buffer = fs::read(&args[1])?;
let program = watson::parse(&buffer)?;
task::block_on(run(program))?;
} else {
eprintln!("sleepyprint ");
exit(1);
}
Ok(())
}
```
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or
http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or
http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in watson
by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.