@@ -135,7 +135,29 @@ impl From<f64> for Value {
135135}
136136
137137/// The result of an exection.
138- pub type ExecutionResult = sys:: FizzyExecutionResult ;
138+ pub struct ExecutionResult ( sys:: FizzyExecutionResult ) ;
139+
140+ impl ExecutionResult {
141+ /// True if execution has resulted in a trap.
142+ pub fn trapped ( & self ) -> bool {
143+ self . 0 . trapped
144+ }
145+
146+ /// The optional return value. Only a single return value is allowed in WebAssembly 1.0.
147+ pub fn value ( & self ) -> Option < Value > {
148+ if !self . 0 . trapped && self . 0 . has_value {
149+ Some ( self . 0 . value )
150+ } else {
151+ None
152+ }
153+ }
154+ }
155+
156+ impl From < ExecutionResult > for sys:: FizzyExecutionResult {
157+ fn from ( v : ExecutionResult ) -> Self {
158+ v. 0
159+ }
160+ }
139161
140162impl Instance {
141163 /// Unsafe execution of a given function index with the given values.
@@ -145,7 +167,9 @@ impl Instance {
145167 args : & [ Value ] ,
146168 depth : i32 ,
147169 ) -> ExecutionResult {
148- sys:: fizzy_execute ( self . ptr . as_ptr ( ) , func_idx, args. as_ptr ( ) , depth)
170+ ExecutionResult {
171+ 0 : sys:: fizzy_execute ( self . ptr . as_ptr ( ) , func_idx, args. as_ptr ( ) , depth) ,
172+ }
149173 }
150174}
151175
@@ -203,29 +227,29 @@ mod tests {
203227 let mut instance = instance. unwrap ( ) ;
204228
205229 let result = unsafe { instance. unsafe_execute ( 0 , & [ ] , 0 ) } ;
206- assert ! ( !result. trapped) ;
207- assert ! ( !result. has_value ) ;
230+ assert ! ( !result. trapped( ) ) ;
231+ assert ! ( !result. value ( ) . is_some ( ) ) ;
208232
209233 let result = unsafe { instance. unsafe_execute ( 1 , & [ ] , 0 ) } ;
210- assert ! ( !result. trapped) ;
211- assert ! ( result. has_value ) ;
212- assert_eq ! ( result. value. as_i32( ) , 42 ) ;
234+ assert ! ( !result. trapped( ) ) ;
235+ assert ! ( result. value ( ) . is_some ( ) ) ;
236+ assert_eq ! ( result. value( ) . unwrap ( ) . as_i32( ) , 42 ) ;
213237
214238 // Explicit type specification
215239 let result =
216240 unsafe { instance. unsafe_execute ( 2 , & [ ( 42 as i32 ) . into ( ) , ( 2 as i32 ) . into ( ) ] , 0 ) } ;
217- assert ! ( !result. trapped) ;
218- assert ! ( result. has_value ) ;
219- assert_eq ! ( result. value. as_i32( ) , 21 ) ;
241+ assert ! ( !result. trapped( ) ) ;
242+ assert ! ( result. value ( ) . is_some ( ) ) ;
243+ assert_eq ! ( result. value( ) . unwrap ( ) . as_i32( ) , 21 ) ;
220244
221245 // Implicit i64 types (even though the code expects i32)
222246 let result = unsafe { instance. unsafe_execute ( 2 , & [ 42 . into ( ) , 2 . into ( ) ] , 0 ) } ;
223- assert ! ( !result. trapped) ;
224- assert ! ( result. has_value ) ;
225- assert_eq ! ( result. value. as_i32( ) , 21 ) ;
247+ assert ! ( !result. trapped( ) ) ;
248+ assert ! ( result. value ( ) . is_some ( ) ) ;
249+ assert_eq ! ( result. value( ) . unwrap ( ) . as_i32( ) , 21 ) ;
226250
227251 let result = unsafe { instance. unsafe_execute ( 3 , & [ ] , 0 ) } ;
228- assert ! ( result. trapped) ;
229- assert ! ( !result. has_value ) ;
252+ assert ! ( result. trapped( ) ) ;
253+ assert ! ( !result. value ( ) . is_some ( ) ) ;
230254 }
231255}
0 commit comments