Update from feedback

Deprecate more methods on `Instance`, add `into_iter` method on
`Exports`, add FuncSig to ImportType and other updates.
This commit is contained in:
Mark McCaskey
2020-03-30 17:38:51 -07:00
parent 7cd9e82015
commit 7ca721bd83
5 changed files with 54 additions and 19 deletions

View File

@ -242,6 +242,10 @@ impl Instance {
/// # Ok(())
/// # }
/// ```
#[deprecated(
since = "0.17.0",
note = "Please use `let f: DynFunc = instance.exports.get(name)?; f.call(params)?;` instead"
)]
pub fn call(&self, name: &str, params: &[Value]) -> CallResult<Vec<Value>> {
let export_index =
self.module
@ -933,6 +937,21 @@ impl Exports {
let module = self.module.borrow();
(inst_inner, module)
}
/// Iterate the exports.
///
/// ```
/// # use wasmer_runtime_core::instance::Instance;
/// # fn iterate_exports_example(instance: &Instance) {
/// for (export_name, export_value) in instance.exports.into_iter() {
/// println!("Found export `{}` with value `{:?}`", export_name, export_value);
/// }
/// # }
/// ```
pub fn into_iter(&self) -> ExportIter {
let (inst_inner, module) = self.get_inner();
ExportIter::new(&module, &inst_inner)
}
}
#[cfg(test)]

View File

@ -9,10 +9,10 @@ use crate::{
import::ImportObject,
structures::{Map, TypedIndex},
types::{
FuncIndex, FuncSig, GlobalDescriptor, GlobalIndex, GlobalInit, ImportedFuncIndex,
ImportedGlobalIndex, ImportedMemoryIndex, ImportedTableIndex, Initializer,
LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryDescriptor, MemoryIndex,
SigIndex, TableDescriptor, TableIndex,
FuncDescriptor, FuncIndex, FuncSig, GlobalDescriptor, GlobalIndex, GlobalInit,
ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex, ImportedTableIndex,
Initializer, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex, MemoryDescriptor,
MemoryIndex, SigIndex, TableDescriptor, TableIndex,
},
Instance,
};
@ -227,12 +227,16 @@ impl Module {
let info = &self.inner.info;
let imported_functions = info.imported_functions.values().map(|import_name| {
let imported_functions = info.imported_functions.iter().map(|(idx, import_name)| {
let (namespace, name) = get_import_name(info, import_name);
let sig = info
.signatures
.get(*info.func_assoc.get(FuncIndex::new(idx.index())).unwrap())
.unwrap();
Import {
namespace,
name,
ty: ImportType::Function,
ty: sig.into(),
}
});
let imported_memories =
@ -333,8 +337,7 @@ impl Clone for Module {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImportType {
/// The import is a function.
// TODO: why does function have no data?
Function,
Function(FuncDescriptor),
/// The import is a global variable.
Global(GlobalDescriptor),
/// A Wasm linear memory.
@ -343,6 +346,16 @@ pub enum ImportType {
Table(TableDescriptor),
}
impl From<FuncDescriptor> for ImportType {
fn from(other: FuncDescriptor) -> Self {
ImportType::Function(other)
}
}
impl From<&FuncDescriptor> for ImportType {
fn from(other: &FuncDescriptor) -> Self {
ImportType::Function(other.clone())
}
}
impl From<MemoryDescriptor> for ImportType {
fn from(other: MemoryDescriptor) -> Self {
ImportType::Memory(other)

View File

@ -359,6 +359,9 @@ pub struct FuncSig {
returns: Cow<'static, [Type]>,
}
/// Information about a function.
pub type FuncDescriptor = FuncSig;
impl FuncSig {
/// Creates a new function signatures with the given parameter and return types.
pub fn new<Params, Returns>(params: Params, returns: Returns) -> Self