Skip to content

Commit

Permalink
fix: hang up when in loop
Browse files Browse the repository at this point in the history
  • Loading branch information
skanehira committed Jul 22, 2023
1 parent 5b29a49 commit a6347d0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/execution/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ pub fn br(labels: &mut Vec<Label>, stack: &mut Vec<Value>, level: &u32) -> Resul
.with_context(|| Error::NotFoundLabel(label_index))?;

let pc = if kind == LabelKind::Loop {
// since it jumps to the beginning of the loop,
// NOTE: we still need loop label to jump to the beginning of the loop.
labels.drain(label_index + 1..);
// NOTE: since it jumps to the beginning of the loop,
// the stack is unwound without considering the return value.
stack_unwind(stack, sp, 0)?;
start.with_context(|| Error::NotFoundStartPc)?
Expand Down
7 changes: 6 additions & 1 deletion src/execution/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,19 @@ impl Runtime {
Instruction::If(block) => {
let cond: Value = stack.pop1()?;

// cal pc when the if block is end
// calc pc when the end of block
let next_pc = get_end_address(insts, frame.pc)?;

if !cond.is_true() {
// if the condition is false, skip the if block
frame.pc = get_else_or_end_address(insts, frame.pc)? as isize;
}

// NOTE: if block has no any instruction, just continue
if next_pc == frame.pc as usize {
continue;
}

let label = Label {
start: None,
kind: LabelKind::If,
Expand Down

0 comments on commit a6347d0

Please sign in to comment.