Skip to content

Commit

Permalink
Clean-up table.init (#1205)
Browse files Browse the repository at this point in the history
* remove TableEntity::init_untyped

* swap parameters in TableEntity::init method
  • Loading branch information
Robbepop authored Sep 28, 2024
1 parent d69b56e commit 9f46524
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 46 deletions.
2 changes: 1 addition & 1 deletion crates/wasmi/src/engine/executor/instrs/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ impl<'engine> Executor<'engine> {
&self.get_table(table_index),
&self.get_element_segment(element_index),
);
table.init(dst_index, element, src_index, len, Some(fuel))?;
table.init(element, dst_index, src_index, len, Some(fuel))?;
self.try_next_instr_at(3)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/wasmi/src/module/instantiate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl Module {
.store
.inner
.resolve_table_and_element_mut(&table, &element);
table.init_untyped(dst_index, elem.items(), None)?;
table.init(elem, dst_index, 0, len_items, None)?;
// Now drop the active element segment as commanded by the Wasm spec.
elem.drop_items();
}
Expand Down
45 changes: 1 addition & 44 deletions crates/wasmi/src/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,49 +334,6 @@ impl TableEntity {
Ok(())
}

/// Initialize `self[dst_index..]` from `elements`.
///
/// # Errors
///
/// Returns an error if the range is out of bounds of either the source or destination tables.
///
/// # Panics
///
/// - Panics if the `instance` cannot resolve all the `element` func indices.
/// - If the [`ElementSegmentEntity`] element type does not match the [`Table`] element type.
/// Note: This is a panic instead of an error since it is asserted at Wasm validation time.
pub fn init_untyped(
&mut self,
dst_index: u32,
elements: &[UntypedVal],
fuel: Option<&mut Fuel>,
) -> Result<(), TrapCode> {
let table_type = self.ty();
assert!(
table_type.element().is_ref(),
"table.init currently only works on reftypes"
);
// Convert parameters to indices.
let dst_index = dst_index as usize;
let len = elements.len();
let dst_items = self
.elements
.get_mut(dst_index..)
.and_then(|items| items.get_mut(..len))
.ok_or(TrapCode::TableOutOfBounds)?;
if len == 0 {
// Bail out early if nothing needs to be initialized.
// The Wasm spec demands to still perform the bounds check
// so we cannot bail out earlier.
return Ok(());
}
if let Some(fuel) = fuel {
fuel.consume_fuel_if(|costs| costs.fuel_for_copies(len as u64))?;
}
dst_items.copy_from_slice(elements);
Ok(())
}

/// Initialize `len` elements from `src_element[src_index..]` into `self[dst_index..]`.
///
/// # Errors
Expand All @@ -389,8 +346,8 @@ impl TableEntity {
/// Note: This is a panic instead of an error since it is asserted at Wasm validation time.
pub fn init(
&mut self,
dst_index: u32,
element: &ElementSegmentEntity,
dst_index: u32,
src_index: u32,
len: u32,
fuel: Option<&mut Fuel>,
Expand Down

0 comments on commit 9f46524

Please sign in to comment.