1 use crate::{CFuncType, CGlobalType, CMemoryType, CTableType, CTagType};
2 use crate::{wasm_functype_t, wasm_globaltype_t, wasm_memorytype_t, wasm_tabletype_t};
3 use wasmtime::ExternType;
4 
5 #[repr(C)]
6 #[derive(Clone)]
7 pub struct wasm_externtype_t {
8     pub(crate) which: CExternType,
9 }
10 
11 wasmtime_c_api_macros::declare_ty!(wasm_externtype_t);
12 
13 #[derive(Clone)]
14 pub(crate) enum CExternType {
15     Func(CFuncType),
16     Global(CGlobalType),
17     Memory(CMemoryType),
18     Table(CTableType),
19     Tag(CTagType),
20 }
21 
22 impl CExternType {
new(ty: ExternType) -> CExternType23     pub(crate) fn new(ty: ExternType) -> CExternType {
24         match ty {
25             ExternType::Func(f) => CExternType::Func(CFuncType::new(f)),
26             ExternType::Global(f) => CExternType::Global(CGlobalType::new(f)),
27             ExternType::Memory(f) => CExternType::Memory(CMemoryType::new(f)),
28             ExternType::Table(f) => CExternType::Table(CTableType::new(f)),
29             ExternType::Tag(t) => CExternType::Tag(CTagType::new(t)),
30         }
31     }
32 }
33 
34 pub type wasm_externkind_t = u8;
35 
36 pub const WASM_EXTERN_FUNC: wasm_externkind_t = 0;
37 pub const WASM_EXTERN_GLOBAL: wasm_externkind_t = 1;
38 pub const WASM_EXTERN_TABLE: wasm_externkind_t = 2;
39 pub const WASM_EXTERN_MEMORY: wasm_externkind_t = 3;
40 /// Value returned by `wasm_externtype_kind` for exception tags.
41 /// This extends the `wasm_externkind_t` range (0-3 in wasm.h) with tag support.
42 pub const WASMTIME_EXTERNTYPE_TAG: wasm_externkind_t = 4;
43 
44 impl wasm_externtype_t {
from_extern_type(ty: ExternType) -> wasm_externtype_t45     pub(crate) fn from_extern_type(ty: ExternType) -> wasm_externtype_t {
46         wasm_externtype_t {
47             which: CExternType::new(ty),
48         }
49     }
50 
from_cextern_type(ty: CExternType) -> wasm_externtype_t51     pub(crate) fn from_cextern_type(ty: CExternType) -> wasm_externtype_t {
52         wasm_externtype_t { which: ty }
53     }
54 }
55 
56 #[unsafe(no_mangle)]
wasm_externtype_kind(et: &wasm_externtype_t) -> wasm_externkind_t57 pub extern "C" fn wasm_externtype_kind(et: &wasm_externtype_t) -> wasm_externkind_t {
58     match &et.which {
59         CExternType::Func(_) => WASM_EXTERN_FUNC,
60         CExternType::Table(_) => WASM_EXTERN_TABLE,
61         CExternType::Global(_) => WASM_EXTERN_GLOBAL,
62         CExternType::Memory(_) => WASM_EXTERN_MEMORY,
63         CExternType::Tag(_) => WASMTIME_EXTERNTYPE_TAG,
64     }
65 }
66 
67 #[unsafe(no_mangle)]
wasm_externtype_as_functype(et: &wasm_externtype_t) -> Option<&wasm_functype_t>68 pub extern "C" fn wasm_externtype_as_functype(et: &wasm_externtype_t) -> Option<&wasm_functype_t> {
69     wasm_externtype_as_functype_const(et)
70 }
71 
72 #[unsafe(no_mangle)]
wasm_externtype_as_functype_const( et: &wasm_externtype_t, ) -> Option<&wasm_functype_t>73 pub extern "C" fn wasm_externtype_as_functype_const(
74     et: &wasm_externtype_t,
75 ) -> Option<&wasm_functype_t> {
76     wasm_functype_t::try_from(et)
77 }
78 
79 #[unsafe(no_mangle)]
wasm_externtype_as_globaltype( et: &wasm_externtype_t, ) -> Option<&wasm_globaltype_t>80 pub extern "C" fn wasm_externtype_as_globaltype(
81     et: &wasm_externtype_t,
82 ) -> Option<&wasm_globaltype_t> {
83     wasm_externtype_as_globaltype_const(et)
84 }
85 
86 #[unsafe(no_mangle)]
wasm_externtype_as_globaltype_const( et: &wasm_externtype_t, ) -> Option<&wasm_globaltype_t>87 pub extern "C" fn wasm_externtype_as_globaltype_const(
88     et: &wasm_externtype_t,
89 ) -> Option<&wasm_globaltype_t> {
90     wasm_globaltype_t::try_from(et)
91 }
92 
93 #[unsafe(no_mangle)]
wasm_externtype_as_tabletype( et: &wasm_externtype_t, ) -> Option<&wasm_tabletype_t>94 pub extern "C" fn wasm_externtype_as_tabletype(
95     et: &wasm_externtype_t,
96 ) -> Option<&wasm_tabletype_t> {
97     wasm_externtype_as_tabletype_const(et)
98 }
99 
100 #[unsafe(no_mangle)]
wasm_externtype_as_tabletype_const( et: &wasm_externtype_t, ) -> Option<&wasm_tabletype_t>101 pub extern "C" fn wasm_externtype_as_tabletype_const(
102     et: &wasm_externtype_t,
103 ) -> Option<&wasm_tabletype_t> {
104     wasm_tabletype_t::try_from(et)
105 }
106 
107 #[unsafe(no_mangle)]
wasm_externtype_as_memorytype( et: &wasm_externtype_t, ) -> Option<&wasm_memorytype_t>108 pub extern "C" fn wasm_externtype_as_memorytype(
109     et: &wasm_externtype_t,
110 ) -> Option<&wasm_memorytype_t> {
111     wasm_externtype_as_memorytype_const(et)
112 }
113 
114 #[unsafe(no_mangle)]
wasm_externtype_as_memorytype_const( et: &wasm_externtype_t, ) -> Option<&wasm_memorytype_t>115 pub extern "C" fn wasm_externtype_as_memorytype_const(
116     et: &wasm_externtype_t,
117 ) -> Option<&wasm_memorytype_t> {
118     wasm_memorytype_t::try_from(et)
119 }
120