@@ -10,10 +10,19 @@ use crate::function::{CallContext, Function, RawCall};
10
10
use crate :: runtime:: Runtime ;
11
11
use crate :: utils:: { cstr_to_str, str_to_cstr_owned} ;
12
12
13
+ #[ derive( Debug ) ]
14
+ struct DropModule ( NonNull < ffi:: M3Module > ) ;
15
+
16
+ impl Drop for DropModule {
17
+ fn drop ( & mut self ) {
18
+ unsafe { ffi:: m3_FreeModule ( self . 0 . as_ptr ( ) ) } ;
19
+ }
20
+ }
21
+
13
22
/// A parsed module which can be loaded into a [`Runtime`].
14
23
pub struct ParsedModule {
15
24
data : Box < [ u8 ] > ,
16
- raw : ffi :: IM3Module ,
25
+ raw : DropModule ,
17
26
env : Environment ,
18
27
}
19
28
@@ -26,21 +35,24 @@ impl ParsedModule {
26
35
let res = unsafe {
27
36
ffi:: m3_ParseModule ( env. as_ptr ( ) , & mut module, data. as_ptr ( ) , data. len ( ) as u32 )
28
37
} ;
29
- Error :: from_ffi_res ( res) . map ( |_| ParsedModule {
38
+ Error :: from_ffi_res ( res) ?;
39
+ let module = NonNull :: new ( module)
40
+ . expect ( "module pointer is non-null after m3_ParseModule if result is not error" ) ;
41
+ Ok ( ParsedModule {
30
42
data,
31
- raw : module,
43
+ raw : DropModule ( module) ,
32
44
env : env. clone ( ) ,
33
45
} )
34
46
}
35
47
36
48
pub ( crate ) fn as_ptr ( & self ) -> ffi:: IM3Module {
37
- self . raw
49
+ self . raw . 0 . as_ptr ( )
38
50
}
39
51
40
- pub ( crate ) fn take_data ( mut self ) -> Box < [ u8 ] > {
41
- let res = mem :: replace ( & mut self . data , < Box < [ u8 ] > > :: default ( ) ) ;
42
- mem:: forget ( self ) ;
43
- res
52
+ pub ( crate ) fn take_data ( self ) -> Box < [ u8 ] > {
53
+ let ParsedModule { data, raw , env : _env } = self ;
54
+ mem:: forget ( raw ) ;
55
+ data
44
56
}
45
57
46
58
/// The environment this module was parsed in.
@@ -49,12 +61,6 @@ impl ParsedModule {
49
61
}
50
62
}
51
63
52
- impl Drop for ParsedModule {
53
- fn drop ( & mut self ) {
54
- unsafe { ffi:: m3_FreeModule ( self . raw ) } ;
55
- }
56
- }
57
-
58
64
/// A loaded module belonging to a specific runtime. Allows for linking and looking up functions.
59
65
// needs no drop as loaded modules will be cleaned up by the runtime
60
66
pub struct Module < ' rt > {
0 commit comments