@@ -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+
149213struct TestSuite ( BTreeMap < String , TestGroup > ) ;
150214
151215impl TestSuite {
0 commit comments