Update Table::grow's return to be the previous size (#1653)

* Update `Table::grow`'s return to be the previous size

This brings it in line with `Memory::grow` and the `table.grow`
instruction which return the size of the table previously, not the size
of the table currently.

* Comment successful return
This commit is contained in:
Alex Crichton
2020-05-05 12:03:31 -05:00
committed by GitHub
parent fa54422854
commit 8e934e6762
2 changed files with 8 additions and 5 deletions

View File

@@ -371,6 +371,8 @@ impl Table {
/// Grows the size of this table by `delta` more elements, initialization
/// all new elements to `init`.
///
/// Returns the previous size of this table if successful.
///
/// # Errors
///
/// Returns an error if the table cannot be grown by `delta`, for example
@@ -381,8 +383,7 @@ impl Table {
let item = into_checked_anyfunc(init, &self.instance.store)?;
if let Some(len) = self.instance.table_grow(index, delta) {
for i in 0..delta {
let i = len - (delta - i);
set_table_item(&self.instance, index, i, item.clone())?;
set_table_item(&self.instance, index, len + i, item.clone())?;
}
Ok(len)
} else {

View File

@@ -44,9 +44,11 @@ impl Table {
/// Grow table by the specified amount of elements.
///
/// Returns `None` if table can't be grown by the specified amount
/// of elements.
/// of elements. Returns the previous size of the table if growth is
/// successful.
pub fn grow(&self, delta: u32) -> Option<u32> {
let new_len = match self.size().checked_add(delta) {
let size = self.size();
let new_len = match size.checked_add(delta) {
Some(len) => {
if let Some(max) = self.maximum {
if len > max {
@@ -63,7 +65,7 @@ impl Table {
usize::try_from(new_len).unwrap(),
VMCallerCheckedAnyfunc::default(),
);
Some(new_len)
Some(size)
}
/// Get reference to the specified element.