Files
wasmtime/crates/api/tests/import_calling_export.rs
Alex Crichton fb60a21930 Reduce number of crates needed for Config usage
This commit is an attempt to reduce the number of crates necessary to
link to when using `wasmtime::Config` in "default mode" or with only one
or two tweaks. The change moves to a builder-style pattern for `Config`
to only require importing crates as necessary if you configure a
particular setting. This then also propagates that change to `Context`
as well by taking a `Config` instead of requiring that all arguments are
passed alone.
2019-11-13 08:32:13 -08:00

71 lines
1.8 KiB
Rust

extern crate alloc;
use alloc::rc::Rc;
use core::cell::{Ref, RefCell};
use std::fs::read;
use wasmtime_api::*;
#[test]
fn test_import_calling_export() {
struct Callback {
pub other: RefCell<Option<HostRef<Func>>>,
}
impl Callable for Callback {
fn call(&self, _params: &[Val], _results: &mut [Val]) -> Result<(), HostRef<Trap>> {
self.other
.borrow()
.as_ref()
.expect("expected a function ref")
.borrow()
.call(&[])
.expect("expected function not to trap");
Ok(())
}
}
let engine = HostRef::new(Engine::default());
let store = HostRef::new(Store::new(&engine));
let module = HostRef::new(
Module::new(
&store,
&read("tests/import_calling_export.wasm").expect("failed to read wasm file"),
)
.expect("failed to create module"),
);
let callback = Rc::new(Callback {
other: RefCell::new(None),
});
let callback_func = HostRef::new(Func::new(
&store,
FuncType::new(Box::new([]), Box::new([])),
callback.clone(),
));
let imports = vec![callback_func.into()];
let instance = HostRef::new(
Instance::new(&store, &module, imports.as_slice()).expect("failed to instantiate module"),
);
let exports = Ref::map(instance.borrow(), |instance| instance.exports());
assert!(!exports.is_empty());
let run_func = exports[0]
.func()
.expect("expected a run func in the module");
*callback.other.borrow_mut() = Some(
exports[1]
.func()
.expect("expected an other func in the module")
.clone(),
);
run_func
.borrow()
.call(&[])
.expect("expected function not to trap");
}