wasmtime_c_api/types/
global.rs1use crate::{CExternType, wasm_externtype_t, wasm_valtype_t};
2use wasmtime::GlobalType;
3
4pub type wasm_mutability_t = u8;
5
6pub const WASM_CONST: wasm_mutability_t = 0;
7pub const WASM_VAR: wasm_mutability_t = 1;
8
9#[repr(transparent)]
10#[derive(Clone)]
11pub struct wasm_globaltype_t {
12 ext: wasm_externtype_t,
13}
14
15wasmtime_c_api_macros::declare_ty!(wasm_globaltype_t);
16
17#[derive(Clone)]
18pub(crate) struct CGlobalType {
19 pub(crate) ty: GlobalType,
20}
21
22impl wasm_globaltype_t {
23 pub(crate) fn new(ty: GlobalType) -> wasm_globaltype_t {
24 wasm_globaltype_t {
25 ext: wasm_externtype_t::from_extern_type(ty.into()),
26 }
27 }
28
29 pub(crate) fn try_from(e: &wasm_externtype_t) -> Option<&wasm_globaltype_t> {
30 match &e.which {
31 CExternType::Global(_) => Some(unsafe { &*(e as *const _ as *const _) }),
32 _ => None,
33 }
34 }
35
36 pub(crate) fn ty(&self) -> &CGlobalType {
37 match &self.ext.which {
38 CExternType::Global(f) => &f,
39 _ => unsafe { std::hint::unreachable_unchecked() },
40 }
41 }
42}
43
44impl CGlobalType {
45 pub(crate) fn new(ty: GlobalType) -> CGlobalType {
46 CGlobalType { ty }
47 }
48}
49
50#[unsafe(no_mangle)]
51pub extern "C" fn wasm_globaltype_new(
52 ty: Box<wasm_valtype_t>,
53 mutability: wasm_mutability_t,
54) -> Option<Box<wasm_globaltype_t>> {
55 use wasmtime::Mutability::*;
56 let mutability = match mutability {
57 WASM_CONST => Const,
58 WASM_VAR => Var,
59 _ => return None,
60 };
61 let ty = GlobalType::new((*ty).clone(), mutability);
62 Some(Box::new(wasm_globaltype_t::new(ty)))
63}
64
65#[unsafe(no_mangle)]
66pub extern "C" fn wasm_globaltype_content(gt: &wasm_globaltype_t) -> &wasm_valtype_t {
67 gt.ty().ty.content()
68}
69
70#[unsafe(no_mangle)]
71pub extern "C" fn wasm_globaltype_mutability(gt: &wasm_globaltype_t) -> wasm_mutability_t {
72 use wasmtime::Mutability::*;
73 let gt = gt.ty();
74 match gt.ty.mutability() {
75 Const => WASM_CONST,
76 Var => WASM_VAR,
77 }
78}
79
80#[unsafe(no_mangle)]
81pub extern "C" fn wasm_globaltype_as_externtype(ty: &wasm_globaltype_t) -> &wasm_externtype_t {
82 &ty.ext
83}
84
85#[unsafe(no_mangle)]
86pub extern "C" fn wasm_globaltype_as_externtype_const(
87 ty: &wasm_globaltype_t,
88) -> &wasm_externtype_t {
89 &ty.ext
90}