summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/cli/src/bin.rs2
-rw-r--r--crates/tinywasm/src/instance.rs13
-rw-r--r--crates/tinywasm/src/runtime/executor/mod.rs9
-rw-r--r--crates/tinywasm/src/store.rs7
-rw-r--r--crates/tinywasm/tests/testsuite/mod.rs2
-rw-r--r--crates/tinywasm/tests/testsuite/run.rs10
-rw-r--r--crates/tinywasm/tests/testsuite/util.rs18
7 files changed, 46 insertions, 15 deletions
diff --git a/crates/cli/src/bin.rs b/crates/cli/src/bin.rs
index 2712751..fc680b1 100644
--- a/crates/cli/src/bin.rs
+++ b/crates/cli/src/bin.rs
@@ -117,7 +117,7 @@ fn run(module: Module, func: Option<String>, args: Vec<WasmValue>) -> Result<()>
let instance = module.instantiate(&mut store)?;
if let Some(func) = func {
- let func = instance.get_func(&store, &func)?;
+ let func = instance.exported_func_by_name(&store, &func)?;
let res = func.call(&mut store, &args)?;
info!("{res:?}");
}
diff --git a/crates/tinywasm/src/instance.rs b/crates/tinywasm/src/instance.rs
index 2fa9cf3..6ef303a 100644
--- a/crates/tinywasm/src/instance.rs
+++ b/crates/tinywasm/src/instance.rs
@@ -58,8 +58,17 @@ impl ModuleInstance {
&self.0.types[addr as usize]
}
+ // resolve a function address to the index of the function in the store
+ pub(crate) fn func_addr(&self, addr: FuncAddr) -> FuncAddr {
+ self.0.func_addrs[addr as usize]
+ }
+
+ pub(crate) fn func_addrs(&self) -> &[FuncAddr] {
+ &self.0.func_addrs
+ }
+
/// Get an exported function by name
- pub fn get_func(&self, store: &Store, name: &str) -> Result<FuncHandle> {
+ pub fn exported_func_by_name(&self, store: &Store, name: &str) -> Result<FuncHandle> {
if self.0.store_id != store.id() {
return Err(Error::InvalidStore);
}
@@ -90,7 +99,7 @@ impl ModuleInstance {
P: IntoWasmValueTuple,
R: FromWasmValueTuple,
{
- let func = self.get_func(store, name)?;
+ let func = self.exported_func_by_name(store, name)?;
Ok(TypedFuncHandle {
func,
marker: core::marker::PhantomData,
diff --git a/crates/tinywasm/src/runtime/executor/mod.rs b/crates/tinywasm/src/runtime/executor/mod.rs
index 6486b38..6f0e820 100644
--- a/crates/tinywasm/src/runtime/executor/mod.rs
+++ b/crates/tinywasm/src/runtime/executor/mod.rs
@@ -18,6 +18,10 @@ use traits::*;
impl DefaultRuntime {
pub(crate) fn exec(&self, store: &mut Store, stack: &mut Stack, module: ModuleInstance) -> Result<()> {
+ log::info!("exports: {:?}", module.exports());
+ log::info!("func_addrs: {:?}", module.func_addrs());
+ log::info!("store funcs: {:?}", store.data.funcs.len());
+
// The current call frame, gets updated inside of exec_one
let mut cf = stack.call_stack.pop()?;
@@ -106,8 +110,9 @@ fn exec_one(
Call(v) => {
debug!("start call");
// prepare the call frame
- let func = store.get_func(*v as usize)?;
- let func_ty = module.func_ty(*v);
+ let func_idx = module.func_addr(*v);
+ let func = store.get_func(func_idx as usize)?;
+ let func_ty = module.func_ty(func_idx);
debug!("params: {:?}", func_ty.params);
debug!("stack: {:?}", stack.values);
diff --git a/crates/tinywasm/src/store.rs b/crates/tinywasm/src/store.rs
index ff44d0d..f28a6cb 100644
--- a/crates/tinywasm/src/store.rs
+++ b/crates/tinywasm/src/store.rs
@@ -124,18 +124,21 @@ impl Store {
Ok(())
}
+ /// Add functions to the store, returning their addresses in the store
pub(crate) fn add_funcs(&mut self, funcs: Vec<Function>, idx: ModuleInstanceAddr) -> Vec<FuncAddr> {
- let mut func_addrs = Vec::with_capacity(funcs.len());
+ let func_count = self.data.funcs.len();
+ let mut func_addrs = Vec::with_capacity(func_count);
for (i, func) in funcs.into_iter().enumerate() {
self.data.funcs.push(Rc::new(FunctionInstance {
func,
_module_instance: idx,
}));
- func_addrs.push(i as FuncAddr);
+ func_addrs.push((i + func_count) as FuncAddr);
}
func_addrs
}
+ /// Get the function at the actual index in the store
pub(crate) fn get_func(&self, addr: usize) -> Result<&Rc<FunctionInstance>> {
self.data
.funcs
diff --git a/crates/tinywasm/tests/testsuite/mod.rs b/crates/tinywasm/tests/testsuite/mod.rs
index cfbe882..d372af5 100644
--- a/crates/tinywasm/tests/testsuite/mod.rs
+++ b/crates/tinywasm/tests/testsuite/mod.rs
@@ -161,8 +161,6 @@ impl TestGroup {
}
fn stats(&self) -> (usize, usize) {
- log::error!("stats: {:?}", self.tests);
-
let mut passed_count = 0;
let mut failed_count = 0;
diff --git a/crates/tinywasm/tests/testsuite/run.rs b/crates/tinywasm/tests/testsuite/run.rs
index 93c33b9..33154bd 100644
--- a/crates/tinywasm/tests/testsuite/run.rs
+++ b/crates/tinywasm/tests/testsuite/run.rs
@@ -51,7 +51,7 @@ impl TestSuite {
debug!("got wat module");
let result = catch_unwind_silent(move || parse_module_bytes(&module.encode().unwrap()))
- .map_err(|e| eyre!("failed to parse module: {:?}", e))
+ .map_err(|e| eyre!("failed to parse module: {:?}", try_downcast_panic(e)))
.and_then(|res| res);
match &result {
@@ -77,7 +77,7 @@ impl TestSuite {
};
let res = catch_unwind_silent(|| parse_module_bytes(&module))
- .map_err(|e| eyre!("failed to parse module: {:?}", e))
+ .map_err(|e| eyre!("failed to parse module: {:?}", try_downcast_panic(e)))
.and_then(|res| res);
test_group.add_result(
@@ -96,7 +96,7 @@ impl TestSuite {
message: _,
} => {
let res = catch_unwind_silent(move || parse_module_bytes(&module.encode().unwrap()))
- .map_err(|e| eyre!("failed to parse module: {:?}", e))
+ .map_err(|e| eyre!("failed to parse module: {:?}", try_downcast_panic(e)))
.and_then(|res| res);
test_group.add_result(
@@ -133,7 +133,7 @@ impl TestSuite {
Err(err) => test_group.add_result(
&format!("AssertTrap({})", i),
span.linecol_in(wast),
- Err(eyre!("test panicked: {:?}", err)),
+ Err(eyre!("test panicked: {:?}", try_downcast_panic(err))),
),
Ok(Err(tinywasm::Error::Trap(_))) => {
test_group.add_result(&format!("AssertTrap({})", i), span.linecol_in(wast), Ok(()))
@@ -222,7 +222,7 @@ impl TestSuite {
});
let res = res
- .map_err(|e| eyre!("test panicked: {:?}", e.downcast_ref::<&str>()))
+ .map_err(|e| eyre!("test panicked: {:?}", try_downcast_panic(e)))
.and_then(|r| r);
test_group.add_result(
diff --git a/crates/tinywasm/tests/testsuite/util.rs b/crates/tinywasm/tests/testsuite/util.rs
index 85b2a06..82c2699 100644
--- a/crates/tinywasm/tests/testsuite/util.rs
+++ b/crates/tinywasm/tests/testsuite/util.rs
@@ -3,6 +3,22 @@ use std::panic;
use eyre::{eyre, Result};
use tinywasm_types::{TinyWasmModule, WasmValue};
+pub fn try_downcast_panic(panic: Box<dyn std::any::Any + Send>) -> String {
+ let info = panic
+ .downcast_ref::<panic::PanicInfo>()
+ .or(None)
+ .map(|p| p.to_string())
+ .clone();
+ let info_string = panic.downcast_ref::<String>().cloned();
+ let info_str = panic.downcast::<&str>().ok().map(|s| *s);
+
+ info.unwrap_or(
+ info_str
+ .unwrap_or(&info_string.unwrap_or("unknown panic".to_owned()))
+ .to_string(),
+ )
+}
+
pub fn exec_fn(
module: Option<&TinyWasmModule>,
name: &str,
@@ -15,7 +31,7 @@ pub fn exec_fn(
let mut store = tinywasm::Store::new();
let module = tinywasm::Module::from(module);
let instance = module.instantiate(&mut store)?;
- instance.get_func(&store, name)?.call(&mut store, args)
+ instance.exported_func_by_name(&store, name)?.call(&mut store, args)
}
pub fn catch_unwind_silent<F: FnOnce() -> R + panic::UnwindSafe, R>(f: F) -> std::thread::Result<R> {