Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 85deebc

Browse files
fix: breaking in loops
Signed-off-by: Henry Gressmann <[email protected]>
1 parent f59963d commit 85deebc

File tree

8 files changed

+40
-44
lines changed

8 files changed

+40
-44
lines changed

crates/tinywasm/src/runtime/executor/mod.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -198,25 +198,8 @@ fn exec_one(
198198
}
199199

200200
If(args, else_offset, end_offset) => {
201-
if stack.values.pop_t::<i32>()? == 0 {
202-
if let Some(else_offset) = else_offset {
203-
log::debug!("entering else at {}", cf.instr_ptr + *else_offset);
204-
cf.enter_label(
205-
LabelFrame {
206-
instr_ptr: cf.instr_ptr + *else_offset,
207-
end_instr_ptr: cf.instr_ptr + *end_offset,
208-
stack_ptr: stack.values.len(), // - params,
209-
args: crate::LabelArgs::new(*args, module)?,
210-
ty: BlockType::Else,
211-
},
212-
&mut stack.values,
213-
);
214-
cf.instr_ptr += *else_offset;
215-
} else {
216-
log::info!("skipping if");
217-
cf.instr_ptr += *end_offset
218-
}
219-
} else {
201+
// truthy value is on the top of the stack, so enter the then block
202+
if stack.values.pop_t::<i32>()? != 0 {
220203
log::trace!("entering then");
221204
cf.enter_label(
222205
LabelFrame {
@@ -227,7 +210,26 @@ fn exec_one(
227210
ty: BlockType::If,
228211
},
229212
&mut stack.values,
230-
)
213+
);
214+
return Ok(ExecResult::Ok);
215+
}
216+
217+
// falsy value is on the top of the stack
218+
if let Some(else_offset) = else_offset {
219+
log::debug!("entering else at {}", cf.instr_ptr + *else_offset);
220+
cf.enter_label(
221+
LabelFrame {
222+
instr_ptr: cf.instr_ptr + *else_offset,
223+
end_instr_ptr: cf.instr_ptr + *end_offset,
224+
stack_ptr: stack.values.len(), // - params,
225+
args: crate::LabelArgs::new(*args, module)?,
226+
ty: BlockType::Else,
227+
},
228+
&mut stack.values,
229+
);
230+
cf.instr_ptr += *else_offset;
231+
} else {
232+
cf.instr_ptr += *end_offset;
231233
}
232234
}
233235

@@ -285,7 +287,7 @@ fn exec_one(
285287

286288
Br(v) => break_to!(cf, stack, v),
287289
BrIf(v) => {
288-
if stack.values.pop_t::<i32>()? > 0 {
290+
if stack.values.pop_t::<i32>()? != 0 {
289291
break_to!(cf, stack, v);
290292
}
291293
}

crates/tinywasm/src/runtime/executor/traits.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ macro_rules! impl_wasm_float_ops {
1616
impl WasmFloatOps for $t {
1717
// https://webassembly.github.io/spec/core/exec/numerics.html#op-fnearest
1818
fn wasm_nearest(self) -> Self {
19-
log::info!("wasm_nearest: {}", self);
2019
match self {
2120
x if x.is_nan() => x,
2221
x if x.is_infinite() || x == 0.0 => x,

crates/tinywasm/src/runtime/stack/call_stack.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,28 @@ impl CallFrame {
7575
/// Returns `None` if there is no block at the given index (e.g. if we need to return, this is handled by the caller)
7676
#[inline]
7777
pub(crate) fn break_to(&mut self, break_to_relative: u32, value_stack: &mut super::ValueStack) -> Option<()> {
78+
log::debug!("break_to_relative: {}", break_to_relative);
7879
let break_to = self.labels.get_relative_to_top(break_to_relative as usize)?;
79-
value_stack.break_to(break_to.stack_ptr, break_to.args.results);
8080

8181
// instr_ptr points to the label instruction, but the next step
8282
// will increment it by 1 since we're changing the "current" instr_ptr
8383
match break_to.ty {
8484
BlockType::Loop => {
8585
// this is a loop, so we want to jump back to the start of the loop
86+
// We also want to push the params to the stack
87+
value_stack.break_to(break_to.stack_ptr, break_to.args.params);
88+
8689
self.instr_ptr = break_to.instr_ptr;
8790

8891
// we also want to trim the label stack to the loop (but not including the loop)
8992
self.labels.truncate(self.labels.len() - break_to_relative as usize);
9093
}
9194
BlockType::Block | BlockType::If | BlockType::Else => {
92-
// this is a block, so we want to jump to the next instruction after the block ends (the inst_ptr will be incremented by 1 before the next instruction is executed)
95+
// this is a block, so we want to jump to the next instruction after the block ends
96+
// We also want to push the block's results to the stack
97+
value_stack.break_to(break_to.stack_ptr, break_to.args.results);
98+
99+
// (the inst_ptr will be incremented by 1 before the next instruction is executed)
93100
self.instr_ptr = break_to.end_instr_ptr;
94101

95102
// we also want to trim the label stack, including the block

crates/tinywasm/src/runtime/stack/value_stack.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ impl ValueStack {
9292
}
9393

9494
pub(crate) fn break_to(&mut self, new_stack_size: usize, result_count: usize) {
95+
assert!(self.top >= result_count);
9596
self.stack.copy_within((self.top - result_count)..self.top, new_stack_size);
9697
self.top = new_stack_size + result_count;
9798
self.stack.truncate(self.top);

crates/tinywasm/tests/generated/mvp.csv

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

crates/tinywasm/tests/generated/progress-mvp.svg

Lines changed: 2 additions & 2 deletions
Loading

crates/tinywasm/tests/test-mvp.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@ fn test_mvp() -> Result<()> {
1717

1818
TestSuite::set_log_level(log::LevelFilter::Off);
1919
test_suite.run_spec_group(wasm_testsuite::MVP_TESTS)?;
20-
2120
test_suite.save_csv("./tests/generated/mvp.csv", env!("CARGO_PKG_VERSION"))?;
2221

2322
if test_suite.failed() {
2423
println!();
25-
Err(eyre!(format!(
26-
"{}:\n{:#?}",
27-
"failed one or more tests".red().bold(),
28-
test_suite,
29-
)))
24+
Err(eyre!(format!("{}:\n{:#?}", "failed one or more tests".red().bold(), test_suite,)))
3025
} else {
3126
println!("\n\npassed all tests:\n{:#?}", test_suite);
3227
Ok(())

crates/tinywasm/tests/test-wast.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ fn main() -> Result<()> {
2020
let cwd = std::env::current_dir()?;
2121

2222
// if current dir is crates/tinywasm, then we want to go up 2 levels
23-
let mut wast_file = if cwd.ends_with("crates/tinywasm") {
24-
PathBuf::from("../../")
25-
} else {
26-
PathBuf::from("./")
27-
};
23+
let mut wast_file = if cwd.ends_with("crates/tinywasm") { PathBuf::from("../../") } else { PathBuf::from("./") };
2824

2925
wast_file.push(&args[2]);
3026
let wast_file = cwd.join(wast_file);
@@ -34,7 +30,7 @@ fn main() -> Result<()> {
3430
}
3531

3632
fn test_wast(wast_file: &str) -> Result<()> {
37-
TestSuite::set_log_level(log::LevelFilter::Info);
33+
TestSuite::set_log_level(log::LevelFilter::Debug);
3834

3935
let args = std::env::args().collect::<Vec<_>>();
4036
println!("args: {:?}", args);
@@ -48,11 +44,7 @@ fn test_wast(wast_file: &str) -> Result<()> {
4844
println!();
4945
test_suite.print_errors();
5046
println!();
51-
Err(eyre!(format!(
52-
"{}:\n{:#?}",
53-
"failed one or more tests".red().bold(),
54-
test_suite,
55-
)))
47+
Err(eyre!(format!("{}:\n{:#?}", "failed one or more tests".red().bold(), test_suite,)))
5648
} else {
5749
println!("\n\npassed all tests:\n{:#?}", test_suite);
5850
Ok(())

0 commit comments

Comments
 (0)