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
use crate::{
error::{ExportError, RuntimeError},
get_global_store, new,
types::{TableDescriptor, Value},
};
#[derive(Clone)]
pub struct Table {
new_table: new::wasmer::Table,
}
impl Table {
pub fn new(descriptor: TableDescriptor, initial_value: Value) -> Result<Self, RuntimeError> {
Ok(Self {
new_table: new::wasmer::Table::new(&get_global_store(), descriptor, initial_value)?,
})
}
pub fn descriptor(&self) -> TableDescriptor {
self.new_table.ty().clone()
}
pub fn set(&self, index: u32, value: Value) -> Result<(), RuntimeError> {
self.new_table.set(index, value)
}
pub fn get(&self, index: u32) -> Option<Value> {
self.new_table.get(index)
}
pub fn size(&self) -> u32 {
self.new_table.size()
}
pub fn grow(&self, delta: u32, initial_value: Value) -> Result<u32, RuntimeError> {
self.new_table.grow(delta, initial_value)
}
}
impl From<&new::wasmer::Table> for Table {
fn from(new_table: &new::wasmer::Table) -> Self {
Self {
new_table: new_table.clone(),
}
}
}
impl<'a> new::wasmer::Exportable<'a> for Table {
fn to_export(&self) -> new::wasmer_vm::Export {
self.new_table.to_export()
}
fn get_self_from_extern(r#extern: &'a new::wasmer::Extern) -> Result<&'a Self, ExportError> {
match r#extern {
new::wasmer::Extern::Table(table) => Ok(
Box::leak(Box::<Table>::new(table.into())),
),
_ => Err(ExportError::IncompatibleType),
}
}
}
#[cfg(test)]
mod table_tests {
use super::{Table, TableDescriptor};
use crate::types::{Type, Value};
#[test]
fn test_initial_table_size() {
let table = Table::new(
TableDescriptor {
ty: Type::FuncRef,
minimum: 10,
maximum: Some(20),
},
Value::null(),
)
.unwrap();
assert_eq!(table.size(), 10);
}
}