1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use super::super::externals::wasm_extern_t;
use super::{
    wasm_functype_t, wasm_globaltype_t, wasm_memorytype_t, wasm_tabletype_t, WasmFunctionType,
    WasmGlobalType, WasmMemoryType, WasmTableType,
};
use std::convert::{TryFrom, TryInto};
use std::mem;
use thiserror::Error;
use wasmer_api::ExternType;

#[allow(non_camel_case_types)]
pub type wasm_externkind_t = u8;

#[allow(non_camel_case_types)]
#[repr(u8)]
pub enum wasm_externkind_enum {
    WASM_EXTERN_FUNC = 0,
    WASM_EXTERN_GLOBAL = 1,
    WASM_EXTERN_TABLE = 2,
    WASM_EXTERN_MEMORY = 3,
}

impl From<ExternType> for wasm_externkind_enum {
    fn from(other: ExternType) -> Self {
        (&other).into()
    }
}
impl From<&ExternType> for wasm_externkind_enum {
    fn from(other: &ExternType) -> Self {
        match other {
            ExternType::Function(_) => Self::WASM_EXTERN_FUNC,
            ExternType::Global(_) => Self::WASM_EXTERN_GLOBAL,
            ExternType::Table(_) => Self::WASM_EXTERN_TABLE,
            ExternType::Memory(_) => Self::WASM_EXTERN_MEMORY,
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) enum WasmExternType {
    Function(WasmFunctionType),
    Global(WasmGlobalType),
    Table(WasmTableType),
    Memory(WasmMemoryType),
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone)]
pub struct wasm_externtype_t {
    pub(crate) inner: WasmExternType,
}

impl wasm_externtype_t {
    pub(crate) fn new(extern_type: ExternType) -> Self {
        Self {
            inner: match extern_type {
                ExternType::Function(function_type) => {
                    WasmExternType::Function(WasmFunctionType::new(function_type))
                }
                ExternType::Global(global_type) => {
                    WasmExternType::Global(WasmGlobalType::new(global_type))
                }
                ExternType::Table(table_type) => {
                    WasmExternType::Table(WasmTableType::new(table_type))
                }
                ExternType::Memory(memory_type) => {
                    WasmExternType::Memory(WasmMemoryType::new(memory_type))
                }
            },
        }
    }
}

impl From<ExternType> for wasm_externtype_t {
    fn from(extern_type: ExternType) -> Self {
        Self::new(extern_type)
    }
}

impl From<&ExternType> for wasm_externtype_t {
    fn from(other: &ExternType) -> Self {
        other.clone().into()
    }
}

#[no_mangle]
pub unsafe extern "C" fn wasm_extern_type(r#extern: &wasm_extern_t) -> Box<wasm_externtype_t> {
    Box::new(wasm_externtype_t::new(r#extern.ty()))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_extern_kind(r#extern: &wasm_extern_t) -> wasm_externkind_t {
    wasm_externkind_enum::from(r#extern.ty()) as wasm_externkind_t
}

#[no_mangle]
pub unsafe extern "C" fn wasm_externtype_delete(_extern_type: Option<Box<wasm_externtype_t>>) {}

#[no_mangle]
pub extern "C" fn wasm_externtype_copy(extern_type: &wasm_externtype_t) -> Box<wasm_externtype_t> {
    Box::new(extern_type.clone())
}

#[no_mangle]
pub unsafe extern "C" fn wasm_externtype_kind(
    extern_type: &wasm_externtype_t,
) -> wasm_externkind_t {
    (match extern_type.inner {
        WasmExternType::Function(_) => wasm_externkind_enum::WASM_EXTERN_FUNC,
        WasmExternType::Global(_) => wasm_externkind_enum::WASM_EXTERN_GLOBAL,
        WasmExternType::Table(_) => wasm_externkind_enum::WASM_EXTERN_TABLE,
        WasmExternType::Memory(_) => wasm_externkind_enum::WASM_EXTERN_MEMORY,
    }) as wasm_externkind_t
}

#[derive(Debug, Clone, Error)]
#[error("failed to convert from `wasm_externtype_t`: {0}")]
pub struct ExternTypeConversionError(&'static str);

impl From<&'static str> for ExternTypeConversionError {
    fn from(other: &'static str) -> Self {
        Self(other)
    }
}

impl TryFrom<&'static wasm_externtype_t> for &'static wasm_functype_t {
    type Error = ExternTypeConversionError;

    fn try_from(other: &'static wasm_externtype_t) -> Result<Self, Self::Error> {
        if let WasmExternType::Function(_) = other.inner {
            Ok(unsafe { mem::transmute::<&'static wasm_externtype_t, Self>(other) })
        } else {
            Err(ExternTypeConversionError("Wrong type: expected function"))
        }
    }
}

impl TryFrom<&'static wasm_externtype_t> for &'static wasm_globaltype_t {
    type Error = ExternTypeConversionError;

    fn try_from(other: &'static wasm_externtype_t) -> Result<Self, Self::Error> {
        if let WasmExternType::Global(_) = other.inner {
            Ok(unsafe { mem::transmute::<&'static wasm_externtype_t, Self>(other) })
        } else {
            Err(ExternTypeConversionError("Wrong type: expected global"))
        }
    }
}

impl TryFrom<&'static wasm_externtype_t> for &'static wasm_tabletype_t {
    type Error = ExternTypeConversionError;

    fn try_from(other: &'static wasm_externtype_t) -> Result<Self, Self::Error> {
        if let WasmExternType::Table(_) = other.inner {
            Ok(unsafe { mem::transmute::<&'static wasm_externtype_t, Self>(other) })
        } else {
            Err(ExternTypeConversionError("Wrong type: expected table"))
        }
    }
}

impl TryFrom<&'static wasm_externtype_t> for &'static wasm_memorytype_t {
    type Error = ExternTypeConversionError;

    fn try_from(other: &'static wasm_externtype_t) -> Result<Self, Self::Error> {
        if let WasmExternType::Memory(_) = other.inner {
            Ok(unsafe { mem::transmute::<&'static wasm_externtype_t, Self>(other) })
        } else {
            Err(ExternTypeConversionError("Wrong type: expected memory"))
        }
    }
}

#[no_mangle]
pub unsafe extern "C" fn wasm_externtype_as_functype_const(
    extern_type: &'static wasm_externtype_t,
) -> Option<&'static wasm_functype_t> {
    Some(c_try!(extern_type.try_into()))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_externtype_as_functype(
    extern_type: &'static wasm_externtype_t,
) -> Option<&'static wasm_functype_t> {
    Some(c_try!(extern_type.try_into()))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_functype_as_externtype_const(
    function_type: &'static wasm_functype_t,
) -> &'static wasm_externtype_t {
    &function_type.extern_type
}

#[no_mangle]
pub unsafe extern "C" fn wasm_functype_as_externtype(
    function_type: &'static wasm_functype_t,
) -> &'static wasm_externtype_t {
    &function_type.extern_type
}

#[no_mangle]
pub unsafe extern "C" fn wasm_externtype_as_globaltype_const(
    extern_type: &'static wasm_externtype_t,
) -> Option<&'static wasm_globaltype_t> {
    Some(c_try!(extern_type.try_into()))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_externtype_as_globaltype(
    extern_type: &'static wasm_externtype_t,
) -> Option<&'static wasm_globaltype_t> {
    Some(c_try!(extern_type.try_into()))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_globaltype_as_externtype_const(
    global_type: &'static wasm_globaltype_t,
) -> &'static wasm_externtype_t {
    &global_type.extern_type
}

#[no_mangle]
pub unsafe extern "C" fn wasm_globaltype_as_externtype(
    global_type: &'static wasm_globaltype_t,
) -> &'static wasm_externtype_t {
    &global_type.extern_type
}

#[no_mangle]
pub unsafe extern "C" fn wasm_externtype_as_tabletype_const(
    extern_type: &'static wasm_externtype_t,
) -> Option<&'static wasm_tabletype_t> {
    Some(c_try!(extern_type.try_into()))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_externtype_as_tabletype(
    extern_type: &'static wasm_externtype_t,
) -> Option<&'static wasm_tabletype_t> {
    Some(c_try!(extern_type.try_into()))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_tabletype_as_externtype_const(
    table_type: &'static wasm_tabletype_t,
) -> &'static wasm_externtype_t {
    &table_type.extern_type
}

#[no_mangle]
pub unsafe extern "C" fn wasm_tabletype_as_externtype(
    table_type: &'static wasm_tabletype_t,
) -> &'static wasm_externtype_t {
    &table_type.extern_type
}

#[no_mangle]
pub unsafe extern "C" fn wasm_externtype_as_memorytype_const(
    extern_type: &'static wasm_externtype_t,
) -> Option<&'static wasm_memorytype_t> {
    Some(c_try!(extern_type.try_into()))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_externtype_as_memorytype(
    extern_type: &'static wasm_externtype_t,
) -> Option<&'static wasm_memorytype_t> {
    Some(c_try!(extern_type.try_into()))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_memorytype_as_externtype_const(
    memory_type: &'static wasm_memorytype_t,
) -> &'static wasm_externtype_t {
    &memory_type.extern_type
}

#[no_mangle]
pub unsafe extern "C" fn wasm_memorytype_as_externtype(
    memory_type: &'static wasm_memorytype_t,
) -> &'static wasm_externtype_t {
    &memory_type.extern_type
}