1
+ //! Spin core execution engine
2
+ //!
3
+ //! This crate provides low-level Wasm and WASI functionality required by Spin.
4
+ //! Most of this functionality consists of wrappers around [`wasmtime`] and
5
+ //! [`wasmtime_wasi`] that narrows the flexibility of `wasmtime` to the set of
6
+ //! features used by Spin (such as only supporting `wasmtime`'s async calling style).
7
+
8
+ #![ deny( missing_docs) ]
9
+
1
10
mod host_component;
2
11
mod io;
3
12
mod limits;
@@ -14,8 +23,12 @@ pub use wasmtime::{self, Instance, Module, Trap};
14
23
use self :: host_component:: { HostComponents , HostComponentsBuilder } ;
15
24
16
25
pub use host_component:: { HostComponent , HostComponentDataHandle , HostComponentsData } ;
26
+ pub use io:: OutputBuffer ;
17
27
pub use store:: { Store , StoreBuilder } ;
18
28
29
+ /// Global configuration for `EngineBuilder`.
30
+ ///
31
+ /// This is currently only used for advanced (undocumented) use cases.
19
32
pub struct Config {
20
33
inner : wasmtime:: Config ,
21
34
}
@@ -37,6 +50,7 @@ impl Default for Config {
37
50
}
38
51
}
39
52
53
+ /// Host state data associated with individual [Store]s and [Instance]s.
40
54
pub struct Data < T > {
41
55
inner : T ,
42
56
wasi : WasiCtx ,
@@ -56,8 +70,12 @@ impl<T> AsMut<T> for Data<T> {
56
70
}
57
71
}
58
72
73
+ /// An alias for [`wasmtime::Linker`] specialized to [`Data`].
59
74
pub type Linker < T > = wasmtime:: Linker < Data < T > > ;
60
75
76
+ /// A builder interface for configuring a new [`Engine`].
77
+ ///
78
+ /// A new [`EngineBuilder`] can be obtained with [`Engine::builder`].
61
79
pub struct EngineBuilder < T > {
62
80
engine : wasmtime:: Engine ,
63
81
linker : Linker < T > ,
@@ -78,13 +96,30 @@ impl<T: Send + Sync> EngineBuilder<T> {
78
96
} )
79
97
}
80
98
99
+ /// Adds definition(s) to the built [`Engine`].
100
+ ///
101
+ /// This method's signature is meant to be used with
102
+ /// [`wit-bindgen`](https://github.com/bytecodealliance/wit-bindgen)'s
103
+ /// generated `add_to_linker` functions, e.g.:
104
+ ///
105
+ /// ```ignore
106
+ /// wit_bindgen_wasmtime::import!({paths: ["my-interface.wit"], async: *});
107
+ /// // ...
108
+ /// let mut builder: EngineBuilder<my_interface::MyInterfaceData> = Engine::builder();
109
+ /// builder.link_import(my_interface::MyInterface::add_to_linker)?;
110
+ /// ```
81
111
pub fn link_import (
82
112
& mut self ,
83
113
f : impl FnOnce ( & mut Linker < T > , fn ( & mut Data < T > ) -> & mut T ) -> Result < ( ) > ,
84
114
) -> Result < ( ) > {
85
115
f ( & mut self . linker , Data :: as_mut)
86
116
}
87
117
118
+ /// Adds a [`HostComponent`] to the built [`Engine`].
119
+ ///
120
+ /// Returns a [`HostComponentDataHandle`] which can be passed to
121
+ /// [`HostComponentsData`] to access or set associated
122
+ /// [`HostComponent::Data`] for an instance.
88
123
pub fn add_host_component < HC : HostComponent + Send + Sync + ' static > (
89
124
& mut self ,
90
125
host_component : HC ,
@@ -93,6 +128,11 @@ impl<T: Send + Sync> EngineBuilder<T> {
93
128
. add_host_component ( & mut self . linker , host_component)
94
129
}
95
130
131
+ /// Builds an [`Engine`] from this builder with the given host state data.
132
+ ///
133
+ /// Note that this data will generally go entirely unused, but is needed
134
+ /// by the implementation of [`Engine::instantiate_pre`]. If `T: Default`,
135
+ /// it is probably preferable to use [`EngineBuilder::build`].
96
136
pub fn build_with_data ( self , instance_pre_data : T ) -> Engine < T > {
97
137
let host_components = self . host_components_builder . build ( ) ;
98
138
@@ -112,11 +152,14 @@ impl<T: Send + Sync> EngineBuilder<T> {
112
152
}
113
153
114
154
impl < T : Default + Send + Sync > EngineBuilder < T > {
155
+ /// Builds an [`Engine`] from this builder.
115
156
pub fn build ( self ) -> Engine < T > {
116
157
self . build_with_data ( T :: default ( ) )
117
158
}
118
159
}
119
160
161
+ /// An `Engine` is a global context for the initialization and execution of
162
+ /// Spin components.
120
163
pub struct Engine < T > {
121
164
inner : wasmtime:: Engine ,
122
165
linker : Linker < T > ,
@@ -125,14 +168,17 @@ pub struct Engine<T> {
125
168
}
126
169
127
170
impl < T : Send + Sync > Engine < T > {
171
+ /// Creates a new [`EngineBuilder`] with the given [`Config`].
128
172
pub fn builder ( config : & Config ) -> Result < EngineBuilder < T > > {
129
173
EngineBuilder :: new ( config)
130
174
}
131
175
176
+ /// Creates a new [`StoreBuilder`].
132
177
pub fn store_builder ( & self ) -> StoreBuilder {
133
178
StoreBuilder :: new ( self . inner . clone ( ) , & self . host_components )
134
179
}
135
180
181
+ /// Creates a new [`InstancePre`] for the given [`Module`].
136
182
#[ instrument( skip_all) ]
137
183
pub fn instantiate_pre ( & self , module : & Module ) -> Result < InstancePre < T > > {
138
184
let mut store = self . instance_pre_store . lock ( ) . unwrap ( ) ;
@@ -147,11 +193,15 @@ impl<T> AsRef<wasmtime::Engine> for Engine<T> {
147
193
}
148
194
}
149
195
196
+ /// A pre-initialized instance that is ready to be instantiated.
197
+ ///
198
+ /// See [`wasmtime::InstancePre`] for more information.
150
199
pub struct InstancePre < T > {
151
200
inner : wasmtime:: InstancePre < Data < T > > ,
152
201
}
153
202
154
203
impl < T : Send + Sync > InstancePre < T > {
204
+ /// Instantiates this instance with the given [`Store`].
155
205
#[ instrument( skip_all) ]
156
206
pub async fn instantiate_async ( & self , store : & mut Store < T > ) -> Result < Instance > {
157
207
self . inner . instantiate_async ( store) . await
0 commit comments