Skip to main content

marine_wasm_backend_traits/
lib.rs

1/*
2 * Copyright 2023 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17pub mod errors;
18pub mod exports;
19pub mod imports;
20pub mod store;
21pub mod wasi;
22pub mod wtype;
23pub mod module;
24pub mod instance;
25pub mod caller;
26pub mod function;
27pub mod macros;
28
29/// Helper functions for backend implementations.
30pub mod impl_utils;
31
32pub mod prelude {
33    pub use crate::errors::*;
34    pub use crate::exports::*;
35    pub use crate::imports::*;
36    pub use crate::store::*;
37    pub use crate::wasi::*;
38    pub use crate::wtype::*;
39    pub use crate::module::*;
40    pub use crate::instance::*;
41    pub use crate::caller::*;
42    pub use crate::function::*;
43    pub use crate::WasmBackend;
44    pub use crate::DelayedContextLifetime;
45}
46
47pub use prelude::*;
48
49use it_memory_traits::MemoryView;
50
51use std::marker::PhantomData;
52
53/// A core trait for any backend. It serves two purposes:
54/// * handles initialization of the library if needed
55/// * provides access to all public types -- like `mod` but for trait impls.
56pub trait WasmBackend: Clone + Send + Sync + 'static {
57    /// A type that stores all the data, while most of the types are handles to data from `Store`.
58    type Store: Store<Self>;
59    /// A compiled, but not instantiated module.
60    type Module: Module<Self>;
61    /// An object that holds all the functions that are given to `Module` as imports.
62    type Imports: Imports<Self>; // maybe rename to Linker?
63    /// An instantiated module ready to be executed.
64    type Instance: Instance<Self>;
65    /// A temporary immutable handle to `Store`.
66    type Context<'c>: Context<Self>;
67    /// A temporary mutable handle to `Store`
68    type ContextMut<'c>: ContextMut<Self>;
69    /// A type that is used to pass context to imports.
70    type ImportCallContext<'c>: ImportCallContext<Self>;
71    /// A host function prepared to be used as import for instantiating a module, contained in `Store`.
72    type HostFunction: HostFunction<Self> + FuncConstructor<Self>;
73    /// An export function from a wasm instance, contained in `Store`
74    type ExportFunction: ExportFunction<Self>;
75    /// A wasm memory.
76    type Memory: Memory<Self>;
77    /// A view to the wasm memory.
78    type MemoryView: MemoryView<DelayedContextLifetime<Self>>;
79
80    /// Type that provides all WASI-related APIs.
81    type Wasi: WasiImplementation<Self>;
82
83    /// Creates a new wasm backend with default configuration. In future, a configuration
84    /// may be passed as argument. The only option at the moment is an asynchronous backend.
85    fn new_async() -> WasmBackendResult<Self>;
86}
87
88/// This struct is a helper, that allows passing `<WB as WasmBackend>::ContextMut` as template parameter,
89/// but not specify a lifetime. Any local lifetime can be used instead.
90pub struct DelayedContextLifetime<WB: WasmBackend> {
91    _data: PhantomData<WB>,
92}
93
94impl<WB: WasmBackend> it_memory_traits::Store for DelayedContextLifetime<WB> {
95    type ActualStore<'c> = <WB as WasmBackend>::ContextMut<'c>;
96}