@@ -30,11 +30,29 @@ pub fn parse(input: &[u8]) -> Option<Module> {
3030 Some ( Module { ptr : ptr } )
3131}
3232
33- pub struct Instance { }
33+ pub struct Instance {
34+ ptr : * mut sys:: fizzy_instance ,
35+ }
36+
37+ impl Drop for Instance {
38+ fn drop ( & mut self ) {
39+ println ! ( "Dropping instance" ) ;
40+ assert ! ( !self . ptr. is_null( ) ) ;
41+ unsafe { sys:: fizzy_free_instance ( self . ptr ) }
42+ }
43+ }
3444
3545impl Module {
3646 fn instantiate ( mut self ) -> Option < Instance > {
37- None
47+ assert ! ( !self . ptr. is_null( ) ) ;
48+ let module = self . ptr ;
49+ // Reset pointer to avoid drop to kick in.
50+ self . ptr = std:: ptr:: null_mut ( ) ;
51+ let ptr = unsafe { sys:: fizzy_instantiate ( module, std:: ptr:: null_mut ( ) , 0 ) } ;
52+ if ptr. is_null ( ) {
53+ return None ;
54+ }
55+ Some ( Instance { ptr : ptr } )
3856 }
3957}
4058
@@ -60,4 +78,12 @@ mod tests {
6078 assert ! ( parse( & [ 0x00 , 0x61 , 0x73 , 0x6d , 0x01 , 0x00 , 0x00 , 0x00 ] ) . is_some( ) ) ;
6179 assert ! ( parse( & [ 0x00 , 0x61 , 0x73 , 0x6d , 0x01 , 0x00 , 0x00 , 0x01 ] ) . is_none( ) ) ;
6280 }
81+
82+ #[ test]
83+ fn instantiate_wasm ( ) {
84+ let module = parse ( & [ 0x00 , 0x61 , 0x73 , 0x6d , 0x01 , 0x00 , 0x00 , 0x00 ] ) ;
85+ assert ! ( module. is_some( ) ) ;
86+ let instance = module. unwrap ( ) . instantiate ( ) ;
87+ assert ! ( instance. is_some( ) ) ;
88+ }
6389}
0 commit comments