Convert usages of Instance::{func,dyn_func} to exports.get

This commit is contained in:
Mark McCaskey
2020-03-26 18:13:45 -07:00
parent 2fb8f8197b
commit 3eff8c1973
11 changed files with 24 additions and 17 deletions

View File

@ -190,7 +190,7 @@ fn bench_metering(c: &mut Criterion) {
let module = compile_with(&wasm_binary, &compiler).unwrap(); let module = compile_with(&wasm_binary, &compiler).unwrap();
let import_object = imports! {}; let import_object = imports! {};
let instance = module.instantiate(&import_object).unwrap(); let instance = module.instantiate(&import_object).unwrap();
let add_to: Func<(i32, i32), i32> = instance.func("add_to").unwrap(); let add_to: Func<(i32, i32), i32> = instance.exports.get("add_to").unwrap();
b.iter(|| black_box(add_to.call(100, 4))) b.iter(|| black_box(add_to.call(100, 4)))
}) })
.with_function("Gas Metering", |b| { .with_function("Gas Metering", |b| {
@ -203,7 +203,7 @@ fn bench_metering(c: &mut Criterion) {
}, },
}; };
let gas_instance = gas_module.instantiate(&gas_import_object).unwrap(); let gas_instance = gas_module.instantiate(&gas_import_object).unwrap();
let gas_add_to: Func<(i32, i32), i32> = gas_instance.func("add_to").unwrap(); let gas_add_to: Func<(i32, i32), i32> = gas_instance.exports.get("add_to").unwrap();
b.iter(|| black_box(gas_add_to.call(100, 4))) b.iter(|| black_box(gas_add_to.call(100, 4)))
}) })
.with_function("Built-in Metering", |b| { .with_function("Built-in Metering", |b| {
@ -215,7 +215,8 @@ fn bench_metering(c: &mut Criterion) {
.instantiate(&metering_import_object) .instantiate(&metering_import_object)
.unwrap(); .unwrap();
metering::set_points_used(&mut metering_instance, 0u64); metering::set_points_used(&mut metering_instance, 0u64);
let metering_add_to: Func<(i32, i32), i32> = metering_instance.func("add_to").unwrap(); let metering_add_to: Func<(i32, i32), i32> =
metering_instance.exports.get("add_to").unwrap();
b.iter(|| black_box(metering_add_to.call(100, 4))) b.iter(|| black_box(metering_add_to.call(100, 4)))
}), }),
); );

View File

@ -103,7 +103,7 @@ mod tests {
set_points_used(&mut instance, 0u64); set_points_used(&mut instance, 0u64);
let add_to: Func<(i32, i32), i32> = instance.func("add_to").unwrap(); let add_to: Func<(i32, i32), i32> = instance.exports.get("add_to").unwrap();
let cv_pushed = if let Some(msm) = instance.module.runnable_module.get_module_state_map() { let cv_pushed = if let Some(msm) = instance.module.runnable_module.get_module_state_map() {
push_code_version(CodeVersion { push_code_version(CodeVersion {
@ -145,7 +145,7 @@ mod tests {
set_points_used(&mut instance, 0u64); set_points_used(&mut instance, 0u64);
let add_to: Func<(i32, i32), i32> = instance.func("add_to").unwrap(); let add_to: Func<(i32, i32), i32> = instance.exports.get("add_to").unwrap();
let cv_pushed = if let Some(msm) = instance.module.runnable_module.get_module_state_map() { let cv_pushed = if let Some(msm) = instance.module.runnable_module.get_module_state_map() {
push_code_version(CodeVersion { push_code_version(CodeVersion {

View File

@ -36,14 +36,14 @@ fn new_api_works() {
assert_eq!(double.call(5).unwrap(), 10); assert_eq!(double.call(5).unwrap(), 10);
let add_one: DynFunc = instance.exports.get("add_one").unwrap(); let add_one: DynFunc = instance.exports.get("add_one").unwrap();
assert_eq!(add_one.call(&[Value::I32(5)]).unwrap(), &[Value::I32(6)]); assert_eq!(add_one.call(&[Value::I32(5)]).unwrap(), &[Value::I32(6)]);
let add_one_memory: Option<DynFunc> = instance.exports.get("my_global"); let add_one_memory: Result<DynFunc, _> = instance.exports.get("my_global");
assert!(add_one_memory.is_none()); assert!(add_one_memory.is_err());
} }
macro_rules! call_and_assert { macro_rules! call_and_assert {
($instance:ident, $function:ident( $( $inputs:ty ),* ) -> $output:ty, ( $( $arguments:expr ),* ) == $expected_value:expr) => { ($instance:ident, $function:ident( $( $inputs:ty ),* ) -> $output:ty, ( $( $arguments:expr ),* ) == $expected_value:expr) => {
#[allow(unused_parens)] #[allow(unused_parens)]
let $function: Func<( $( $inputs ),* ), $output> = $instance.func(stringify!($function)).expect(concat!("Failed to get the `", stringify!($function), "` export function.")); let $function: Func<( $( $inputs ),* ), $output> = $instance.exports.get(stringify!($function)).expect(concat!("Failed to get the `", stringify!($function), "` export function."));
let result = $function.call( $( $arguments ),* ); let result = $function.call( $( $arguments ),* );

View File

@ -934,6 +934,10 @@ pub struct Exports {
module: Arc<ModuleInner>, module: Arc<ModuleInner>,
} }
// this is safe because the lifetime of `Exports` is tied to `Instance` and
// `*const InstanceInner` comes from a `Pin<Box<InstanceInner>>`
unsafe impl Send for Exports {}
impl Exports { impl Exports {
/// Get an export. /// Get an export.
/// ///

View File

@ -71,6 +71,7 @@ static WASM: &'static [u8] = &[
use wasmer_runtime::{ use wasmer_runtime::{
instantiate, instantiate,
DynFunc,
Value, Value,
imports, imports,
error, error,
@ -83,7 +84,8 @@ fn main() -> error::Result<()> {
let instance = instantiate(WASM, &import_object)?; let instance = instantiate(WASM, &import_object)?;
let values = instance let values = instance
.dyn_func("add_one")? .exports
.get::<DynFunc>("add_one")?
.call(&[Value::I32(42)])?; .call(&[Value::I32(42)])?;
assert_eq!(values[0], Value::I32(43)); assert_eq!(values[0], Value::I32(43));

View File

@ -4,7 +4,7 @@ use criterion::Criterion;
use tempfile::tempdir; use tempfile::tempdir;
use wasmer_runtime::{ use wasmer_runtime::{
cache::{Cache, FileSystemCache, WasmHash}, cache::{Cache, FileSystemCache, WasmHash},
compile, func, imports, instantiate, validate, compile, func, imports, instantiate, validate, Func,
}; };
use wasmer_runtime_core::vm::Ctx; use wasmer_runtime_core::vm::Ctx;
@ -71,7 +71,7 @@ fn calling_fn_benchmark(c: &mut Criterion) {
); );
let instance = instantiate(SIMPLE_WASM, &imports).unwrap(); let instance = instantiate(SIMPLE_WASM, &imports).unwrap();
c.bench_function("calling fn", move |b| { c.bench_function("calling fn", move |b| {
let entry_point = instance.func::<i32, i32>("plugin_entrypoint").unwrap(); let entry_point: Func<i32, i32> = instance.exports.get("plugin_entrypoint").unwrap();
b.iter(|| entry_point.call(2).unwrap()) b.iter(|| entry_point.call(2).unwrap())
}); });
} }

View File

@ -64,7 +64,7 @@ fn main() -> Result<(), error::Error> {
}, },
})?; })?;
let foo: Func<(), i32> = instance.func("dbz")?; let foo: Func<(), i32> = instance.exports.get("dbz")?;
let result = foo.call(); let result = foo.call();

View File

@ -187,7 +187,7 @@ mod tests {
let import_object = imports! {}; let import_object = imports! {};
let instance = cached_module.instantiate(&import_object).unwrap(); let instance = cached_module.instantiate(&import_object).unwrap();
let add_one: Func<i32, i32> = instance.func("add_one").unwrap(); let add_one: Func<i32, i32> = instance.exports.get("add_one").unwrap();
let value = add_one.call(42).unwrap(); let value = add_one.call(42).unwrap();

View File

@ -66,7 +66,7 @@
//! //!
//! let mut instance = instantiate(WASM, &import_object)?; //! let mut instance = instantiate(WASM, &import_object)?;
//! //!
//! let add_one: Func<i32, i32> = instance.func("add_one")?; //! let add_one: Func<i32, i32> = instance.exports.get("add_one")?;
//! //!
//! let value = add_one.call(42)?; //! let value = add_one.call(42)?;
//! //!

View File

@ -36,7 +36,7 @@ fn error_propagation() {
}) })
.unwrap(); .unwrap();
let foo: Func<(), ()> = instance.func("call_err").unwrap(); let foo: Func<(), ()> = instance.exports.get("call_err").unwrap();
let result = foo.call(); let result = foo.call();

View File

@ -35,7 +35,7 @@ fn serializing_works() {
let state_bytes = { let state_bytes = {
let instance = module.instantiate(&import_object).unwrap(); let instance = module.instantiate(&import_object).unwrap();
let start: Func<(), ()> = instance.func("_start").unwrap(); let start: Func<(), ()> = instance.exports.get("_start").unwrap();
start.call().unwrap(); start.call().unwrap();
let state = get_wasi_state(instance.context()); let state = get_wasi_state(instance.context());
@ -52,7 +52,7 @@ fn serializing_works() {
instance.context_mut().data = Box::into_raw(wasi_state) as *mut c_void; instance.context_mut().data = Box::into_raw(wasi_state) as *mut c_void;
let second_entry: Func<(), i32> = instance.func("second_entry").unwrap(); let second_entry: Func<(), i32> = instance.exports.get("second_entry").unwrap();
let result = second_entry.call().unwrap(); let result = second_entry.call().unwrap();
assert_eq!(result, true as i32); assert_eq!(result, true as i32);
} }