1 use crate::wasmtime_component_resource_type_t;
2 use std::mem::{ManuallyDrop, MaybeUninit};
3 use wasmtime::component::types::*;
4 
5 #[derive(Clone, PartialEq)]
6 #[repr(C, u8)]
7 pub enum wasmtime_component_valtype_t {
8     Bool,
9     S8,
10     S16,
11     S32,
12     S64,
13     U8,
14     U16,
15     U32,
16     U64,
17     F32,
18     F64,
19     Char,
20     String,
21     List(Box<wasmtime_component_list_type_t>),
22     Record(Box<wasmtime_component_record_type_t>),
23     Tuple(Box<wasmtime_component_tuple_type_t>),
24     Variant(Box<wasmtime_component_variant_type_t>),
25     Enum(Box<wasmtime_component_enum_type_t>),
26     Option(Box<wasmtime_component_option_type_t>),
27     Result(Box<wasmtime_component_result_type_t>),
28     Flags(Box<wasmtime_component_flags_type_t>),
29     Own(Box<wasmtime_component_resource_type_t>),
30     Borrow(Box<wasmtime_component_resource_type_t>),
31     Future(Box<wasmtime_component_future_type_t>),
32     Stream(Box<wasmtime_component_stream_type_t>),
33     ErrorContext,
34     Map(Box<wasmtime_component_map_type_t>),
35 }
36 
37 impl From<Type> for wasmtime_component_valtype_t {
from(item: Type) -> Self38     fn from(item: Type) -> Self {
39         match item {
40             Type::Bool => Self::Bool,
41             Type::S8 => Self::S8,
42             Type::S16 => Self::S16,
43             Type::S32 => Self::S32,
44             Type::S64 => Self::S64,
45             Type::U8 => Self::U8,
46             Type::U16 => Self::U16,
47             Type::U32 => Self::U32,
48             Type::U64 => Self::U64,
49             Type::Float32 => Self::F32,
50             Type::Float64 => Self::F64,
51             Type::Char => Self::Char,
52             Type::String => Self::String,
53             Type::List(ty) => Self::List(Box::new(ty.into())),
54             Type::Record(ty) => Self::Record(Box::new(ty.into())),
55             Type::Tuple(ty) => Self::Tuple(Box::new(ty.into())),
56             Type::Variant(ty) => Self::Variant(Box::new(ty.into())),
57             Type::Enum(ty) => Self::Enum(Box::new(ty.into())),
58             Type::Option(ty) => Self::Option(Box::new(ty.into())),
59             Type::Result(ty) => Self::Result(Box::new(ty.into())),
60             Type::Flags(ty) => Self::Flags(Box::new(ty.into())),
61             Type::Own(ty) => Self::Own(Box::new(ty.into())),
62             Type::Borrow(ty) => Self::Borrow(Box::new(ty.into())),
63             Type::Future(ty) => Self::Future(Box::new(ty.into())),
64             Type::Stream(ty) => Self::Stream(Box::new(ty.into())),
65             Type::Map(ty) => Self::Map(Box::new(ty.into())),
66             Type::ErrorContext => Self::ErrorContext,
67         }
68     }
69 }
70 
71 #[unsafe(no_mangle)]
wasmtime_component_valtype_clone( ty: &wasmtime_component_valtype_t, ret: &mut MaybeUninit<wasmtime_component_valtype_t>, )72 pub extern "C" fn wasmtime_component_valtype_clone(
73     ty: &wasmtime_component_valtype_t,
74     ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
75 ) {
76     ret.write(ty.clone());
77 }
78 
79 #[unsafe(no_mangle)]
wasmtime_component_valtype_equal( a: &wasmtime_component_valtype_t, b: &wasmtime_component_valtype_t, ) -> bool80 pub extern "C" fn wasmtime_component_valtype_equal(
81     a: &wasmtime_component_valtype_t,
82     b: &wasmtime_component_valtype_t,
83 ) -> bool {
84     a == b
85 }
86 
87 #[unsafe(no_mangle)]
wasmtime_component_valtype_delete( ret: &mut ManuallyDrop<wasmtime_component_valtype_t>, )88 pub unsafe extern "C" fn wasmtime_component_valtype_delete(
89     ret: &mut ManuallyDrop<wasmtime_component_valtype_t>,
90 ) {
91     unsafe { ManuallyDrop::drop(ret) };
92 }
93 
94 type_wrapper! {
95     #[derive(PartialEq)]
96     pub struct wasmtime_component_list_type_t {
97         pub(crate) ty: List,
98     }
99 
100     clone: wasmtime_component_list_type_clone,
101     delete: wasmtime_component_list_type_delete,
102     equal: wasmtime_component_list_type_equal,
103 }
104 
105 #[unsafe(no_mangle)]
wasmtime_component_list_type_element( ty: &wasmtime_component_list_type_t, type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>, )106 pub extern "C" fn wasmtime_component_list_type_element(
107     ty: &wasmtime_component_list_type_t,
108     type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
109 ) {
110     type_ret.write(ty.ty.ty().into());
111 }
112 
113 type_wrapper! {
114     #[derive(PartialEq)]
115     pub struct wasmtime_component_map_type_t {
116         pub(crate) ty: Map,
117     }
118 
119     clone: wasmtime_component_map_type_clone,
120     delete: wasmtime_component_map_type_delete,
121     equal: wasmtime_component_map_type_equal,
122 }
123 
124 #[unsafe(no_mangle)]
wasmtime_component_map_type_key( ty: &wasmtime_component_map_type_t, type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>, )125 pub extern "C" fn wasmtime_component_map_type_key(
126     ty: &wasmtime_component_map_type_t,
127     type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
128 ) {
129     type_ret.write(ty.ty.key().into());
130 }
131 
132 #[unsafe(no_mangle)]
wasmtime_component_map_type_value( ty: &wasmtime_component_map_type_t, type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>, )133 pub extern "C" fn wasmtime_component_map_type_value(
134     ty: &wasmtime_component_map_type_t,
135     type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
136 ) {
137     type_ret.write(ty.ty.value().into());
138 }
139 
140 type_wrapper! {
141     #[derive(PartialEq)]
142     pub struct wasmtime_component_record_type_t {
143         pub(crate) ty: Record,
144     }
145     clone: wasmtime_component_record_type_clone,
146     delete: wasmtime_component_record_type_delete,
147     equal: wasmtime_component_record_type_equal,
148 }
149 
150 #[unsafe(no_mangle)]
wasmtime_component_record_type_field_count( ty: &wasmtime_component_record_type_t, ) -> usize151 pub extern "C" fn wasmtime_component_record_type_field_count(
152     ty: &wasmtime_component_record_type_t,
153 ) -> usize {
154     ty.ty.fields().len()
155 }
156 
157 #[unsafe(no_mangle)]
wasmtime_component_record_type_field_nth( ty: &wasmtime_component_record_type_t, nth: usize, name_ret: &mut MaybeUninit<*const u8>, name_len_ret: &mut MaybeUninit<usize>, type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>, ) -> bool158 pub extern "C" fn wasmtime_component_record_type_field_nth(
159     ty: &wasmtime_component_record_type_t,
160     nth: usize,
161     name_ret: &mut MaybeUninit<*const u8>,
162     name_len_ret: &mut MaybeUninit<usize>,
163     type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
164 ) -> bool {
165     match ty.ty.fields().nth(nth) {
166         Some(Field { name, ty }) => {
167             let name: &str = name;
168             name_ret.write(name.as_ptr());
169             name_len_ret.write(name.len());
170             type_ret.write(ty.into());
171             true
172         }
173         None => false,
174     }
175 }
176 
177 type_wrapper! {
178     #[derive(PartialEq)]
179     pub struct wasmtime_component_tuple_type_t {
180         pub(crate) ty: Tuple,
181     }
182     clone: wasmtime_component_tuple_type_clone,
183     delete: wasmtime_component_tuple_type_delete,
184     equal: wasmtime_component_tuple_type_equal,
185 }
186 
187 #[unsafe(no_mangle)]
wasmtime_component_tuple_type_types_count( ty: &wasmtime_component_tuple_type_t, ) -> usize188 pub extern "C" fn wasmtime_component_tuple_type_types_count(
189     ty: &wasmtime_component_tuple_type_t,
190 ) -> usize {
191     ty.ty.types().len()
192 }
193 
194 #[unsafe(no_mangle)]
wasmtime_component_tuple_type_types_nth( ty: &wasmtime_component_tuple_type_t, nth: usize, type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>, ) -> bool195 pub extern "C" fn wasmtime_component_tuple_type_types_nth(
196     ty: &wasmtime_component_tuple_type_t,
197     nth: usize,
198     type_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
199 ) -> bool {
200     match ty.ty.types().nth(nth) {
201         Some(item) => {
202             type_ret.write(item.into());
203             true
204         }
205         None => false,
206     }
207 }
208 
209 type_wrapper! {
210     #[derive(PartialEq)]
211     pub struct wasmtime_component_variant_type_t {
212         pub(crate) ty: Variant,
213     }
214     clone: wasmtime_component_variant_type_clone,
215     delete: wasmtime_component_variant_type_delete,
216     equal: wasmtime_component_variant_type_equal,
217 }
218 
219 #[unsafe(no_mangle)]
wasmtime_component_variant_type_case_count( ty: &wasmtime_component_variant_type_t, ) -> usize220 pub extern "C" fn wasmtime_component_variant_type_case_count(
221     ty: &wasmtime_component_variant_type_t,
222 ) -> usize {
223     ty.ty.cases().len()
224 }
225 
226 #[unsafe(no_mangle)]
wasmtime_component_variant_type_case_nth( ty: &wasmtime_component_variant_type_t, nth: usize, name_ret: &mut MaybeUninit<*const u8>, name_len_ret: &mut MaybeUninit<usize>, has_payload_ret: &mut MaybeUninit<bool>, payload_ret: &mut MaybeUninit<wasmtime_component_valtype_t>, ) -> bool227 pub extern "C" fn wasmtime_component_variant_type_case_nth(
228     ty: &wasmtime_component_variant_type_t,
229     nth: usize,
230     name_ret: &mut MaybeUninit<*const u8>,
231     name_len_ret: &mut MaybeUninit<usize>,
232     has_payload_ret: &mut MaybeUninit<bool>,
233     payload_ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
234 ) -> bool {
235     match ty.ty.cases().nth(nth) {
236         Some(Case { name, ty }) => {
237             let name: &str = name;
238             name_ret.write(name.as_ptr());
239             name_len_ret.write(name.len());
240             match ty {
241                 Some(payload) => {
242                     has_payload_ret.write(true);
243                     payload_ret.write(payload.into());
244                 }
245                 None => {
246                     has_payload_ret.write(false);
247                 }
248             }
249             true
250         }
251         None => false,
252     }
253 }
254 
255 type_wrapper! {
256     #[derive(PartialEq)]
257     pub struct wasmtime_component_enum_type_t {
258         pub(crate) ty: Enum,
259     }
260     clone: wasmtime_component_enum_type_clone,
261     delete: wasmtime_component_enum_type_delete,
262     equal: wasmtime_component_enum_type_equal,
263 }
264 
265 #[unsafe(no_mangle)]
wasmtime_component_enum_type_names_count( ty: &wasmtime_component_enum_type_t, ) -> usize266 pub extern "C" fn wasmtime_component_enum_type_names_count(
267     ty: &wasmtime_component_enum_type_t,
268 ) -> usize {
269     ty.ty.names().len()
270 }
271 
272 #[unsafe(no_mangle)]
wasmtime_component_enum_type_names_nth( ty: &wasmtime_component_enum_type_t, nth: usize, name_ret: &mut MaybeUninit<*const u8>, name_len_ret: &mut MaybeUninit<usize>, ) -> bool273 pub extern "C" fn wasmtime_component_enum_type_names_nth(
274     ty: &wasmtime_component_enum_type_t,
275     nth: usize,
276     name_ret: &mut MaybeUninit<*const u8>,
277     name_len_ret: &mut MaybeUninit<usize>,
278 ) -> bool {
279     match ty.ty.names().nth(nth) {
280         Some(name) => {
281             let name: &str = name;
282             name_ret.write(name.as_ptr());
283             name_len_ret.write(name.len());
284             true
285         }
286         None => false,
287     }
288 }
289 
290 type_wrapper! {
291     #[derive(PartialEq)]
292     pub struct wasmtime_component_option_type_t {
293         pub(crate) ty: OptionType,
294     }
295     clone: wasmtime_component_option_type_clone,
296     delete: wasmtime_component_option_type_delete,
297     equal: wasmtime_component_option_type_equal,
298 }
299 
300 #[unsafe(no_mangle)]
wasmtime_component_option_type_ty( ty: &wasmtime_component_option_type_t, ret: &mut MaybeUninit<wasmtime_component_valtype_t>, )301 pub extern "C" fn wasmtime_component_option_type_ty(
302     ty: &wasmtime_component_option_type_t,
303     ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
304 ) {
305     ret.write(ty.ty.ty().into());
306 }
307 
308 type_wrapper! {
309     #[derive(PartialEq)]
310     pub struct wasmtime_component_result_type_t {
311         pub(crate) ty: ResultType,
312     }
313     clone: wasmtime_component_result_type_clone,
314     delete: wasmtime_component_result_type_delete,
315     equal: wasmtime_component_result_type_equal,
316 }
317 
318 #[unsafe(no_mangle)]
wasmtime_component_result_type_ok( ty: &wasmtime_component_result_type_t, ret: &mut MaybeUninit<wasmtime_component_valtype_t>, ) -> bool319 pub extern "C" fn wasmtime_component_result_type_ok(
320     ty: &wasmtime_component_result_type_t,
321     ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
322 ) -> bool {
323     match ty.ty.ok() {
324         Some(ok) => {
325             ret.write(ok.into());
326             true
327         }
328         None => false,
329     }
330 }
331 
332 #[unsafe(no_mangle)]
wasmtime_component_result_type_err( ty: &wasmtime_component_result_type_t, ret: &mut MaybeUninit<wasmtime_component_valtype_t>, ) -> bool333 pub extern "C" fn wasmtime_component_result_type_err(
334     ty: &wasmtime_component_result_type_t,
335     ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
336 ) -> bool {
337     match ty.ty.err() {
338         Some(err) => {
339             ret.write(err.into());
340             true
341         }
342         None => false,
343     }
344 }
345 
346 type_wrapper! {
347     #[derive(PartialEq)]
348     pub struct wasmtime_component_flags_type_t {
349         pub(crate) ty: Flags,
350     }
351     clone: wasmtime_component_flags_type_clone,
352     delete: wasmtime_component_flags_type_delete,
353     equal: wasmtime_component_flags_type_equal,
354 }
355 
356 #[unsafe(no_mangle)]
wasmtime_component_flags_type_names_count( ty: &wasmtime_component_flags_type_t, ) -> usize357 pub extern "C" fn wasmtime_component_flags_type_names_count(
358     ty: &wasmtime_component_flags_type_t,
359 ) -> usize {
360     ty.ty.names().len()
361 }
362 
363 #[unsafe(no_mangle)]
wasmtime_component_flags_type_names_nth( ty: &wasmtime_component_flags_type_t, nth: usize, name_ret: &mut MaybeUninit<*const u8>, name_len_ret: &mut MaybeUninit<usize>, ) -> bool364 pub extern "C" fn wasmtime_component_flags_type_names_nth(
365     ty: &wasmtime_component_flags_type_t,
366     nth: usize,
367     name_ret: &mut MaybeUninit<*const u8>,
368     name_len_ret: &mut MaybeUninit<usize>,
369 ) -> bool {
370     match ty.ty.names().nth(nth) {
371         Some(name) => {
372             let name: &str = name;
373             name_ret.write(name.as_ptr());
374             name_len_ret.write(name.len());
375             true
376         }
377         None => false,
378     }
379 }
380 
381 type_wrapper! {
382     #[derive(PartialEq)]
383     pub struct wasmtime_component_future_type_t {
384         pub(crate) ty: FutureType,
385     }
386     clone: wasmtime_component_future_type_clone,
387     delete: wasmtime_component_future_type_delete,
388     equal: wasmtime_component_future_type_equal,
389 }
390 
391 #[unsafe(no_mangle)]
wasmtime_component_future_type_ty( ty: &wasmtime_component_future_type_t, ret: &mut MaybeUninit<wasmtime_component_valtype_t>, ) -> bool392 pub extern "C" fn wasmtime_component_future_type_ty(
393     ty: &wasmtime_component_future_type_t,
394     ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
395 ) -> bool {
396     match ty.ty.ty() {
397         Some(ty) => {
398             ret.write(ty.into());
399             true
400         }
401         None => false,
402     }
403 }
404 
405 type_wrapper! {
406     #[derive(PartialEq)]
407     pub struct wasmtime_component_stream_type_t {
408         pub(crate) ty: StreamType,
409     }
410     clone: wasmtime_component_stream_type_clone,
411     delete: wasmtime_component_stream_type_delete,
412     equal: wasmtime_component_stream_type_equal,
413 }
414 
415 #[unsafe(no_mangle)]
wasmtime_component_stream_type_ty( ty: &wasmtime_component_stream_type_t, ret: &mut MaybeUninit<wasmtime_component_valtype_t>, ) -> bool416 pub extern "C" fn wasmtime_component_stream_type_ty(
417     ty: &wasmtime_component_stream_type_t,
418     ret: &mut MaybeUninit<wasmtime_component_valtype_t>,
419 ) -> bool {
420     match ty.ty.ty() {
421         Some(ty) => {
422             ret.write(ty.into());
423             true
424         }
425         None => false,
426     }
427 }
428