it_lilo/traits/
allocatable.rs

1/*
2 * Copyright 2021 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 futures::future::BoxFuture;
18use it_memory_traits::MemoryView;
19use thiserror::Error as ThisError;
20
21pub const DEFAULT_MEMORY_INDEX: usize = 0;
22
23pub trait Allocatable<MV: MemoryView<Store>, Store: it_memory_traits::Store>: Send {
24    fn allocate<'this, 'store: 'this, 'store_inner: 'this>(
25        &'this mut self,
26        store: &'store mut <Store as it_memory_traits::Store>::ActualStore<'store_inner>,
27        size: u32,
28        type_tag: u32,
29    ) -> BoxFuture<'this, Result<(u32, MV), AllocatableError>>;
30}
31
32#[derive(Debug, ThisError)]
33pub enum AllocatableError {
34    /// The memory doesn't exist.
35    #[error("memory `{memory_index}` does not exist")]
36    MemoryIsMissing {
37        /// The memory index.
38        memory_index: usize,
39    },
40
41    /// The local or import function doesn't exist.
42    #[error("the allocate function with index `{function_index}` doesn't exist in Wasm module")]
43    AllocateFuncIsMissing {
44        /// The local or import function index.
45        function_index: u32,
46    },
47
48    /// Failed to call a allocate function.
49    #[error(r#"call to allocate function was failed: {reason}"#)]
50    AllocateCallFailed {
51        /// error returned by the allocate function
52        #[source]
53        reason: anyhow::Error,
54    },
55
56    /// Allocate input types doesn't match with needed.
57    #[error(
58        "allocate func doesn't receive two i32 values,\
59             probably a Wasm module's built with unsupported sdk version"
60    )]
61    AllocateFuncIncompatibleSignature,
62
63    /// Allocate output types doesn't match with needed.
64    #[error(
65        "allocate func doesn't return a one value of I32 type,\
66             probably a Wasm module's built with unsupported sdk version"
67    )]
68    AllocateFuncIncompatibleOutput,
69
70    // TODO: make it generic in future.
71    /// User defined error.
72    #[error("{0}")]
73    UserDefinedError(String),
74}