Return a WasmResult from ModuleEnvironment methods (#886)

* [wasm] return a WasmResult from `declare_table_elements`

This method in particular needs to accommodate failure because any table index other than zero is
currently invalid.

* [wasm] additional failure handling improvements

- Adds `WasmResult<()>` as the return type for most of the `ModuleEnvironment` methods that
previously returned nothing.

- Replaces some panics with `WasmError::Unsupported` now that the methods can return a result.

- Adds a `wasm_unsupported!()` macro for early returns with a formatted unsupported message.
This commit is contained in:
Adam C. Foltzer
2019-08-07 13:23:32 -07:00
committed by GitHub
parent 00b8d019c9
commit 73670aab43
7 changed files with 204 additions and 91 deletions

View File

@@ -367,8 +367,9 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
self.info.config
}
fn declare_signature(&mut self, sig: ir::Signature) {
fn declare_signature(&mut self, sig: ir::Signature) -> WasmResult<()> {
self.info.signatures.push(sig);
Ok(())
}
fn declare_func_import(
@@ -376,7 +377,7 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
sig_index: SignatureIndex,
module: &'data str,
field: &'data str,
) {
) -> WasmResult<()> {
assert_eq!(
self.info.functions.len(),
self.info.imported_funcs.len(),
@@ -386,32 +387,48 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
self.info
.imported_funcs
.push((String::from(module), String::from(field)));
Ok(())
}
fn declare_func_type(&mut self, sig_index: SignatureIndex) {
fn declare_func_type(&mut self, sig_index: SignatureIndex) -> WasmResult<()> {
self.info.functions.push(Exportable::new(sig_index));
Ok(())
}
fn declare_global(&mut self, global: Global) {
fn declare_global(&mut self, global: Global) -> WasmResult<()> {
self.info.globals.push(Exportable::new(global));
Ok(())
}
fn declare_global_import(&mut self, global: Global, module: &'data str, field: &'data str) {
fn declare_global_import(
&mut self,
global: Global,
module: &'data str,
field: &'data str,
) -> WasmResult<()> {
self.info.globals.push(Exportable::new(global));
self.info
.imported_globals
.push((String::from(module), String::from(field)));
Ok(())
}
fn declare_table(&mut self, table: Table) {
fn declare_table(&mut self, table: Table) -> WasmResult<()> {
self.info.tables.push(Exportable::new(table));
Ok(())
}
fn declare_table_import(&mut self, table: Table, module: &'data str, field: &'data str) {
fn declare_table_import(
&mut self,
table: Table,
module: &'data str,
field: &'data str,
) -> WasmResult<()> {
self.info.tables.push(Exportable::new(table));
self.info
.imported_tables
.push((String::from(module), String::from(field)));
Ok(())
}
fn declare_table_elements(
@@ -420,19 +437,27 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
_base: Option<GlobalIndex>,
_offset: usize,
_elements: Box<[FuncIndex]>,
) {
) -> WasmResult<()> {
// We do nothing
Ok(())
}
fn declare_memory(&mut self, memory: Memory) {
fn declare_memory(&mut self, memory: Memory) -> WasmResult<()> {
self.info.memories.push(Exportable::new(memory));
Ok(())
}
fn declare_memory_import(&mut self, memory: Memory, module: &'data str, field: &'data str) {
fn declare_memory_import(
&mut self,
memory: Memory,
module: &'data str,
field: &'data str,
) -> WasmResult<()> {
self.info.memories.push(Exportable::new(memory));
self.info
.imported_memories
.push((String::from(module), String::from(field)));
Ok(())
}
fn declare_data_initialization(
@@ -441,37 +466,55 @@ impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
_base: Option<GlobalIndex>,
_offset: usize,
_data: &'data [u8],
) {
) -> WasmResult<()> {
// We do nothing
Ok(())
}
fn declare_func_export(&mut self, func_index: FuncIndex, name: &'data str) {
fn declare_func_export(&mut self, func_index: FuncIndex, name: &'data str) -> WasmResult<()> {
self.info.functions[func_index]
.export_names
.push(String::from(name));
Ok(())
}
fn declare_table_export(&mut self, table_index: TableIndex, name: &'data str) {
fn declare_table_export(
&mut self,
table_index: TableIndex,
name: &'data str,
) -> WasmResult<()> {
self.info.tables[table_index]
.export_names
.push(String::from(name));
Ok(())
}
fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &'data str) {
fn declare_memory_export(
&mut self,
memory_index: MemoryIndex,
name: &'data str,
) -> WasmResult<()> {
self.info.memories[memory_index]
.export_names
.push(String::from(name));
Ok(())
}
fn declare_global_export(&mut self, global_index: GlobalIndex, name: &'data str) {
fn declare_global_export(
&mut self,
global_index: GlobalIndex,
name: &'data str,
) -> WasmResult<()> {
self.info.globals[global_index]
.export_names
.push(String::from(name));
Ok(())
}
fn declare_start_func(&mut self, func_index: FuncIndex) {
fn declare_start_func(&mut self, func_index: FuncIndex) -> WasmResult<()> {
debug_assert!(self.info.start_func.is_none());
self.info.start_func = Some(func_index);
Ok(())
}
fn define_function_body(