logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use crate::{types::ExternRef, FuncRef, ValType, WasmValue};

/// Describes the mapping of Rust type to Wasm type.
///
/// ```rust
/// use wasmedge_sdk::{WasmValType, ValType};
///
/// assert_eq!(i32::WASM_TYPE, ValType::I32);
/// ```
///
pub trait WasmValType {
    /// The Wasm type.
    const WASM_TYPE: ValType;
}

/// The `impl_wasm_val_type` macro is used to generate the following struct
///
/// ```ignore
/// impl WasmValType for i32 {
///     const WASM_TYPE: ValType = ValType::I32;
/// }
/// ```
macro_rules! impl_wasm_val_type {
    ($t:ty, $w:expr) => {
        impl WasmValType for $t {
            const WASM_TYPE: ValType = $w;
        }
    };
}

impl_wasm_val_type!(i8, ValType::I32);
impl_wasm_val_type!(u8, ValType::I32);
impl_wasm_val_type!(i16, ValType::I32);
impl_wasm_val_type!(u16, ValType::I32);
impl_wasm_val_type!(i32, ValType::I32);
impl_wasm_val_type!(u32, ValType::I64);
impl_wasm_val_type!(i64, ValType::I64);
impl_wasm_val_type!(f32, ValType::F32);
impl_wasm_val_type!(f64, ValType::F64);
impl_wasm_val_type!(i128, ValType::V128);
impl_wasm_val_type!(FuncRef, ValType::FuncRef);
impl_wasm_val_type!(ExternRef, ValType::ExternRef);

#[cfg(test)]
mod test_wasm_val_type {
    use super::*;

    #[test]
    fn test_wasm_types() {
        assert_eq!(i8::WASM_TYPE, ValType::I32);
        assert_eq!(u8::WASM_TYPE, ValType::I32);
        assert_eq!(i16::WASM_TYPE, ValType::I32);
        assert_eq!(u16::WASM_TYPE, ValType::I32);
        assert_eq!(i32::WASM_TYPE, ValType::I32);
        assert_eq!(u32::WASM_TYPE, ValType::I64);
        assert_eq!(i64::WASM_TYPE, ValType::I64);
        assert_eq!(f32::WASM_TYPE, ValType::F32);
        assert_eq!(f64::WASM_TYPE, ValType::F64);
        assert_eq!(i128::WASM_TYPE, ValType::V128);
        assert_eq!(FuncRef::WASM_TYPE, ValType::FuncRef);
        assert_eq!(ExternRef::WASM_TYPE, ValType::ExternRef);
    }
}

/// Describes the mapping of a tuple of Rust types to Wasm types.
///
/// ```rust
/// use wasmedge_sdk::{FuncRef, types::ExternRef, ValType, WasmValTypeList};
///
/// assert_eq!(
///      <(i32, i64, f32, f64, FuncRef, ExternRef)>::wasm_types(),
///      [ValType::I32, ValType::I64, ValType::F32, ValType::F64, ValType::FuncRef, ValType::ExternRef]
/// );
/// ```
pub trait WasmValTypeList
where
    Self: Sized,
{
    /// The array type that can hold all the represented values.
    ///
    /// Note that all values are stored in their binary form.
    type Array: AsMut<[i128]>;

    /// Get the Wasm types for the tuple (list) of currently
    /// represented values.
    fn wasm_types() -> &'static [ValType];
}

macro_rules! impl_wasm_val_type_list {
    ( $($o:ident),* ) => {
        #[allow(unused_parens, dead_code)]
        impl< $( $o ),* >
            WasmValTypeList
        for ( $( $o ),* )
        where
            $( $o: WasmValType ),*
        {
            type Array = [i128; count_idents!( $( $o ),* )];

            fn wasm_types() -> &'static [ValType] {
                &[
                    $(
                        $o::WASM_TYPE
                    ),*
                ]
            }
        }
    };
}

// Count the number of identifiers at compile-time.
macro_rules! count_idents {
    ( $($idents:ident),* ) => {
        {
            #[allow(dead_code, non_camel_case_types)]
            enum Idents { $( $idents, )* __CountIdentsLast }
            const COUNT: usize = Idents::__CountIdentsLast as usize;
            COUNT
        }
    };
}

impl_wasm_val_type_list!();
impl_wasm_val_type_list!(A1);
impl_wasm_val_type_list!(A1, A2);
impl_wasm_val_type_list!(A1, A2, A3);
impl_wasm_val_type_list!(A1, A2, A3, A4);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5, A6);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5, A6, A7);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5, A6, A7, A8);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5, A6, A7, A8, A9);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15);
impl_wasm_val_type_list!(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21,
    A22
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21,
    A22, A23
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21,
    A22, A23, A24
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21,
    A22, A23, A24, A25
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21,
    A22, A23, A24, A25, A26
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21,
    A22, A23, A24, A25, A26, A27
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21,
    A22, A23, A24, A25, A26, A27, A28
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21,
    A22, A23, A24, A25, A26, A27, A28, A29
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21,
    A22, A23, A24, A25, A26, A27, A28, A29, A30
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21,
    A22, A23, A24, A25, A26, A27, A28, A29, A30, A31
);
impl_wasm_val_type_list!(
    A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21,
    A22, A23, A24, A25, A26, A27, A28, A29, A30, A31, A32
);

#[cfg(test)]
mod test_wasm_val_type_list {
    use super::*;

    #[test]
    fn test_wasm_types_for_multi_values() {
        assert_eq!(<()>::wasm_types(), []);
        assert_eq!(<(i32, i32)>::wasm_types(), [ValType::I32, ValType::I32]);
        assert_eq!(<(i64, i64)>::wasm_types(), [ValType::I64, ValType::I64]);
        assert_eq!(<(f32, f32)>::wasm_types(), [ValType::F32, ValType::F32]);
        assert_eq!(<(f64, f64)>::wasm_types(), [ValType::F64, ValType::F64]);
        assert_eq!(
            <(i32, i64, f32, f64, FuncRef, ExternRef)>::wasm_types(),
            [
                ValType::I32,
                ValType::I64,
                ValType::F32,
                ValType::F64,
                ValType::FuncRef,
                ValType::ExternRef,
            ]
        );
        assert_eq!(
            <(FuncRef, ExternRef)>::wasm_types(),
            [ValType::FuncRef, ValType::ExternRef]
        );
    }
}

/// Defines the function converting a value of Rust type to the one of Wasm type.
pub trait WasmVal {
    fn to_wasm_value(self) -> WasmValue;
}
impl WasmVal for i8 {
    fn to_wasm_value(self) -> WasmValue {
        WasmValue::from_i32(self as i32)
    }
}
impl WasmVal for u8 {
    fn to_wasm_value(self) -> WasmValue {
        WasmValue::from_i32(self as i32)
    }
}
impl WasmVal for i16 {
    fn to_wasm_value(self) -> WasmValue {
        WasmValue::from_i32(self as i32)
    }
}
impl WasmVal for u16 {
    fn to_wasm_value(self) -> WasmValue {
        WasmValue::from_i32(self as i32)
    }
}
impl WasmVal for i32 {
    fn to_wasm_value(self) -> WasmValue {
        WasmValue::from_i32(self)
    }
}
impl WasmVal for u32 {
    fn to_wasm_value(self) -> WasmValue {
        WasmValue::from_i64(self as i64)
    }
}
impl WasmVal for i64 {
    fn to_wasm_value(self) -> WasmValue {
        WasmValue::from_i64(self)
    }
}
impl WasmVal for f32 {
    fn to_wasm_value(self) -> WasmValue {
        WasmValue::from_f32(self)
    }
}
impl WasmVal for f64 {
    fn to_wasm_value(self) -> WasmValue {
        WasmValue::from_f64(self)
    }
}
impl WasmVal for i128 {
    fn to_wasm_value(self) -> WasmValue {
        WasmValue::from_v128(self)
    }
}
impl WasmVal for FuncRef {
    fn to_wasm_value(self) -> WasmValue {
        WasmValue::from_func_ref(self.inner)
    }
}
impl WasmVal for ExternRef {
    fn to_wasm_value(self) -> WasmValue {
        self.inner
    }
}

/// Used to pass arguments to [Func::call](crate::Func::call), [FuncRef::call](crate::FuncRef::call), [Engine::run_func](crate::Engine::run_func), or [Engine::run_func_ref](crate::Engine::run_func_ref). Notice that to use the macro, it is required to use `WasmVal` trait. If the version of rust used is less than v1.63, please place `#![feature(explicit_generic_args_with_impl_trait)]` on the root of the program.
#[macro_export]
macro_rules! params {
    ( $( $x:expr ),* ) => {
        {
            let mut temp_vec = vec![];
            $(
                temp_vec.push($x.to_wasm_value());

            )*
            temp_vec
        }
    };
}