casper_wasmi/
types.rs

1use crate::{pwasm::PwasmCompat, ValueType};
2use alloc::borrow::Cow;
3use casper_wasm::elements::{FunctionType, GlobalType, MemoryType, TableType};
4
5/// Signature of a [function].
6///
7/// Signature of a function consists of zero or more parameter [types][type] and zero or one return [type].
8///
9/// Two signatures are considered equal if they have equal list of parameters and equal return types.
10///
11/// [type]: enum.ValueType.html
12/// [function]: struct.FuncInstance.html
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct Signature {
15    params: Cow<'static, [ValueType]>,
16    return_type: Option<ValueType>,
17}
18
19impl Signature {
20    /// Creates new signature with givens
21    /// parameter types and optional return type.
22    ///
23    /// # Examples
24    ///
25    /// ```rust
26    /// use casper_wasmi::{Signature, ValueType};
27    ///
28    /// // s1: (i32) -> ()
29    /// let s1 = Signature::new(&[ValueType::I32][..], None);
30    ///
31    /// // s2: () -> i32
32    /// let s2 = Signature::new(&[][..], Some(ValueType::I32));
33    ///
34    /// // s3: (I64) -> ()
35    /// let dynamic_params = vec![ValueType::I64];
36    /// let s3 = Signature::new(dynamic_params, None);
37    /// ```
38    pub fn new<C: Into<Cow<'static, [ValueType]>>>(
39        params: C,
40        return_type: Option<ValueType>,
41    ) -> Signature {
42        Signature {
43            params: params.into(),
44            return_type,
45        }
46    }
47
48    /// Returns parameter types of this signature.
49    pub fn params(&self) -> &[ValueType] {
50        self.params.as_ref()
51    }
52
53    /// Returns return type of this signature.
54    pub fn return_type(&self) -> Option<ValueType> {
55        self.return_type
56    }
57
58    pub(crate) fn from_elements(func_type: &FunctionType) -> Signature {
59        Signature {
60            params: func_type
61                .params()
62                .iter()
63                .cloned()
64                .map(ValueType::from_elements)
65                .collect(),
66            return_type: func_type
67                .results()
68                .first()
69                .map(|vty| ValueType::from_elements(*vty)),
70        }
71    }
72}
73
74/// Description of a global variable.
75///
76/// Primarly used to describe imports of global variables.
77/// See [`ImportResolver`] for details.
78///
79/// [`ImportResolver`]: trait.ImportResolver.html
80pub struct GlobalDescriptor {
81    value_type: ValueType,
82    mutable: bool,
83}
84
85impl GlobalDescriptor {
86    pub(crate) fn from_elements(global_type: &GlobalType) -> GlobalDescriptor {
87        GlobalDescriptor {
88            value_type: ValueType::from_elements(global_type.content_type()),
89            mutable: global_type.is_mutable(),
90        }
91    }
92
93    /// Returns [`ValueType`] of the requested global.
94    ///
95    /// [`ValueType`]: enum.ValueType.html
96    pub fn value_type(&self) -> ValueType {
97        self.value_type
98    }
99
100    /// Returns whether the requested global mutable.
101    pub fn is_mutable(&self) -> bool {
102        self.mutable
103    }
104}
105
106/// Description of a table.
107///
108/// Primarly used to describe imports of tables.
109/// See [`ImportResolver`] for details.
110///
111/// [`ImportResolver`]: trait.ImportResolver.html
112pub struct TableDescriptor {
113    initial: u32,
114    maximum: Option<u32>,
115}
116
117impl TableDescriptor {
118    pub(crate) fn from_elements(table_type: &TableType) -> TableDescriptor {
119        TableDescriptor {
120            initial: table_type.limits().initial(),
121            maximum: table_type.limits().maximum(),
122        }
123    }
124
125    /// Returns initial size of the requested table.
126    pub fn initial(&self) -> u32 {
127        self.initial
128    }
129
130    /// Returns maximum size of the requested table.
131    pub fn maximum(&self) -> Option<u32> {
132        self.maximum
133    }
134}
135
136/// Description of a linear memory.
137///
138/// Primarly used to describe imports of linear memories.
139/// See [`ImportResolver`] for details.
140///
141/// [`ImportResolver`]: trait.ImportResolver.html
142pub struct MemoryDescriptor {
143    initial: u32,
144    maximum: Option<u32>,
145}
146
147impl MemoryDescriptor {
148    pub(crate) fn from_elements(memory_type: &MemoryType) -> MemoryDescriptor {
149        MemoryDescriptor {
150            initial: memory_type.limits().initial(),
151            maximum: memory_type.limits().maximum(),
152        }
153    }
154
155    /// Returns initial size (in pages) of the requested memory.
156    pub fn initial(&self) -> u32 {
157        self.initial
158    }
159
160    /// Returns maximum size (in pages) of the requested memory.
161    pub fn maximum(&self) -> Option<u32> {
162        self.maximum
163    }
164}