reference types: Implement the table.size and table.grow instructions (#1894)
Part of #929
This commit is contained in:
@@ -395,12 +395,28 @@ impl Table {
|
||||
/// error if `init` is not of the right type.
|
||||
pub fn grow(&self, delta: u32, init: Val) -> Result<u32> {
|
||||
let index = self.wasmtime_table_index();
|
||||
let item = into_checked_anyfunc(init, &self.instance.store)?;
|
||||
if let Some(len) = self.instance.table_grow(index, delta) {
|
||||
for i in 0..delta {
|
||||
set_table_item(&self.instance, index, len + i, item.clone())?;
|
||||
let orig_size = match self.ty().element() {
|
||||
ValType::FuncRef => {
|
||||
let init = into_checked_anyfunc(init, &self.instance.store)?;
|
||||
self.instance
|
||||
.defined_table_grow(index, delta, runtime::TableElement::FuncRef(init))
|
||||
}
|
||||
Ok(len)
|
||||
ValType::ExternRef => {
|
||||
let init = match init {
|
||||
Val::ExternRef(Some(x)) => Some(x.inner),
|
||||
Val::ExternRef(None) => None,
|
||||
_ => bail!("incorrect init value for growing table"),
|
||||
};
|
||||
self.instance.defined_table_grow(
|
||||
index,
|
||||
delta,
|
||||
runtime::TableElement::ExternRef(init),
|
||||
)
|
||||
}
|
||||
_ => unreachable!("only `funcref` and `externref` tables are supported"),
|
||||
};
|
||||
if let Some(size) = orig_size {
|
||||
Ok(size)
|
||||
} else {
|
||||
bail!("failed to grow table by `{}`", delta)
|
||||
}
|
||||
|
||||
@@ -394,12 +394,14 @@ impl TableType {
|
||||
}
|
||||
|
||||
pub(crate) fn from_wasmtime_table(table: &wasm::Table) -> TableType {
|
||||
assert!(if let wasm::TableElementType::Func = table.ty {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
});
|
||||
let ty = ValType::FuncRef;
|
||||
let ty = match table.ty {
|
||||
wasm::TableElementType::Func => ValType::FuncRef,
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
wasm::TableElementType::Val(ir::types::R64) => ValType::ExternRef,
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
wasm::TableElementType::Val(ir::types::R32) => ValType::ExternRef,
|
||||
_ => panic!("only `funcref` and `externref` tables supported"),
|
||||
};
|
||||
let limits = Limits::new(table.minimum, table.maximum);
|
||||
TableType::new(ty, limits)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user