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
use crate::*;
use js_sys::{Function, Object, Reflect, WebAssembly};
use wasm_bindgen::JsValue;

pub struct Instance {
    instance: WebAssembly::Instance,
    exports: JsValue,
    _closures: Vec<DropHandler>,
}

impl Instance {
    pub fn new(
        _store: impl AsContextMut,
        module: &Module,
        _: impl AsRef<[()]>,
    ) -> Result<Self, Error> {
        let imports = Object::new();
        Self::new_with_imports(module, &imports, vec![])
    }

    pub(crate) fn new_with_imports(
        module: &Module,
        imports: &Object,
        closures: Vec<DropHandler>,
    ) -> Result<Self, Error> {
        let instance = WebAssembly::Instance::new(&module.module, imports)?;
        let exports = Reflect::get(instance.as_ref(), &"exports".into())?;
        Ok(Self {
            instance,
            exports,
            _closures: closures, // TODO: not saving this object does not cause tests to fail. Why?
        })
    }

    pub fn get_typed_func<Params: ToJsParams, Results: FromJsResults>(
        &self,
        _store: impl AsContextMut,
        name: &str,
    ) -> Result<TypedFunc<Params, Results>, Error> {
        let function = Reflect::get(&self.exports, &name.into())?;

        if !function.is_function() {
            return Err(Error::ExportedFnNotFound(name.into()));
        }

        let function: Function = function.into();

        if function.length() != Params::number_of_args() {
            return Err(Error::IncorrectNumOfArgs(
                name.into(),
                Params::number_of_args(),
                function.length(),
            ));
        }

        Ok(TypedFunc::new(&self.instance, function))
    }
}