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 {
/// 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<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,
args: &[RuntimeValue],
externals: &mut E,
) -> 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() {
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()),
}
}
}