1use crate::{
2 WasmtimeStoreContext, WasmtimeStoreContextMut, handle_result, wasm_extern_t, wasm_tabletype_t,
3 wasmtime_error_t, wasmtime_val_t,
4};
5#[cfg(feature = "gc")]
6use crate::{wasm_ref_t, wasm_store_t};
7use std::mem::MaybeUninit;
8use wasmtime::{Extern, Table, format_err};
9#[cfg(feature = "gc")]
10use wasmtime::{Ref, RootScope, TableType};
11
12#[derive(Clone)]
13#[repr(transparent)]
14pub struct wasm_table_t {
15 ext: wasm_extern_t,
16}
17
18wasmtime_c_api_macros::declare_ref!(wasm_table_t);
19
20pub type wasm_table_size_t = u32;
21
22impl wasm_table_t {
23 pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_table_t> {
24 match &e.which {
25 Extern::Table(_) => Some(unsafe { &*(e as *const _ as *const _) }),
26 _ => None,
27 }
28 }
29
30 fn table(&self) -> Table {
31 match self.ext.which {
32 Extern::Table(t) => t,
33 _ => unsafe { std::hint::unreachable_unchecked() },
34 }
35 }
36}
37
38#[cfg(feature = "gc")]
39fn option_wasm_ref_t_to_ref(r: Option<&wasm_ref_t>, table_ty: &TableType) -> Ref {
40 r.map(|r| r.r.clone())
41 .unwrap_or_else(|| Ref::null(table_ty.element().heap_type()))
42}
43
44#[unsafe(no_mangle)]
45#[cfg(feature = "gc")]
46pub unsafe extern "C" fn wasm_table_new(
47 store: &mut wasm_store_t,
48 tt: &wasm_tabletype_t,
49 init: Option<&wasm_ref_t>,
50) -> Option<Box<wasm_table_t>> {
51 let tt = tt.ty().ty.clone();
52 let init = option_wasm_ref_t_to_ref(init, &tt);
53 let table = Table::new(store.store.context_mut(), tt, init).ok()?;
54 Some(Box::new(wasm_table_t {
55 ext: wasm_extern_t {
56 store: store.store.clone(),
57 which: table.into(),
58 },
59 }))
60}
61
62#[unsafe(no_mangle)]
63pub unsafe extern "C" fn wasm_table_type(t: &wasm_table_t) -> Box<wasm_tabletype_t> {
64 let table = t.table();
65 let store = t.ext.store.context();
66 Box::new(wasm_tabletype_t::new(table.ty(&store)))
67}
68
69#[unsafe(no_mangle)]
70#[cfg(feature = "gc")]
71pub unsafe extern "C" fn wasm_table_get(
72 t: &mut wasm_table_t,
73 index: wasm_table_size_t,
74) -> Option<Box<wasm_ref_t>> {
75 let table = t.table();
76 let mut store = RootScope::new(t.ext.store.context_mut());
77 let r = table.get(&mut store, u64::from(index))?;
78 wasm_ref_t::new(r)
79}
80
81#[unsafe(no_mangle)]
82#[cfg(feature = "gc")]
83pub unsafe extern "C" fn wasm_table_set(
84 t: &mut wasm_table_t,
85 index: wasm_table_size_t,
86 r: Option<&wasm_ref_t>,
87) -> bool {
88 let table = t.table();
89 let val = option_wasm_ref_t_to_ref(r, &table.ty(t.ext.store.context()));
90 table
91 .set(t.ext.store.context_mut(), u64::from(index), val)
92 .is_ok()
93}
94
95#[unsafe(no_mangle)]
96pub unsafe extern "C" fn wasm_table_size(t: &wasm_table_t) -> wasm_table_size_t {
97 let table = t.table();
98 let store = t.ext.store.context();
99 u32::try_from(table.size(&store)).unwrap()
100}
101
102#[unsafe(no_mangle)]
103#[cfg(feature = "gc")]
104pub unsafe extern "C" fn wasm_table_grow(
105 t: &mut wasm_table_t,
106 delta: wasm_table_size_t,
107 init: Option<&wasm_ref_t>,
108) -> bool {
109 let table = t.table();
110 let init = option_wasm_ref_t_to_ref(init, &table.ty(t.ext.store.context()));
111 table
112 .grow(t.ext.store.context_mut(), u64::from(delta), init)
113 .is_ok()
114}
115
116#[unsafe(no_mangle)]
117pub extern "C" fn wasm_table_as_extern(t: &mut wasm_table_t) -> &mut wasm_extern_t {
118 &mut t.ext
119}
120
121#[unsafe(no_mangle)]
122pub extern "C" fn wasm_table_as_extern_const(t: &wasm_table_t) -> &wasm_extern_t {
123 &t.ext
124}
125
126#[unsafe(no_mangle)]
127pub unsafe extern "C" fn wasmtime_table_new(
128 mut store: WasmtimeStoreContextMut<'_>,
129 tt: &wasm_tabletype_t,
130 init: &wasmtime_val_t,
131 out: &mut Table,
132) -> Option<Box<wasmtime_error_t>> {
133 #[cfg(feature = "gc")]
134 let mut store = RootScope::new(&mut store);
135
136 handle_result(
137 init.to_val(&mut store)
138 .ref_()
139 .ok_or_else(|| format_err!("wasmtime_table_new init value is not a reference"))
140 .and_then(|init| Table::new(&mut store, tt.ty().ty.clone(), init)),
141 |table| *out = table,
142 )
143}
144
145#[unsafe(no_mangle)]
146pub unsafe extern "C" fn wasmtime_table_type(
147 store: WasmtimeStoreContext<'_>,
148 table: &Table,
149) -> Box<wasm_tabletype_t> {
150 Box::new(wasm_tabletype_t::new(table.ty(store)))
151}
152
153#[unsafe(no_mangle)]
154pub extern "C" fn wasmtime_table_get(
155 store: WasmtimeStoreContextMut<'_>,
156 table: &Table,
157 index: u64,
158 ret: &mut MaybeUninit<wasmtime_val_t>,
159) -> bool {
160 let mut store = store;
161
162 #[cfg(feature = "gc")]
163 let mut store = RootScope::new(&mut store);
164
165 match table.get(&mut store, index) {
166 Some(r) => {
167 ret.write(wasmtime_val_t::from_val(&mut store, r.into()));
168 true
169 }
170 None => false,
171 }
172}
173
174#[unsafe(no_mangle)]
175pub unsafe extern "C" fn wasmtime_table_set(
176 mut store: WasmtimeStoreContextMut<'_>,
177 table: &Table,
178 index: u64,
179 val: &wasmtime_val_t,
180) -> Option<Box<wasmtime_error_t>> {
181 #[cfg(feature = "gc")]
182 let mut store = RootScope::new(&mut store);
183
184 handle_result(
185 val.to_val(&mut store)
186 .ref_()
187 .ok_or_else(|| format_err!("wasmtime_table_set value is not a reference"))
188 .and_then(|val| table.set(&mut store, index, val)),
189 |()| {},
190 )
191}
192
193#[unsafe(no_mangle)]
194pub extern "C" fn wasmtime_table_size(store: WasmtimeStoreContext<'_>, table: &Table) -> u64 {
195 table.size(store)
196}
197
198#[unsafe(no_mangle)]
199pub unsafe extern "C" fn wasmtime_table_grow(
200 mut store: WasmtimeStoreContextMut<'_>,
201 table: &Table,
202 delta: u64,
203 val: &wasmtime_val_t,
204 prev_size: &mut u64,
205) -> Option<Box<wasmtime_error_t>> {
206 #[cfg(feature = "gc")]
207 let mut store = RootScope::new(&mut store);
208
209 handle_result(
210 val.to_val(&mut store)
211 .ref_()
212 .ok_or_else(|| format_err!("wasmtime_table_grow value is not a reference"))
213 .and_then(|val| table.grow(&mut store, delta, val)),
214 |prev| *prev_size = prev,
215 )
216}