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        }
29    }
30}
31
32pub type wasm_externkind_t = u8;
33
34pub const WASM_EXTERN_FUNC: wasm_externkind_t = 0;
35pub const WASM_EXTERN_GLOBAL: wasm_externkind_t = 1;
36pub const WASM_EXTERN_TABLE: wasm_externkind_t = 2;
37pub const WASM_EXTERN_MEMORY: wasm_externkind_t = 3;
38
39impl wasm_externtype_t {
40    pub(crate) fn from_extern_type(ty: ExternType) -> wasm_externtype_t {
41        wasm_externtype_t {
42            which: CExternType::new(ty),
43        }
44    }
45
46    pub(crate) fn from_cextern_type(ty: CExternType) -> wasm_externtype_t {
47        wasm_externtype_t { which: ty }
48    }
49}
50
51#[unsafe(no_mangle)]
52pub extern "C" fn wasm_externtype_kind(et: &wasm_externtype_t) -> wasm_externkind_t {
53    match &et.which {
54        CExternType::Func(_) => WASM_EXTERN_FUNC,
55        CExternType::Table(_) => WASM_EXTERN_TABLE,
56        CExternType::Global(_) => WASM_EXTERN_GLOBAL,
57        CExternType::Memory(_) => WASM_EXTERN_MEMORY,
58    }
59}
60
61#[unsafe(no_mangle)]
62pub extern "C" fn wasm_externtype_as_functype(et: &wasm_externtype_t) -> Option<&wasm_functype_t> {
63    wasm_externtype_as_functype_const(et)
64}
65
66#[unsafe(no_mangle)]
67pub extern "C" fn wasm_externtype_as_functype_const(
68    et: &wasm_externtype_t,
69) -> Option<&wasm_functype_t> {
70    wasm_functype_t::try_from(et)
71}
72
73#[unsafe(no_mangle)]
74pub extern "C" fn wasm_externtype_as_globaltype(
75    et: &wasm_externtype_t,
76) -> Option<&wasm_globaltype_t> {
77    wasm_externtype_as_globaltype_const(et)
78}
79
80#[unsafe(no_mangle)]
81pub extern "C" fn wasm_externtype_as_globaltype_const(
82    et: &wasm_externtype_t,
83) -> Option<&wasm_globaltype_t> {
84    wasm_globaltype_t::try_from(et)
85}
86
87#[unsafe(no_mangle)]
88pub extern "C" fn wasm_externtype_as_tabletype(
89    et: &wasm_externtype_t,
90) -> Option<&wasm_tabletype_t> {
91    wasm_externtype_as_tabletype_const(et)
92}
93
94#[unsafe(no_mangle)]
95pub extern "C" fn wasm_externtype_as_tabletype_const(
96    et: &wasm_externtype_t,
97) -> Option<&wasm_tabletype_t> {
98    wasm_tabletype_t::try_from(et)
99}
100
101#[unsafe(no_mangle)]
102pub extern "C" fn wasm_externtype_as_memorytype(
103    et: &wasm_externtype_t,
104) -> Option<&wasm_memorytype_t> {
105    wasm_externtype_as_memorytype_const(et)
106}
107
108#[unsafe(no_mangle)]
109pub extern "C" fn wasm_externtype_as_memorytype_const(
110    et: &wasm_externtype_t,
111) -> Option<&wasm_memorytype_t> {
112    wasm_memorytype_t::try_from(et)
113}