2019-01-12 21:24:17 +00:00
|
|
|
use crate::export::Export;
|
2019-01-11 03:59:57 +00:00
|
|
|
use hashbrown::{hash_map::Entry, HashMap};
|
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
pub trait LikeNamespace {
|
2019-01-13 21:44:14 +00:00
|
|
|
fn get_export(&mut self, name: &str) -> Option<Export>;
|
2019-01-12 21:24:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 23:28:54 +00:00
|
|
|
pub trait IsExport {
|
|
|
|
fn to_export(&mut self) -> Export;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IsExport for Export {
|
|
|
|
fn to_export(&mut self) -> Export {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 20:34:15 +00:00
|
|
|
/// All of the import data used when instantiating.
|
|
|
|
///
|
|
|
|
/// It's suggested that you use the [`imports!`] macro
|
|
|
|
/// instead of creating an `ImportObject` by hand.
|
|
|
|
///
|
|
|
|
/// [`imports!`]: macro.imports.html
|
|
|
|
///
|
|
|
|
/// # Usage:
|
|
|
|
/// ```
|
|
|
|
/// # use wasmer_runtime_core::imports;
|
|
|
|
/// # use wasmer_runtime_core::vm::Ctx;
|
|
|
|
/// let import_object = imports! {
|
|
|
|
/// "env" => {
|
|
|
|
/// "foo" => foo<[i32] -> [i32]>,
|
|
|
|
/// },
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// extern fn foo(n: i32, _: &mut Ctx) -> i32 {
|
|
|
|
/// n
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-01-21 22:43:04 +00:00
|
|
|
pub struct ImportObject {
|
|
|
|
map: HashMap<String, Box<dyn LikeNamespace>>,
|
2019-01-11 03:59:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
impl ImportObject {
|
2019-01-23 20:34:15 +00:00
|
|
|
/// Create a new `ImportObject`.
|
2019-01-11 03:59:57 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
map: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 20:34:15 +00:00
|
|
|
/// Register anything that implements `LikeNamespace` as a namespace.
|
|
|
|
///
|
|
|
|
/// # Usage:
|
|
|
|
/// ```
|
|
|
|
/// # use wasmer_runtime_core::Instance;
|
|
|
|
/// # use wasmer_runtime_core::import::{ImportObject, Namespace};
|
|
|
|
/// fn register(instance: Instance, namespace: Namespace) {
|
|
|
|
/// let mut import_object = ImportObject::new();
|
|
|
|
///
|
|
|
|
/// import_object.register("namespace0", instance);
|
|
|
|
/// import_object.register("namespace1", namespace);
|
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-01-21 22:43:04 +00:00
|
|
|
pub fn register<S, N>(&mut self, name: S, namespace: N) -> Option<Box<dyn LikeNamespace>>
|
2019-01-13 03:02:19 +00:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
2019-01-21 22:43:04 +00:00
|
|
|
N: LikeNamespace + 'static,
|
2019-01-13 03:02:19 +00:00
|
|
|
{
|
2019-01-12 21:24:17 +00:00
|
|
|
match self.map.entry(name.into()) {
|
|
|
|
Entry::Vacant(empty) => {
|
|
|
|
empty.insert(Box::new(namespace));
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Entry::Occupied(mut occupied) => Some(occupied.insert(Box::new(namespace))),
|
|
|
|
}
|
2019-01-11 03:59:57 +00:00
|
|
|
}
|
2019-01-12 21:24:17 +00:00
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
pub fn get_namespace(&mut self, namespace: &str) -> Option<&mut (dyn LikeNamespace + 'static)> {
|
2019-01-13 21:44:14 +00:00
|
|
|
self.map
|
|
|
|
.get_mut(namespace)
|
|
|
|
.map(|namespace| &mut **namespace)
|
2019-01-11 03:59:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-13 04:48:21 +00:00
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
pub struct Namespace {
|
2019-01-25 23:28:54 +00:00
|
|
|
map: HashMap<String, Box<dyn IsExport>>,
|
2019-01-13 04:48:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
impl Namespace {
|
2019-01-13 04:48:21 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
map: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 23:28:54 +00:00
|
|
|
pub fn insert<S, E>(&mut self, name: S, export: E) -> Option<Box<dyn IsExport>>
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
E: IsExport + 'static,
|
|
|
|
{
|
|
|
|
self.map.insert(name.into(), Box::new(export))
|
2019-01-13 04:48:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
impl LikeNamespace for Namespace {
|
2019-01-13 21:44:14 +00:00
|
|
|
fn get_export(&mut self, name: &str) -> Option<Export> {
|
2019-01-25 23:28:54 +00:00
|
|
|
self.map
|
|
|
|
.get_mut(name)
|
|
|
|
.map(|is_export| is_export.to_export())
|
2019-01-13 04:48:21 +00:00
|
|
|
}
|
2019-01-13 21:44:14 +00:00
|
|
|
}
|