127 lines
3.0 KiB
Rust
Raw Normal View History

2019-01-12 16:24:17 -05:00
use crate::export::Export;
2019-01-10 22:59:57 -05:00
use hashbrown::{hash_map::Entry, HashMap};
use std::{
cell::{Ref, RefCell},
rc::Rc,
};
2019-01-10 22:59:57 -05:00
pub trait LikeNamespace {
2019-02-02 15:58:33 -08:00
fn get_export(&self, name: &str) -> Option<Export>;
2019-01-12 16:24:17 -05:00
}
pub trait IsExport {
2019-02-02 15:58:33 -08:00
fn to_export(&self) -> Export;
}
impl IsExport for Export {
2019-02-02 15:58:33 -08:00
fn to_export(&self) -> Export {
self.clone()
}
}
/// 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:
/// ```
2019-01-28 10:59:05 -08:00
/// # use wasmer_runtime_core::{imports, func};
/// # use wasmer_runtime_core::vm::Ctx;
/// let import_object = imports! {
/// "env" => {
/// "foo" => func!(foo),
/// },
/// };
///
/// fn foo(_: &mut Ctx, n: i32) -> i32 {
/// n
/// }
/// ```
pub struct ImportObject {
map: Rc<RefCell<HashMap<String, Box<dyn LikeNamespace>>>>,
2019-01-10 22:59:57 -05:00
}
impl ImportObject {
/// Create a new `ImportObject`.
2019-01-10 22:59:57 -05:00
pub fn new() -> Self {
Self {
map: Rc::new(RefCell::new(HashMap::new())),
2019-01-10 22:59:57 -05: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);
/// // ...
/// }
/// ```
pub fn register<S, N>(&mut self, name: S, namespace: N) -> Option<Box<dyn LikeNamespace>>
2019-01-12 22:02:19 -05:00
where
S: Into<String>,
N: LikeNamespace + 'static,
2019-01-12 22:02:19 -05:00
{
let mut map = self.map.borrow_mut();
match map.entry(name.into()) {
2019-01-12 16:24:17 -05:00
Entry::Vacant(empty) => {
empty.insert(Box::new(namespace));
None
}
Entry::Occupied(mut occupied) => Some(occupied.insert(Box::new(namespace))),
}
2019-01-10 22:59:57 -05:00
}
2019-01-12 16:24:17 -05:00
pub fn get_namespace(&self, namespace: &str) -> Option<Ref<dyn LikeNamespace + 'static>> {
let map_ref = self.map.borrow();
if map_ref.contains_key(namespace) {
Some(Ref::map(map_ref, |map| &*map[namespace]))
} else {
None
}
}
pub fn clone_ref(&self) -> Self {
Self {
map: Rc::clone(&self.map),
}
2019-01-10 22:59:57 -05:00
}
}
pub struct Namespace {
map: HashMap<String, Box<dyn IsExport>>,
}
impl Namespace {
pub fn new() -> Self {
Self {
map: HashMap::new(),
}
}
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))
}
}
impl LikeNamespace for Namespace {
2019-02-02 15:58:33 -08:00
fn get_export(&self, name: &str) -> Option<Export> {
self.map.get(name).map(|is_export| is_export.to_export())
}
2019-01-13 16:44:14 -05:00
}