diff --git a/lib/api/src/lib.rs b/lib/api/src/lib.rs index 9ca45696c..44b6a46e4 100644 --- a/lib/api/src/lib.rs +++ b/lib/api/src/lib.rs @@ -99,7 +99,7 @@ pub mod types { pub mod error { //! Various error types returned by Wasmer APIs. - pub use wasmer_runtime_core::error::*; + pub use wasmer_runtime_core::error::{CompileError, CompileResult}; } pub use prelude::*; diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index d18c57105..cf607e1b3 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -349,15 +349,15 @@ impl<'a> EmscriptenData<'a> { pub fn set_up_emscripten(instance: &mut Instance) -> CallResult<()> { // ATINIT // (used by C++) - if let Ok(_func) = instance.exports.get::("globalCtors") { - instance.call("globalCtors", &[])?; + if let Ok(func) = instance.exports.get::("globalCtors") { + func.call(&[])?; } - if let Ok(_func) = instance + if let Ok(func) = instance .exports .get::("___emscripten_environ_constructor") { - instance.call("___emscripten_environ_constructor", &[])?; + func.call(&[])?; } Ok(()) } @@ -380,13 +380,12 @@ pub fn emscripten_call_main(instance: &mut Instance, path: &str, args: &[&str]) let mut new_args = vec![path]; new_args.extend(args); let (argc, argv) = store_module_arguments(instance.context_mut(), new_args); - instance.call( - func_name, - &[Value::I32(argc as i32), Value::I32(argv as i32)], - )?; + let func: DynFunc = instance.exports.get(func_name)?; + func.call(&[Value::I32(argc as i32), Value::I32(argv as i32)])?; } 0 => { - instance.call(func_name, &[])?; + let func: DynFunc = instance.exports.get(func_name)?; + func.call(&[])?; } _ => { return Err(CallError::Resolve(ResolveError::ExportWrongType { @@ -420,7 +419,8 @@ pub fn run_emscripten_instance( debug!("Running entry point: {}", &ep); let arg = unsafe { allocate_cstr_on_stack(instance.context_mut(), args[0]).0 }; //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 { emscripten_call_main(instance, path, &args)?; } diff --git a/lib/runtime-core/src/instance.rs b/lib/runtime-core/src/instance.rs index c708ef150..a3110bc04 100644 --- a/lib/runtime-core/src/instance.rs +++ b/lib/runtime-core/src/instance.rs @@ -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> { 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)] diff --git a/lib/runtime-core/src/module.rs b/lib/runtime-core/src/module.rs index 13fb64535..382d422fe 100644 --- a/lib/runtime-core/src/module.rs +++ b/lib/runtime-core/src/module.rs @@ -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 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 for ImportType { fn from(other: MemoryDescriptor) -> Self { ImportType::Memory(other) diff --git a/lib/runtime-core/src/types.rs b/lib/runtime-core/src/types.rs index 8a0a0d82f..7c0eb4630 100644 --- a/lib/runtime-core/src/types.rs +++ b/lib/runtime-core/src/types.rs @@ -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: Params, returns: Returns) -> Self