wasmtime_c_api/types/
memory.rs

1use crate::{wasm_externtype_t, wasm_limits_t, CExternType};
2use std::cell::OnceCell;
3use std::convert::TryFrom;
4use wasmtime::{MemoryType, MemoryTypeBuilder};
5
6#[repr(transparent)]
7#[derive(Clone)]
8pub struct wasm_memorytype_t {
9    ext: wasm_externtype_t,
10}
11
12wasmtime_c_api_macros::declare_ty!(wasm_memorytype_t);
13
14#[derive(Clone)]
15pub(crate) struct CMemoryType {
16    pub(crate) ty: MemoryType,
17    limits_cache: OnceCell<wasm_limits_t>,
18}
19
20impl wasm_memorytype_t {
21    pub(crate) fn new(ty: MemoryType) -> wasm_memorytype_t {
22        wasm_memorytype_t {
23            ext: wasm_externtype_t::from_extern_type(ty.into()),
24        }
25    }
26
27    pub(crate) fn try_from(e: &wasm_externtype_t) -> Option<&wasm_memorytype_t> {
28        match &e.which {
29            CExternType::Memory(_) => Some(unsafe { &*(e as *const _ as *const _) }),
30            _ => None,
31        }
32    }
33
34    pub(crate) fn ty(&self) -> &CMemoryType {
35        match &self.ext.which {
36            CExternType::Memory(f) => &f,
37            _ => unsafe { std::hint::unreachable_unchecked() },
38        }
39    }
40}
41
42impl CMemoryType {
43    pub(crate) fn new(ty: MemoryType) -> CMemoryType {
44        CMemoryType {
45            ty,
46            limits_cache: OnceCell::new(),
47        }
48    }
49}
50
51#[unsafe(no_mangle)]
52pub extern "C" fn wasm_memorytype_new(limits: &wasm_limits_t) -> Box<wasm_memorytype_t> {
53    Box::new(wasm_memorytype_t::new(MemoryType::new(
54        limits.min,
55        limits.max(),
56    )))
57}
58
59#[unsafe(no_mangle)]
60pub extern "C" fn wasm_memorytype_limits(mt: &wasm_memorytype_t) -> &wasm_limits_t {
61    let mt = mt.ty();
62    mt.limits_cache.get_or_init(|| wasm_limits_t {
63        min: u32::try_from(mt.ty.minimum()).unwrap(),
64        max: u32::try_from(mt.ty.maximum().unwrap_or(u64::from(u32::max_value()))).unwrap(),
65    })
66}
67
68#[unsafe(no_mangle)]
69pub extern "C" fn wasmtime_memorytype_new(
70    minimum: u64,
71    maximum_specified: bool,
72    maximum: u64,
73    memory64: bool,
74    shared: bool,
75) -> Box<wasm_memorytype_t> {
76    let maximum = if maximum_specified {
77        Some(maximum)
78    } else {
79        None
80    };
81
82    Box::new(wasm_memorytype_t::new(
83        MemoryTypeBuilder::default()
84            .min(minimum)
85            .max(maximum)
86            .memory64(memory64)
87            .shared(shared)
88            .build()
89            .unwrap(),
90    ))
91}
92
93#[unsafe(no_mangle)]
94pub extern "C" fn wasmtime_memorytype_minimum(mt: &wasm_memorytype_t) -> u64 {
95    mt.ty().ty.minimum()
96}
97
98#[unsafe(no_mangle)]
99pub extern "C" fn wasmtime_memorytype_maximum(mt: &wasm_memorytype_t, out: &mut u64) -> bool {
100    match mt.ty().ty.maximum() {
101        Some(max) => {
102            *out = max;
103            true
104        }
105        None => false,
106    }
107}
108
109#[unsafe(no_mangle)]
110pub extern "C" fn wasmtime_memorytype_is64(mt: &wasm_memorytype_t) -> bool {
111    mt.ty().ty.is_64()
112}
113
114#[unsafe(no_mangle)]
115pub extern "C" fn wasmtime_memorytype_isshared(mt: &wasm_memorytype_t) -> bool {
116    mt.ty().ty.is_shared()
117}
118
119#[unsafe(no_mangle)]
120pub extern "C" fn wasm_memorytype_as_externtype(ty: &wasm_memorytype_t) -> &wasm_externtype_t {
121    &ty.ext
122}
123
124#[unsafe(no_mangle)]
125pub extern "C" fn wasm_memorytype_as_externtype_const(
126    ty: &wasm_memorytype_t,
127) -> &wasm_externtype_t {
128    &ty.ext
129}