From 0e355684a2561bd2718cc74ba5af7811fb47fcf6 Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Mon, 5 Feb 2018 18:05:59 +0300 Subject: [PATCH] Publish FuncInstance::invoke --- src/func.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/func.rs b/src/func.rs index 7662e85..5b5b038 100644 --- a/src/func.rs +++ b/src/func.rs @@ -75,7 +75,6 @@ impl fmt::Debug for FuncInstance { } impl FuncInstance { - /// Allocate a function instance for a host function. /// /// When this function instance will be called by the wasm code, @@ -128,20 +127,30 @@ impl FuncInstance { } } - pub(crate) fn invoke( + /// Invoke this function. + /// + /// # Errors + /// + /// Returns `Err` if `args` types is not match function [`signature`] or + /// if [`Trap`] at execution time occured. + /// + /// [`signature`]: #method.signature + /// [`Trap`]: #enum.Trap.html + pub fn invoke( func: &FuncRef, args: &[RuntimeValue], externals: &mut E, ) -> Result, Trap> { - debug_assert!(check_function_args(func.signature(), &args).is_ok()); + check_function_args(func.signature(), &args).map_err(|_| Trap::UnexpectedSignature)?; match *func.as_internal() { FuncInstanceInternal::Internal { .. } => { let mut interpreter = Interpreter::new(externals); interpreter.start_execution(func, args) } - FuncInstanceInternal::Host { ref host_func_index, .. } => { - externals.invoke_index(*host_func_index, args.into()) - } + FuncInstanceInternal::Host { + ref host_func_index, + .. + } => externals.invoke_index(*host_func_index, args.into()), } } }