Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/wasmtime/src/runtime/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,18 @@ impl Table {
ty: TableType,
init: Ref,
) -> Result<Table> {
init.ensure_matches_ty(store, ty.element())
.context("type mismatch: value does not match table element type")?;
let table = generate_table_export(store, limiter, &ty).await?;
// Tables are always allocated as all zeroes, so skip the fill below if
// the value being inserted is all zeros.
if init.is_zero_pattern() {
if cfg!(debug_assertions) {
let (table, _) = table.wasmtime_table(store, None);
table.debug_assert_all_zero();
}
return Ok(table);
}
table._fill(store, 0, init, ty.minimum())?;
Ok(table)
}
Expand Down Expand Up @@ -606,6 +617,15 @@ impl Ref {
_ => unreachable!("checked that the value matches the type above"),
}
}

fn is_zero_pattern(&self) -> bool {
match self {
Ref::Extern(None) | Ref::Any(None) | Ref::Exn(None) | Ref::Func(None) => true,
Ref::Extern(Some(_)) | Ref::Any(Some(_)) | Ref::Exn(Some(_)) | Ref::Func(Some(_)) => {
false
}
}
}
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions crates/wasmtime/src/runtime/trampoline/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub async fn create_table(
);

let table_id = module.tables.push(wasmtime_table)?;
module.table_initialization.push(Default::default())?;

// TODO: can this `exports.insert` get removed?
let name = module.strings.insert("")?;
Expand Down
15 changes: 15 additions & 0 deletions crates/wasmtime/src/runtime/vm/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,21 @@ impl Table {
}
}
}

pub fn debug_assert_all_zero(&self) {
match self.element_type() {
TableElementType::Func => {
let (funcrefs, _lazy_init) = self.funcrefs();
debug_assert!(funcrefs.iter().all(|f| f.0.is_none()));
}
TableElementType::GcRef => {
debug_assert!(self.gc_refs().iter().all(|r| r.is_none()));
}
TableElementType::Cont => {
debug_assert!(self.contrefs().iter().all(|c| c.is_none()));
}
}
}
}

// The default table representation is an empty funcref table that cannot grow.
Expand Down
Loading