Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to call function at index on Ctx #803

Merged
merged 7 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/runtime-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 24 additions & 1 deletion lib/runtime-core/src/vm.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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)]
Expand Down