marine_wasm_backend_traits/
instance.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::AsContextMut;
18use crate::Export;
19use crate::ResolveResult;
20use crate::WasmBackend;
21
22/// A handle to an instantiated Wasm module. Cloning is cheap.
23pub trait Instance<WB: WasmBackend>: Clone {
24    /// Returns an `Iterator` to all exports of this instance.
25    fn export_iter<'a>(
26        &'a self,
27        store: <WB as WasmBackend>::ContextMut<'a>,
28    ) -> Box<dyn Iterator<Item = (&'a str, Export<WB>)> + 'a>;
29
30    /// Returns nth exported memory, None if there is no nth memory.
31    /// No guaranties is known for memory order, but almost always a module has only one memory,
32    /// hence the only valid value for `memory_index` is 0.
33    fn get_nth_memory(
34        &self,
35        store: &mut impl AsContextMut<WB>,
36        memory_index: u32, // TODO: refactor memory indexing with enums
37    ) -> Option<<WB as WasmBackend>::Memory>;
38
39    /// Returns a memory export with given name.
40    /// # Errors:
41    ///     Returns an error if there is no export with such name, or it is not a memory.
42    fn get_memory(
43        &self,
44        store: &mut impl AsContextMut<WB>,
45        memory_name: &str,
46    ) -> ResolveResult<<WB as WasmBackend>::Memory>;
47
48    /// Returns an exported function with the given name.
49    /// # Errors:
50    ///     Returns an error if there is no export with such name, or it is not a function.
51    fn get_function(
52        &self,
53        store: &mut impl AsContextMut<WB>,
54        name: &str,
55    ) -> ResolveResult<<WB as WasmBackend>::ExportFunction>;
56}