Skip to content

Commit

Permalink
feat: add wat support to cli
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
  • Loading branch information
explodingcamera committed Dec 4, 2023
1 parent 1fb5208 commit 829db60
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 11 deletions.
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ argh="0.1"
color-eyre="0.6"
log="0.4"
pretty_env_logger="0.5"
wast={version="69.0", optional=true}

[features]
default=["wast"]
wast=["dep:wast"]
21 changes: 16 additions & 5 deletions crates/cli/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::str::FromStr;
use argh::FromArgs;
use color_eyre::eyre::Result;
use log::info;
use tinywasm::{self, WasmValue};
use tinywasm::{self, Module, WasmValue};
mod util;
mod wat;

#[derive(FromArgs)]
/// TinyWasm CLI
Expand Down Expand Up @@ -68,20 +69,30 @@ fn main() -> Result<()> {
.filter_level(level)
.init();

let cwd = std::env::current_dir()?;

match args.nested {
TinyWasmSubcommand::Run(Run { wasm_file, engine }) => {
let wasm = std::fs::read(wasm_file)?;
let path = cwd.join(wasm_file.clone());
let module = match wasm_file.ends_with(".wat") {
true => {
let wat = std::fs::read_to_string(path)?;
let wasm = wat::wat2wasm(&wat);
tinywasm::Module::parse_bytes(&wasm)?
}
false => tinywasm::Module::parse_file(path)?,
};

match engine {
Engine::Main => run(&wasm),
Engine::Main => run(module),
}
}
}
}

fn run(wasm: &[u8]) -> Result<()> {
fn run(module: Module) -> Result<()> {
let mut store = tinywasm::Store::default();

let module = tinywasm::Module::parse_bytes(wasm)?;
let instance = module.instantiate(&mut store)?;
let func = instance.get_func(&store, "add")?;
let params = vec![WasmValue::I32(2), WasmValue::I32(2)];
Expand Down
10 changes: 10 additions & 0 deletions crates/cli/wat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use wast::{
parser::{self, ParseBuffer},
Wat,
};

pub fn wat2wasm<'a>(wat: &str) -> Vec<u8> {
let buf = ParseBuffer::new(wat).expect("failed to create parse buffer");
let mut module = parser::parse::<Wat>(&buf).expect("failed to parse wat");
module.encode().expect("failed to encode wat")
}
4 changes: 2 additions & 2 deletions crates/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ impl Parser {
#[cfg(feature = "std")]
pub fn parse_module_file(
&self,
path: impl AsRef<crate::std::path::Path>,
path: impl AsRef<crate::std::path::Path> + Clone,
) -> Result<TinyWasmModule> {
use alloc::format;
let f = crate::std::fs::File::open("log.txt").map_err(|e| {
let f = crate::std::fs::File::open(path.clone()).map_err(|e| {
ParseError::Other(format!("Error opening file {:?}: {}", path.as_ref(), e))
})?;

Expand Down
4 changes: 4 additions & 0 deletions crates/parser/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ impl ModuleReader {
validator.end(offset)?;
self.end_reached = true;
}
CustomSection(reader) => {
debug!("Found custom section");
debug!("Skipping custom section: {:?}", reader.name());
}
UnknownSection { .. } => {
return Err(ParseError::UnsupportedSection("Unknown section".into()))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tinywasm/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Module {
}

#[cfg(feature = "std")]
pub fn parse_file(path: impl AsRef<crate::std::path::Path>) -> Result<Self> {
pub fn parse_file(path: impl AsRef<crate::std::path::Path> + Clone) -> Result<Self> {
let parser = tinywasm_parser::Parser::new();
let data = parser.parse_module_file(path)?;
Ok(data.into())
Expand Down
3 changes: 0 additions & 3 deletions examples/wasm/add.wat
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
;; name: add ; description: add two numbers
(module
(func $add (export "add") (param $a i32) (param $b i32) (result i32)
local.get $a
Expand All @@ -10,5 +9,3 @@
local.get $b
i64.add)
)


0 comments on commit 829db60

Please sign in to comment.