1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//! The import module contains the implementation data structures and helper functions used to
//! manipulate and access a wasm module's imports including memories, tables, globals, and
//! functions.
use crate::export::Export;
use std::collections::VecDeque;
use std::collections::{hash_map::Entry, HashMap};
use std::{
    borrow::{Borrow, BorrowMut},
    ffi::c_void,
    sync::{Arc, Mutex},
};

/// This trait represents objects that act as a namespace for imports. For example, an `Instance`
/// or `ImportObject` could be considered namespaces that could provide imports to an instance.
pub trait LikeNamespace {
    /// Gets an export by name.
    fn get_export(&self, name: &str) -> Option<Export>;
    /// Gets all exports in the namespace.
    fn get_exports(&self) -> Vec<(String, Export)>;
    /// Maybe insert an `Export` by name into the namespace.
    fn maybe_insert(&mut self, name: &str, export: Export) -> Option<()>;
}

/// A trait that represents `Export` values.
pub trait IsExport {
    /// Gets self as `Export`.
    fn to_export(&self) -> Export;
}

impl IsExport for Export {
    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:
/// ```
/// # 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
/// }
/// ```
#[derive(Clone)]
pub struct ImportObject {
    map: Arc<Mutex<HashMap<String, Box<dyn LikeNamespace + Send>>>>,
    pub(crate) state_creator:
        Option<Arc<dyn Fn() -> (*mut c_void, fn(*mut c_void)) + Send + Sync + 'static>>,
    /// Allow missing functions to be generated and instantiation to continue when required
    /// functions are not provided.
    pub allow_missing_functions: bool,
}

impl ImportObject {
    /// Create a new `ImportObject`.
    pub fn new() -> Self {
        Self {
            map: Arc::new(Mutex::new(HashMap::new())),
            state_creator: None,
            allow_missing_functions: false,
        }
    }

    /// Create a new `ImportObject` which generates data from the provided state creator.
    pub fn new_with_data<F>(state_creator: F) -> Self
    where
        F: Fn() -> (*mut c_void, fn(*mut c_void)) + 'static + Send + Sync,
    {
        Self {
            map: Arc::new(Mutex::new(HashMap::new())),
            state_creator: Some(Arc::new(state_creator)),
            allow_missing_functions: false,
        }
    }

    pub(crate) fn call_state_creator(&self) -> Option<(*mut c_void, fn(*mut c_void))> {
        self.state_creator.as_ref().map(|state_gen| state_gen())
    }

    /// 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>>
    where
        S: Into<String>,
        N: LikeNamespace + Send + 'static,
    {
        let mut guard = self.map.lock().unwrap();
        let map = guard.borrow_mut();

        match map.entry(name.into()) {
            Entry::Vacant(empty) => {
                empty.insert(Box::new(namespace));
                None
            }
            Entry::Occupied(mut occupied) => Some(occupied.insert(Box::new(namespace))),
        }
    }

    /// Apply a function on the namespace if it exists
    /// If your function can fail, consider using `maybe_with_namespace`
    pub fn with_namespace<Func, InnerRet>(&self, namespace: &str, f: Func) -> Option<InnerRet>
    where
        Func: FnOnce(&(dyn LikeNamespace + Send)) -> InnerRet,
        InnerRet: Sized,
    {
        let guard = self.map.lock().unwrap();
        let map_ref = guard.borrow();
        if map_ref.contains_key(namespace) {
            Some(f(map_ref[namespace].as_ref()))
        } else {
            None
        }
    }

    /// The same as `with_namespace` but takes a function that may fail
    /// # Usage:
    /// ```
    /// # use wasmer_runtime_core::import::{ImportObject, LikeNamespace};
    /// # use wasmer_runtime_core::export::Export;
    /// fn get_export(imports: &ImportObject, namespace: &str, name: &str) -> Option<Export> {
    ///     imports.maybe_with_namespace(namespace, |ns| ns.get_export(name))
    /// }
    /// ```
    pub fn maybe_with_namespace<Func, InnerRet>(&self, namespace: &str, f: Func) -> Option<InnerRet>
    where
        Func: FnOnce(&(dyn LikeNamespace + Send)) -> Option<InnerRet>,
        InnerRet: Sized,
    {
        let guard = self.map.lock().unwrap();
        let map_ref = guard.borrow();
        map_ref
            .get(namespace)
            .map(|ns| ns.as_ref())
            .and_then(|ns| f(ns))
    }

    fn get_objects(&self) -> VecDeque<(String, String, Export)> {
        let mut out = VecDeque::new();
        let guard = self.map.lock().unwrap();
        let map = guard.borrow();
        for (name, ns) in map.iter() {
            for (id, exp) in ns.get_exports() {
                out.push_back((name.clone(), id, exp));
            }
        }
        out
    }

    /// Returns true if the ImportObject contains namespace with the provided name.
    pub fn contains_namespace(&self, name: &str) -> bool {
        self.map.lock().unwrap().borrow().contains_key(name)
    }
}

/// Iterator for an `ImportObject`'s exports.
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()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.elements.len();

        (len, Some(len))
    }
}

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 guard = self.map.lock().unwrap();
        let map = guard.borrow_mut();
        for (ns, id, exp) in iter.into_iter() {
            if let Some(like_ns) = map.get_mut(&ns) {
                like_ns.maybe_insert(&id, exp);
            } else {
                let mut new_ns = Namespace::new();
                new_ns.insert(id, exp);
                map.insert(ns, Box::new(new_ns));
            }
        }
    }
}

/// The top-level container for the two-level wasm imports
pub struct Namespace {
    map: HashMap<String, Box<dyn IsExport + Send>>,
}

impl Namespace {
    /// Create a new empty `Namespace`.
    pub fn new() -> Self {
        Self {
            map: HashMap::new(),
        }
    }

    /// Insert a new `Export` into the namespace with the given name.
    pub fn insert<S, E>(&mut self, name: S, export: E) -> Option<Box<dyn IsExport + Send>>
    where
        S: Into<String>,
        E: IsExport + Send + 'static,
    {
        self.map.insert(name.into(), Box::new(export))
    }

    /// Returns true if the `Namespace` contains the given name.
    pub fn contains_key<S>(&mut self, key: S) -> bool
    where
        S: Into<String>,
    {
        self.map.contains_key(&key.into())
    }
}

impl LikeNamespace for Namespace {
    fn get_export(&self, name: &str) -> Option<Export> {
        self.map.get(name).map(|is_export| is_export.to_export())
    }

    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<()> {
        self.map.insert(name.to_owned(), Box::new(export));
        Some(())
    }
}

#[cfg(test)]
mod test {
    use crate::export::Export;
    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 small_cat_export =
            imports1.maybe_with_namespace("cat", |cat_ns| cat_ns.get_export("small"));
        assert!(small_cat_export.is_some());

        let entries = imports1.maybe_with_namespace("dog", |dog_ns| {
            Some((dog_ns.get_export("happy")?, dog_ns.get_export("small")?))
        });
        assert!(entries.is_some());
    }

    #[test]
    fn extending_conflict_overwrites() {
        let mut imports1 = imports! {
            "dog" => {
                "happy" => Global::new(Value::I32(0)),
            },
        };

        let imports2 = imports! {
            "dog" => {
                "happy" => Global::new(Value::I32(4)),
            },
        };

        imports1.extend(imports2);
        let happy_dog_entry = imports1
            .maybe_with_namespace("dog", |dog_ns| dog_ns.get_export("happy"))
            .unwrap();

        assert!(if let Export::Global(happy_dog_global) = happy_dog_entry {
            happy_dog_global.get() == Value::I32(4)
        } else {
            false
        });
    }
}