Skip to main content

wasmtime_c_api/
table.rs

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 r = table.get(t.ext.store.context_mut(), u64::from(index))?;
77    wasm_ref_t::new(r)
78}
79
80#[unsafe(no_mangle)]
81#[cfg(feature = "gc")]
82pub unsafe extern "C" fn wasm_table_set(
83    t: &mut wasm_table_t,
84    index: wasm_table_size_t,
85    r: Option<&wasm_ref_t>,
86) -> bool {
87    let table = t.table();
88    let val = option_wasm_ref_t_to_ref(r, &table.ty(t.ext.store.context()));
89    table
90        .set(t.ext.store.context_mut(), u64::from(index), val)
91        .is_ok()
92}
93
94#[unsafe(no_mangle)]
95pub unsafe extern "C" fn wasm_table_size(t: &wasm_table_t) -> wasm_table_size_t {
96    let table = t.table();
97    let store = t.ext.store.context();
98    u32::try_from(table.size(&store)).unwrap()
99}
100
101#[unsafe(no_mangle)]
102#[cfg(feature = "gc")]
103pub unsafe extern "C" fn wasm_table_grow(
104    t: &mut wasm_table_t,
105    delta: wasm_table_size_t,
106    init: Option<&wasm_ref_t>,
107) -> bool {
108    let table = t.table();
109    let init = option_wasm_ref_t_to_ref(init, &table.ty(t.ext.store.context()));
110    table
111        .grow(t.ext.store.context_mut(), u64::from(delta), init)
112        .is_ok()
113}
114
115#[unsafe(no_mangle)]
116pub extern "C" fn wasm_table_as_extern(t: &mut wasm_table_t) -> &mut wasm_extern_t {
117    &mut t.ext
118}
119
120#[unsafe(no_mangle)]
121pub extern "C" fn wasm_table_as_extern_const(t: &wasm_table_t) -> &wasm_extern_t {
122    &t.ext
123}
124
125#[unsafe(no_mangle)]
126pub unsafe extern "C" fn wasmtime_table_new(
127    mut store: WasmtimeStoreContextMut<'_>,
128    tt: &wasm_tabletype_t,
129    init: &wasmtime_val_t,
130    out: &mut Table,
131) -> Option<Box<wasmtime_error_t>> {
132    #[cfg(feature = "gc")]
133    let mut store = RootScope::new(&mut store);
134
135    handle_result(
136        init.to_val(&mut store)
137            .ref_()
138            .ok_or_else(|| format_err!("wasmtime_table_new init value is not a reference"))
139            .and_then(|init| Table::new(&mut store, tt.ty().ty.clone(), init)),
140        |table| *out = table,
141    )
142}
143
144#[unsafe(no_mangle)]
145pub unsafe extern "C" fn wasmtime_table_type(
146    store: WasmtimeStoreContext<'_>,
147    table: &Table,
148) -> Box<wasm_tabletype_t> {
149    Box::new(wasm_tabletype_t::new(table.ty(store)))
150}
151
152#[unsafe(no_mangle)]
153pub extern "C" fn wasmtime_table_get(
154    store: WasmtimeStoreContextMut<'_>,
155    table: &Table,
156    index: u64,
157    ret: &mut MaybeUninit<wasmtime_val_t>,
158) -> bool {
159    let mut store = store;
160
161    #[cfg(feature = "gc")]
162    let mut store = RootScope::new(&mut store);
163
164    match table.get(&mut store, index) {
165        Some(r) => {
166            crate::initialize(ret, wasmtime_val_t::from_val(&mut store, r.into()));
167            true
168        }
169        None => false,
170    }
171}
172
173#[unsafe(no_mangle)]
174pub unsafe extern "C" fn wasmtime_table_set(
175    mut store: WasmtimeStoreContextMut<'_>,
176    table: &Table,
177    index: u64,
178    val: &wasmtime_val_t,
179) -> Option<Box<wasmtime_error_t>> {
180    #[cfg(feature = "gc")]
181    let mut store = RootScope::new(&mut store);
182
183    handle_result(
184        val.to_val(&mut store)
185            .ref_()
186            .ok_or_else(|| format_err!("wasmtime_table_set value is not a reference"))
187            .and_then(|val| table.set(&mut store, index, val)),
188        |()| {},
189    )
190}
191
192#[unsafe(no_mangle)]
193pub extern "C" fn wasmtime_table_size(store: WasmtimeStoreContext<'_>, table: &Table) -> u64 {
194    table.size(store)
195}
196
197#[unsafe(no_mangle)]
198pub unsafe extern "C" fn wasmtime_table_grow(
199    mut store: WasmtimeStoreContextMut<'_>,
200    table: &Table,
201    delta: u64,
202    val: &wasmtime_val_t,
203    prev_size: &mut u64,
204) -> Option<Box<wasmtime_error_t>> {
205    #[cfg(feature = "gc")]
206    let mut store = RootScope::new(&mut store);
207
208    handle_result(
209        val.to_val(&mut store)
210            .ref_()
211            .ok_or_else(|| format_err!("wasmtime_table_grow value is not a reference"))
212            .and_then(|val| table.grow(&mut store, delta, val)),
213        |prev| *prev_size = prev,
214    )
215}