1
+ use alloc:: vec:: Vec ;
1
2
use core:: cmp:: { Eq , PartialEq } ;
2
3
use core:: hash:: { Hash , Hasher } ;
3
4
use core:: marker:: PhantomData ;
4
5
use core:: ptr:: { self , NonNull } ;
5
- use core:: slice;
6
6
use core:: str;
7
7
8
8
use crate :: error:: { Error , Result } ;
9
9
use crate :: runtime:: Runtime ;
10
10
use crate :: utils:: cstr_to_str;
11
- use crate :: wasm3_priv;
12
- use crate :: { WasmArgs , WasmType } ;
11
+ use crate :: { WasmArgs , WasmType , Module } ;
13
12
14
13
/// Calling Context for a host function.
15
14
pub struct CallContext < ' cc > {
@@ -25,24 +24,15 @@ impl<'cc> CallContext<'cc> {
25
24
}
26
25
}
27
26
28
- unsafe fn mallocated ( & self ) -> * mut ffi:: M3MemoryHeader {
29
- self . runtime . as_ref ( ) . memory . mallocated
30
- }
31
-
32
27
/// Returns the raw memory of the runtime associated with this context.
33
28
///
34
29
/// # Safety
35
30
///
36
31
/// The returned pointer may get invalidated when wasm function objects are called due to reallocations.
37
32
pub unsafe fn memory ( & self ) -> * const [ u8 ] {
38
- let mallocated = self . mallocated ( ) ;
39
- let len = ( * mallocated) . length as usize ;
40
- let data = if len == 0 {
41
- ptr:: NonNull :: dangling ( ) . as_ptr ( )
42
- } else {
43
- mallocated. offset ( 1 ) . cast ( )
44
- } ;
45
- ptr:: slice_from_raw_parts ( data, len)
33
+ let mut memory_size = 0u32 ;
34
+ let data = ffi:: m3_GetMemory ( self . runtime . as_ptr ( ) , & mut memory_size as * mut u32 , 0 ) ;
35
+ ptr:: slice_from_raw_parts ( data, memory_size as usize )
46
36
}
47
37
48
38
/// Returns the raw memory of the runtime associated with this context.
@@ -51,22 +41,18 @@ impl<'cc> CallContext<'cc> {
51
41
///
52
42
/// The returned pointer may get invalidated when wasm function objects are called due to reallocations.
53
43
pub unsafe fn memory_mut ( & self ) -> * mut [ u8 ] {
54
- let mallocated = self . mallocated ( ) ;
55
- let len = ( * mallocated) . length as usize ;
56
- let data = if len == 0 {
57
- ptr:: NonNull :: dangling ( ) . as_ptr ( )
58
- } else {
59
- mallocated. offset ( 1 ) . cast ( )
60
- } ;
61
- ptr:: slice_from_raw_parts_mut ( data, len)
44
+ let mut memory_size = 0u32 ;
45
+ let data = ffi:: m3_GetMemory ( self . runtime . as_ptr ( ) , & mut memory_size as * mut u32 , 0 ) ;
46
+ ptr:: slice_from_raw_parts_mut ( data, memory_size as usize )
62
47
}
63
48
}
64
49
65
50
// redefine of ffi::RawCall without the Option<T> around it
66
51
/// Type of a raw host function for wasm3.
67
52
pub type RawCall = unsafe extern "C" fn (
68
53
runtime : ffi:: IM3Runtime ,
69
- _sp : ffi:: m3stack_t ,
54
+ ctx : ffi:: IM3ImportContext ,
55
+ _sp : * mut u64 ,
70
56
_mem : * mut cty:: c_void ,
71
57
) -> * const cty:: c_void ;
72
58
@@ -102,7 +88,17 @@ where
102
88
{
103
89
/// The name of this function.
104
90
pub fn name ( & self ) -> & str {
105
- unsafe { cstr_to_str ( self . raw . as_ref ( ) . name ) }
91
+ unsafe { cstr_to_str ( ffi:: m3_GetFunctionName ( self . raw . as_ptr ( ) ) ) }
92
+ }
93
+
94
+ /// The module containing this function.
95
+ pub fn module ( & self ) -> Option < Module < ' rt > > {
96
+ let module = unsafe { ffi:: m3_GetFunctionModule ( self . raw . as_ptr ( ) ) } ;
97
+ if !module. is_null ( ) {
98
+ Some ( Module :: from_raw ( self . rt , module) )
99
+ } else {
100
+ None
101
+ }
106
102
}
107
103
}
108
104
@@ -111,77 +107,43 @@ where
111
107
Args : WasmArgs ,
112
108
Ret : WasmType ,
113
109
{
114
- pub ( crate ) fn validate_sig ( mut func : NNM3Function ) -> Result < ( ) > {
115
- let & ffi:: M3FuncType {
116
- returnType : ret,
117
- argTypes : ref args,
118
- numArgs : num,
119
- ..
120
- } = unsafe { & * func. as_mut ( ) . funcType } ;
121
- // argTypes is actually dynamically sized.
122
- let args = unsafe { slice:: from_raw_parts ( args. as_ptr ( ) , num as usize ) } ;
123
- match Ret :: TYPE_INDEX == ret && Args :: validate_types ( args) {
124
- true => Ok ( ( ) ) ,
125
- false => Err ( Error :: InvalidFunctionSignature ) ,
126
- }
110
+ fn validate_sig ( func : NNM3Function ) -> bool {
111
+ let num_args = unsafe { ffi:: m3_GetArgCount ( func. as_ptr ( ) ) } ;
112
+ let num_rets = unsafe { ffi:: m3_GetRetCount ( func. as_ptr ( ) ) } ;
113
+ let args = ( 0 ..num_args)
114
+ . map ( |i| unsafe { ffi:: m3_GetArgType ( func. as_ptr ( ) , i) } )
115
+ . collect :: < Vec < _ > > ( ) ;
116
+ let rets = ( 0 ..num_rets)
117
+ . map ( |i| unsafe { ffi:: m3_GetRetType ( func. as_ptr ( ) , i) } )
118
+ . collect :: < Vec < _ > > ( ) ;
119
+ return Args :: validate_types ( & args) &&
120
+ ( ( rets. len ( ) == 0 && Ret :: TYPE_INDEX == ffi:: M3ValueType :: c_m3Type_none) ||
121
+ ( rets. len ( ) == 1 && Ret :: TYPE_INDEX == rets[ 0 ] ) ) ;
127
122
}
128
123
129
124
#[ inline]
130
125
pub ( crate ) fn from_raw ( rt : & ' rt Runtime , raw : NNM3Function ) -> Result < Self > {
131
- Self :: validate_sig ( raw) ?;
132
- let this = Function {
126
+ if !Self :: validate_sig ( raw) {
127
+ return Err ( Error :: InvalidFunctionSignature ) ;
128
+ }
129
+ Ok ( Function {
133
130
raw,
134
131
rt,
135
132
_pd : PhantomData ,
136
- } ;
137
- // make sure the function is compiled
138
- this. compile ( )
139
- }
140
-
141
- #[ inline]
142
- pub ( crate ) fn compile ( self ) -> Result < Self > {
143
- unsafe {
144
- if self . raw . as_ref ( ) . compiled . is_null ( ) {
145
- Error :: from_ffi_res ( wasm3_priv:: Compile_Function ( self . raw . as_ptr ( ) ) ) ?;
146
- }
147
- } ;
148
- Ok ( self )
133
+ } )
149
134
}
150
135
151
136
fn call_impl ( & self , args : Args ) -> Result < Ret > {
152
- let stack = self . rt . stack_mut ( ) ;
153
- let ret = unsafe {
154
- args. push_on_stack ( stack) ;
155
- Self :: call_impl_ (
156
- self . raw . as_ref ( ) . compiled ,
157
- stack. cast ( ) ,
158
- self . rt . mallocated ( ) ,
159
- 0 ,
160
- 0.0 ,
161
- )
137
+ let mut argv = args. ptrs_vec ( ) ;
138
+ let result = unsafe {
139
+ ffi:: m3_Call ( self . raw . as_ptr ( ) , argv. len ( ) as u32 , argv. as_mut_ptr ( ) )
162
140
} ;
163
- Error :: from_ffi_res ( ret. cast ( ) ) . map ( |( ) | unsafe { Ret :: pop_from_stack ( stack. cast ( ) ) } )
164
- }
165
-
166
- #[ inline]
167
- unsafe fn call_impl_ (
168
- _pc : ffi:: pc_t ,
169
- _sp : ffi:: m3stack_t ,
170
- _mem : * mut ffi:: M3MemoryHeader ,
171
- _r0 : ffi:: m3reg_t ,
172
- _fp0 : f64 ,
173
- ) -> ffi:: m3ret_t {
174
- let possible_trap = ffi:: m3_Yield ( ) ;
175
- if !possible_trap. is_null ( ) {
176
- possible_trap. cast ( )
177
- } else {
178
- ( * _pc. cast :: < ffi:: IM3Operation > ( ) ) . expect ( "IM3Operation was null" ) (
179
- _pc. add ( 1 ) ,
180
- _sp,
181
- _mem,
182
- _r0,
183
- _fp0,
184
- )
141
+ Error :: from_ffi_res ( result) ?;
142
+ unsafe {
143
+ let mut ret = core:: mem:: MaybeUninit :: < Ret > :: uninit ( ) ;
144
+ let result = ffi:: m3_GetResultsV ( self . raw . as_ptr ( ) , ret. as_mut_ptr ( ) ) ;
145
+ Error :: from_ffi_res ( result) ?;
146
+ Ok ( ret. assume_init ( ) )
185
147
}
186
148
}
187
149
}
0 commit comments