Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit 4697f7c

Browse files
test(tinywasm): compare test results - we actually pass the majority now
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
1 parent b4cb5f9 commit 4697f7c

File tree

3 files changed

+75
-15
lines changed

3 files changed

+75
-15
lines changed

crates/tinywasm/src/runtime/executor/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ fn exec_one(
9191

9292
// if cond != 0, we already have the right value on the stack
9393
if cond == 0 {
94-
stack.values.last_mut().map(|v| *v = val2);
94+
let _ = stack.values.pop()?;
95+
stack.values.push(val2);
9596
}
9697
}
9798
Call(v) => {

crates/tinywasm/src/runtime/stack/value_stack.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ impl Default for ValueStack {
2323
}
2424

2525
impl ValueStack {
26-
#[inline]
27-
pub(crate) fn last_mut(&mut self) -> Option<&mut RawWasmValue> {
28-
self.stack.last_mut()
29-
}
30-
3126
#[inline]
3227
pub(crate) fn len(&self) -> usize {
3328
assert!(self.top <= self.stack.len());

crates/tinywasm/tests/mvp.rs

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn test_mvp() -> Result<()> {
7272
},
7373
);
7474
}
75-
AssertReturn { span, exec, results: _ } => {
75+
AssertReturn { span, exec, results } => {
7676
let Some(module) = last_module.as_ref() else {
7777
// println!("no module found for assert_return: {:?}", exec);
7878
continue;
@@ -84,15 +84,30 @@ fn test_mvp() -> Result<()> {
8484
let instance = module.instantiate(&mut store)?;
8585

8686
use wast::WastExecute::*;
87-
match exec {
87+
let invoke = match exec {
8888
Wat(_) => return Result::Ok(()), // not used by the testsuite
8989
Get { module: _, global: _ } => return Result::Ok(()),
90-
Invoke(invoke) => {
91-
for arg in invoke.args {
92-
let arg = get_tinywasm_wasm_value(arg)?;
93-
let _res = instance.get_func(&store, invoke.name)?.call(&mut store, &[arg])?;
94-
// TODO: check the result
95-
}
90+
Invoke(invoke) => invoke,
91+
};
92+
93+
let args = invoke
94+
.args
95+
.into_iter()
96+
.map(wastarg2tinywasmvalue)
97+
.collect::<Result<Vec<_>>>()?;
98+
let res = instance.get_func(&store, invoke.name)?.call(&mut store, &args)?;
99+
let expected = results
100+
.into_iter()
101+
.map(wastret2tinywasmvalue)
102+
.collect::<Result<Vec<_>>>()?;
103+
104+
if res.len() != expected.len() {
105+
return Result::Err(eyre!("expected {} results, got {}", expected.len(), res.len()));
106+
}
107+
108+
for (i, (res, expected)) in res.iter().zip(expected).enumerate() {
109+
if res != &expected {
110+
return Result::Err(eyre!("result {} did not match: {:?} != {:?}", i, res, expected));
96111
}
97112
}
98113

@@ -130,7 +145,7 @@ fn test_mvp() -> Result<()> {
130145
}
131146
}
132147

133-
fn get_tinywasm_wasm_value(arg: wast::WastArg) -> Result<tinywasm_types::WasmValue> {
148+
fn wastarg2tinywasmvalue(arg: wast::WastArg) -> Result<tinywasm_types::WasmValue> {
134149
let wast::WastArg::Core(arg) = arg else {
135150
return Err(eyre!("unsupported arg type"));
136151
};
@@ -146,6 +161,55 @@ fn get_tinywasm_wasm_value(arg: wast::WastArg) -> Result<tinywasm_types::WasmVal
146161
})
147162
}
148163

164+
fn wastret2tinywasmvalue(arg: wast::WastRet) -> Result<tinywasm_types::WasmValue> {
165+
let wast::WastRet::Core(arg) = arg else {
166+
return Err(eyre!("unsupported arg type"));
167+
};
168+
169+
use tinywasm_types::WasmValue;
170+
use wast::core::WastRetCore::*;
171+
Ok(match arg {
172+
F32(f) => nanpattern2tinywasmvalue(f)?,
173+
F64(f) => nanpattern2tinywasmvalue(f)?,
174+
I32(i) => WasmValue::I32(i),
175+
I64(i) => WasmValue::I64(i),
176+
_ => return Err(eyre!("unsupported arg type")),
177+
})
178+
}
179+
180+
enum Bits {
181+
U32(u32),
182+
U64(u64),
183+
}
184+
trait FloatToken {
185+
fn bits(&self) -> Bits;
186+
}
187+
impl FloatToken for wast::token::Float32 {
188+
fn bits(&self) -> Bits {
189+
Bits::U32(self.bits)
190+
}
191+
}
192+
impl FloatToken for wast::token::Float64 {
193+
fn bits(&self) -> Bits {
194+
Bits::U64(self.bits)
195+
}
196+
}
197+
198+
fn nanpattern2tinywasmvalue<T>(arg: wast::core::NanPattern<T>) -> Result<tinywasm_types::WasmValue>
199+
where
200+
T: FloatToken,
201+
{
202+
use wast::core::NanPattern::*;
203+
Ok(match arg {
204+
CanonicalNan => tinywasm_types::WasmValue::F32(f32::NAN),
205+
ArithmeticNan => tinywasm_types::WasmValue::F32(f32::NAN),
206+
Value(v) => match v.bits() {
207+
Bits::U32(v) => tinywasm_types::WasmValue::F32(f32::from_bits(v)),
208+
Bits::U64(v) => tinywasm_types::WasmValue::F64(f64::from_bits(v)),
209+
},
210+
})
211+
}
212+
149213
struct TestSuite(BTreeMap<String, TestGroup>);
150214

151215
impl TestSuite {

0 commit comments

Comments
 (0)