wasmer_c_api/wasm_c_api/externals/
table.rs

1use crate::error::update_last_error;
2
3use super::super::store::wasm_store_t;
4use super::super::types::{wasm_ref_t, wasm_table_size_t, wasm_tabletype_t};
5use super::wasm_extern_t;
6use wasmer_api::Extern;
7
8#[allow(non_camel_case_types)]
9#[repr(C)]
10#[derive(Clone)]
11pub struct wasm_table_t {
12    pub(crate) extern_: wasm_extern_t,
13}
14
15impl wasm_table_t {
16    pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_table_t> {
17        match &e.inner {
18            Extern::Table(_) => Some(unsafe { &*(e as *const _ as *const _) }),
19            _ => None,
20        }
21    }
22}
23
24#[unsafe(no_mangle)]
25pub unsafe extern "C" fn wasm_table_new(
26    _store: Option<&wasm_store_t>,
27    _table_type: Option<&wasm_tabletype_t>,
28    _init: *const wasm_ref_t,
29) -> Option<Box<wasm_table_t>> {
30    todo!("get val from init somehow");
31}
32
33#[unsafe(no_mangle)]
34pub unsafe extern "C" fn wasm_table_delete(_table: Option<Box<wasm_table_t>>) {}
35
36#[unsafe(no_mangle)]
37pub unsafe extern "C" fn wasm_table_copy(
38    table: Option<&wasm_table_t>,
39) -> Option<Box<wasm_table_t>> {
40    table.cloned().map(Box::new)
41}
42
43#[unsafe(no_mangle)]
44pub unsafe extern "C" fn wasm_table_size(table: Option<&wasm_table_t>) -> usize {
45    let Some(table) = table else {
46        update_last_error("table pointer is null");
47        return 0;
48    };
49    let store_ref = unsafe { table.extern_.store.store() };
50    table.extern_.table().size(&store_ref) as _
51}
52
53#[unsafe(no_mangle)]
54pub unsafe extern "C" fn wasm_table_same(
55    wasm_table1: &wasm_table_t,
56    wasm_table2: &wasm_table_t,
57) -> bool {
58    wasm_table1.extern_.table() == wasm_table2.extern_.table()
59}
60
61#[unsafe(no_mangle)]
62pub unsafe extern "C" fn wasm_table_grow(
63    _table: &mut wasm_table_t,
64    _delta: wasm_table_size_t,
65    _init: *mut wasm_ref_t,
66) -> bool {
67    // TODO: maybe need to look at result to return `true`; also maybe report error here
68    //wasm_table.inner.grow(delta, init).is_ok()
69    todo!("Blocked on transforming ExternRef into a val type")
70}