Publish FuncInstance::invoke

This commit is contained in:
Sergey Pepyakin 2018-02-05 18:05:59 +03:00
parent c5e28d0a8e
commit 0e355684a2
1 changed files with 15 additions and 6 deletions

View File

@ -75,7 +75,6 @@ impl fmt::Debug for FuncInstance {
} }
impl FuncInstance { impl FuncInstance {
/// Allocate a function instance for a host function. /// Allocate a function instance for a host function.
/// ///
/// When this function instance will be called by the wasm code, /// When this function instance will be called by the wasm code,
@ -128,20 +127,30 @@ impl FuncInstance {
} }
} }
pub(crate) fn invoke<E: Externals>( /// 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<E: Externals>(
func: &FuncRef, func: &FuncRef,
args: &[RuntimeValue], args: &[RuntimeValue],
externals: &mut E, externals: &mut E,
) -> Result<Option<RuntimeValue>, Trap> { ) -> Result<Option<RuntimeValue>, 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() { match *func.as_internal() {
FuncInstanceInternal::Internal { .. } => { FuncInstanceInternal::Internal { .. } => {
let mut interpreter = Interpreter::new(externals); let mut interpreter = Interpreter::new(externals);
interpreter.start_execution(func, args) interpreter.start_execution(func, args)
} }
FuncInstanceInternal::Host { ref host_func_index, .. } => { FuncInstanceInternal::Host {
externals.invoke_index(*host_func_index, args.into()) ref host_func_index,
} ..
} => externals.invoke_index(*host_func_index, args.into()),
} }
} }
} }