Skip to main content

wasmedge_sdk/
types.rs

1//! Defines the general types.
2
3use wasmedge_sys::WasmValue;
4
5/// Defines runtime values that a WebAssembly module can either consume or produce.
6#[derive(Debug, Clone)]
7pub enum Val {
8    /// 32-bit integer.
9    ///
10    /// Integers are not inherently signed or unsigned, their interpretation is determined by individual operations.
11    I32(i32),
12    /// 64-bit integer.
13    ///
14    /// Integers are not inherently signed or unsigned, their interpretation is determined by individual operations.
15    I64(i64),
16    /// 32-bit floating-point data as defined by the [IEEE 754-2019](https://ieeexplore.ieee.org/document/8766229).
17    F32(f32),
18    /// 64-bit floating-point data as defined by the [IEEE 754-2019](https://ieeexplore.ieee.org/document/8766229).
19    F64(f64),
20    /// 128-bit vector of packed integer or floating-point data.
21    ///
22    /// The packed data can be interpreted as signed or unsigned integers, single or double precision floating-point
23    /// values, or a single 128 bit type. The interpretation is determined by individual operations.
24    V128(i128),
25    /// A reference to a [host function](crate::Func).
26    ///
27    /// `FuncRef(None)` is the null function reference, created by `ref.null
28    /// func` in Wasm.
29    // FuncRef(Option<FuncRef>),
30    /// A reference to opaque data in the wasm instance.
31    ///
32    /// `ExternRef(None)` is the null external reference, created by `ref.null
33    /// extern` in Wasm.
34    ExternRef(Option<ExternRef>),
35}
36impl From<Val> for WasmValue {
37    fn from(val: Val) -> Self {
38        match val {
39            Val::I32(i) => WasmValue::from_i32(i),
40            Val::I64(i) => WasmValue::from_i64(i),
41            Val::F32(i) => WasmValue::from_f32(i),
42            Val::F64(i) => WasmValue::from_f64(i),
43            Val::V128(i) => WasmValue::from_v128(i),
44            // Val::FuncRef(Some(func_ref)) => WasmValue::from_func_ref(func_ref.inner),
45            // Val::FuncRef(None) => WasmValue::from_null_ref(RefType::FuncRef),
46            Val::ExternRef(Some(extern_ref)) => extern_ref.inner,
47            Val::ExternRef(None) => WasmValue::null_extern_ref(),
48        }
49    }
50}
51impl From<WasmValue> for Val {
52    fn from(value: WasmValue) -> Self {
53        match value.ty() {
54            wasmedge_types::ValType::I32 => Val::I32(value.to_i32()),
55            wasmedge_types::ValType::I64 => Val::I64(value.to_i64()),
56            wasmedge_types::ValType::F32 => Val::F32(value.to_f32()),
57            wasmedge_types::ValType::F64 => Val::F64(value.to_f64()),
58            wasmedge_types::ValType::V128 => Val::V128(value.to_v128()),
59            wasmedge_types::ValType::FuncRef => {
60                // Waiting for the fun-ref related proposals to stabilize
61                Val::ExternRef(None)
62            }
63            wasmedge_types::ValType::ExternRef => {
64                if value.is_null_ref() {
65                    Val::ExternRef(None)
66                } else {
67                    Val::ExternRef(Some(ExternRef { inner: value }))
68                }
69            }
70            wasmedge_types::ValType::UnsupportedRef => {
71                // Waiting for more GC-related C APIs.
72                Val::ExternRef(None)
73            }
74        }
75    }
76}
77
78/// A reference to opaque data in the wasm instance.
79#[derive(Debug, Clone, Copy)]
80pub struct ExternRef {
81    pub(crate) inner: WasmValue,
82}
83impl ExternRef {
84    /// Creates a new instance of [ExternRef] wrapping the given value.
85    pub fn new<T>(extern_obj: &mut T) -> Self
86    where
87        T: 'static + Send + Sync,
88    {
89        let inner = WasmValue::from_extern_ref::<T>(extern_obj);
90        Self { inner }
91    }
92}