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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
pub use self::{
element::{ElementSegment, ElementSegmentIdx},
ty::TableType,
};
use super::{AsContext, AsContextMut, Stored};
use crate::{collections::arena::ArenaIndex, core::CoreTable, errors::TableError, Error, Val};
mod element;
mod ty;
/// A raw index to a table entity.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct TableIdx(u32);
impl ArenaIndex for TableIdx {
fn into_usize(self) -> usize {
self.0 as usize
}
fn from_usize(value: usize) -> Self {
let value = value.try_into().unwrap_or_else(|error| {
panic!("index {value} is out of bounds as table index: {error}")
});
Self(value)
}
}
/// A Wasm table reference.
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct Table(Stored<TableIdx>);
impl Table {
/// Creates a new table reference.
pub(super) fn from_inner(stored: Stored<TableIdx>) -> Self {
Self(stored)
}
/// Returns the underlying stored representation.
pub(super) fn as_inner(&self) -> &Stored<TableIdx> {
&self.0
}
/// Creates a new table to the store.
///
/// # Errors
///
/// If `init` does not match the [`TableType`] element type.
pub fn new(mut ctx: impl AsContextMut, ty: TableType, init: Val) -> Result<Self, Error> {
let (inner, mut resource_limiter) = ctx
.as_context_mut()
.store
.store_inner_and_resource_limiter_ref();
let entity = CoreTable::new(ty.core, init.into(), &mut resource_limiter)?;
let table = inner.alloc_table(entity);
Ok(table)
}
/// Returns the type and limits of the table.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Table`].
pub fn ty(&self, ctx: impl AsContext) -> TableType {
let core = ctx.as_context().store.inner.resolve_table(self).ty();
TableType { core }
}
/// Returns the dynamic [`TableType`] of the [`Table`].
///
/// # Note
///
/// This respects the current size of the [`Table`] as
/// its minimum size and is useful for import subtyping checks.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Table`].
pub(crate) fn dynamic_ty(&self, ctx: impl AsContext) -> TableType {
let core = ctx
.as_context()
.store
.inner
.resolve_table(self)
.dynamic_ty();
TableType { core }
}
/// Returns the current size of the [`Table`].
///
/// # Panics
///
/// If `ctx` does not own this [`Table`].
pub fn size(&self, ctx: impl AsContext) -> u64 {
ctx.as_context().store.inner.resolve_table(self).size()
}
/// Grows the table by the given amount of elements.
///
/// Returns the old size of the [`Table`] upon success.
///
/// # Note
///
/// The newly added elements are initialized to the `init` [`Val`].
///
/// # Errors
///
/// - If the table is grown beyond its maximum limits.
/// - If `value` does not match the [`Table`] element type.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Table`].
pub fn grow(
&self,
mut ctx: impl AsContextMut,
delta: u64,
init: Val,
) -> Result<u64, TableError> {
let (inner, mut limiter) = ctx
.as_context_mut()
.store
.store_inner_and_resource_limiter_ref();
let table = inner.resolve_table_mut(self);
table.grow(delta, init.into(), None, &mut limiter)
}
/// Returns the [`Table`] element value at `index`.
///
/// Returns `None` if `index` is out of bounds.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Table`].
pub fn get(&self, ctx: impl AsContext, index: u64) -> Option<Val> {
ctx.as_context()
.store
.inner
.resolve_table(self)
.get(index)
.map(Val::from)
}
/// Sets the [`Val`] of this [`Table`] at `index`.
///
/// # Errors
///
/// - If `index` is out of bounds.
/// - If `value` does not match the [`Table`] element type.
///
/// # Panics
///
/// Panics if `ctx` does not own this [`Table`].
pub fn set(
&self,
mut ctx: impl AsContextMut,
index: u64,
value: Val,
) -> Result<(), TableError> {
ctx.as_context_mut()
.store
.inner
.resolve_table_mut(self)
.set(index, value.into())
}
/// Returns `true` if `lhs` and `rhs` [`Table`] refer to the same entity.
///
/// # Note
///
/// We do not implement `Eq` and `PartialEq` and
/// intentionally keep this API hidden from users.
#[inline]
pub(crate) fn eq(lhs: &Self, rhs: &Self) -> bool {
lhs.as_inner() == rhs.as_inner()
}
/// Copy `len` elements from `src_table[src_index..]` into
/// `dst_table[dst_index..]`.
///
/// # Errors
///
/// Returns an error if the range is out of bounds of either the source or
/// destination tables.
///
/// # Panics
///
/// Panics if `store` does not own either `dst_table` or `src_table`.
pub fn copy(
mut store: impl AsContextMut,
dst_table: &Table,
dst_index: u64,
src_table: &Table,
src_index: u64,
len: u64,
) -> Result<(), TableError> {
if Self::eq(dst_table, src_table) {
// The `dst_table` and `src_table` are the same table
// therefore we have to copy within the same table.
let table = store
.as_context_mut()
.store
.inner
.resolve_table_mut(dst_table);
table
.copy_within(dst_index, src_index, len, None)
.map_err(|_| TableError::CopyOutOfBounds)
} else {
// The `dst_table` and `src_table` are different entities
// therefore we have to copy from one table to the other.
let (dst_table, src_table, _fuel) = store
.as_context_mut()
.store
.inner
.resolve_table_pair_and_fuel(dst_table, src_table);
CoreTable::copy(dst_table, dst_index, src_table, src_index, len, None)
.map_err(|_| TableError::CopyOutOfBounds)
}
}
/// Fill `table[dst..(dst + len)]` with the given value.
///
/// # Errors
///
/// - If `val` has a type mismatch with the element type of the [`Table`].
/// - If the region to be filled is out of bounds for the [`Table`].
/// - If `val` originates from a different [`Store`] than the [`Table`].
///
/// # Panics
///
/// If `ctx` does not own `dst_table` or `src_table`.
///
/// [`Store`]: [`crate::Store`]
pub fn fill(
&self,
mut ctx: impl AsContextMut,
dst: u64,
val: Val,
len: u64,
) -> Result<(), TableError> {
ctx.as_context_mut()
.store
.inner
.resolve_table_mut(self)
.fill(dst, val.into(), len, None)
}
}