Skip to main content

wasmtime_c_api/types/
val.rs

1use crate::{wasm_engine_t, wasm_functype_t, wasmtime_array_type_t, wasmtime_struct_type_t};
2use std::mem::{ManuallyDrop, MaybeUninit};
3use wasmtime::{HeapType, RefType, ValType};
4
5pub type wasm_valtype_t = ValType;
6
7wasmtime_c_api_macros::declare_ty!(wasm_valtype_t);
8
9pub type wasm_valkind_t = u8;
10pub const WASM_I32: wasm_valkind_t = 0;
11pub const WASM_I64: wasm_valkind_t = 1;
12pub const WASM_F32: wasm_valkind_t = 2;
13pub const WASM_F64: wasm_valkind_t = 3;
14pub const WASM_EXTERNREF: wasm_valkind_t = 128;
15pub const WASM_FUNCREF: wasm_valkind_t = 129;
16
17#[unsafe(no_mangle)]
18pub extern "C" fn wasm_valtype_new(kind: wasm_valkind_t) -> Box<wasm_valtype_t> {
19    Box::new(match kind {
20        WASM_I32 => ValType::I32,
21        WASM_I64 => ValType::I64,
22        WASM_F32 => ValType::F32,
23        WASM_F64 => ValType::F64,
24        WASM_EXTERNREF => ValType::EXTERNREF,
25        WASM_FUNCREF => ValType::FUNCREF,
26        _ => crate::abort("unexpected value type kind"),
27    })
28}
29
30#[unsafe(no_mangle)]
31pub extern "C" fn wasm_valtype_kind(vt: &wasm_valtype_t) -> wasm_valkind_t {
32    match vt {
33        ValType::I32 => WASM_I32,
34        ValType::I64 => WASM_I64,
35        ValType::F32 => WASM_F32,
36        ValType::F64 => WASM_F64,
37        // TODO
38        ValType::V128 => crate::abort("support for v128 "),
39        ValType::Ref(r) => match (r.is_nullable(), r.heap_type()) {
40            (true, HeapType::Extern) => WASM_EXTERNREF,
41            (true, HeapType::Func) => WASM_FUNCREF,
42            // TODO
43            _ => crate::abort("support for non-externref and non-funcref references"),
44        },
45    }
46}
47
48#[unsafe(no_mangle)]
49pub extern "C" fn wasmtime_wasm_valtype_v128() -> Box<wasm_valtype_t> {
50    Box::new(ValType::V128)
51}
52
53#[unsafe(no_mangle)]
54pub extern "C" fn wasmtime_wasm_valtype_equal(a: &wasm_valtype_t, b: &wasm_valtype_t) -> bool {
55    ValType::eq(a, b)
56}
57
58#[repr(C, u8)]
59#[derive(Clone)]
60pub enum wasmtime_valtype_t {
61    I32,
62    I64,
63    F32,
64    F64,
65    V128,
66    Ref(wasmtime_reftype_t),
67}
68
69#[unsafe(no_mangle)]
70pub extern "C" fn wasmtime_valtype_new(
71    ty: &wasm_valtype_t,
72    ret: &mut MaybeUninit<wasmtime_valtype_t>,
73) {
74    ret.write(match ty {
75        ValType::I32 => wasmtime_valtype_t::I32,
76        ValType::I64 => wasmtime_valtype_t::I64,
77        ValType::F32 => wasmtime_valtype_t::F32,
78        ValType::F64 => wasmtime_valtype_t::F64,
79        ValType::V128 => wasmtime_valtype_t::V128,
80        ValType::Ref(r) => wasmtime_valtype_t::Ref(wasmtime_reftype_t::from(r)),
81    });
82}
83
84#[unsafe(no_mangle)]
85pub extern "C" fn wasmtime_valtype_clone(
86    ty: &wasmtime_valtype_t,
87    ret: &mut MaybeUninit<wasmtime_valtype_t>,
88) {
89    ret.write(ty.clone());
90}
91
92#[unsafe(no_mangle)]
93pub unsafe extern "C" fn wasmtime_valtype_delete(
94    ret: Option<&mut ManuallyDrop<wasmtime_valtype_t>>,
95) {
96    if let Some(ret) = ret {
97        unsafe {
98            ManuallyDrop::drop(ret);
99        };
100    }
101}
102
103#[unsafe(no_mangle)]
104pub unsafe extern "C" fn wasmtime_valtype_to_wasm(
105    engine: Option<&wasm_engine_t>,
106    r: &wasmtime_valtype_t,
107) -> Box<wasm_valtype_t> {
108    let r = match r {
109        wasmtime_valtype_t::I32 => return Box::new(ValType::I32),
110        wasmtime_valtype_t::I64 => return Box::new(ValType::I64),
111        wasmtime_valtype_t::F32 => return Box::new(ValType::F32),
112        wasmtime_valtype_t::F64 => return Box::new(ValType::F64),
113        wasmtime_valtype_t::V128 => return Box::new(ValType::V128),
114        wasmtime_valtype_t::Ref(r) => r,
115    };
116    let heap_type = match &r.hty {
117        wasmtime_heaptype_t::Extern => HeapType::Extern,
118        wasmtime_heaptype_t::NoExtern => HeapType::NoExtern,
119        wasmtime_heaptype_t::Func => HeapType::Func,
120        wasmtime_heaptype_t::ConcreteFunc(f) => {
121            HeapType::ConcreteFunc(f.ty().ty(&engine.unwrap().engine).clone())
122        }
123        wasmtime_heaptype_t::NoFunc => HeapType::NoFunc,
124        wasmtime_heaptype_t::Any => HeapType::Any,
125        wasmtime_heaptype_t::None => HeapType::None,
126        wasmtime_heaptype_t::Eq => HeapType::Eq,
127        wasmtime_heaptype_t::I31 => HeapType::I31,
128        wasmtime_heaptype_t::Array => HeapType::Array,
129        wasmtime_heaptype_t::ConcreteArray(a) => HeapType::ConcreteArray(a.ty.clone()),
130        wasmtime_heaptype_t::Struct => HeapType::Struct,
131        wasmtime_heaptype_t::ConcreteStruct(s) => HeapType::ConcreteStruct(s.ty.clone()),
132        wasmtime_heaptype_t::Exn => HeapType::Exn,
133        wasmtime_heaptype_t::NoExn => HeapType::NoExn,
134    };
135    Box::new(ValType::Ref(RefType::new(r.nullable, heap_type)))
136}
137
138#[repr(C, u8)]
139#[derive(Clone)]
140pub enum wasmtime_heaptype_t {
141    Extern,
142    NoExtern,
143    Func,
144    ConcreteFunc(Box<wasm_functype_t>),
145    NoFunc,
146    Any,
147    None,
148    Eq,
149    I31,
150    Array,
151    ConcreteArray(Box<wasmtime_array_type_t>),
152    Struct,
153    ConcreteStruct(Box<wasmtime_struct_type_t>),
154    Exn,
155    NoExn,
156}
157
158impl From<HeapType> for wasmtime_heaptype_t {
159    fn from(r: HeapType) -> Self {
160        match r {
161            HeapType::Extern => Self::Extern,
162            HeapType::NoExtern => Self::NoExtern,
163            HeapType::Func => Self::Func,
164            HeapType::ConcreteFunc(f) => Self::ConcreteFunc(Box::new(wasm_functype_t::new(f))),
165            HeapType::NoFunc => Self::NoFunc,
166            HeapType::Any => Self::Any,
167            HeapType::None => Self::None,
168            HeapType::Eq => Self::Eq,
169            HeapType::I31 => Self::I31,
170            HeapType::Array => Self::Array,
171            HeapType::ConcreteArray(a) => {
172                Self::ConcreteArray(Box::new(wasmtime_array_type_t { ty: a }))
173            }
174            HeapType::Struct => Self::Struct,
175            HeapType::ConcreteStruct(s) => {
176                Self::ConcreteStruct(Box::new(wasmtime_struct_type_t { ty: s }))
177            }
178            HeapType::Exn => Self::Exn,
179            HeapType::NoExn => Self::NoExn,
180            HeapType::Cont | HeapType::ConcreteCont(_) | HeapType::NoCont => {
181                crate::abort("missing support for contref")
182            }
183        }
184    }
185}
186
187#[unsafe(no_mangle)]
188pub extern "C" fn wasmtime_heaptype_clone(
189    ty: &wasmtime_heaptype_t,
190    ret: &mut MaybeUninit<wasmtime_heaptype_t>,
191) {
192    ret.write(ty.clone());
193}
194
195#[unsafe(no_mangle)]
196pub unsafe extern "C" fn wasmtime_heaptype_delete(
197    ret: Option<&mut ManuallyDrop<wasmtime_heaptype_t>>,
198) {
199    if let Some(ret) = ret {
200        unsafe {
201            ManuallyDrop::drop(ret);
202        }
203    }
204}
205
206#[repr(C)]
207#[derive(Clone)]
208pub struct wasmtime_reftype_t {
209    pub nullable: bool,
210    pub hty: wasmtime_heaptype_t,
211}
212
213impl From<&RefType> for wasmtime_reftype_t {
214    fn from(r: &RefType) -> Self {
215        Self {
216            nullable: r.is_nullable(),
217            hty: r.heap_type().clone().into(),
218        }
219    }
220}
221
222#[unsafe(no_mangle)]
223pub extern "C" fn wasmtime_reftype_clone(
224    ty: &wasmtime_reftype_t,
225    ret: &mut MaybeUninit<wasmtime_reftype_t>,
226) {
227    ret.write(ty.clone());
228}
229
230#[unsafe(no_mangle)]
231pub unsafe extern "C" fn wasmtime_reftype_delete(
232    ret: Option<&mut ManuallyDrop<wasmtime_reftype_t>>,
233) {
234    if let Some(ret) = ret {
235        unsafe {
236            ManuallyDrop::drop(ret);
237        }
238    }
239}