Skip to content

Commit

Permalink
handle errors with grace
Browse files Browse the repository at this point in the history
  • Loading branch information
ovstinga authored and camilbancioiu committed Oct 4, 2022
1 parent be6d6e1 commit f2db71f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
10 changes: 9 additions & 1 deletion lib/runtime-c-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ pub unsafe extern "C" fn wasmer_instantiate_with_options(
wasmer_result_t::WASMER_OK
}

/// todo: add documentation
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_instantiate_reset(
Expand All @@ -264,7 +265,14 @@ pub unsafe extern "C" fn wasmer_instantiate_reset(
}

let instance = &mut *(instance as *mut Instance);
let _result = instance.reset();

if let Err(error) = instance.reset() {
update_last_error(CApiError {
msg: error.to_string(),
});
return wasmer_result_t::WASMER_ERROR;
}

wasmer_result_t::WASMER_OK
}

Expand Down
3 changes: 3 additions & 0 deletions lib/runtime-c-api/wasmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,9 @@ wasmer_result_t wasmer_instantiate(wasmer_instance_t **instance,
wasmer_import_t *imports,
int imports_len);

/**
* todo: add documentation
*/
wasmer_result_t wasmer_instantiate_reset(wasmer_instance_t *instance);

wasmer_result_t wasmer_instantiate_with_options(wasmer_instance_t **instance,
Expand Down
1 change: 1 addition & 0 deletions lib/runtime-c-api/wasmer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@ wasmer_result_t wasmer_instantiate(wasmer_instance_t **instance,
wasmer_import_t *imports,
int imports_len);

/// todo: add documentation
wasmer_result_t wasmer_instantiate_reset(wasmer_instance_t *instance);

wasmer_result_t wasmer_instantiate_with_options(wasmer_instance_t **instance,
Expand Down
29 changes: 14 additions & 15 deletions lib/runtime-core/src/backing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
error::{CreationError, LinkError, LinkResult, RuntimeResult, RuntimeError},
error::{CreationError, LinkError, LinkResult, RuntimeError, RuntimeResult},
export::{Context, Export},
global::Global,
import::ImportObject,
Expand All @@ -10,9 +10,9 @@ use crate::{
table::Table,
typed_func::{always_trap, Func},
types::{
ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex, ImportedTableIndex,
Initializer, LocalFuncIndex, LocalGlobalIndex, LocalMemoryIndex, LocalOrImport,
LocalTableIndex, SigIndex, Value, GlobalInit,
GlobalInit, ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex,
ImportedTableIndex, Initializer, LocalFuncIndex, LocalGlobalIndex, LocalMemoryIndex,
LocalOrImport, LocalTableIndex, SigIndex, Value,
},
vm,
};
Expand Down Expand Up @@ -104,40 +104,39 @@ impl LocalBacking {
}

/// todo: add documentation
pub(crate) fn reset(
&mut self,
module_info: &ModuleInfo
) -> RuntimeResult<()> {
pub(crate) fn reset(&mut self, module_info: &ModuleInfo) -> RuntimeResult<()> {
// todo: remove debug prints
println!("Resetting ~ Local Backing:");
Self::reset_globals(&module_info, &mut self.globals)?;
Ok(())
}

fn reset_globals(
module_info: &ModuleInfo,
globals: &mut SliceMap<LocalGlobalIndex, Global>
globals: &mut SliceMap<LocalGlobalIndex, Global>,
) -> RuntimeResult<()> {
// todo: remove debug prints
let init_globals = &module_info.globals;
for (index, init_global) in init_globals.iter() {
let GlobalInit {desc, init} = init_global;
let GlobalInit { desc, init } = init_global;

let value = if let Initializer::Const(v) = init {
v.clone()
} else {
return Err(RuntimeError(Box::new("Can only reset const globals")));
};

match globals.get(index) {
match globals.get_mut(index) {
Some(g) => {
if desc.mutable == true && value != g.get() {
if desc.mutable == false && value != g.get() {
return Err(RuntimeError(Box::new("Immutable global found changed")));
}
g.set(value);
},
None => return Err(RuntimeError(Box::new("Missing global value to reset")))
}
None => return Err(RuntimeError(Box::new("Missing global value to reset"))),
};
}

println!(" [x] globals");
Ok(())
}

Expand Down

0 comments on commit f2db71f

Please sign in to comment.