summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/tinywasm/src/engine.rs11
-rw-r--r--crates/tinywasm/src/error.rs2
-rw-r--r--crates/tinywasm/src/interpreter/executor.rs40
-rw-r--r--crates/tinywasm/src/interpreter/stack/block_stack.rs14
-rw-r--r--crates/tinywasm/src/interpreter/values.rs2
-rw-r--r--crates/tinywasm/tests/test-wasm-tail-call.rs1
6 files changed, 28 insertions, 42 deletions
diff --git a/crates/tinywasm/src/engine.rs b/crates/tinywasm/src/engine.rs
index 261f18c..fe30513 100644
--- a/crates/tinywasm/src/engine.rs
+++ b/crates/tinywasm/src/engine.rs
@@ -52,10 +52,10 @@ pub const DEFAULT_VALUE_STACK_128_SIZE: usize = 4 * 1024; // 4k slots
pub const DEFAULT_VALUE_STACK_REF_SIZE: usize = 4 * 1024; // 4k slots
/// Default initial size for the block stack (control frames).
-pub const DEFAULT_BLOCK_STACK_SIZE: usize = 1024; // 1024 frames
+pub const DEFAULT_BLOCK_STACK_SIZE: usize = 2048; // 1024 frames
/// Default initial size for the call stack (function frames).
-pub const DEFAULT_CALL_STACK_SIZE: usize = 1024; // 1024 frames
+pub const DEFAULT_CALL_STACK_SIZE: usize = 2048; // 1024 frames
/// Configuration for the WebAssembly interpreter
#[derive(Debug, Clone)]
@@ -71,8 +71,9 @@ pub struct Config {
pub stack_ref_size: usize,
/// Initial size of the call stack.
pub call_stack_size: usize,
- /// Initial size of the control stack (block stack).
- pub block_stack_size: usize,
+
+ /// Initial size of the block stack.
+ pub block_stack_initial_size: usize,
}
impl Config {
@@ -90,7 +91,7 @@ impl Default for Config {
stack_128_size: DEFAULT_VALUE_STACK_128_SIZE,
stack_ref_size: DEFAULT_VALUE_STACK_REF_SIZE,
call_stack_size: DEFAULT_CALL_STACK_SIZE,
- block_stack_size: DEFAULT_BLOCK_STACK_SIZE,
+ block_stack_initial_size: DEFAULT_BLOCK_STACK_SIZE,
}
}
}
diff --git a/crates/tinywasm/src/error.rs b/crates/tinywasm/src/error.rs
index 4788ac4..51b5c46 100644
--- a/crates/tinywasm/src/error.rs
+++ b/crates/tinywasm/src/error.rs
@@ -1,8 +1,8 @@
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::{fmt::Display, ops::ControlFlow};
-use tinywasm_types::archive::TwasmError;
use tinywasm_types::FuncType;
+use tinywasm_types::archive::TwasmError;
#[cfg(feature = "parser")]
pub use tinywasm_parser::ParseError;
diff --git a/crates/tinywasm/src/interpreter/executor.rs b/crates/tinywasm/src/interpreter/executor.rs
index dcdd2f8..08a24cf 100644
--- a/crates/tinywasm/src/interpreter/executor.rs
+++ b/crates/tinywasm/src/interpreter/executor.rs
@@ -88,16 +88,16 @@ impl<'store> Executor<'store> {
CallIndirect(ty, table) => return self.exec_call_indirect::<false>(*ty, *table),
ReturnCall(v) => return self.exec_call_direct::<true>(*v),
ReturnCallIndirect(ty, table) => return self.exec_call_indirect::<true>(*ty, *table),
- If(end, el) => self.exec_if(*end, *el, (StackHeight::default(), StackHeight::default())).to_cf()?,
- IfWithType(ty, end, el) => self.exec_if(*end, *el, (StackHeight::default(), (*ty).into())).to_cf()?,
- IfWithFuncType(ty, end, el) => self.exec_if(*end, *el, self.resolve_functype(*ty)).to_cf()?,
+ If(end, el) => self.exec_if(*end, *el, (StackHeight::default(), StackHeight::default())),
+ IfWithType(ty, end, el) => self.exec_if(*end, *el, (StackHeight::default(), (*ty).into())),
+ IfWithFuncType(ty, end, el) => self.exec_if(*end, *el, self.resolve_functype(*ty)),
Else(end_offset) => self.exec_else(*end_offset),
- Loop(end) => self.enter_block(*end, BlockType::Loop, (StackHeight::default(), StackHeight::default())).to_cf()?,
- LoopWithType(ty, end) => self.enter_block(*end, BlockType::Loop, (StackHeight::default(), (*ty).into())).to_cf()?,
- LoopWithFuncType(ty, end) => self.enter_block(*end, BlockType::Loop, self.resolve_functype(*ty)).to_cf()?,
- Block(end) => self.enter_block(*end, BlockType::Block, (StackHeight::default(), StackHeight::default())).to_cf()?,
- BlockWithType(ty, end) => self.enter_block(*end, BlockType::Block, (StackHeight::default(), (*ty).into())).to_cf()?,
- BlockWithFuncType(ty, end) => self.enter_block(*end, BlockType::Block, self.resolve_functype(*ty)).to_cf()?,
+ Loop(end) => self.enter_block(*end, BlockType::Loop, (StackHeight::default(), StackHeight::default())),
+ LoopWithType(ty, end) => self.enter_block(*end, BlockType::Loop, (StackHeight::default(), (*ty).into())),
+ LoopWithFuncType(ty, end) => self.enter_block(*end, BlockType::Loop, self.resolve_functype(*ty)),
+ Block(end) => self.enter_block(*end, BlockType::Block, (StackHeight::default(), StackHeight::default())),
+ BlockWithType(ty, end) => self.enter_block(*end, BlockType::Block, (StackHeight::default(), (*ty).into())),
+ BlockWithFuncType(ty, end) => self.enter_block(*end, BlockType::Block, self.resolve_functype(*ty)),
Br(v) => return self.exec_br(*v),
BrIf(v) => return self.exec_br_if(*v),
BrTable(default, len) => return self.exec_brtable(*default, *len),
@@ -635,26 +635,21 @@ impl<'store> Executor<'store> {
}
}
- fn exec_if(
- &mut self,
- else_offset: u32,
- end_offset: u32,
- (params, results): (StackHeight, StackHeight),
- ) -> Result<()> {
+ fn exec_if(&mut self, else_offset: u32, end_offset: u32, (params, results): (StackHeight, StackHeight)) {
// truthy value is on the top of the stack, so enter the then block
if self.store.stack.values.pop::<i32>() != 0 {
- self.enter_block(end_offset, BlockType::If, (params, results))?;
- return Ok(());
+ self.enter_block(end_offset, BlockType::If, (params, results));
+ return;
}
// falsy value is on the top of the stack
if else_offset == 0 {
self.cf.jump(end_offset);
- return Ok(());
+ return;
}
self.cf.jump(else_offset);
- self.enter_block(end_offset - else_offset, BlockType::Else, (params, results))
+ self.enter_block(end_offset - else_offset, BlockType::Else, (params, results));
}
fn exec_else(&mut self, end_offset: u32) {
self.exec_end_block();
@@ -664,12 +659,7 @@ impl<'store> Executor<'store> {
let ty = self.module.func_ty(idx);
((&*ty.params).into(), (&*ty.results).into())
}
- fn enter_block(
- &mut self,
- end_instr_offset: u32,
- ty: BlockType,
- (params, results): (StackHeight, StackHeight),
- ) -> Result<()> {
+ fn enter_block(&mut self, end_instr_offset: u32, ty: BlockType, (params, results): (StackHeight, StackHeight)) {
self.store.stack.blocks.push(BlockFrame {
instr_ptr: self.cf.instr_ptr() as u32,
end_instr_offset,
diff --git a/crates/tinywasm/src/interpreter/stack/block_stack.rs b/crates/tinywasm/src/interpreter/stack/block_stack.rs
index 68dda84..f653460 100644
--- a/crates/tinywasm/src/interpreter/stack/block_stack.rs
+++ b/crates/tinywasm/src/interpreter/stack/block_stack.rs
@@ -2,14 +2,13 @@ use crate::engine::Config;
use alloc::vec::Vec;
use crate::interpreter::values::{StackHeight, StackLocation};
-use crate::{Result, Trap};
#[derive(Debug)]
pub(crate) struct BlockStack(Vec<BlockFrame>);
impl BlockStack {
pub(crate) fn new(config: &Config) -> Self {
- Self(Vec::with_capacity(config.block_stack_size))
+ Self(Vec::with_capacity(config.block_stack_initial_size))
}
pub(crate) fn clear(&mut self) {
@@ -20,20 +19,15 @@ impl BlockStack {
self.0.len()
}
- pub(crate) fn push(&mut self, block: BlockFrame) -> Result<()> {
- if self.0.len() >= self.0.capacity() {
- return Err(Trap::BlockStackOverflow.into());
- }
-
+ pub(crate) fn push(&mut self, block: BlockFrame) {
self.0.push(block);
- Ok(())
}
/// get the label at the given index, where 0 is the top of the stack
pub(crate) fn get_relative_to(&self, index: u32, offset: u32) -> Option<&BlockFrame> {
- let len = (self.0.len() as u32) - offset;
+ let len = (self.0.len() as u32).checked_sub(offset)?;
- // the vast majority of wasm functions don't use break to return
+ // the vast majority of wasm functions don't use break to return, but it is allowed in the spec
if index >= len {
return None;
}
diff --git a/crates/tinywasm/src/interpreter/values.rs b/crates/tinywasm/src/interpreter/values.rs
index ac05bd4..447b163 100644
--- a/crates/tinywasm/src/interpreter/values.rs
+++ b/crates/tinywasm/src/interpreter/values.rs
@@ -1,4 +1,4 @@
-use crate::{interpreter::value128::Value128, Result};
+use crate::{Result, interpreter::value128::Value128};
use super::stack::{Locals, ValueStack};
use tinywasm_types::{ExternRef, FuncRef, LocalAddr, ValType, WasmValue};
diff --git a/crates/tinywasm/tests/test-wasm-tail-call.rs b/crates/tinywasm/tests/test-wasm-tail-call.rs
index f888f38..b6d8b2b 100644
--- a/crates/tinywasm/tests/test-wasm-tail-call.rs
+++ b/crates/tinywasm/tests/test-wasm-tail-call.rs
@@ -8,6 +8,7 @@ fn main() -> Result<()> {
let mut test_suite = TestSuite::new();
test_suite.run_files(proposal(&Proposal::TailCall))?;
+ test_suite.print_errors();
test_suite.save_csv("./tests/generated/wasm-tail-call.csv", env!("CARGO_PKG_VERSION"))?;
test_suite.report_status()
}