Deprecate Instance::func and Instance::dyn_func

This commit is contained in:
Mark McCaskey 2020-03-26 17:11:37 -07:00
parent a18371eb91
commit 2fb8f8197b
7 changed files with 154 additions and 101 deletions

View File

@ -1,6 +1,6 @@
/// This example demonstrates the use of callbacks: calling functions (Host and Wasm)
/// passed to us from the Wasm via hostcall
use wasmer_runtime::{compile_with, compiler_for_backend, func, imports, Backend, Ctx};
use wasmer_runtime::{compile_with, compiler_for_backend, func, imports, Backend, Ctx, Func};
use wasmer_runtime_core::{structures::TypedIndex, types::TableIndex};
static WASM: &'static str = "examples/callback-guest/callback-guest.wasm";
@ -40,7 +40,7 @@ fn main() {
.instantiate(&imports)
.expect("failed to instantiate wasm module");
let entry_point = instance.func::<(u32, u32), u32>("main").unwrap();
let entry_point: Func<(u32, u32), u32> = instance.exports.get("main").unwrap();
entry_point.call(0, 0).expect("START");
}

View File

@ -1,5 +1,7 @@
use rayon::prelude::*;
use wasmer_runtime::{compile_with, compiler_for_backend, func, imports, instantiate, Backend};
use wasmer_runtime::{
compile_with, compiler_for_backend, func, imports, instantiate, Backend, Func,
};
use wasmer_runtime_core::{
memory::ptr::{Array, WasmPtr},
vm::Ctx,
@ -61,7 +63,8 @@ fn main() {
.clone()
.instantiate(&imports)
.expect("failed to instantiate wasm module");
let check_password = instance.func::<(u64, u64), u64>("check_password").unwrap();
let check_password: Func<(u64, u64), u64> =
instance.exports.get("check_password").unwrap();
let j = i * 10000;
let result = check_password.call(j, j + 10000).unwrap();
print!(".");
@ -101,7 +104,7 @@ fn main() {
let instance =
instantiate(&wasm_bytes[..], &imports).expect("failed to instantiate wasm module");
let check_password = instance.func::<(u64, u64), u64>("check_password").unwrap();
let check_password: Func<(u64, u64), u64> = instance.exports.get("check_password").unwrap();
let mut out: Option<RetStr> = None;
for i in (0..=u64::max_value()).step_by(10000) {

View File

@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use wasmer_runtime::{compile, func, imports};
use wasmer_runtime::{compile, func, imports, Func};
use wasmer_runtime_core::vm::Ctx;
use wasmer_wasi::{
generate_import_object_for_version,
@ -159,7 +159,7 @@ fn main() {
initialize(instance.context_mut());
// get a reference to the function "plugin_entrypoint" which takes an i32 and returns an i32
let entry_point = instance.func::<(i32), i32>("plugin_entrypoint").unwrap();
let entry_point: Func<i32, i32> = instance.exports.get("plugin_entrypoint").unwrap();
// call the "entry_point" function in WebAssembly with the number "2" as the i32 argument
let result = entry_point.call(2).expect("failed to execute plugin");
println!("result: {}", result);

View File

@ -33,7 +33,7 @@ use wasmer_runtime_core::{
types::{ElementType, FuncSig, MemoryDescriptor, TableDescriptor, Type, Value},
units::Pages,
vm::Ctx,
Func, Instance, IsExport, Module,
DynFunc, Func, Instance, IsExport, Module,
};
#[cfg(unix)]
@ -181,75 +181,89 @@ impl<'a> EmscriptenData<'a> {
globals: &'a EmscriptenGlobalsData,
mapped_dirs: HashMap<String, PathBuf>,
) -> EmscriptenData<'a> {
let malloc = instance.func("_malloc").or(instance.func("malloc")).ok();
let free = instance.func("_free").or(instance.func("free")).ok();
let memalign = instance
.func("_memalign")
.or(instance.func("memalign"))
let malloc = instance
.exports
.get("_malloc")
.or(instance.exports.get("malloc"))
.ok();
let memset = instance.func("_memset").or(instance.func("memset")).ok();
let stack_alloc = instance.func("stackAlloc").ok();
let free = instance
.exports
.get("_free")
.or(instance.exports.get("free"))
.ok();
let memalign = instance
.exports
.get("_memalign")
.or(instance.exports.get("memalign"))
.ok();
let memset = instance
.exports
.get("_memset")
.or(instance.exports.get("memset"))
.ok();
let stack_alloc = instance.exports.get("stackAlloc").ok();
let dyn_call_i = instance.func("dynCall_i").ok();
let dyn_call_ii = instance.func("dynCall_ii").ok();
let dyn_call_iii = instance.func("dynCall_iii").ok();
let dyn_call_iiii = instance.func("dynCall_iiii").ok();
let dyn_call_iifi = instance.func("dynCall_iifi").ok();
let dyn_call_v = instance.func("dynCall_v").ok();
let dyn_call_vi = instance.func("dynCall_vi").ok();
let dyn_call_vii = instance.func("dynCall_vii").ok();
let dyn_call_viii = instance.func("dynCall_viii").ok();
let dyn_call_viiii = instance.func("dynCall_viiii").ok();
let dyn_call_i = instance.exports.get("dynCall_i").ok();
let dyn_call_ii = instance.exports.get("dynCall_ii").ok();
let dyn_call_iii = instance.exports.get("dynCall_iii").ok();
let dyn_call_iiii = instance.exports.get("dynCall_iiii").ok();
let dyn_call_iifi = instance.exports.get("dynCall_iifi").ok();
let dyn_call_v = instance.exports.get("dynCall_v").ok();
let dyn_call_vi = instance.exports.get("dynCall_vi").ok();
let dyn_call_vii = instance.exports.get("dynCall_vii").ok();
let dyn_call_viii = instance.exports.get("dynCall_viii").ok();
let dyn_call_viiii = instance.exports.get("dynCall_viiii").ok();
// round 2
let dyn_call_dii = instance.func("dynCall_dii").ok();
let dyn_call_diiii = instance.func("dynCall_diiii").ok();
let dyn_call_iiiii = instance.func("dynCall_iiiii").ok();
let dyn_call_iiiiii = instance.func("dynCall_iiiiii").ok();
let dyn_call_iiiiiii = instance.func("dynCall_iiiiiii").ok();
let dyn_call_iiiiiiii = instance.func("dynCall_iiiiiiii").ok();
let dyn_call_iiiiiiiii = instance.func("dynCall_iiiiiiiii").ok();
let dyn_call_iiiiiiiiii = instance.func("dynCall_iiiiiiiiii").ok();
let dyn_call_iiiiiiiiiii = instance.func("dynCall_iiiiiiiiiii").ok();
let dyn_call_vd = instance.func("dynCall_vd").ok();
let dyn_call_viiiii = instance.func("dynCall_viiiii").ok();
let dyn_call_viiiiii = instance.func("dynCall_viiiiii").ok();
let dyn_call_viiiiiii = instance.func("dynCall_viiiiiii").ok();
let dyn_call_viiiiiiii = instance.func("dynCall_viiiiiiii").ok();
let dyn_call_viiiiiiiii = instance.func("dynCall_viiiiiiiii").ok();
let dyn_call_viiiiiiiiii = instance.func("dynCall_viiiiiiiiii").ok();
let dyn_call_iij = instance.func("dynCall_iij").ok();
let dyn_call_iji = instance.func("dynCall_iji").ok();
let dyn_call_iiji = instance.func("dynCall_iiji").ok();
let dyn_call_iiijj = instance.func("dynCall_iiijj").ok();
let dyn_call_j = instance.func("dynCall_j").ok();
let dyn_call_ji = instance.func("dynCall_ji").ok();
let dyn_call_jii = instance.func("dynCall_jii").ok();
let dyn_call_jij = instance.func("dynCall_jij").ok();
let dyn_call_jjj = instance.func("dynCall_jjj").ok();
let dyn_call_viiij = instance.func("dynCall_viiij").ok();
let dyn_call_viiijiiii = instance.func("dynCall_viiijiiii").ok();
let dyn_call_viiijiiiiii = instance.func("dynCall_viiijiiiiii").ok();
let dyn_call_viij = instance.func("dynCall_viij").ok();
let dyn_call_viiji = instance.func("dynCall_viiji").ok();
let dyn_call_viijiii = instance.func("dynCall_viijiii").ok();
let dyn_call_viijj = instance.func("dynCall_viijj").ok();
let dyn_call_vj = instance.func("dynCall_vj").ok();
let dyn_call_vjji = instance.func("dynCall_vjji").ok();
let dyn_call_vij = instance.func("dynCall_vij").ok();
let dyn_call_viji = instance.func("dynCall_viji").ok();
let dyn_call_vijiii = instance.func("dynCall_vijiii").ok();
let dyn_call_vijj = instance.func("dynCall_vijj").ok();
let dyn_call_viid = instance.func("dynCall_viid").ok();
let dyn_call_vidd = instance.func("dynCall_vidd").ok();
let dyn_call_viidii = instance.func("dynCall_viidii").ok();
let dyn_call_viidddddddd = instance.func("dynCall_viidddddddd").ok();
let dyn_call_dii = instance.exports.get("dynCall_dii").ok();
let dyn_call_diiii = instance.exports.get("dynCall_diiii").ok();
let dyn_call_iiiii = instance.exports.get("dynCall_iiiii").ok();
let dyn_call_iiiiii = instance.exports.get("dynCall_iiiiii").ok();
let dyn_call_iiiiiii = instance.exports.get("dynCall_iiiiiii").ok();
let dyn_call_iiiiiiii = instance.exports.get("dynCall_iiiiiiii").ok();
let dyn_call_iiiiiiiii = instance.exports.get("dynCall_iiiiiiiii").ok();
let dyn_call_iiiiiiiiii = instance.exports.get("dynCall_iiiiiiiiii").ok();
let dyn_call_iiiiiiiiiii = instance.exports.get("dynCall_iiiiiiiiiii").ok();
let dyn_call_vd = instance.exports.get("dynCall_vd").ok();
let dyn_call_viiiii = instance.exports.get("dynCall_viiiii").ok();
let dyn_call_viiiiii = instance.exports.get("dynCall_viiiiii").ok();
let dyn_call_viiiiiii = instance.exports.get("dynCall_viiiiiii").ok();
let dyn_call_viiiiiiii = instance.exports.get("dynCall_viiiiiiii").ok();
let dyn_call_viiiiiiiii = instance.exports.get("dynCall_viiiiiiiii").ok();
let dyn_call_viiiiiiiiii = instance.exports.get("dynCall_viiiiiiiiii").ok();
let dyn_call_iij = instance.exports.get("dynCall_iij").ok();
let dyn_call_iji = instance.exports.get("dynCall_iji").ok();
let dyn_call_iiji = instance.exports.get("dynCall_iiji").ok();
let dyn_call_iiijj = instance.exports.get("dynCall_iiijj").ok();
let dyn_call_j = instance.exports.get("dynCall_j").ok();
let dyn_call_ji = instance.exports.get("dynCall_ji").ok();
let dyn_call_jii = instance.exports.get("dynCall_jii").ok();
let dyn_call_jij = instance.exports.get("dynCall_jij").ok();
let dyn_call_jjj = instance.exports.get("dynCall_jjj").ok();
let dyn_call_viiij = instance.exports.get("dynCall_viiij").ok();
let dyn_call_viiijiiii = instance.exports.get("dynCall_viiijiiii").ok();
let dyn_call_viiijiiiiii = instance.exports.get("dynCall_viiijiiiiii").ok();
let dyn_call_viij = instance.exports.get("dynCall_viij").ok();
let dyn_call_viiji = instance.exports.get("dynCall_viiji").ok();
let dyn_call_viijiii = instance.exports.get("dynCall_viijiii").ok();
let dyn_call_viijj = instance.exports.get("dynCall_viijj").ok();
let dyn_call_vj = instance.exports.get("dynCall_vj").ok();
let dyn_call_vjji = instance.exports.get("dynCall_vjji").ok();
let dyn_call_vij = instance.exports.get("dynCall_vij").ok();
let dyn_call_viji = instance.exports.get("dynCall_viji").ok();
let dyn_call_vijiii = instance.exports.get("dynCall_vijiii").ok();
let dyn_call_vijj = instance.exports.get("dynCall_vijj").ok();
let dyn_call_viid = instance.exports.get("dynCall_viid").ok();
let dyn_call_vidd = instance.exports.get("dynCall_vidd").ok();
let dyn_call_viidii = instance.exports.get("dynCall_viidii").ok();
let dyn_call_viidddddddd = instance.exports.get("dynCall_viidddddddd").ok();
let stack_save = instance.func("stackSave").ok();
let stack_restore = instance.func("stackRestore").ok();
let stack_save = instance.exports.get("stackSave").ok();
let stack_restore = instance.exports.get("stackRestore").ok();
let set_threw = instance
.func("_setThrew")
.or(instance.func("setThrew"))
.exports
.get("_setThrew")
.or(instance.exports.get("setThrew"))
.ok();
EmscriptenData {
@ -335,11 +349,14 @@ impl<'a> EmscriptenData<'a> {
pub fn set_up_emscripten(instance: &mut Instance) -> CallResult<()> {
// ATINIT
// (used by C++)
if let Ok(_func) = instance.dyn_func("globalCtors") {
if let Ok(_func) = instance.exports.get::<DynFunc>("globalCtors") {
instance.call("globalCtors", &[])?;
}
if let Ok(_func) = instance.dyn_func("___emscripten_environ_constructor") {
if let Ok(_func) = instance
.exports
.get::<DynFunc>("___emscripten_environ_constructor")
{
instance.call("___emscripten_environ_constructor", &[])?;
}
Ok(())
@ -350,9 +367,9 @@ pub fn set_up_emscripten(instance: &mut Instance) -> CallResult<()> {
///
/// If you don't want to set it up yourself, consider using [`run_emscripten_instance`].
pub fn emscripten_call_main(instance: &mut Instance, path: &str, args: &[&str]) -> CallResult<()> {
let (func_name, main_func) = match instance.dyn_func("_main") {
let (func_name, main_func) = match instance.exports.get::<DynFunc>("_main") {
Ok(func) => Ok(("_main", func)),
Err(_e) => match instance.dyn_func("main") {
Err(_e) => match instance.exports.get::<DynFunc>("main") {
Ok(func) => Ok(("main", func)),
Err(e) => Err(e),
},

View File

@ -1,6 +1,7 @@
//! This module contains types to manipulate and access a Wasm module's exports
//! including memories, tables, globals, and functions.
use crate::{
error,
global::Global,
instance::{Exports, InstanceInner},
memory::Memory,
@ -102,5 +103,5 @@ impl<'a> Iterator for ExportIter<'a> {
pub trait Exportable<'a>: Sized {
/// Implementation of how to get the export corresponding to the implementing type
/// from an [`Instance`] by name.
fn get_self(exports: &'a Exports, name: &str) -> Option<Self>;
fn get_self(exports: &'a Exports, name: &str) -> error::ResolveResult<Self>;
}

View File

@ -181,12 +181,13 @@ impl Instance {
/// # Ok(())
/// # }
/// ```
#[deprecated(since = "0.17.0", note = "Please use `instance.exports.get()` instead")]
pub fn func<Args, Rets>(&self, name: &str) -> ResolveResult<Func<Args, Rets, Wasm>>
where
Args: WasmTypeList,
Rets: WasmTypeList,
{
func(&*self.module, &self.inner, name)
self.exports.get(name)
}
/// Resolve a function by name.
@ -224,8 +225,9 @@ impl Instance {
/// # Ok(())
/// # }
/// ```
#[deprecated(since = "0.17.0", note = "Please use `instance.exports.get()` instead")]
pub fn dyn_func(&self, name: &str) -> ResolveResult<DynFunc> {
dyn_func(&*self.module, &self.inner, name)
self.exports.get(name)
}
/// Call an exported WebAssembly function given the export name.
@ -323,7 +325,6 @@ impl Instance {
}
/// Private function implementing the inner logic of `Instance::func`
// TODO: reevaluate this lifetime
fn func<'a, Args, Rets>(
module: &'a ModuleInner,
inst_inner: &'a InstanceInner,
@ -844,52 +845,79 @@ impl<'a> DynFunc<'a> {
}
impl<'a> Exportable<'a> for Memory {
fn get_self(exports: &'a Exports, name: &str) -> Option<Self> {
fn get_self(exports: &'a Exports, name: &str) -> ResolveResult<Self> {
let (inst_inner, module) = exports.get_inner();
let export_index = module.info.exports.get(name)?;
let export_index =
module
.info
.exports
.get(name)
.ok_or_else(|| ResolveError::ExportNotFound {
name: name.to_string(),
})?;
if let ExportIndex::Memory(idx) = export_index {
Some(inst_inner.get_memory_from_index(module, *idx))
Ok(inst_inner.get_memory_from_index(module, *idx))
} else {
None
Err(ResolveError::ExportWrongType {
name: name.to_string(),
})
}
}
}
impl<'a> Exportable<'a> for Table {
fn get_self(exports: &'a Exports, name: &str) -> Option<Self> {
fn get_self(exports: &'a Exports, name: &str) -> ResolveResult<Self> {
let (inst_inner, module) = exports.get_inner();
let export_index = module.info.exports.get(name)?;
let export_index =
module
.info
.exports
.get(name)
.ok_or_else(|| ResolveError::ExportNotFound {
name: name.to_string(),
})?;
if let ExportIndex::Table(idx) = export_index {
Some(inst_inner.get_table_from_index(module, *idx))
Ok(inst_inner.get_table_from_index(module, *idx))
} else {
None
Err(ResolveError::ExportWrongType {
name: name.to_string(),
})
}
}
}
impl<'a> Exportable<'a> for Global {
fn get_self(exports: &'a Exports, name: &str) -> Option<Self> {
fn get_self(exports: &'a Exports, name: &str) -> ResolveResult<Self> {
let (inst_inner, module) = exports.get_inner();
let export_index = module.info.exports.get(name)?;
let export_index =
module
.info
.exports
.get(name)
.ok_or_else(|| ResolveError::ExportNotFound {
name: name.to_string(),
})?;
if let ExportIndex::Global(idx) = export_index {
Some(inst_inner.get_global_from_index(module, *idx))
Ok(inst_inner.get_global_from_index(module, *idx))
} else {
None
Err(ResolveError::ExportWrongType {
name: name.to_string(),
})
}
}
}
impl<'a> Exportable<'a> for DynFunc<'a> {
fn get_self(exports: &'a Exports, name: &str) -> Option<Self> {
fn get_self(exports: &'a Exports, name: &str) -> ResolveResult<Self> {
let (inst_inner, module) = exports.get_inner();
dyn_func(module, inst_inner, name).ok()
dyn_func(module, inst_inner, name)
}
}
impl<'a, Args: WasmTypeList, Rets: WasmTypeList> Exportable<'a> for Func<'a, Args, Rets, Wasm> {
fn get_self(exports: &'a Exports, name: &str) -> Option<Self> {
fn get_self(exports: &'a Exports, name: &str) -> ResolveResult<Self> {
let (inst_inner, module) = exports.get_inner();
func(module, inst_inner, name).ok()
func(module, inst_inner, name)
}
}
@ -928,7 +956,7 @@ impl Exports {
/// # Some(())
/// # }
/// ```
pub fn get<'a, T: Exportable<'a>>(&'a self, name: &str) -> Option<T> {
pub fn get<'a, T: Exportable<'a>>(&'a self, name: &str) -> ResolveResult<T> {
T::get_self(self, name)
}

View File

@ -31,7 +31,7 @@ use wasmer_llvm_backend::{
};
use wasmer_runtime::{
cache::{Cache as BaseCache, FileSystemCache, WasmHash},
Backend, Value, VERSION,
Backend, DynFunc, Func, Value, VERSION,
};
#[cfg(feature = "managed")]
use wasmer_runtime_core::tiering::{run_tiering, InteractiveShellContext, ShellExitOperation};
@ -437,8 +437,10 @@ fn execute_wasi(
.instantiate(&import_object)
.map_err(|e| format!("Can't instantiate WASI module: {:?}", e))?;
let start: wasmer_runtime::Func<(), ()> =
instance.func("_start").map_err(|e| format!("{:?}", e))?;
let start: Func<(), ()> = instance
.exports
.get("_start")
.map_err(|e| format!("{:?}", e))?;
#[cfg(feature = "managed")]
{
@ -506,7 +508,8 @@ fn execute_wasi(
eprintln!("WARNING: Invoking aribtrary functions with WASI is not officially supported in the WASI standard yet. Use this feature at your own risk!");
let args = options.parse_args(&module, invoke_fn)?;
let invoke_result = instance
.dyn_func(invoke_fn)
.exports
.get::<DynFunc>(invoke_fn)
.map_err(|e| format!("Invoke failed: {:?}", e))?
.call(&args)
.map_err(|e| format!("Calling invoke fn failed: {:?}", e))?;
@ -900,7 +903,8 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
};
let result = instance
.dyn_func(&invoke_fn)
.exports
.get::<DynFunc>(&invoke_fn)
.map_err(|e| format!("{:?}", e))?
.call(&args)
.map_err(|e| format!("{:?}", e))?;