Add method to call function at index on Ctx

This commit is contained in:
Mark McCaskey
2019-09-16 15:01:18 -07:00
parent 793fd60f6a
commit b35a522d28
2 changed files with 25 additions and 2 deletions

View File

@ -518,7 +518,7 @@ impl LikeNamespace for Rc<Instance> {
}
#[must_use]
fn call_func_with_index(
pub(crate) fn call_func_with_index(
info: &ModuleInfo,
runnable: &dyn RunnableModule,
import_backing: &ImportBacking,

View File

@ -1,9 +1,11 @@
pub use crate::backing::{ImportBacking, LocalBacking, INTERNALS_SIZE};
use crate::{
error::{CallError, CallResult, RuntimeError},
instance::call_func_with_index,
memory::{Memory, MemoryType},
module::{ModuleInfo, ModuleInner},
structures::TypedIndex,
types::{LocalOrImport, MemoryIndex},
types::{FuncIndex, LocalOrImport, MemoryIndex, Value},
vmcalls,
};
use std::{
@ -393,6 +395,27 @@ impl Ctx {
(*self.internal.internals)[field.index()] = value;
}
}
/// Calls a host or Wasm function at the given index
pub fn call_with_index(&mut self, index: FuncIndex, args: &[Value]) -> CallResult<Vec<Value>> {
let module = unsafe { &(*self.module) };
if module.info.func_assoc.get(index).is_none() {
return Err(CallError::Runtime(RuntimeError::Trap {
msg: format!("Index out of bounds: {}", index.index()).into_boxed_str(),
}));
}
let mut output = vec![];
call_func_with_index(
&module.info,
module.runnable_module.as_ref(),
unsafe { &*self.import_backing },
self as *mut Ctx,
index,
args,
&mut output,
)?;
Ok(output)
}
}
#[doc(hidden)]