@@ -88,6 +88,7 @@ pub struct EntryPoint {
8888}
8989
9090impl EntryPoint {
91+ /// Call this entry point.
9192 pub fn call ( & self , data_ptr : Pointer < u8 > , data_len : WordSize ) -> anyhow:: Result < u64 > {
9293 let data_ptr = u32:: from ( data_ptr) as i32 ;
9394 let data_len = u32:: from ( data_len) as i32 ;
@@ -108,6 +109,31 @@ impl EntryPoint {
108109 } ,
109110 } ) . map ( |results| results[ 0 ] . unwrap_i64 ( ) as u64 )
110111 }
112+
113+ pub fn direct ( func : wasmtime:: Func ) -> std:: result:: Result < Self , & ' static str > {
114+ match ( func. ty ( ) . params ( ) , func. ty ( ) . results ( ) ) {
115+ ( & [ wasmtime:: ValType :: I32 , wasmtime:: ValType :: I32 ] , & [ wasmtime:: ValType :: I64 ] ) => {
116+ Ok ( Self { func, call_type : EntryPointType :: Direct } )
117+ }
118+ _ => {
119+ Err ( "Invalid signature" )
120+ }
121+ }
122+ }
123+
124+ pub fn wrapped ( dispatcher : wasmtime:: Func , func : u32 ) -> std:: result:: Result < Self , & ' static str > {
125+ match ( dispatcher. ty ( ) . params ( ) , dispatcher. ty ( ) . results ( ) ) {
126+ (
127+ & [ wasmtime:: ValType :: I32 , wasmtime:: ValType :: I32 , wasmtime:: ValType :: I32 ] ,
128+ & [ wasmtime:: ValType :: I64 ] ,
129+ ) => {
130+ Ok ( Self { func : dispatcher, call_type : EntryPointType :: Wrapped ( func) } )
131+ } ,
132+ _ => {
133+ Err ( "Invalid signature" )
134+ }
135+ }
136+ }
111137}
112138
113139/// Wrap the given WebAssembly Instance of a wasm module with Substrate-runtime.
@@ -199,16 +225,13 @@ impl InstanceWrapper {
199225 let func = extern_func ( & export)
200226 . ok_or_else ( || Error :: from ( format ! ( "Export {} is not a function" , method) ) ) ?
201227 . clone ( ) ;
202- match ( func. ty ( ) . params ( ) , func. ty ( ) . results ( ) ) {
203- ( & [ wasmtime:: ValType :: I32 , wasmtime:: ValType :: I32 ] , & [ wasmtime:: ValType :: I64 ] ) => { }
204- _ => {
205- return Err ( Error :: from ( format ! (
206- "method {} have an unsupported signature" ,
228+ EntryPoint :: direct ( func)
229+ . map_err ( |_|
230+ Error :: from ( format ! (
231+ "Function '{}' has invalid signature." ,
207232 method,
208- ) ) )
209- }
210- }
211- EntryPoint { call_type : EntryPointType :: Direct , func }
233+ ) )
234+ ) ?
212235 } ,
213236 CallSite :: Table ( func_ref) => {
214237 let table = self . instance . get_table ( "__indirect_function_table" ) . ok_or ( Error :: NoTable ) ?;
@@ -220,39 +243,31 @@ impl InstanceWrapper {
220243 . ok_or ( Error :: FunctionRefIsNull ( func_ref) ) ?
221244 . clone ( ) ;
222245
223- match ( func. ty ( ) . params ( ) , func. ty ( ) . results ( ) ) {
224- ( & [ wasmtime:: ValType :: I32 , wasmtime:: ValType :: I32 ] , & [ wasmtime:: ValType :: I64 ] ) => { }
225- _ => {
226- return Err ( Error :: from ( format ! (
227- "Function @{} have an unsupported signature" ,
246+ EntryPoint :: direct ( func)
247+ . map_err ( |_|
248+ Error :: from ( format ! (
249+ "Function @{} has invalid signature." ,
228250 func_ref,
229- ) ) )
230- }
231- }
232- EntryPoint { call_type : EntryPointType :: Direct , func }
233- } ,
251+ ) )
252+ ) ?
253+ } ,
234254 CallSite :: TableWithWrapper { dispatcher_ref, func } => {
235255 let table = self . instance . get_table ( "__indirect_function_table" ) . ok_or ( Error :: NoTable ) ?;
236256 let val = table. get ( dispatcher_ref)
237257 . ok_or ( Error :: NoTableEntryWithIndex ( dispatcher_ref) ) ?;
238258 let dispatcher = val
239259 . funcref ( )
240260 . ok_or ( Error :: TableElementIsNotAFunction ( dispatcher_ref) ) ?
241- . ok_or ( Error :: FunctionRefIsNull ( dispatcher_ref) ) ?;
242-
243- match ( dispatcher. ty ( ) . params ( ) , dispatcher. ty ( ) . results ( ) ) {
244- (
245- & [ wasmtime:: ValType :: I32 , wasmtime:: ValType :: I32 , wasmtime:: ValType :: I32 ] ,
246- & [ wasmtime:: ValType :: I64 ] ,
247- ) => { } ,
248- _ => {
249- return Err ( Error :: from ( format ! (
250- "Function @{} have an unsupported signature" ,
261+ . ok_or ( Error :: FunctionRefIsNull ( dispatcher_ref) ) ?
262+ . clone ( ) ;
263+
264+ EntryPoint :: wrapped ( dispatcher, func)
265+ . map_err ( |_|
266+ Error :: from ( format ! (
267+ "Function @{} has invalid signature." ,
251268 dispatcher_ref,
252- ) ) )
253- }
254- }
255- EntryPoint { call_type : EntryPointType :: Wrapped ( func) , func : dispatcher. clone ( ) }
269+ ) )
270+ ) ?
256271 } ,
257272 } )
258273 }
0 commit comments