1use crate::{wasm_extern_t, wasm_ref_t, wasm_store_t, wasm_tabletype_t, WasmRef};
2use alloc::boxed::Box;
3use core::hint;
4use wasmi::{Extern, ExternRef, FuncRef, Table, TableType};
5
6#[derive(Clone)]
10#[repr(transparent)]
11pub struct wasm_table_t {
12 inner: wasm_extern_t,
13}
14
15wasmi_c_api_macros::declare_ref!(wasm_table_t);
16
17pub type wasm_table_size_t = u32;
19
20impl wasm_table_t {
21 pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_table_t> {
22 match &e.which {
23 Extern::Table(_) => Some(unsafe { &*(e as *const _ as *const _) }),
24 _ => None,
25 }
26 }
27
28 pub(crate) fn try_from_mut(e: &mut wasm_extern_t) -> Option<&mut wasm_table_t> {
29 match &mut e.which {
30 Extern::Table(_) => Some(unsafe { &mut *(e as *mut _ as *mut _) }),
31 _ => None,
32 }
33 }
34
35 fn table(&self) -> Table {
37 match self.inner.which {
38 Extern::Table(t) => t,
39 _ => unsafe { hint::unreachable_unchecked() },
40 }
41 }
42}
43
44fn option_wasm_ref_t_to_ref(r: Option<&wasm_ref_t>, table_ty: &TableType) -> WasmRef {
48 r.map(|r| r.inner.clone())
49 .unwrap_or_else(|| match table_ty.element() {
50 wasmi::core::ValType::FuncRef => WasmRef::Func(FuncRef::null()),
51 wasmi::core::ValType::ExternRef => WasmRef::Extern(ExternRef::null()),
52 invalid => panic!("encountered invalid table type: {invalid:?}"),
53 })
54}
55
56#[no_mangle]
65#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
66pub unsafe extern "C" fn wasm_table_new(
67 store: &mut wasm_store_t,
68 tt: &wasm_tabletype_t,
69 init: Option<&wasm_ref_t>,
70) -> Option<Box<wasm_table_t>> {
71 let tt = tt.ty().ty;
72 let init = option_wasm_ref_t_to_ref(init, &tt);
73 let table = Table::new(store.inner.context_mut(), tt, init.into()).ok()?;
74 Some(Box::new(wasm_table_t {
75 inner: wasm_extern_t {
76 store: store.inner.clone(),
77 which: table.into(),
78 },
79 }))
80}
81
82#[no_mangle]
91#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
92pub unsafe extern "C" fn wasm_table_type(t: &wasm_table_t) -> Box<wasm_tabletype_t> {
93 let table = t.table();
94 let store = t.inner.store.context();
95 Box::new(wasm_tabletype_t::new(table.ty(store)))
96}
97
98#[no_mangle]
107#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
108pub unsafe extern "C" fn wasm_table_get(
109 t: &mut wasm_table_t,
110 index: wasm_table_size_t,
111) -> Option<Box<wasm_ref_t>> {
112 let table = t.table();
113 let value = table.get(t.inner.store.context_mut(), index)?;
114 let wasm_ref = match value {
115 wasmi::Val::FuncRef(r) => WasmRef::Func(r),
116 wasmi::Val::ExternRef(r) => WasmRef::Extern(r),
117 invalid => panic!("encountered invalid value in table at {index}: {invalid:?}"),
118 };
119 wasm_ref_t::new(wasm_ref)
120}
121
122#[no_mangle]
131#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
132pub unsafe extern "C" fn wasm_table_set(
133 t: &mut wasm_table_t,
134 index: wasm_table_size_t,
135 new_value: Option<&wasm_ref_t>,
136) -> bool {
137 let table = t.table();
138 let new_value = option_wasm_ref_t_to_ref(new_value, &table.ty(t.inner.store.context()));
139 table
140 .set(t.inner.store.context_mut(), index, new_value.into())
141 .is_ok()
142}
143
144#[no_mangle]
153#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
154pub unsafe extern "C" fn wasm_table_size(t: &wasm_table_t) -> wasm_table_size_t {
155 let table = t.table();
156 let store = t.inner.store.context();
157 table.size(store)
158}
159#[no_mangle]
170#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
171pub unsafe extern "C" fn wasm_table_grow(
172 t: &mut wasm_table_t,
173 delta: wasm_table_size_t,
174 init: Option<&wasm_ref_t>,
175) -> bool {
176 let table = t.table();
177 let init = option_wasm_ref_t_to_ref(init, &table.ty(t.inner.store.context()));
178 table
179 .grow(t.inner.store.context_mut(), delta, init.into())
180 .is_ok()
181}
182
183#[no_mangle]
185#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
186pub extern "C" fn wasm_table_as_extern(t: &mut wasm_table_t) -> &mut wasm_extern_t {
187 &mut t.inner
188}
189
190#[no_mangle]
192#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
193pub extern "C" fn wasm_table_as_extern_const(t: &wasm_table_t) -> &wasm_extern_t {
194 &t.inner
195}