lucet_module/
types.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::{Display, Formatter};
3
4#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
5pub enum ValueType {
6    I32,
7    I64,
8    F32,
9    F64,
10}
11
12impl Display for ValueType {
13    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
14        match self {
15            ValueType::I32 => write!(f, "I32"),
16            ValueType::I64 => write!(f, "I64"),
17            ValueType::F32 => write!(f, "F32"),
18            ValueType::F64 => write!(f, "F64"),
19        }
20    }
21}
22
23/// A signature for a function in a wasm module.
24///
25/// Note that this does not explicitly name VMContext as a parameter! It is assumed that all wasm
26/// functions take VMContext as their first parameter.
27#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
28pub struct Signature {
29    pub params: Vec<ValueType>,
30    // In the future, wasm may permit this to be a Vec of ValueType
31    pub ret_ty: Option<ValueType>,
32}
33
34impl Display for Signature {
35    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36        write!(f, "(")?;
37        for (i, p) in self.params.iter().enumerate() {
38            if i == 0 {
39                write!(f, "{}", p)?;
40            } else {
41                write!(f, ", {}", p)?;
42            }
43        }
44        write!(f, ") -> ")?;
45        match self.ret_ty {
46            Some(ty) => write!(f, "{}", ty),
47            None => write!(f, "()"),
48        }
49    }
50}
51
52#[macro_export]
53macro_rules! lucet_signature {
54    ((() -> ())) => {
55        $crate::Signature {
56            params: vec![],
57            ret_ty: None
58        }
59    };
60    (($($arg_ty:ident),*) -> ()) => {
61        $crate::Signature {
62            params: vec![$($crate::ValueType::$arg_ty),*],
63            ret_ty: None,
64        }
65    };
66    (($($arg_ty:ident),*) -> $ret_ty:ident) => {
67        $crate::Signature {
68            params: vec![$($crate::ValueType::$arg_ty),*],
69            ret_ty: Some($crate::ValueType::$ret_ty),
70        }
71    };
72}