marine_wasm_backend_traits/module.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
17use crate::ModuleCreationResult;
18use crate::InstantiationResult;
19use crate::WasmBackend;
20
21use futures::future::BoxFuture;
22
23/// A handle to compiled wasm module.
24pub trait Module<WB: WasmBackend>: Sized {
25 /// Compiles a wasm bytes into a module and extracts custom sections.
26 fn new(store: &mut <WB as WasmBackend>::Store, wasm: &[u8]) -> ModuleCreationResult<Self>;
27
28 /// Returns custom sections corresponding to `name`, empty slice if there is no sections.
29 fn custom_sections(&self, name: &str) -> &[Vec<u8>];
30
31 /// Instantiates module by allocating memory, VM state and linking imports with ones from `import` argument.
32 /// Does not call `_start` or `_initialize` functions.
33 ///
34 /// # Panics:
35 ///
36 /// If the `Store` given is not the same with `Store` used to create `Imports` and this object.
37 fn instantiate<'args>(
38 &'args self,
39 store: &'args mut <WB as WasmBackend>::Store,
40 imports: &'args <WB as WasmBackend>::Imports,
41 ) -> BoxFuture<'args, InstantiationResult<<WB as WasmBackend>::Instance>>;
42}