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