Skip to main content

wasmi_core/
index_ty.rs

1use crate::ValType;
2
3/// The index type used for addressing memories and tables.
4#[derive(Debug, Copy, Clone, PartialEq, Eq)]
5pub enum IndexType {
6    /// A 32-bit address type.
7    I32,
8    /// A 64-bit address type.
9    I64,
10}
11
12impl IndexType {
13    /// Returns the [`ValType`] associated to `self`.
14    pub fn ty(&self) -> ValType {
15        match self {
16            IndexType::I32 => ValType::I32,
17            IndexType::I64 => ValType::I64,
18        }
19    }
20
21    /// Returns `true` if `self` is [`IndexType::I64`].
22    pub fn is_64(&self) -> bool {
23        matches!(self, Self::I64)
24    }
25
26    /// Returns the maximum size for Wasm memories and tables for `self`.
27    pub fn max_size(&self) -> u128 {
28        const WASM32_MAX_SIZE: u128 = 1 << 32;
29        const WASM64_MAX_SIZE: u128 = 1 << 64;
30        match self {
31            Self::I32 => WASM32_MAX_SIZE,
32            Self::I64 => WASM64_MAX_SIZE,
33        }
34    }
35
36    /// Returns the minimum [`IndexType`] between `self` and `other`.
37    pub fn min(&self, other: &Self) -> Self {
38        match (self, other) {
39            (IndexType::I64, IndexType::I64) => IndexType::I64,
40            _ => IndexType::I32,
41        }
42    }
43}