Skip to content

Commit e25773f

Browse files
chore: undo BlockStack changes
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent af3029b commit e25773f

File tree

6 files changed

+28
-42
lines changed

6 files changed

+28
-42
lines changed

crates/tinywasm/src/engine.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ pub const DEFAULT_VALUE_STACK_128_SIZE: usize = 4 * 1024; // 4k slots
5252
pub const DEFAULT_VALUE_STACK_REF_SIZE: usize = 4 * 1024; // 4k slots
5353

5454
/// Default initial size for the block stack (control frames).
55-
pub const DEFAULT_BLOCK_STACK_SIZE: usize = 1024; // 1024 frames
55+
pub const DEFAULT_BLOCK_STACK_SIZE: usize = 2048; // 1024 frames
5656

5757
/// Default initial size for the call stack (function frames).
58-
pub const DEFAULT_CALL_STACK_SIZE: usize = 1024; // 1024 frames
58+
pub const DEFAULT_CALL_STACK_SIZE: usize = 2048; // 1024 frames
5959

6060
/// Configuration for the WebAssembly interpreter
6161
#[derive(Debug, Clone)]
@@ -71,8 +71,9 @@ pub struct Config {
7171
pub stack_ref_size: usize,
7272
/// Initial size of the call stack.
7373
pub call_stack_size: usize,
74-
/// Initial size of the control stack (block stack).
75-
pub block_stack_size: usize,
74+
75+
/// Initial size of the block stack.
76+
pub block_stack_initial_size: usize,
7677
}
7778

7879
impl Config {
@@ -90,7 +91,7 @@ impl Default for Config {
9091
stack_128_size: DEFAULT_VALUE_STACK_128_SIZE,
9192
stack_ref_size: DEFAULT_VALUE_STACK_REF_SIZE,
9293
call_stack_size: DEFAULT_CALL_STACK_SIZE,
93-
block_stack_size: DEFAULT_BLOCK_STACK_SIZE,
94+
block_stack_initial_size: DEFAULT_BLOCK_STACK_SIZE,
9495
}
9596
}
9697
}

crates/tinywasm/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use alloc::string::{String, ToString};
22
use alloc::vec::Vec;
33
use core::{fmt::Display, ops::ControlFlow};
4-
use tinywasm_types::archive::TwasmError;
54
use tinywasm_types::FuncType;
5+
use tinywasm_types::archive::TwasmError;
66

77
#[cfg(feature = "parser")]
88
pub use tinywasm_parser::ParseError;

crates/tinywasm/src/interpreter/executor.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ impl<'store> Executor<'store> {
8888
CallIndirect(ty, table) => return self.exec_call_indirect::<false>(*ty, *table),
8989
ReturnCall(v) => return self.exec_call_direct::<true>(*v),
9090
ReturnCallIndirect(ty, table) => return self.exec_call_indirect::<true>(*ty, *table),
91-
If(end, el) => self.exec_if(*end, *el, (StackHeight::default(), StackHeight::default())).to_cf()?,
92-
IfWithType(ty, end, el) => self.exec_if(*end, *el, (StackHeight::default(), (*ty).into())).to_cf()?,
93-
IfWithFuncType(ty, end, el) => self.exec_if(*end, *el, self.resolve_functype(*ty)).to_cf()?,
91+
If(end, el) => self.exec_if(*end, *el, (StackHeight::default(), StackHeight::default())),
92+
IfWithType(ty, end, el) => self.exec_if(*end, *el, (StackHeight::default(), (*ty).into())),
93+
IfWithFuncType(ty, end, el) => self.exec_if(*end, *el, self.resolve_functype(*ty)),
9494
Else(end_offset) => self.exec_else(*end_offset),
95-
Loop(end) => self.enter_block(*end, BlockType::Loop, (StackHeight::default(), StackHeight::default())).to_cf()?,
96-
LoopWithType(ty, end) => self.enter_block(*end, BlockType::Loop, (StackHeight::default(), (*ty).into())).to_cf()?,
97-
LoopWithFuncType(ty, end) => self.enter_block(*end, BlockType::Loop, self.resolve_functype(*ty)).to_cf()?,
98-
Block(end) => self.enter_block(*end, BlockType::Block, (StackHeight::default(), StackHeight::default())).to_cf()?,
99-
BlockWithType(ty, end) => self.enter_block(*end, BlockType::Block, (StackHeight::default(), (*ty).into())).to_cf()?,
100-
BlockWithFuncType(ty, end) => self.enter_block(*end, BlockType::Block, self.resolve_functype(*ty)).to_cf()?,
95+
Loop(end) => self.enter_block(*end, BlockType::Loop, (StackHeight::default(), StackHeight::default())),
96+
LoopWithType(ty, end) => self.enter_block(*end, BlockType::Loop, (StackHeight::default(), (*ty).into())),
97+
LoopWithFuncType(ty, end) => self.enter_block(*end, BlockType::Loop, self.resolve_functype(*ty)),
98+
Block(end) => self.enter_block(*end, BlockType::Block, (StackHeight::default(), StackHeight::default())),
99+
BlockWithType(ty, end) => self.enter_block(*end, BlockType::Block, (StackHeight::default(), (*ty).into())),
100+
BlockWithFuncType(ty, end) => self.enter_block(*end, BlockType::Block, self.resolve_functype(*ty)),
101101
Br(v) => return self.exec_br(*v),
102102
BrIf(v) => return self.exec_br_if(*v),
103103
BrTable(default, len) => return self.exec_brtable(*default, *len),
@@ -635,26 +635,21 @@ impl<'store> Executor<'store> {
635635
}
636636
}
637637

638-
fn exec_if(
639-
&mut self,
640-
else_offset: u32,
641-
end_offset: u32,
642-
(params, results): (StackHeight, StackHeight),
643-
) -> Result<()> {
638+
fn exec_if(&mut self, else_offset: u32, end_offset: u32, (params, results): (StackHeight, StackHeight)) {
644639
// truthy value is on the top of the stack, so enter the then block
645640
if self.store.stack.values.pop::<i32>() != 0 {
646-
self.enter_block(end_offset, BlockType::If, (params, results))?;
647-
return Ok(());
641+
self.enter_block(end_offset, BlockType::If, (params, results));
642+
return;
648643
}
649644

650645
// falsy value is on the top of the stack
651646
if else_offset == 0 {
652647
self.cf.jump(end_offset);
653-
return Ok(());
648+
return;
654649
}
655650

656651
self.cf.jump(else_offset);
657-
self.enter_block(end_offset - else_offset, BlockType::Else, (params, results))
652+
self.enter_block(end_offset - else_offset, BlockType::Else, (params, results));
658653
}
659654
fn exec_else(&mut self, end_offset: u32) {
660655
self.exec_end_block();
@@ -664,12 +659,7 @@ impl<'store> Executor<'store> {
664659
let ty = self.module.func_ty(idx);
665660
((&*ty.params).into(), (&*ty.results).into())
666661
}
667-
fn enter_block(
668-
&mut self,
669-
end_instr_offset: u32,
670-
ty: BlockType,
671-
(params, results): (StackHeight, StackHeight),
672-
) -> Result<()> {
662+
fn enter_block(&mut self, end_instr_offset: u32, ty: BlockType, (params, results): (StackHeight, StackHeight)) {
673663
self.store.stack.blocks.push(BlockFrame {
674664
instr_ptr: self.cf.instr_ptr() as u32,
675665
end_instr_offset,

crates/tinywasm/src/interpreter/stack/block_stack.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ use crate::engine::Config;
22
use alloc::vec::Vec;
33

44
use crate::interpreter::values::{StackHeight, StackLocation};
5-
use crate::{Result, Trap};
65

76
#[derive(Debug)]
87
pub(crate) struct BlockStack(Vec<BlockFrame>);
98

109
impl BlockStack {
1110
pub(crate) fn new(config: &Config) -> Self {
12-
Self(Vec::with_capacity(config.block_stack_size))
11+
Self(Vec::with_capacity(config.block_stack_initial_size))
1312
}
1413

1514
pub(crate) fn clear(&mut self) {
@@ -20,20 +19,15 @@ impl BlockStack {
2019
self.0.len()
2120
}
2221

23-
pub(crate) fn push(&mut self, block: BlockFrame) -> Result<()> {
24-
if self.0.len() >= self.0.capacity() {
25-
return Err(Trap::BlockStackOverflow.into());
26-
}
27-
22+
pub(crate) fn push(&mut self, block: BlockFrame) {
2823
self.0.push(block);
29-
Ok(())
3024
}
3125

3226
/// get the label at the given index, where 0 is the top of the stack
3327
pub(crate) fn get_relative_to(&self, index: u32, offset: u32) -> Option<&BlockFrame> {
34-
let len = (self.0.len() as u32) - offset;
28+
let len = (self.0.len() as u32).checked_sub(offset)?;
3529

36-
// the vast majority of wasm functions don't use break to return
30+
// the vast majority of wasm functions don't use break to return, but it is allowed in the spec
3731
if index >= len {
3832
return None;
3933
}

crates/tinywasm/src/interpreter/values.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{interpreter::value128::Value128, Result};
1+
use crate::{Result, interpreter::value128::Value128};
22

33
use super::stack::{Locals, ValueStack};
44
use tinywasm_types::{ExternRef, FuncRef, LocalAddr, ValType, WasmValue};

crates/tinywasm/tests/test-wasm-tail-call.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fn main() -> Result<()> {
88

99
let mut test_suite = TestSuite::new();
1010
test_suite.run_files(proposal(&Proposal::TailCall))?;
11+
test_suite.print_errors();
1112
test_suite.save_csv("./tests/generated/wasm-tail-call.csv", env!("CARGO_PKG_VERSION"))?;
1213
test_suite.report_status()
1314
}

0 commit comments

Comments
 (0)