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