wasmtime_c_api/
table.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::{
    handle_result, wasm_extern_t, wasm_ref_t, wasm_store_t, wasm_tabletype_t, wasmtime_error_t,
    wasmtime_val_t, WasmtimeStoreContext, WasmtimeStoreContextMut,
};
use anyhow::anyhow;
use std::mem::MaybeUninit;
use wasmtime::{Extern, Ref, RootScope, Table, TableType};

#[derive(Clone)]
#[repr(transparent)]
pub struct wasm_table_t {
    ext: wasm_extern_t,
}

wasmtime_c_api_macros::declare_ref!(wasm_table_t);

pub type wasm_table_size_t = u32;

impl wasm_table_t {
    pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_table_t> {
        match &e.which {
            Extern::Table(_) => Some(unsafe { &*(e as *const _ as *const _) }),
            _ => None,
        }
    }

    fn table(&self) -> Table {
        match self.ext.which {
            Extern::Table(t) => t,
            _ => unsafe { std::hint::unreachable_unchecked() },
        }
    }
}

fn option_wasm_ref_t_to_ref(r: Option<&wasm_ref_t>, table_ty: &TableType) -> Ref {
    r.map(|r| r.r.clone())
        .unwrap_or_else(|| Ref::null(table_ty.element().heap_type()))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_table_new(
    store: &mut wasm_store_t,
    tt: &wasm_tabletype_t,
    init: Option<&wasm_ref_t>,
) -> Option<Box<wasm_table_t>> {
    let tt = tt.ty().ty.clone();
    let init = option_wasm_ref_t_to_ref(init, &tt);
    let table = Table::new(store.store.context_mut(), tt, init).ok()?;
    Some(Box::new(wasm_table_t {
        ext: wasm_extern_t {
            store: store.store.clone(),
            which: table.into(),
        },
    }))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_table_type(t: &wasm_table_t) -> Box<wasm_tabletype_t> {
    let table = t.table();
    let store = t.ext.store.context();
    Box::new(wasm_tabletype_t::new(table.ty(&store)))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_table_get(
    t: &mut wasm_table_t,
    index: wasm_table_size_t,
) -> Option<Box<wasm_ref_t>> {
    let table = t.table();
    let r = table.get(t.ext.store.context_mut(), index)?;
    wasm_ref_t::new(r)
}

#[no_mangle]
pub unsafe extern "C" fn wasm_table_set(
    t: &mut wasm_table_t,
    index: wasm_table_size_t,
    r: Option<&wasm_ref_t>,
) -> bool {
    let table = t.table();
    let val = option_wasm_ref_t_to_ref(r, &table.ty(t.ext.store.context()));
    table.set(t.ext.store.context_mut(), index, val).is_ok()
}

#[no_mangle]
pub unsafe extern "C" fn wasm_table_size(t: &wasm_table_t) -> wasm_table_size_t {
    let table = t.table();
    let store = t.ext.store.context();
    table.size(&store)
}

#[no_mangle]
pub unsafe extern "C" fn wasm_table_grow(
    t: &mut wasm_table_t,
    delta: wasm_table_size_t,
    init: Option<&wasm_ref_t>,
) -> bool {
    let table = t.table();
    let init = option_wasm_ref_t_to_ref(init, &table.ty(t.ext.store.context()));
    table.grow(t.ext.store.context_mut(), delta, init).is_ok()
}

#[no_mangle]
pub extern "C" fn wasm_table_as_extern(t: &mut wasm_table_t) -> &mut wasm_extern_t {
    &mut t.ext
}

#[no_mangle]
pub extern "C" fn wasm_table_as_extern_const(t: &wasm_table_t) -> &wasm_extern_t {
    &t.ext
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_new(
    mut store: WasmtimeStoreContextMut<'_>,
    tt: &wasm_tabletype_t,
    init: &wasmtime_val_t,
    out: &mut Table,
) -> Option<Box<wasmtime_error_t>> {
    let mut scope = RootScope::new(&mut store);
    handle_result(
        init.to_val(&mut scope)
            .ref_()
            .ok_or_else(|| anyhow!("wasmtime_table_new init value is not a reference"))
            .and_then(|init| Table::new(scope, tt.ty().ty.clone(), init)),
        |table| *out = table,
    )
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_type(
    store: WasmtimeStoreContext<'_>,
    table: &Table,
) -> Box<wasm_tabletype_t> {
    Box::new(wasm_tabletype_t::new(table.ty(store)))
}

#[no_mangle]
pub extern "C" fn wasmtime_table_get(
    store: WasmtimeStoreContextMut<'_>,
    table: &Table,
    index: u32,
    ret: &mut MaybeUninit<wasmtime_val_t>,
) -> bool {
    let mut scope = RootScope::new(store);
    match table.get(&mut scope, index) {
        Some(r) => {
            crate::initialize(ret, wasmtime_val_t::from_val(&mut scope, r.into()));
            true
        }
        None => false,
    }
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_set(
    mut store: WasmtimeStoreContextMut<'_>,
    table: &Table,
    index: u32,
    val: &wasmtime_val_t,
) -> Option<Box<wasmtime_error_t>> {
    let mut scope = RootScope::new(&mut store);
    handle_result(
        val.to_val(&mut scope)
            .ref_()
            .ok_or_else(|| anyhow!("wasmtime_table_set value is not a reference"))
            .and_then(|val| table.set(scope, index, val)),
        |()| {},
    )
}

#[no_mangle]
pub extern "C" fn wasmtime_table_size(store: WasmtimeStoreContext<'_>, table: &Table) -> u32 {
    table.size(store)
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_table_grow(
    mut store: WasmtimeStoreContextMut<'_>,
    table: &Table,
    delta: u32,
    val: &wasmtime_val_t,
    prev_size: &mut u32,
) -> Option<Box<wasmtime_error_t>> {
    let mut scope = RootScope::new(&mut store);
    handle_result(
        val.to_val(&mut scope)
            .ref_()
            .ok_or_else(|| anyhow!("wasmtime_table_grow value is not a reference"))
            .and_then(|val| table.grow(scope, delta, val)),
        |prev| *prev_size = prev,
    )
}