Skip to main content

wasmtime_c_api/types/
extern.rs

1use crate::{CFuncType, CGlobalType, CMemoryType, CTableType, CTagType};
2use crate::{wasm_functype_t, wasm_globaltype_t, wasm_memorytype_t, wasm_tabletype_t};
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    Tag(CTagType),
20}
21
22impl CExternType {
23    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
34pub type wasm_externkind_t = u8;
35
36pub const WASM_EXTERN_FUNC: wasm_externkind_t = 0;
37pub const WASM_EXTERN_GLOBAL: wasm_externkind_t = 1;
38pub const WASM_EXTERN_TABLE: wasm_externkind_t = 2;
39pub 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.
42pub const WASMTIME_EXTERNTYPE_TAG: wasm_externkind_t = 4;
43
44impl wasm_externtype_t {
45    pub(crate) fn from_extern_type(ty: ExternType) -> wasm_externtype_t {
46        wasm_externtype_t {
47            which: CExternType::new(ty),
48        }
49    }
50
51    pub(crate) fn from_cextern_type(ty: CExternType) -> wasm_externtype_t {
52        wasm_externtype_t { which: ty }
53    }
54}
55
56#[unsafe(no_mangle)]
57pub 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)]
68pub 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)]
73pub 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)]
80pub 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)]
87pub 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)]
94pub 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)]
101pub 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)]
108pub 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)]
115pub 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}