wasmtime_c_api/types/
extern.rs

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