xref: /wasmtime-44.0.1/crates/c-api/src/table.rs (revision 331b0dee)
1 use crate::r#ref::{ref_to_val, val_into_ref};
2 use crate::{
3     handle_result, wasm_extern_t, wasm_ref_t, wasm_store_t, wasm_tabletype_t, wasmtime_error_t,
4     wasmtime_val_t, CStoreContext, CStoreContextMut,
5 };
6 use std::mem::MaybeUninit;
7 use wasmtime::{Extern, Table, TableType, Val, ValType};
8 
9 #[derive(Clone)]
10 #[repr(transparent)]
11 pub struct wasm_table_t {
12     ext: wasm_extern_t,
13 }
14 
15 wasmtime_c_api_macros::declare_ref!(wasm_table_t);
16 
17 pub type wasm_table_size_t = u32;
18 
19 impl wasm_table_t {
20     pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_table_t> {
21         match &e.which {
22             Extern::Table(_) => Some(unsafe { &*(e as *const _ as *const _) }),
23             _ => None,
24         }
25     }
26 
27     fn table(&self) -> Table {
28         match self.ext.which {
29             Extern::Table(t) => t,
30             _ => unsafe { std::hint::unreachable_unchecked() },
31         }
32     }
33 }
34 
35 fn ref_to_val_for_table(r: Option<&wasm_ref_t>, table_ty: &TableType) -> Val {
36     r.map_or_else(
37         || match table_ty.element() {
38             ValType::FuncRef => Val::FuncRef(None),
39             ValType::ExternRef => Val::ExternRef(None),
40             ty => panic!("unsupported table element type: {:?}", ty),
41         },
42         |r| ref_to_val(r),
43     )
44 }
45 
46 #[no_mangle]
47 pub unsafe extern "C" fn wasm_table_new(
48     store: &mut wasm_store_t,
49     tt: &wasm_tabletype_t,
50     init: Option<&wasm_ref_t>,
51 ) -> Option<Box<wasm_table_t>> {
52     let init = ref_to_val_for_table(init, &tt.ty().ty);
53     let table = Table::new(store.store.context_mut(), tt.ty().ty.clone(), 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 #[no_mangle]
63 pub 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 #[no_mangle]
70 pub unsafe extern "C" fn wasm_table_get(
71     t: &mut wasm_table_t,
72     index: wasm_table_size_t,
73 ) -> Option<Box<wasm_ref_t>> {
74     let table = t.table();
75     let val = table.get(t.ext.store.context_mut(), index)?;
76     val_into_ref(val)
77 }
78 
79 #[no_mangle]
80 pub unsafe extern "C" fn wasm_table_set(
81     t: &mut wasm_table_t,
82     index: wasm_table_size_t,
83     r: Option<&wasm_ref_t>,
84 ) -> bool {
85     let table = t.table();
86     let val = ref_to_val_for_table(r, &table.ty(t.ext.store.context()));
87     table.set(t.ext.store.context_mut(), index, val).is_ok()
88 }
89 
90 #[no_mangle]
91 pub unsafe extern "C" fn wasm_table_size(t: &wasm_table_t) -> wasm_table_size_t {
92     let table = t.table();
93     let store = t.ext.store.context();
94     table.size(&store)
95 }
96 
97 #[no_mangle]
98 pub unsafe extern "C" fn wasm_table_grow(
99     t: &mut wasm_table_t,
100     delta: wasm_table_size_t,
101     init: Option<&wasm_ref_t>,
102 ) -> bool {
103     let table = t.table();
104     let init = ref_to_val_for_table(init, &table.ty(t.ext.store.context()));
105     table.grow(t.ext.store.context_mut(), delta, init).is_ok()
106 }
107 
108 #[no_mangle]
109 pub extern "C" fn wasm_table_as_extern(t: &mut wasm_table_t) -> &mut wasm_extern_t {
110     &mut t.ext
111 }
112 
113 #[no_mangle]
114 pub extern "C" fn wasm_table_as_extern_const(t: &wasm_table_t) -> &wasm_extern_t {
115     &t.ext
116 }
117 
118 #[no_mangle]
119 pub unsafe extern "C" fn wasmtime_table_new(
120     store: CStoreContextMut<'_>,
121     tt: &wasm_tabletype_t,
122     init: &wasmtime_val_t,
123     out: &mut Table,
124 ) -> Option<Box<wasmtime_error_t>> {
125     handle_result(
126         Table::new(store, tt.ty().ty.clone(), init.to_val()),
127         |table| *out = table,
128     )
129 }
130 
131 #[no_mangle]
132 pub unsafe extern "C" fn wasmtime_table_type(
133     store: CStoreContext<'_>,
134     table: &Table,
135 ) -> Box<wasm_tabletype_t> {
136     Box::new(wasm_tabletype_t::new(table.ty(store)))
137 }
138 
139 #[no_mangle]
140 pub extern "C" fn wasmtime_table_get(
141     store: CStoreContextMut<'_>,
142     table: &Table,
143     index: u32,
144     ret: &mut MaybeUninit<wasmtime_val_t>,
145 ) -> bool {
146     match table.get(store, index) {
147         Some(val) => {
148             crate::initialize(ret, wasmtime_val_t::from_val(val));
149             true
150         }
151         None => false,
152     }
153 }
154 
155 #[no_mangle]
156 pub unsafe extern "C" fn wasmtime_table_set(
157     store: CStoreContextMut<'_>,
158     table: &Table,
159     index: u32,
160     val: &wasmtime_val_t,
161 ) -> Option<Box<wasmtime_error_t>> {
162     handle_result(table.set(store, index, val.to_val()), |()| {})
163 }
164 
165 #[no_mangle]
166 pub extern "C" fn wasmtime_table_size(store: CStoreContext<'_>, table: &Table) -> u32 {
167     table.size(store)
168 }
169 
170 #[no_mangle]
171 pub unsafe extern "C" fn wasmtime_table_grow(
172     store: CStoreContextMut<'_>,
173     table: &Table,
174     delta: u32,
175     val: &wasmtime_val_t,
176     prev_size: &mut u32,
177 ) -> Option<Box<wasmtime_error_t>> {
178     handle_result(table.grow(store, delta, val.to_val()), |prev| {
179         *prev_size = prev
180     })
181 }
182