diff options
Diffstat (limited to 'crates/cli')
| -rw-r--r-- | crates/cli/Cargo.toml | 83 | ||||
| -rw-r--r-- | crates/cli/src/output.rs | 7 | ||||
| -rw-r--r-- | crates/cli/src/value_parse.rs | 2 | ||||
| -rw-r--r-- | crates/cli/src/wast_runner.rs | 89 |
4 files changed, 89 insertions, 92 deletions
diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 1aed0cc..7dbd4d2 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -1,53 +1,52 @@ [package] -name="tinywasm-cli" -version.workspace=true -description="Minimal command-line interface for TinyWasm" -edition.workspace=true -license.workspace=true -authors.workspace=true -repository.workspace=true -documentation="https://docs.rs/tinywasm-cli" -rust-version.workspace=true -keywords=["tinywasm", "wasm", "webassembly", "cli", "runtime"] -categories=["command-line-utilities", "wasm"] -readme="README.md" +name = "tinywasm-cli" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +description = "Minimal command-line interface for TinyWasm" +documentation = "https://docs.rs/tinywasm-cli" +readme = "README.md" +repository.workspace = true +license.workspace = true +keywords = ["cli", "runtime", "tinywasm", "wasm", "webassembly"] +categories = ["command-line-utilities", "wasm"] [lib] -name="tinywasm_cli" -path="src/lib.rs" +name = "tinywasm_cli" +path = "src/lib.rs" [[bin]] -name="tinywasm" -path="src/bin.rs" +name = "tinywasm" +path = "src/bin.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap={version="4.6", features=["derive"]} -clap_complete="4.6" -eyre.workspace=true -log.workspace=true -pretty_env_logger.workspace=true -tinywasm={workspace=true, features=[ - "std", - "parser", - "log", - "archive", - "canonicalize-nans", - "debug", - "parallel-parser", -]} -wat={workspace=true, optional=true} -wast={workspace=true, optional=true} -owo-colors={workspace=true} -anstream={version="1.0"} - -[features] -default=["wat", "wast"] -wat=["dep:wat"] -wast=["dep:wast"] +anstream = { version = "1.0" } +clap = { version = "4.6", features = ["derive"] } +clap_complete = "4.6" +eyre.workspace = true +log.workspace = true +owo-colors = { workspace = true } +pretty_env_logger.workspace = true +tinywasm = { workspace = true, features = [ + "archive", + "canonicalize-nans", + "debug", + "log", + "parallel-parser", + "parser", + "std", +] } +wast = { workspace = true, optional = true } +wat = { workspace = true, optional = true } [dev-dependencies] -assert_cmd="2.2" -predicates="3.1" -tempfile="3.27" +assert_cmd = "2.2" +predicates = "3.1" +tempfile = "3.27" + +[features] +default = ["wast", "wat"] +wast = ["dep:wast"] +wat = ["dep:wat"] diff --git a/crates/cli/src/output.rs b/crates/cli/src/output.rs index bf568ed..40a271e 100644 --- a/crates/cli/src/output.rs +++ b/crates/cli/src/output.rs @@ -30,8 +30,9 @@ pub fn format_wasm_type(ty: WasmType) -> &'static str { WasmType::F32 => "f32", WasmType::F64 => "f64", WasmType::V128 => "v128", - WasmType::RefFunc => "funcref", - WasmType::RefExtern => "externref", + WasmType::Ref(ty) if ty.is_func() => "funcref", + WasmType::Ref(ty) if ty.is_extern() => "externref", + WasmType::Ref(_) => "ref", } } @@ -62,7 +63,7 @@ pub fn format_table_type(ty: &TableType) -> String { MemoryArch::I64 => "i64", }; let max = ty.size_max.map(|v| v.to_string()).unwrap_or_else(|| "unbounded".to_string()); - format!("table[{arch} {}] initial={} max={max}", format_wasm_type(ty.element_type), ty.size_initial) + format!("table[{arch} {}] initial={} max={max}", format_wasm_type(WasmType::Ref(ty.element_type)), ty.size_initial) } pub fn format_global_type(ty: &GlobalType) -> String { diff --git a/crates/cli/src/value_parse.rs b/crates/cli/src/value_parse.rs index 92c0363..eabbdc4 100644 --- a/crates/cli/src/value_parse.rs +++ b/crates/cli/src/value_parse.rs @@ -21,7 +21,7 @@ fn parse_arg(index: usize, ty: WasmType, value: &str) -> Result<WasmValue> { .parse::<i128>() .map(|v| WasmValue::V128(v.to_le_bytes())) .map_err(|e| format_error(index, ty, value, e))?, - WasmType::RefFunc | WasmType::RefExtern => { + WasmType::Ref(_) => { bail!( "unsupported CLI argument type at position {}: {}; use the embedding API for reference values", index + 1, diff --git a/crates/cli/src/wast_runner.rs b/crates/cli/src/wast_runner.rs index 99f1efe..c773663 100644 --- a/crates/cli/src/wast_runner.rs +++ b/crates/cli/src/wast_runner.rs @@ -7,7 +7,7 @@ use std::time::Duration; use eyre::{Context, Result, bail, eyre}; use log::{debug, error}; -use tinywasm::types::{ExternRef, FuncRef, MemoryType, TableType, WasmType, WasmValue}; +use tinywasm::types::{ExternRef, FuncRef, MemoryType, RefType, RefValue, TableType, WasmType, WasmValue}; use tinywasm::{ExecProgress, Global, HostFunction, Imports, Memory, Module, ModuleInstance, Store, Table}; use wast::{QuoteWat, core::AbstractHeapType}; @@ -154,16 +154,8 @@ impl WastRunner { fn imports(store: &mut Store, modules: &HashMap<String, ModuleInstance>) -> Result<Imports> { let mut imports = Imports::new(); - let table = Table::new( - store, - TableType::new(WasmType::RefFunc, 10, Some(20)), - WasmValue::default_for(WasmType::RefFunc), - )?; - let table64 = Table::new( - store, - TableType::new64(WasmType::RefFunc, 10, Some(20)), - WasmValue::default_for(WasmType::RefFunc), - )?; + let table = Table::new(store, TableType::new(RefType::FUNCREF, 10, Some(20)), RefValue::Null.into())?; + let table64 = Table::new(store, TableType::new64(RefType::FUNCREF, 10, Some(20)), RefValue::Null.into())?; let memory = Memory::new(store, MemoryType::default().with_page_count_initial(1).with_page_count_max(Some(2)))?; let global_i32 = Global::new(store, tinywasm::types::GlobalType::new(WasmType::I32, false), WasmValue::I32(666))?; @@ -467,7 +459,7 @@ impl WastRunner { let expected = expected_alternatives .iter() .filter_map(|alts| alts.first()) - .find(|exp| module_global.eq_loose(exp)); + .find(|exp| exp.matches(&module_global)); if expected.is_none() { test_group.add_result( &format!("AssertReturn(unsupported-{i})"), @@ -516,7 +508,7 @@ impl WastRunner { } if expected_alternatives.iter().any(|expected| { expected.len() == outcomes.len() - && outcomes.iter().zip(expected.iter()).all(|(outcome, exp)| outcome.eq_loose(exp)) + && outcomes.iter().zip(expected.iter()).all(|(outcome, exp)| exp.matches(outcome)) }) { Ok(()) } else { @@ -735,7 +727,7 @@ fn convert_wastargs(args: Vec<wast::WastArg>) -> Result<Vec<WasmValue>> { args.into_iter().map(wastarg2tinywasmvalue).collect() } -fn convert_wastret<'a>(args: impl Iterator<Item = wast::WastRet<'a>>) -> Result<Vec<Vec<WasmValue>>> { +fn convert_wastret<'a>(args: impl Iterator<Item = wast::WastRet<'a>>) -> Result<Vec<Vec<ExpectedValue>>> { let mut alternatives = vec![Vec::new()]; for arg in args { let choices = wastret2tinywasmvalues(arg)?; @@ -763,14 +755,10 @@ fn wastarg2tinywasmvalue(arg: wast::WastArg) -> Result<WasmValue> { I32(i) => WasmValue::I32(i), I64(i) => WasmValue::I64(i), V128(i) => WasmValue::V128(i.to_le_bytes()), - RefExtern(v) => WasmValue::RefExtern(ExternRef::new(Some(v))), + RefExtern(v) => ExternRef::new(v).into(), RefNull(t) => match t { - wast::core::HeapType::Abstract { shared: false, ty: AbstractHeapType::Func } => { - WasmValue::RefFunc(FuncRef::null()) - } - wast::core::HeapType::Abstract { shared: false, ty: AbstractHeapType::Extern } => { - WasmValue::RefExtern(ExternRef::null()) - } + wast::core::HeapType::Abstract { shared: false, ty: AbstractHeapType::Func } => RefValue::Null.into(), + wast::core::HeapType::Abstract { shared: false, ty: AbstractHeapType::Extern } => RefValue::Null.into(), _ => { bail!("unsupported arg type: refnull: {:?}", t); } @@ -797,7 +785,26 @@ fn wast_v128_to_bytes(i: wast::core::V128Pattern) -> [u8; 16] { res.try_into().unwrap() } -fn wastret2tinywasmvalues(ret: wast::WastRet) -> Result<Vec<WasmValue>> { +#[derive(Clone, Copy)] +enum ExpectedValue { + Exact(WasmValue), + RefNull, + RefFunc, + RefExtern, +} + +impl ExpectedValue { + fn matches(&self, value: &WasmValue) -> bool { + match self { + Self::Exact(expected) => value.eq_loose(expected), + Self::RefNull => matches!(value, WasmValue::Ref(RefValue::Null)), + Self::RefFunc => matches!(value, WasmValue::Ref(RefValue::Func(_))), + Self::RefExtern => matches!(value, WasmValue::Ref(RefValue::Extern(_))), + } + } +} + +fn wastret2tinywasmvalues(ret: wast::WastRet) -> Result<Vec<ExpectedValue>> { let wast::WastRet::Core(ret) = ret else { bail!("unsupported arg type"); }; @@ -809,32 +816,22 @@ fn wastret2tinywasmvalues(ret: wast::WastRet) -> Result<Vec<WasmValue>> { } } -fn wastretcore2tinywasmvalue(ret: wast::core::WastRetCore) -> Result<WasmValue> { +fn wastretcore2tinywasmvalue(ret: wast::core::WastRetCore) -> Result<ExpectedValue> { use wast::core::WastRetCore::{F32, F64, I32, I64, RefExtern, RefFunc, RefNull, V128}; Ok(match ret { - F32(f) => nanpattern2tinywasmvalue(f)?, - F64(f) => nanpattern2tinywasmvalue(f)?, - I32(i) => WasmValue::I32(i), - I64(i) => WasmValue::I64(i), - V128(i) => WasmValue::V128(wast_v128_to_bytes(i)), - RefNull(t) => match t { - Some(wast::core::HeapType::Abstract { shared: false, ty: AbstractHeapType::Func }) => { - WasmValue::RefFunc(FuncRef::null()) - } - Some(wast::core::HeapType::Abstract { shared: false, ty: AbstractHeapType::Extern }) => { - WasmValue::RefExtern(ExternRef::null()) - } - _ => { - bail!("unsupported arg type: refnull: {:?}", t); - } - }, - RefExtern(v) => WasmValue::RefExtern(ExternRef::new(v)), - RefFunc(v) => WasmValue::RefFunc(FuncRef::new(match v { - Some(wast::token::Index::Num(n, _)) => Some(n), - _ => { - bail!("unsupported arg type: reffunc: {:?}", v); - } - })), + F32(f) => ExpectedValue::Exact(nanpattern2tinywasmvalue(f)?), + F64(f) => ExpectedValue::Exact(nanpattern2tinywasmvalue(f)?), + I32(i) => ExpectedValue::Exact(WasmValue::I32(i)), + I64(i) => ExpectedValue::Exact(WasmValue::I64(i)), + V128(i) => ExpectedValue::Exact(WasmValue::V128(wast_v128_to_bytes(i))), + RefNull(_) => ExpectedValue::RefNull, + RefExtern(Some(v)) => ExpectedValue::Exact(ExternRef::new(v).into()), + RefExtern(None) => ExpectedValue::RefExtern, + RefFunc(Some(wast::token::Index::Num(n, _))) => ExpectedValue::Exact(FuncRef::new(n).into()), + RefFunc(None) => ExpectedValue::RefFunc, + RefFunc(v) => { + bail!("unsupported arg type: reffunc: {:?}", v); + } a => { bail!("unsupported arg type {:?}", a); } |
