wasmtime_c_api/types/
export.rs

1use crate::{wasm_externtype_t, wasm_name_t, CExternType};
2use std::cell::OnceCell;
3
4#[repr(C)]
5#[derive(Clone)]
6pub struct wasm_exporttype_t {
7    name: String,
8    ty: CExternType,
9    name_cache: OnceCell<wasm_name_t>,
10    type_cache: OnceCell<wasm_externtype_t>,
11}
12
13wasmtime_c_api_macros::declare_ty!(wasm_exporttype_t);
14
15impl wasm_exporttype_t {
16    pub(crate) fn new(name: String, ty: CExternType) -> wasm_exporttype_t {
17        wasm_exporttype_t {
18            name,
19            ty,
20            name_cache: OnceCell::new(),
21            type_cache: OnceCell::new(),
22        }
23    }
24}
25
26#[unsafe(no_mangle)]
27pub extern "C" fn wasm_exporttype_new(
28    name: &mut wasm_name_t,
29    ty: Box<wasm_externtype_t>,
30) -> Option<Box<wasm_exporttype_t>> {
31    let name = name.take();
32    let name = String::from_utf8(name).ok()?;
33    Some(Box::new(wasm_exporttype_t::new(name, ty.which.clone())))
34}
35
36#[unsafe(no_mangle)]
37pub extern "C" fn wasm_exporttype_name(et: &wasm_exporttype_t) -> &wasm_name_t {
38    et.name_cache
39        .get_or_init(|| wasm_name_t::from_name(et.name.clone()))
40}
41
42#[unsafe(no_mangle)]
43pub extern "C" fn wasm_exporttype_type(et: &wasm_exporttype_t) -> &wasm_externtype_t {
44    et.type_cache
45        .get_or_init(|| wasm_externtype_t::from_cextern_type(et.ty.clone()))
46}