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