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