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
pub(crate) use self::builder::InstanceEntityBuilder;
use self::cache::{AnyHandleAndEntity, HandleAndEntity};
pub use self::{
entity::InstanceEntity,
exports::{Export, ExportsIter, Extern, ExternType},
layout::{DataAddr, ElemAddr, FuncAddr, GlobalAddr, InstanceLayout, MemoryAddr, TableAddr},
thin_ptr::ThinPtr,
};
use crate::{
AsContext,
AsContextMut,
Error,
Func,
Global,
Memory,
Module,
StoreContext,
Table,
TypedFunc,
WasmParams,
WasmResults,
func::FuncError,
store::Stored,
};
mod builder;
mod cache;
mod entity;
mod exports;
mod handle;
mod layout;
mod thin_ptr;
#[cfg(test)]
mod tests;
define_handle! {
/// An instantiated WebAssembly [`Module`].
///
/// This type represents an instantiation of a [`Module`].
/// It primarily allows to access its [`exports`](Instance::exports)
/// to call functions, get or set globals, read or write memory, etc.
///
/// When interacting with any Wasm code you will want to create an
/// [`Instance`] in order to execute anything.
///
/// Instances are owned by a [`Store`](crate::Store).
/// Create new instances using [`Linker::instantiate_and_start`](crate::Linker::instantiate_and_start).
struct Instance(u32, Stored) => InstanceEntity;
}
impl Instance {
/// Creates a new [`Instance`] from the pre-compiled [`Module`] and the list of `imports`.
///
/// Uses the official [Wasm instantiation procedure] in order to resolve and type-check
/// the provided `imports` and match them with the required imports of the [`Module`].
///
/// # Note
///
/// - This function intentionally is rather low-level for [`Instance`] creation.
/// Please use the [`Linker`](crate::Linker) type for a more high-level API for Wasm
/// module instantiation with name-based resolution.
/// - Wasm module instantiation implies running the Wasm `start` function which is _not_
/// to be confused with WASI's `_start` function.
///
/// # Usage
///
/// The `imports` are intended to correspond 1:1 with the required imports as returned by [`Module::imports`].
/// For each import type returned by [`Module::imports`], create an [`Extern`] which corresponds to that type.
/// Collect the [`Extern`] values created this way into a list and pass them to this function.
///
/// # Errors
///
/// - If the number of provided imports does not match the number of imports required by the [`Module`].
/// - If the type of any provided [`Extern`] does not match the corresponding required [`ExternType`].
/// - If the `start` function, that is run at the end of the Wasm module instantiation, traps.
/// - If Wasm module or instance related resource limits are exceeded.
///
/// # Panics
///
/// If any [`Extern`] does not originate from the provided `store`.
///
/// [Wasm instantiation procedure]: https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation
pub fn new(
mut store: impl AsContextMut,
module: &Module,
imports: &[Extern],
) -> Result<Instance, Error> {
let instance = Module::instantiate(module, &mut store, imports.iter().cloned())?;
Ok(instance)
}
/// Returns the value exported to the given `name` if any.
///
/// # Panics
///
/// Panics if `store` does not own this [`Instance`].
pub fn get_export(&self, store: impl AsContext, name: &str) -> Option<Extern> {
store
.as_context()
.store
.inner
.resolve_instance(self)
.get_export(name)
}
/// Looks up an exported [`Func`] value by `name`.
///
/// Returns `None` if there was no export named `name`,
/// or if there was but it wasn’t a function.
///
/// # Panics
///
/// If `store` does not own this [`Instance`].
pub fn get_func(&self, store: impl AsContext, name: &str) -> Option<Func> {
self.get_export(store, name)?.into_func()
}
/// Looks up an exported [`Func`] value by `name`.
///
/// Returns `None` if there was no export named `name`,
/// or if there was but it wasn’t a function.
///
/// # Errors
///
/// - If there is no export named `name`.
/// - If there is no exported function named `name`.
/// - If `Params` or `Results` do not match the exported function type.
///
/// # Panics
///
/// If `store` does not own this [`Instance`].
pub fn get_typed_func<Params, Results>(
&self,
store: impl AsContext,
name: &str,
) -> Result<TypedFunc<Params, Results>, Error>
where
Params: WasmParams,
Results: WasmResults,
{
self.get_export(&store, name)
.and_then(Extern::into_func)
.ok_or_else(|| Error::from(FuncError::ExportedFuncNotFound))?
.typed::<Params, Results>(store)
}
/// Looks up an exported [`Global`] value by `name`.
///
/// Returns `None` if there was no export named `name`,
/// or if there was but it wasn’t a global variable.
///
/// # Panics
///
/// If `store` does not own this [`Instance`].
pub fn get_global(&self, store: impl AsContext, name: &str) -> Option<Global> {
self.get_export(store, name)?.into_global()
}
/// Looks up an exported [`Table`] value by `name`.
///
/// Returns `None` if there was no export named `name`,
/// or if there was but it wasn’t a table.
///
/// # Panics
///
/// If `store` does not own this [`Instance`].
pub fn get_table(&self, store: impl AsContext, name: &str) -> Option<Table> {
self.get_export(store, name)?.into_table()
}
/// Looks up an exported [`Memory`] value by `name`.
///
/// Returns `None` if there was no export named `name`,
/// or if there was but it wasn’t a memory.
///
/// # Panics
///
/// If `store` does not own this [`Instance`].
pub fn get_memory(&self, store: impl AsContext, name: &str) -> Option<Memory> {
self.get_export(store, name)?.into_memory()
}
/// Returns an iterator over the exports of the [`Instance`].
///
/// The order of the yielded exports is not specified.
///
/// # Panics
///
/// Panics if `store` does not own this [`Instance`].
pub fn exports<'ctx, T: 'ctx>(
&self,
store: impl Into<StoreContext<'ctx, T>>,
) -> ExportsIter<'ctx> {
store.into().store.inner.resolve_instance(self).exports()
}
}