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 {
//! Various error types returned by Wasmer APIs.
pub use wasmer_runtime_core::error::*;
pub use wasmer_runtime_core::error::{CompileError, CompileResult};
}
pub use prelude::*;

View File

@ -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::<DynFunc>("globalCtors") {
instance.call("globalCtors", &[])?;
if let Ok(func) = instance.exports.get::<DynFunc>("globalCtors") {
func.call(&[])?;
}
if let Ok(_func) = instance
if let Ok(func) = instance
.exports
.get::<DynFunc>("___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)?;
}

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