Revert Table::get method to return Option

This commit is contained in:
Mark McCaskey
2021-02-19 07:42:48 -08:00
parent 3b7f8d71a9
commit 0b34d0d86d
5 changed files with 13 additions and 21 deletions

View File

@@ -950,7 +950,7 @@ mod inner {
use std::marker::PhantomData;
use std::panic::{self, AssertUnwindSafe};
use wasmer_types::{FunctionType, NativeWasmType, Type};
use wasmer_vm::{raise_user_trap, resume_panic, VMExternRef, VMFuncRef, VMFunctionBody};
use wasmer_vm::{raise_user_trap, resume_panic, VMFunctionBody};
/// A trait to convert a Rust value to a `WasmNativeType` value,
/// or to convert `WasmNativeType` value to a Rust value.

View File

@@ -71,7 +71,7 @@ impl Table {
pub fn get(&self, index: u32) -> Option<Val> {
// TODO: review change to make inner table return a trap in Result, maybe we don't
// want that
let item = self.table.get(index).ok()?;
let item = self.table.get(index)?;
Some(ValFuncRef::from_table_reference(item, &self.store))
}

View File

@@ -578,7 +578,7 @@ impl Instance {
&self,
table_index: LocalTableIndex,
index: u32,
) -> Result<TableReference, Trap> {
) -> Option<TableReference> {
self.tables
.get(table_index)
.unwrap_or_else(|| panic!("no table for index {}", table_index.index()))
@@ -596,7 +596,7 @@ impl Instance {
&self,
table_index: TableIndex,
index: u32,
) -> Result<TableReference, Trap> {
) -> Option<TableReference> {
let import = self.imported_table(table_index);
let from = import.from.as_ref();
from.get(index)
@@ -1261,11 +1261,7 @@ impl InstanceHandle {
/// Get table element reference.
///
/// Returns `None` if index is out of bounds.
pub fn table_get(
&self,
table_index: LocalTableIndex,
index: u32,
) -> Result<TableReference, Trap> {
pub fn table_get(&self, table_index: LocalTableIndex, index: u32) -> Option<TableReference> {
self.instance().as_ref().table_get(table_index, index)
}

View File

@@ -322,8 +322,8 @@ pub unsafe extern "C" fn wasmer_table_get(
// TODO: type checking, maybe have specialized accessors
match instance.table_get(table_index, elem_index) {
Ok(table_ref) => table_ref.into(),
Err(trap) => raise_lib_trap(trap),
Some(table_ref) => table_ref.into(),
None => raise_lib_trap(Trap::new_from_runtime(TrapCode::TableAccessOutOfBounds)),
}
}
@@ -342,8 +342,8 @@ pub unsafe extern "C" fn wasmer_imported_table_get(
// TODO: type checking, maybe have specialized accessors
match instance.imported_table_get(table_index, elem_index) {
Ok(table_ref) => table_ref.into(),
Err(trap) => raise_lib_trap(trap),
Some(table_ref) => table_ref.into(),
None => raise_lib_trap(Trap::new_from_runtime(TrapCode::TableAccessOutOfBounds)),
}
}

View File

@@ -45,7 +45,7 @@ pub trait Table: fmt::Debug + Send + Sync {
/// Get reference to the specified element.
///
/// Returns `None` if the index is out of bounds.
fn get(&self, index: u32) -> Result<TableReference, Trap>;
fn get(&self, index: u32) -> Option<TableReference>;
/// Set reference to the specified element.
///
@@ -332,14 +332,10 @@ impl Table for LinearTable {
/// Get reference to the specified element.
///
/// Returns `None` if the index is out of bounds.
fn get(&self, index: u32) -> Result<TableReference, Trap> {
fn get(&self, index: u32) -> Option<TableReference> {
let vec_guard = self.vec.lock().unwrap();
let raw_data = vec_guard
.borrow()
.get(index as usize)
.cloned()
.ok_or_else(|| Trap::new_from_runtime(TrapCode::TableAccessOutOfBounds))?;
Ok(match self.table.ty {
let raw_data = vec_guard.borrow().get(index as usize).cloned()?;
Some(match self.table.ty {
ValType::ExternRef => {
// TODO: there is no matching `drop` for this `clone` implemented yet.
// thus extern refs will always leak memory if this path is touched.