summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/parser/src/conversion.rs5
-rw-r--r--crates/tinywasm/src/func.rs3
-rw-r--r--crates/tinywasm/src/runtime/executor/mod.rs31
-rw-r--r--crates/tinywasm/src/runtime/stack/blocks.rs2
-rw-r--r--crates/tinywasm/src/runtime/stack/call_stack.rs5
-rw-r--r--crates/tinywasm/src/runtime/stack/value_stack.rs80
-rw-r--r--crates/tinywasm/tests/mvp.csv2
-rw-r--r--crates/tinywasm/tests/progress-mvp.svg4
-rw-r--r--crates/tinywasm/tests/testsuite/run.rs8
-rw-r--r--crates/tinywasm/tests/testsuite/util.rs10
-rw-r--r--crates/types/src/lib.rs10
-rw-r--r--examples/wasm/call.wat52
12 files changed, 162 insertions, 50 deletions
diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs
index d921e35..269bb66 100644
--- a/crates/parser/src/conversion.rs
+++ b/crates/parser/src/conversion.rs
@@ -81,10 +81,7 @@ pub(crate) fn convert_blocktype(blocktype: wasmparser::BlockType) -> BlockArgs {
// TODO: maybe solve this differently so we can support 128-bit values
// without having to increase the size of the WasmValue enum
Type(ty) => BlockArgs::Type(convert_valtype(&ty)),
-
- // Wasm 2.0
- FuncType(_ty) => unimplemented!(),
- // FuncType(ty) => BlockArgs::FuncType(*ty),
+ FuncType(ty) => BlockArgs::FuncType(ty),
}
}
diff --git a/crates/tinywasm/src/func.rs b/crates/tinywasm/src/func.rs
index 6a189da..58890f6 100644
--- a/crates/tinywasm/src/func.rs
+++ b/crates/tinywasm/src/func.rs
@@ -1,5 +1,5 @@
use alloc::{format, string::String, string::ToString, vec, vec::Vec};
-use log::debug;
+use log::{debug, info};
use tinywasm_types::{FuncAddr, FuncType, WasmValue};
use crate::{
@@ -34,6 +34,7 @@ impl FuncHandle {
// 4. If the length of the provided argument values is different from the number of expected arguments, then fail
if func_ty.params.len() != params.len() {
+ info!("func_ty.params: {:?}", func_ty.params);
return Err(Error::Other(format!(
"param count mismatch: expected {}, got {}",
func_ty.params.len(),
diff --git a/crates/tinywasm/src/runtime/executor/mod.rs b/crates/tinywasm/src/runtime/executor/mod.rs
index 39788b5..67c55e0 100644
--- a/crates/tinywasm/src/runtime/executor/mod.rs
+++ b/crates/tinywasm/src/runtime/executor/mod.rs
@@ -6,7 +6,7 @@ use crate::{
};
use alloc::vec::Vec;
use log::info;
-use tinywasm_types::{BlockArgs, Instruction};
+use tinywasm_types::{BlockArgs, FuncType, Instruction, ValType};
mod macros;
use macros::*;
@@ -137,37 +137,37 @@ fn exec_one(
info!("end: {:?} (@{})", instrs[end_instr_ptr], end_instr_ptr);
if stack.values.pop_t::<i32>()? != 0 {
+ // let params = stack.values.pop_block_params(*args, &module)?;
cf.labels.push(LabelFrame {
instr_ptr: cf.instr_ptr,
end_instr_ptr: cf.instr_ptr + *end_offset,
- stack_ptr: stack.values.len(),
+ stack_ptr: stack.values.len(), // - params,
args: *args,
ty: BlockType::If,
});
- stack.values.push_block_args(*args)?;
}
}
Loop(args, end_offset) => {
+ // let params = stack.values.pop_block_params(*args, &module)?;
cf.labels.push(LabelFrame {
instr_ptr: cf.instr_ptr,
end_instr_ptr: cf.instr_ptr + *end_offset,
- stack_ptr: stack.values.len(),
+ stack_ptr: stack.values.len(), // - params,
args: *args,
ty: BlockType::Loop,
});
- stack.values.push_block_args(*args)?;
}
Block(args, end_offset) => {
+ // let params = stack.values.pop_block_params(*args, &module)?;
cf.labels.push(LabelFrame {
instr_ptr: cf.instr_ptr,
end_instr_ptr: cf.instr_ptr + *end_offset,
- stack_ptr: stack.values.len(),
+ stack_ptr: stack.values.len(), //- params,
args: *args,
ty: BlockType::Block,
});
- stack.values.push_block_args(*args)?;
}
BrTable(_default, len) => {
@@ -215,17 +215,18 @@ fn exec_one(
panic!("end: no label to end, this should have been validated by the parser");
};
- let res: &[RawWasmValue] = match block.args {
- BlockArgs::Empty => &[],
- BlockArgs::Type(_t) => todo!(),
- BlockArgs::FuncType(_t) => todo!(),
+ let res_count = match block.args {
+ BlockArgs::Empty => 0,
+ BlockArgs::Type(_) => 1,
+ BlockArgs::FuncType(t) => module.func_ty(t).results.len(),
};
- // trim the lable's stack from the stack
- stack.values.trim(block.stack_ptr);
+ info!("we want to keep {} values on the stack", res_count);
+ info!("current block stack ptr: {}", block.stack_ptr);
+ info!("stack: {:?}", stack.values);
- // push the block result values to the stack
- stack.values.extend(res.iter().copied());
+ // trim the lable's stack from the stack
+ stack.values.truncate_keep(block.stack_ptr, res_count)
}
LocalGet(local_index) => stack.values.push(cf.get_local(*local_index as usize)),
diff --git a/crates/tinywasm/src/runtime/stack/blocks.rs b/crates/tinywasm/src/runtime/stack/blocks.rs
index d0ce02a..ea1f110 100644
--- a/crates/tinywasm/src/runtime/stack/blocks.rs
+++ b/crates/tinywasm/src/runtime/stack/blocks.rs
@@ -1,6 +1,6 @@
use alloc::vec::Vec;
use log::info;
-use tinywasm_types::BlockArgs;
+use tinywasm_types::{BlockArgs, FuncType};
#[derive(Debug, Default, Clone)]
pub(crate) struct Labels(Vec<LabelFrame>);
diff --git a/crates/tinywasm/src/runtime/stack/call_stack.rs b/crates/tinywasm/src/runtime/stack/call_stack.rs
index 42fae54..05785af 100644
--- a/crates/tinywasm/src/runtime/stack/call_stack.rs
+++ b/crates/tinywasm/src/runtime/stack/call_stack.rs
@@ -97,7 +97,7 @@ impl CallFrame {
BlockType::Loop => {
// this is a loop, so we want to jump back to the start of the loop
self.instr_ptr = break_to.instr_ptr;
- value_stack.trim(break_to.stack_ptr);
+ value_stack.truncate(break_to.stack_ptr);
// we also want to trim the label stack to the loop (but not including the loop)
self.labels.trim(self.labels.len() - break_to_relative as usize);
@@ -107,11 +107,10 @@ impl CallFrame {
// this is a block, so we want to jump to the next instruction after the block ends
self.instr_ptr = break_to.end_instr_ptr + 1;
- value_stack.trim(break_to.stack_ptr);
+ value_stack.truncate(break_to.stack_ptr);
// we also want to trim the label stack, including the block
self.labels.trim(self.labels.len() - break_to_relative as usize + 1);
- panic!()
}
_ => unimplemented!("break to block type: {:?}", current_label.ty),
}
diff --git a/crates/tinywasm/src/runtime/stack/value_stack.rs b/crates/tinywasm/src/runtime/stack/value_stack.rs
index 1962995..f597159 100644
--- a/crates/tinywasm/src/runtime/stack/value_stack.rs
+++ b/crates/tinywasm/src/runtime/stack/value_stack.rs
@@ -1,5 +1,8 @@
-use crate::{runtime::RawWasmValue, Error, Result};
+use core::ops::Range;
+
+use crate::{runtime::RawWasmValue, Error, ModuleInstance, Result};
use alloc::vec::Vec;
+use log::info;
use tinywasm_types::BlockArgs;
// minimum stack size
@@ -23,6 +26,11 @@ impl Default for ValueStack {
}
impl ValueStack {
+ #[cfg(test)]
+ pub(crate) fn data(&self) -> &[RawWasmValue] {
+ &self.stack
+ }
+
#[inline]
pub(crate) fn len(&self) -> usize {
assert!(self.top <= self.stack.len());
@@ -30,19 +38,24 @@ impl ValueStack {
}
#[inline]
- pub(crate) fn trim(&mut self, n: usize) {
+ pub(crate) fn truncate(&mut self, n: usize) {
assert!(self.top <= self.stack.len());
self.top -= n;
self.stack.truncate(self.top);
}
#[inline]
- pub(crate) fn push_block_args(&self, args: BlockArgs) -> Result<()> {
- match args {
- BlockArgs::Empty => Ok(()),
- BlockArgs::Type(_t) => todo!("support block args (type)"),
- BlockArgs::FuncType(_t) => todo!("support block args (func type)"),
+ // example: [1, 2, 3] n=1, end_keep=1 => [1, 3]
+ // example: [1] n=1, end_keep=1 => [1]
+ pub(crate) fn truncate_keep(&mut self, n: usize, end_keep: usize) {
+ if n == end_keep || n == 0 {
+ return;
}
+
+ assert!(self.top <= self.stack.len());
+ info!("removing from {} to {}", self.top - n, self.top - end_keep);
+ self.stack.drain(self.top - n..self.top - end_keep);
+ self.top -= n - end_keep;
}
#[inline]
@@ -98,3 +111,56 @@ impl ValueStack {
Ok(res)
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::std::panic;
+
+ fn crate_stack<T: Into<RawWasmValue> + Copy>(data: &[T]) -> ValueStack {
+ let mut stack = ValueStack::default();
+ stack.extend(data.iter().map(|v| (*v).into()));
+ stack
+ }
+
+ fn assert_truncate_keep<T: Into<RawWasmValue> + Copy>(data: &[T], n: usize, end_keep: usize, expected: &[T]) {
+ let mut stack = crate_stack(data);
+ stack.truncate_keep(n, end_keep);
+ assert_eq!(
+ stack.data(),
+ expected.iter().map(|v| (*v).into()).collect::<Vec<_>>().as_slice()
+ );
+ }
+
+ fn catch_unwind_silent<F: FnOnce() -> R + panic::UnwindSafe, R>(f: F) -> crate::std::thread::Result<R> {
+ let prev_hook = panic::take_hook();
+ panic::set_hook(alloc::boxed::Box::new(|_| {}));
+ let result = panic::catch_unwind(f);
+ panic::set_hook(prev_hook);
+ result
+ }
+
+ #[test]
+ fn test_truncate_keep() {
+ assert_truncate_keep(&[1, 2, 3], 1, 1, &[1, 2, 3]);
+ assert_truncate_keep(&[1], 1, 1, &[1]);
+ assert_truncate_keep(&[1, 2, 3], 2, 1, &[1, 3]);
+ assert_truncate_keep::<i32>(&[], 0, 0, &[]);
+ catch_unwind_silent(|| assert_truncate_keep(&[1, 2, 3], 4, 1, &[1, 3])).expect_err("should panic");
+ }
+
+ #[test]
+ fn test_value_stack() {
+ let mut stack = ValueStack::default();
+ stack.push(1.into());
+ stack.push(2.into());
+ stack.push(3.into());
+ assert_eq!(stack.len(), 3);
+ assert_eq!(stack.pop_t::<i32>().unwrap(), 3);
+ assert_eq!(stack.len(), 2);
+ assert_eq!(stack.pop_t::<i32>().unwrap(), 2);
+ assert_eq!(stack.len(), 1);
+ assert_eq!(stack.pop_t::<i32>().unwrap(), 1);
+ assert_eq!(stack.len(), 0);
+ }
+}
diff --git a/crates/tinywasm/tests/mvp.csv b/crates/tinywasm/tests/mvp.csv
index 4c7715c..d95dbaf 100644
--- a/crates/tinywasm/tests/mvp.csv
+++ b/crates/tinywasm/tests/mvp.csv
@@ -1,3 +1,3 @@
0.0.3,9258,7567,[{"name":"address.wast","passed":0,"failed":54},{"name":"align.wast","passed":0,"failed":109},{"name":"binary-leb128.wast","passed":66,"failed":25},{"name":"binary.wast","passed":104,"failed":8},{"name":"block.wast","passed":0,"failed":171},{"name":"br.wast","passed":0,"failed":21},{"name":"br_if.wast","passed":0,"failed":30},{"name":"br_table.wast","passed":0,"failed":25},{"name":"call.wast","passed":0,"failed":22},{"name":"call_indirect.wast","passed":0,"failed":56},{"name":"comments.wast","passed":4,"failed":4},{"name":"const.wast","passed":702,"failed":76},{"name":"conversions.wast","passed":0,"failed":93},{"name":"custom.wast","passed":10,"failed":1},{"name":"data.wast","passed":0,"failed":61},{"name":"elem.wast","passed":0,"failed":76},{"name":"endianness.wast","passed":0,"failed":1},{"name":"exports.wast","passed":21,"failed":73},{"name":"f32.wast","passed":1005,"failed":1509},{"name":"f32_bitwise.wast","passed":1,"failed":363},{"name":"f32_cmp.wast","passed":2401,"failed":6},{"name":"f64.wast","passed":1005,"failed":1509},{"name":"f64_bitwise.wast","passed":1,"failed":363},{"name":"f64_cmp.wast","passed":2401,"failed":6},{"name":"fac.wast","passed":0,"failed":2},{"name":"float_exprs.wast","passed":269,"failed":591},{"name":"float_literals.wast","passed":34,"failed":129},{"name":"float_memory.wast","passed":0,"failed":6},{"name":"float_misc.wast","passed":138,"failed":303},{"name":"forward.wast","passed":1,"failed":4},{"name":"func.wast","passed":4,"failed":75},{"name":"func_ptrs.wast","passed":0,"failed":16},{"name":"global.wast","passed":4,"failed":49},{"name":"i32.wast","passed":0,"failed":96},{"name":"i64.wast","passed":0,"failed":42},{"name":"if.wast","passed":0,"failed":118},{"name":"imports.wast","passed":1,"failed":156},{"name":"inline-module.wast","passed":0,"failed":1},{"name":"int_exprs.wast","passed":38,"failed":70},{"name":"int_literals.wast","passed":5,"failed":46},{"name":"labels.wast","passed":1,"failed":28},{"name":"left-to-right.wast","passed":0,"failed":1},{"name":"linking.wast","passed":1,"failed":66},{"name":"load.wast","passed":0,"failed":60},{"name":"local_get.wast","passed":2,"failed":34},{"name":"local_set.wast","passed":5,"failed":48},{"name":"local_tee.wast","passed":0,"failed":42},{"name":"loop.wast","passed":0,"failed":43},{"name":"memory.wast","passed":0,"failed":34},{"name":"memory_grow.wast","passed":0,"failed":19},{"name":"memory_redundancy.wast","passed":0,"failed":1},{"name":"memory_size.wast","passed":0,"failed":6},{"name":"memory_trap.wast","passed":0,"failed":172},{"name":"names.wast","passed":484,"failed":1},{"name":"nop.wast","passed":0,"failed":5},{"name":"return.wast","passed":0,"failed":21},{"name":"select.wast","passed":0,"failed":32},{"name":"skip-stack-guard-page.wast","passed":0,"failed":11},{"name":"stack.wast","passed":0,"failed":2},{"name":"start.wast","passed":0,"failed":10},{"name":"store.wast","passed":0,"failed":59},{"name":"switch.wast","passed":1,"failed":27},{"name":"token.wast","passed":16,"failed":42},{"name":"traps.wast","passed":3,"failed":33},{"name":"type.wast","passed":1,"failed":2},{"name":"unreachable.wast","passed":0,"failed":59},{"name":"unreached-invalid.wast","passed":0,"failed":118},{"name":"unwind.wast","passed":1,"failed":49},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":0,"failed":176}]
0.0.4,9258,7567,[{"name":"address.wast","passed":0,"failed":54},{"name":"align.wast","passed":0,"failed":109},{"name":"binary-leb128.wast","passed":66,"failed":25},{"name":"binary.wast","passed":104,"failed":8},{"name":"block.wast","passed":0,"failed":171},{"name":"br.wast","passed":0,"failed":21},{"name":"br_if.wast","passed":0,"failed":30},{"name":"br_table.wast","passed":0,"failed":25},{"name":"call.wast","passed":0,"failed":22},{"name":"call_indirect.wast","passed":0,"failed":56},{"name":"comments.wast","passed":4,"failed":4},{"name":"const.wast","passed":702,"failed":76},{"name":"conversions.wast","passed":0,"failed":93},{"name":"custom.wast","passed":10,"failed":1},{"name":"data.wast","passed":0,"failed":61},{"name":"elem.wast","passed":0,"failed":76},{"name":"endianness.wast","passed":0,"failed":1},{"name":"exports.wast","passed":21,"failed":73},{"name":"f32.wast","passed":1005,"failed":1509},{"name":"f32_bitwise.wast","passed":1,"failed":363},{"name":"f32_cmp.wast","passed":2401,"failed":6},{"name":"f64.wast","passed":1005,"failed":1509},{"name":"f64_bitwise.wast","passed":1,"failed":363},{"name":"f64_cmp.wast","passed":2401,"failed":6},{"name":"fac.wast","passed":0,"failed":2},{"name":"float_exprs.wast","passed":269,"failed":591},{"name":"float_literals.wast","passed":34,"failed":129},{"name":"float_memory.wast","passed":0,"failed":6},{"name":"float_misc.wast","passed":138,"failed":303},{"name":"forward.wast","passed":1,"failed":4},{"name":"func.wast","passed":4,"failed":75},{"name":"func_ptrs.wast","passed":0,"failed":16},{"name":"global.wast","passed":4,"failed":49},{"name":"i32.wast","passed":0,"failed":96},{"name":"i64.wast","passed":0,"failed":42},{"name":"if.wast","passed":0,"failed":118},{"name":"imports.wast","passed":1,"failed":156},{"name":"inline-module.wast","passed":0,"failed":1},{"name":"int_exprs.wast","passed":38,"failed":70},{"name":"int_literals.wast","passed":5,"failed":46},{"name":"labels.wast","passed":1,"failed":28},{"name":"left-to-right.wast","passed":0,"failed":1},{"name":"linking.wast","passed":1,"failed":66},{"name":"load.wast","passed":0,"failed":60},{"name":"local_get.wast","passed":2,"failed":34},{"name":"local_set.wast","passed":5,"failed":48},{"name":"local_tee.wast","passed":0,"failed":42},{"name":"loop.wast","passed":0,"failed":43},{"name":"memory.wast","passed":0,"failed":34},{"name":"memory_grow.wast","passed":0,"failed":19},{"name":"memory_redundancy.wast","passed":0,"failed":1},{"name":"memory_size.wast","passed":0,"failed":6},{"name":"memory_trap.wast","passed":0,"failed":172},{"name":"names.wast","passed":484,"failed":1},{"name":"nop.wast","passed":0,"failed":5},{"name":"return.wast","passed":0,"failed":21},{"name":"select.wast","passed":0,"failed":32},{"name":"skip-stack-guard-page.wast","passed":0,"failed":11},{"name":"stack.wast","passed":0,"failed":2},{"name":"start.wast","passed":0,"failed":10},{"name":"store.wast","passed":0,"failed":59},{"name":"switch.wast","passed":1,"failed":27},{"name":"token.wast","passed":16,"failed":42},{"name":"traps.wast","passed":3,"failed":33},{"name":"type.wast","passed":1,"failed":2},{"name":"unreachable.wast","passed":0,"failed":59},{"name":"unreached-invalid.wast","passed":0,"failed":118},{"name":"unwind.wast","passed":1,"failed":49},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":0,"failed":176}]
-0.0.5-alpha.0,9262,10924,[{"name":"address.wast","passed":0,"failed":260},{"name":"align.wast","passed":0,"failed":156},{"name":"binary-leb128.wast","passed":66,"failed":25},{"name":"binary.wast","passed":104,"failed":8},{"name":"block.wast","passed":0,"failed":223},{"name":"br.wast","passed":0,"failed":97},{"name":"br_if.wast","passed":0,"failed":118},{"name":"br_table.wast","passed":0,"failed":174},{"name":"call.wast","passed":0,"failed":91},{"name":"call_indirect.wast","passed":0,"failed":170},{"name":"comments.wast","passed":4,"failed":4},{"name":"const.wast","passed":702,"failed":76},{"name":"conversions.wast","passed":0,"failed":619},{"name":"custom.wast","passed":10,"failed":1},{"name":"data.wast","passed":0,"failed":61},{"name":"elem.wast","passed":0,"failed":99},{"name":"endianness.wast","passed":0,"failed":69},{"name":"exports.wast","passed":21,"failed":75},{"name":"f32.wast","passed":1005,"failed":1509},{"name":"f32_bitwise.wast","passed":1,"failed":363},{"name":"f32_cmp.wast","passed":2401,"failed":6},{"name":"f64.wast","passed":1005,"failed":1509},{"name":"f64_bitwise.wast","passed":1,"failed":363},{"name":"f64_cmp.wast","passed":2401,"failed":6},{"name":"fac.wast","passed":0,"failed":8},{"name":"float_exprs.wast","passed":273,"failed":617},{"name":"float_literals.wast","passed":34,"failed":129},{"name":"float_memory.wast","passed":0,"failed":66},{"name":"float_misc.wast","passed":138,"failed":303},{"name":"forward.wast","passed":1,"failed":4},{"name":"func.wast","passed":4,"failed":168},{"name":"func_ptrs.wast","passed":0,"failed":35},{"name":"global.wast","passed":4,"failed":106},{"name":"i32.wast","passed":0,"failed":460},{"name":"i64.wast","passed":0,"failed":416},{"name":"if.wast","passed":0,"failed":241},{"name":"imports.wast","passed":1,"failed":182},{"name":"inline-module.wast","passed":0,"failed":1},{"name":"int_exprs.wast","passed":38,"failed":70},{"name":"int_literals.wast","passed":5,"failed":46},{"name":"labels.wast","passed":1,"failed":28},{"name":"left-to-right.wast","passed":0,"failed":96},{"name":"linking.wast","passed":1,"failed":131},{"name":"load.wast","passed":0,"failed":97},{"name":"local_get.wast","passed":2,"failed":34},{"name":"local_set.wast","passed":5,"failed":48},{"name":"local_tee.wast","passed":0,"failed":97},{"name":"loop.wast","passed":0,"failed":120},{"name":"memory.wast","passed":0,"failed":79},{"name":"memory_grow.wast","passed":0,"failed":96},{"name":"memory_redundancy.wast","passed":0,"failed":5},{"name":"memory_size.wast","passed":0,"failed":42},{"name":"memory_trap.wast","passed":0,"failed":182},{"name":"names.wast","passed":484,"failed":2},{"name":"nop.wast","passed":0,"failed":88},{"name":"return.wast","passed":0,"failed":84},{"name":"select.wast","passed":0,"failed":148},{"name":"skip-stack-guard-page.wast","passed":0,"failed":11},{"name":"stack.wast","passed":0,"failed":7},{"name":"start.wast","passed":0,"failed":16},{"name":"store.wast","passed":0,"failed":68},{"name":"switch.wast","passed":1,"failed":27},{"name":"token.wast","passed":16,"failed":42},{"name":"traps.wast","passed":3,"failed":33},{"name":"type.wast","passed":1,"failed":2},{"name":"unreachable.wast","passed":0,"failed":64},{"name":"unreached-invalid.wast","passed":0,"failed":118},{"name":"unwind.wast","passed":1,"failed":49},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":0,"failed":176}]
+0.0.5-alpha.0,9270,10916,[{"name":"address.wast","passed":0,"failed":260},{"name":"align.wast","passed":0,"failed":156},{"name":"binary-leb128.wast","passed":66,"failed":25},{"name":"binary.wast","passed":104,"failed":8},{"name":"block.wast","passed":0,"failed":223},{"name":"br.wast","passed":0,"failed":97},{"name":"br_if.wast","passed":0,"failed":118},{"name":"br_table.wast","passed":0,"failed":174},{"name":"call.wast","passed":0,"failed":91},{"name":"call_indirect.wast","passed":0,"failed":170},{"name":"comments.wast","passed":4,"failed":4},{"name":"const.wast","passed":702,"failed":76},{"name":"conversions.wast","passed":0,"failed":619},{"name":"custom.wast","passed":10,"failed":1},{"name":"data.wast","passed":0,"failed":61},{"name":"elem.wast","passed":0,"failed":99},{"name":"endianness.wast","passed":0,"failed":69},{"name":"exports.wast","passed":21,"failed":75},{"name":"f32.wast","passed":1005,"failed":1509},{"name":"f32_bitwise.wast","passed":1,"failed":363},{"name":"f32_cmp.wast","passed":2401,"failed":6},{"name":"f64.wast","passed":1005,"failed":1509},{"name":"f64_bitwise.wast","passed":1,"failed":363},{"name":"f64_cmp.wast","passed":2401,"failed":6},{"name":"fac.wast","passed":1,"failed":7},{"name":"float_exprs.wast","passed":273,"failed":617},{"name":"float_literals.wast","passed":34,"failed":129},{"name":"float_memory.wast","passed":0,"failed":66},{"name":"float_misc.wast","passed":138,"failed":303},{"name":"forward.wast","passed":1,"failed":4},{"name":"func.wast","passed":9,"failed":163},{"name":"func_ptrs.wast","passed":0,"failed":35},{"name":"global.wast","passed":4,"failed":106},{"name":"i32.wast","passed":0,"failed":460},{"name":"i64.wast","passed":0,"failed":416},{"name":"if.wast","passed":0,"failed":241},{"name":"imports.wast","passed":1,"failed":182},{"name":"inline-module.wast","passed":0,"failed":1},{"name":"int_exprs.wast","passed":38,"failed":70},{"name":"int_literals.wast","passed":5,"failed":46},{"name":"labels.wast","passed":3,"failed":26},{"name":"left-to-right.wast","passed":0,"failed":96},{"name":"linking.wast","passed":1,"failed":131},{"name":"load.wast","passed":0,"failed":97},{"name":"local_get.wast","passed":2,"failed":34},{"name":"local_set.wast","passed":5,"failed":48},{"name":"local_tee.wast","passed":0,"failed":97},{"name":"loop.wast","passed":0,"failed":120},{"name":"memory.wast","passed":0,"failed":79},{"name":"memory_grow.wast","passed":0,"failed":96},{"name":"memory_redundancy.wast","passed":0,"failed":5},{"name":"memory_size.wast","passed":0,"failed":42},{"name":"memory_trap.wast","passed":0,"failed":182},{"name":"names.wast","passed":484,"failed":2},{"name":"nop.wast","passed":0,"failed":88},{"name":"return.wast","passed":0,"failed":84},{"name":"select.wast","passed":0,"failed":148},{"name":"skip-stack-guard-page.wast","passed":0,"failed":11},{"name":"stack.wast","passed":0,"failed":7},{"name":"start.wast","passed":0,"failed":16},{"name":"store.wast","passed":0,"failed":68},{"name":"switch.wast","passed":1,"failed":27},{"name":"token.wast","passed":16,"failed":42},{"name":"traps.wast","passed":3,"failed":33},{"name":"type.wast","passed":1,"failed":2},{"name":"unreachable.wast","passed":0,"failed":64},{"name":"unreached-invalid.wast","passed":0,"failed":118},{"name":"unwind.wast","passed":1,"failed":49},{"name":"utf8-custom-section-id.wast","passed":176,"failed":0},{"name":"utf8-import-field.wast","passed":176,"failed":0},{"name":"utf8-import-module.wast","passed":176,"failed":0},{"name":"utf8-invalid-encoding.wast","passed":0,"failed":176}]
diff --git a/crates/tinywasm/tests/progress-mvp.svg b/crates/tinywasm/tests/progress-mvp.svg
index 4e3410f..f3903f8 100644
--- a/crates/tinywasm/tests/progress-mvp.svg
+++ b/crates/tinywasm/tests/progress-mvp.svg
@@ -45,10 +45,10 @@ v0.0.4 (9258)
</text>
<polyline fill="none" opacity="1" stroke="#000000" stroke-width="1" points="534,345 534,350 "/>
<text x="837" y="355" dy="0.76em" text-anchor="middle" font-family="Victor Mono" font-size="12.096774193548388" opacity="1" fill="#000000">
-v0.0.5-alpha.0 (9262)
+v0.0.5-alpha.0 (9268)
</text>
<polyline fill="none" opacity="1" stroke="#000000" stroke-width="1" points="837,345 837,350 "/>
+<rect x="691" y="211" width="293" height="133" opacity="0.5" fill="#0000FF" stroke="none"/>
<rect x="85" y="211" width="293" height="133" opacity="0.5" fill="#0000FF" stroke="none"/>
<rect x="388" y="211" width="293" height="133" opacity="0.5" fill="#0000FF" stroke="none"/>
-<rect x="691" y="211" width="293" height="133" opacity="0.5" fill="#0000FF" stroke="none"/>
</svg>
diff --git a/crates/tinywasm/tests/testsuite/run.rs b/crates/tinywasm/tests/testsuite/run.rs
index 56f3a44..ba39f43 100644
--- a/crates/tinywasm/tests/testsuite/run.rs
+++ b/crates/tinywasm/tests/testsuite/run.rs
@@ -1,4 +1,4 @@
-use crate::testsuite::util::{parse_module, wastarg2tinywasmvalue, wastret2tinywasmvalue};
+use crate::testsuite::util::*;
use super::TestSuite;
use eyre::{eyre, Result};
@@ -30,7 +30,7 @@ impl TestSuite {
match directive {
// TODO: needs to support more binary sections
Wat(QuoteWat::Wat(wast::Wat::Module(module))) => {
- let result = std::panic::catch_unwind(|| parse_module(module))
+ let result = catch_unwind_silent(|| parse_module(module))
.map_err(|e| eyre!("failed to parse module: {:?}", e))
.and_then(|res| res);
@@ -50,7 +50,7 @@ impl TestSuite {
module: QuoteWat::Wat(wast::Wat::Module(module)),
message: _,
} => {
- let res = std::panic::catch_unwind(|| parse_module(module).map(|_| ()));
+ let res = catch_unwind_silent(|| parse_module(module).map(|_| ()));
test_group.add_result(
&format!("{}-malformed", name),
span,
@@ -68,7 +68,7 @@ impl TestSuite {
continue;
};
- let res: Result<Result<()>, _> = std::panic::catch_unwind(|| {
+ let res: Result<Result<()>, _> = catch_unwind_silent(|| {
let mut store = tinywasm::Store::new();
let module = tinywasm::Module::from(module);
let instance = module.instantiate(&mut store)?;
diff --git a/crates/tinywasm/tests/testsuite/util.rs b/crates/tinywasm/tests/testsuite/util.rs
index 18effa0..f153f8a 100644
--- a/crates/tinywasm/tests/testsuite/util.rs
+++ b/crates/tinywasm/tests/testsuite/util.rs
@@ -1,6 +1,16 @@
+use std::panic;
+
use eyre::{eyre, Result};
use tinywasm_types::TinyWasmModule;
+pub fn catch_unwind_silent<F: FnOnce() -> R + panic::UnwindSafe, R>(f: F) -> std::thread::Result<R> {
+ let prev_hook = panic::take_hook();
+ panic::set_hook(Box::new(|_| {}));
+ let result = panic::catch_unwind(f);
+ panic::set_hook(prev_hook);
+ result
+}
+
pub fn parse_module(mut module: wast::core::Module) -> Result<TinyWasmModule> {
let parser = tinywasm_parser::Parser::new();
Ok(parser.parse_module_bytes(module.encode().expect("failed to encode module"))?)
diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs
index 4defb23..b7bf623 100644
--- a/crates/types/src/lib.rs
+++ b/crates/types/src/lib.rs
@@ -265,6 +265,16 @@ pub struct FuncType {
pub results: Box<[ValType]>,
}
+impl FuncType {
+ /// Get the number of parameters of a function type.
+ pub fn empty() -> Self {
+ Self {
+ params: Box::new([]),
+ results: Box::new([]),
+ }
+ }
+}
+
/// A WebAssembly Function
#[derive(Debug, Clone)]
pub struct Function {
diff --git a/examples/wasm/call.wat b/examples/wasm/call.wat
index 9d00151..b604c75 100644
--- a/examples/wasm/call.wat
+++ b/examples/wasm/call.wat
@@ -1,15 +1,43 @@
(module
- (func $check_input (param i32) (result i32)
- local.get 0
- i32.const 10
- i32.lt_s ;; Check if input is less than 10
- if (result i32) ;; If so,
- i32.const 1 ;; Set 1 to the stack
- return ;; And return immediately
- else ;; Otherwise,
- i32.const 0 ;; Set 0 to the stack
- return ;; And return immediately
- end) ;; End of the if/else block
+ ;; (func $check_input (param i32) (result i32)
+ ;; i64.const 0 ;; Set 0 to the stack
+ ;; local.get 0
+ ;; i32.const 10
+ ;; i32.lt_s ;; Check if input is less than 10
+ ;; if (param i64) (result i32) ;; If so,
+ ;; i32.const 1 ;; Set 1 to the stack
+ ;; return ;; And return immediately
+ ;; else ;; Otherwise,
+ ;; i32.const 0 ;; Set 0 to the stack
+ ;; return ;; And return immediately
+ ;; end) ;; End of the if/else block
- (export "check" (func $check_input))
+ (func (export "simple_block") (result i32)
+ (block (result i32)
+ (i32.const 0)
+ (i32.const 1)
+ (i32.add)
+ )
+ )
+
+ (func (export "checkloop") (result i32)
+ (block (result i32)
+ (i32.const 0)
+ (loop (param i32)
+ (block (br 2 (i32.const 18)))
+ (br 0 (i32.const 20))
+ )
+ (i32.const 19)
+ )
+ )
+
+
+ (func (export "param") (result i32)
+ (i32.const 1)
+ (loop (param i32) (result i32)
+ (i32.const 2)
+ (i32.add)
+ )
+ )
+ ;; (export "check" (func $check_input))
) \ No newline at end of file