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-03-23 00:11:30 +00:00
|
|
|
use std::collections::VecDeque;
|
2019-03-04 22:41:56 +00:00
|
|
|
use std::{
|
|
|
|
cell::{Ref, RefCell},
|
|
|
|
rc::Rc,
|
|
|
|
};
|
2019-01-11 03:59:57 +00:00
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
pub trait LikeNamespace {
|
2019-02-02 23:58:33 +00:00
|
|
|
fn get_export(&self, name: &str) -> Option<Export>;
|
2019-03-23 00:11:30 +00:00
|
|
|
fn get_exports(&self) -> Vec<(String, Export)>;
|
|
|
|
fn maybe_insert(&mut self, name: &str, export: Export) -> Option<()>;
|
2019-01-12 21:24:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 23:28:54 +00:00
|
|
|
pub trait IsExport {
|
2019-02-02 23:58:33 +00:00
|
|
|
fn to_export(&self) -> Export;
|
2019-01-25 23:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IsExport for Export {
|
2019-02-02 23:58:33 +00:00
|
|
|
fn to_export(&self) -> Export {
|
2019-01-25 23:28:54 +00:00
|
|
|
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:
|
|
|
|
/// ```
|
2019-01-28 18:59:05 +00:00
|
|
|
/// # use wasmer_runtime_core::{imports, func};
|
2019-01-23 20:34:15 +00:00
|
|
|
/// # use wasmer_runtime_core::vm::Ctx;
|
|
|
|
/// let import_object = imports! {
|
|
|
|
/// "env" => {
|
2019-02-02 23:28:50 +00:00
|
|
|
/// "foo" => func!(foo),
|
2019-01-23 20:34:15 +00:00
|
|
|
/// },
|
|
|
|
/// };
|
|
|
|
///
|
2019-02-09 21:33:22 +00:00
|
|
|
/// fn foo(_: &mut Ctx, n: i32) -> i32 {
|
2019-01-23 20:34:15 +00:00
|
|
|
/// n
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-01-21 22:43:04 +00:00
|
|
|
pub struct ImportObject {
|
2019-03-04 22:41:56 +00:00
|
|
|
map: Rc<RefCell<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 {
|
2019-03-04 22:41:56 +00:00
|
|
|
map: Rc::new(RefCell::new(HashMap::new())),
|
2019-01-11 03:59:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-04 22:41:56 +00:00
|
|
|
let mut map = self.map.borrow_mut();
|
|
|
|
|
|
|
|
match map.entry(name.into()) {
|
2019-01-12 21:24:17 +00:00
|
|
|
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-03-04 22:41:56 +00: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-11 03:59:57 +00:00
|
|
|
}
|
2019-03-23 00:11:30 +00:00
|
|
|
|
|
|
|
fn get_objects(&self) -> VecDeque<(String, String, Export)> {
|
|
|
|
let mut out = VecDeque::new();
|
|
|
|
for (name, ns) in self.map.borrow().iter() {
|
|
|
|
for (id, exp) in ns.get_exports() {
|
|
|
|
out.push_back((name.clone(), id, exp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ImportObjectIterator {
|
|
|
|
elements: VecDeque<(String, String, Export)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for ImportObjectIterator {
|
|
|
|
type Item = (String, String, Export);
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.elements.pop_front()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoIterator for ImportObject {
|
|
|
|
type IntoIter = ImportObjectIterator;
|
|
|
|
type Item = (String, String, Export);
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
ImportObjectIterator {
|
|
|
|
elements: self.get_objects(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Extend<(String, String, Export)> for ImportObject {
|
|
|
|
fn extend<T: IntoIterator<Item = (String, String, Export)>>(&mut self, iter: T) {
|
|
|
|
let mut map = self.map.borrow_mut();
|
|
|
|
for (ns, id, exp) in iter.into_iter() {
|
|
|
|
if let Some(like_ns) = map.get_mut(&ns) {
|
2019-03-23 00:29:09 +00:00
|
|
|
like_ns.maybe_insert(&id, exp).expect(&format!(
|
|
|
|
"Insert failed. Duplicate name {} found in namespace {}",
|
|
|
|
id, ns
|
|
|
|
));
|
2019-03-23 00:11:30 +00:00
|
|
|
} else {
|
|
|
|
let mut new_ns = Namespace::new();
|
|
|
|
new_ns.insert(id, exp);
|
|
|
|
map.insert(ns, Box::new(new_ns));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-02-02 23:58:33 +00:00
|
|
|
fn get_export(&self, name: &str) -> Option<Export> {
|
|
|
|
self.map.get(name).map(|is_export| is_export.to_export())
|
2019-01-13 04:48:21 +00:00
|
|
|
}
|
2019-03-23 00:11:30 +00:00
|
|
|
|
|
|
|
fn get_exports(&self) -> Vec<(String, Export)> {
|
|
|
|
self.map
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| (k.clone(), v.to_export()))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maybe_insert(&mut self, name: &str, export: Export) -> Option<()> {
|
|
|
|
if self.map.contains_key(name) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.map.insert(name.to_owned(), Box::new(export));
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::global::Global;
|
|
|
|
use crate::types::Value;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extending_works() {
|
|
|
|
let mut imports1 = imports! {
|
|
|
|
"dog" => {
|
|
|
|
"happy" => Global::new(Value::I32(0)),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let imports2 = imports! {
|
|
|
|
"dog" => {
|
|
|
|
"small" => Global::new(Value::I32(2)),
|
|
|
|
},
|
|
|
|
"cat" => {
|
|
|
|
"small" => Global::new(Value::I32(3)),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
imports1.extend(imports2);
|
|
|
|
|
|
|
|
let cat_ns = imports1.get_namespace("cat").unwrap();
|
|
|
|
assert!(cat_ns.get_export("small").is_some());
|
|
|
|
|
|
|
|
let dog_ns = imports1.get_namespace("dog").unwrap();
|
|
|
|
assert!(dog_ns.get_export("happy").is_some());
|
|
|
|
assert!(dog_ns.get_export("small").is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn extending_conflict_panics() {
|
|
|
|
let mut imports1 = imports! {
|
|
|
|
"dog" => {
|
|
|
|
"happy" => Global::new(Value::I32(0)),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let imports2 = imports! {
|
|
|
|
"dog" => {
|
|
|
|
"happy" => Global::new(Value::I32(4)),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
imports1.extend(imports2);
|
|
|
|
}
|
2019-01-13 21:44:14 +00:00
|
|
|
}
|