linera_wasmer/
instance.rs

1use crate::exports::Exports;
2use crate::module::Module;
3use crate::{Extern, InstantiationError};
4use std::fmt;
5
6use crate::imports::Imports;
7use crate::store::AsStoreMut;
8
9#[cfg(feature = "js")]
10use crate::js::instance as instance_imp;
11#[cfg(feature = "jsc")]
12use crate::jsc::instance as instance_imp;
13#[cfg(feature = "sys")]
14use crate::sys::instance as instance_imp;
15
16/// A WebAssembly Instance is a stateful, executable
17/// instance of a WebAssembly [`Module`].
18///
19/// Instance objects contain all the exported WebAssembly
20/// functions, memories, tables and globals that allow
21/// interacting with WebAssembly.
22///
23/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#module-instances>
24#[derive(Clone, PartialEq, Eq)]
25pub struct Instance {
26    pub(crate) _inner: instance_imp::Instance,
27    pub(crate) module: Module,
28    /// The exports for an instance.
29    pub exports: Exports,
30}
31
32impl Instance {
33    /// Creates a new `Instance` from a WebAssembly [`Module`] and a
34    /// set of imports using [`Imports`] or the [`imports!`] macro helper.
35    ///
36    /// [`imports!`]: crate::imports!
37    /// [`Imports!`]: crate::Imports!
38    ///
39    /// ```
40    /// # use wasmer::{imports, Store, Module, Global, Value, Instance};
41    /// # use wasmer::FunctionEnv;
42    /// # fn main() -> anyhow::Result<()> {
43    /// let mut store = Store::default();
44    /// let env = FunctionEnv::new(&mut store, ());
45    /// let module = Module::new(&store, "(module)")?;
46    /// let imports = imports!{
47    ///   "host" => {
48    ///     "var" => Global::new(&mut store, Value::I32(2))
49    ///   }
50    /// };
51    /// let instance = Instance::new(&mut store, &module, &imports)?;
52    /// # Ok(())
53    /// # }
54    /// ```
55    ///
56    /// ## Errors
57    ///
58    /// The function can return [`InstantiationError`]s.
59    ///
60    /// Those are, as defined by the spec:
61    ///  * Link errors that happen when plugging the imports into the instance
62    ///  * Runtime errors that happen when running the module `start` function.
63    #[allow(clippy::result_large_err)]
64    pub fn new(
65        store: &mut impl AsStoreMut,
66        module: &Module,
67        imports: &Imports,
68    ) -> Result<Self, InstantiationError> {
69        let (_inner, exports) = instance_imp::Instance::new(store, module, imports)?;
70        Ok(Self {
71            _inner,
72            module: module.clone(),
73            exports,
74        })
75    }
76
77    /// Creates a new `Instance` from a WebAssembly [`Module`] and a
78    /// vector of imports.
79    ///
80    /// ## Errors
81    ///
82    /// The function can return [`InstantiationError`]s.
83    ///
84    /// Those are, as defined by the spec:
85    ///  * Link errors that happen when plugging the imports into the instance
86    ///  * Runtime errors that happen when running the module `start` function.
87    #[allow(clippy::result_large_err)]
88    pub fn new_by_index(
89        store: &mut impl AsStoreMut,
90        module: &Module,
91        externs: &[Extern],
92    ) -> Result<Self, InstantiationError> {
93        let (_inner, exports) = instance_imp::Instance::new_by_index(store, module, externs)?;
94        Ok(Self {
95            _inner,
96            module: module.clone(),
97            exports,
98        })
99    }
100
101    /// Gets the [`Module`] associated with this instance.
102    pub fn module(&self) -> &Module {
103        &self.module
104    }
105}
106
107impl fmt::Debug for Instance {
108    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
109        f.debug_struct("Instance")
110            .field("exports", &self.exports)
111            .finish()
112    }
113}