@@ -7,7 +7,7 @@ use std::time::Duration;
77
88use eyre:: { Context , Result , bail, eyre} ;
99use log:: { debug, error} ;
10- use tinywasm:: types:: { ExternRef , FuncRef , MemoryType , TableType , WasmType , WasmValue } ;
10+ use tinywasm:: types:: { ExternRef , FuncRef , MemoryType , RefType , RefValue , TableType , WasmType , WasmValue } ;
1111use tinywasm:: { ExecProgress , Global , HostFunction , Imports , Memory , Module , ModuleInstance , Store , Table } ;
1212use wast:: { QuoteWat , core:: AbstractHeapType } ;
1313
@@ -154,16 +154,8 @@ impl WastRunner {
154154 fn imports ( store : & mut Store , modules : & HashMap < String , ModuleInstance > ) -> Result < Imports > {
155155 let mut imports = Imports :: new ( ) ;
156156
157- let table = Table :: new (
158- store,
159- TableType :: new ( WasmType :: RefFunc , 10 , Some ( 20 ) ) ,
160- WasmValue :: default_for ( WasmType :: RefFunc ) ,
161- ) ?;
162- let table64 = Table :: new (
163- store,
164- TableType :: new64 ( WasmType :: RefFunc , 10 , Some ( 20 ) ) ,
165- WasmValue :: default_for ( WasmType :: RefFunc ) ,
166- ) ?;
157+ let table = Table :: new ( store, TableType :: new ( RefType :: FUNCREF , 10 , Some ( 20 ) ) , RefValue :: Null . into ( ) ) ?;
158+ let table64 = Table :: new ( store, TableType :: new64 ( RefType :: FUNCREF , 10 , Some ( 20 ) ) , RefValue :: Null . into ( ) ) ?;
167159 let memory = Memory :: new ( store, MemoryType :: default ( ) . with_page_count_initial ( 1 ) . with_page_count_max ( Some ( 2 ) ) ) ?;
168160 let global_i32 =
169161 Global :: new ( store, tinywasm:: types:: GlobalType :: new ( WasmType :: I32 , false ) , WasmValue :: I32 ( 666 ) ) ?;
@@ -467,7 +459,7 @@ impl WastRunner {
467459 let expected = expected_alternatives
468460 . iter ( )
469461 . filter_map ( |alts| alts. first ( ) )
470- . find ( |exp| module_global . eq_loose ( exp ) ) ;
462+ . find ( |exp| exp . matches ( & module_global ) ) ;
471463 if expected. is_none ( ) {
472464 test_group. add_result (
473465 & format ! ( "AssertReturn(unsupported-{i})" ) ,
@@ -516,7 +508,7 @@ impl WastRunner {
516508 }
517509 if expected_alternatives. iter ( ) . any ( |expected| {
518510 expected. len ( ) == outcomes. len ( )
519- && outcomes. iter ( ) . zip ( expected. iter ( ) ) . all ( |( outcome, exp) | outcome . eq_loose ( exp ) )
511+ && outcomes. iter ( ) . zip ( expected. iter ( ) ) . all ( |( outcome, exp) | exp . matches ( outcome ) )
520512 } ) {
521513 Ok ( ( ) )
522514 } else {
@@ -735,7 +727,7 @@ fn convert_wastargs(args: Vec<wast::WastArg>) -> Result<Vec<WasmValue>> {
735727 args. into_iter ( ) . map ( wastarg2tinywasmvalue) . collect ( )
736728}
737729
738- fn convert_wastret < ' a > ( args : impl Iterator < Item = wast:: WastRet < ' a > > ) -> Result < Vec < Vec < WasmValue > > > {
730+ fn convert_wastret < ' a > ( args : impl Iterator < Item = wast:: WastRet < ' a > > ) -> Result < Vec < Vec < ExpectedValue > > > {
739731 let mut alternatives = vec ! [ Vec :: new( ) ] ;
740732 for arg in args {
741733 let choices = wastret2tinywasmvalues ( arg) ?;
@@ -763,14 +755,10 @@ fn wastarg2tinywasmvalue(arg: wast::WastArg) -> Result<WasmValue> {
763755 I32 ( i) => WasmValue :: I32 ( i) ,
764756 I64 ( i) => WasmValue :: I64 ( i) ,
765757 V128 ( i) => WasmValue :: V128 ( i. to_le_bytes ( ) ) ,
766- RefExtern ( v) => WasmValue :: RefExtern ( ExternRef :: new ( Some ( v ) ) ) ,
758+ RefExtern ( v) => ExternRef :: new ( v ) . into ( ) ,
767759 RefNull ( t) => match t {
768- wast:: core:: HeapType :: Abstract { shared : false , ty : AbstractHeapType :: Func } => {
769- WasmValue :: RefFunc ( FuncRef :: null ( ) )
770- }
771- wast:: core:: HeapType :: Abstract { shared : false , ty : AbstractHeapType :: Extern } => {
772- WasmValue :: RefExtern ( ExternRef :: null ( ) )
773- }
760+ wast:: core:: HeapType :: Abstract { shared : false , ty : AbstractHeapType :: Func } => RefValue :: Null . into ( ) ,
761+ wast:: core:: HeapType :: Abstract { shared : false , ty : AbstractHeapType :: Extern } => RefValue :: Null . into ( ) ,
774762 _ => {
775763 bail ! ( "unsupported arg type: refnull: {:?}" , t) ;
776764 }
@@ -797,7 +785,26 @@ fn wast_v128_to_bytes(i: wast::core::V128Pattern) -> [u8; 16] {
797785 res. try_into ( ) . unwrap ( )
798786}
799787
800- fn wastret2tinywasmvalues ( ret : wast:: WastRet ) -> Result < Vec < WasmValue > > {
788+ #[ derive( Clone , Copy ) ]
789+ enum ExpectedValue {
790+ Exact ( WasmValue ) ,
791+ RefNull ,
792+ RefFunc ,
793+ RefExtern ,
794+ }
795+
796+ impl ExpectedValue {
797+ fn matches ( & self , value : & WasmValue ) -> bool {
798+ match self {
799+ Self :: Exact ( expected) => value. eq_loose ( expected) ,
800+ Self :: RefNull => matches ! ( value, WasmValue :: Ref ( RefValue :: Null ) ) ,
801+ Self :: RefFunc => matches ! ( value, WasmValue :: Ref ( RefValue :: Func ( _) ) ) ,
802+ Self :: RefExtern => matches ! ( value, WasmValue :: Ref ( RefValue :: Extern ( _) ) ) ,
803+ }
804+ }
805+ }
806+
807+ fn wastret2tinywasmvalues ( ret : wast:: WastRet ) -> Result < Vec < ExpectedValue > > {
801808 let wast:: WastRet :: Core ( ret) = ret else {
802809 bail ! ( "unsupported arg type" ) ;
803810 } ;
@@ -809,32 +816,22 @@ fn wastret2tinywasmvalues(ret: wast::WastRet) -> Result<Vec<WasmValue>> {
809816 }
810817}
811818
812- fn wastretcore2tinywasmvalue ( ret : wast:: core:: WastRetCore ) -> Result < WasmValue > {
819+ fn wastretcore2tinywasmvalue ( ret : wast:: core:: WastRetCore ) -> Result < ExpectedValue > {
813820 use wast:: core:: WastRetCore :: { F32 , F64 , I32 , I64 , RefExtern , RefFunc , RefNull , V128 } ;
814821 Ok ( match ret {
815- F32 ( f) => nanpattern2tinywasmvalue ( f) ?,
816- F64 ( f) => nanpattern2tinywasmvalue ( f) ?,
817- I32 ( i) => WasmValue :: I32 ( i) ,
818- I64 ( i) => WasmValue :: I64 ( i) ,
819- V128 ( i) => WasmValue :: V128 ( wast_v128_to_bytes ( i) ) ,
820- RefNull ( t) => match t {
821- Some ( wast:: core:: HeapType :: Abstract { shared : false , ty : AbstractHeapType :: Func } ) => {
822- WasmValue :: RefFunc ( FuncRef :: null ( ) )
823- }
824- Some ( wast:: core:: HeapType :: Abstract { shared : false , ty : AbstractHeapType :: Extern } ) => {
825- WasmValue :: RefExtern ( ExternRef :: null ( ) )
826- }
827- _ => {
828- bail ! ( "unsupported arg type: refnull: {:?}" , t) ;
829- }
830- } ,
831- RefExtern ( v) => WasmValue :: RefExtern ( ExternRef :: new ( v) ) ,
832- RefFunc ( v) => WasmValue :: RefFunc ( FuncRef :: new ( match v {
833- Some ( wast:: token:: Index :: Num ( n, _) ) => Some ( n) ,
834- _ => {
835- bail ! ( "unsupported arg type: reffunc: {:?}" , v) ;
836- }
837- } ) ) ,
822+ F32 ( f) => ExpectedValue :: Exact ( nanpattern2tinywasmvalue ( f) ?) ,
823+ F64 ( f) => ExpectedValue :: Exact ( nanpattern2tinywasmvalue ( f) ?) ,
824+ I32 ( i) => ExpectedValue :: Exact ( WasmValue :: I32 ( i) ) ,
825+ I64 ( i) => ExpectedValue :: Exact ( WasmValue :: I64 ( i) ) ,
826+ V128 ( i) => ExpectedValue :: Exact ( WasmValue :: V128 ( wast_v128_to_bytes ( i) ) ) ,
827+ RefNull ( _) => ExpectedValue :: RefNull ,
828+ RefExtern ( Some ( v) ) => ExpectedValue :: Exact ( ExternRef :: new ( v) . into ( ) ) ,
829+ RefExtern ( None ) => ExpectedValue :: RefExtern ,
830+ RefFunc ( Some ( wast:: token:: Index :: Num ( n, _) ) ) => ExpectedValue :: Exact ( FuncRef :: new ( n) . into ( ) ) ,
831+ RefFunc ( None ) => ExpectedValue :: RefFunc ,
832+ RefFunc ( v) => {
833+ bail ! ( "unsupported arg type: reffunc: {:?}" , v) ;
834+ }
838835 a => {
839836 bail ! ( "unsupported arg type {:?}" , a) ;
840837 }
0 commit comments