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

@ -99,7 +99,7 @@ pub mod types {
pub mod error { pub mod error {
//! Various error types returned by Wasmer APIs. //! Various error types returned by Wasmer APIs.
pub use wasmer_runtime_core::error::*; pub use wasmer_runtime_core::error::{CompileError, CompileResult};
} }
pub use prelude::*; pub use prelude::*;

View File

@ -349,15 +349,15 @@ impl<'a> EmscriptenData<'a> {
pub fn set_up_emscripten(instance: &mut Instance) -> CallResult<()> { pub fn set_up_emscripten(instance: &mut Instance) -> CallResult<()> {
// ATINIT // ATINIT
// (used by C++) // (used by C++)
if let Ok(_func) = instance.exports.get::<DynFunc>("globalCtors") { if let Ok(func) = instance.exports.get::<DynFunc>("globalCtors") {
instance.call("globalCtors", &[])?; func.call(&[])?;
} }
if let Ok(_func) = instance if let Ok(func) = instance
.exports .exports
.get::<DynFunc>("___emscripten_environ_constructor") .get::<DynFunc>("___emscripten_environ_constructor")
{ {
instance.call("___emscripten_environ_constructor", &[])?; func.call(&[])?;
} }
Ok(()) Ok(())
} }
@ -380,13 +380,12 @@ pub fn emscripten_call_main(instance: &mut Instance, path: &str, args: &[&str])
let mut new_args = vec![path]; let mut new_args = vec![path];
new_args.extend(args); new_args.extend(args);
let (argc, argv) = store_module_arguments(instance.context_mut(), new_args); let (argc, argv) = store_module_arguments(instance.context_mut(), new_args);
instance.call( let func: DynFunc = instance.exports.get(func_name)?;
func_name, func.call(&[Value::I32(argc as i32), Value::I32(argv as i32)])?;
&[Value::I32(argc as i32), Value::I32(argv as i32)],
)?;
} }
0 => { 0 => {
instance.call(func_name, &[])?; let func: DynFunc = instance.exports.get(func_name)?;
func.call(&[])?;
} }
_ => { _ => {
return Err(CallError::Resolve(ResolveError::ExportWrongType { return Err(CallError::Resolve(ResolveError::ExportWrongType {
@ -420,7 +419,8 @@ pub fn run_emscripten_instance(
debug!("Running entry point: {}", &ep); debug!("Running entry point: {}", &ep);
let arg = unsafe { allocate_cstr_on_stack(instance.context_mut(), args[0]).0 }; let arg = unsafe { allocate_cstr_on_stack(instance.context_mut(), args[0]).0 };
//let (argc, argv) = store_module_arguments(instance.context_mut(), args); //let (argc, argv) = store_module_arguments(instance.context_mut(), args);
instance.call(&ep, &[Value::I32(arg as i32)])?; let func: DynFunc = instance.exports.get(&ep)?;
func.call(&[Value::I32(arg as i32)])?;
} else { } else {
emscripten_call_main(instance, path, &args)?; emscripten_call_main(instance, path, &args)?;
} }

View File

@ -242,6 +242,10 @@ impl Instance {
/// # Ok(()) /// # 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>> { pub fn call(&self, name: &str, params: &[Value]) -> CallResult<Vec<Value>> {
let export_index = let export_index =
self.module self.module
@ -933,6 +937,21 @@ impl Exports {
let module = self.module.borrow(); let module = self.module.borrow();
(inst_inner, module) (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)] #[cfg(test)]

View File

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

View File

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