mirror of
https://github.com/pezkuwichain/wasm-instrument.git
synced 2026-06-13 21:01:07 +00:00
stack_height: if instruction should pop one value from the stack (#147)
* stack_height: 'if' instruction should pop one value from the stack * Fix indentation
This commit is contained in:
committed by
GitHub
parent
d9432bafa9
commit
b2272f39bc
@@ -201,6 +201,9 @@ pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, E
|
|||||||
Block(ty) | Loop(ty) | If(ty) => {
|
Block(ty) | Loop(ty) | If(ty) => {
|
||||||
let end_arity = if *ty == BlockType::NoResult { 0 } else { 1 };
|
let end_arity = if *ty == BlockType::NoResult { 0 } else { 1 };
|
||||||
let branch_arity = if let Loop(_) = *opcode { 0 } else { end_arity };
|
let branch_arity = if let Loop(_) = *opcode { 0 } else { end_arity };
|
||||||
|
if let If(_) = *opcode {
|
||||||
|
stack.pop_values(1)?;
|
||||||
|
}
|
||||||
let height = stack.height();
|
let height = stack.height();
|
||||||
stack.push_frame(Frame {
|
stack.push_frame(Frame {
|
||||||
is_polymorphic: false,
|
is_polymorphic: false,
|
||||||
@@ -546,4 +549,52 @@ mod tests {
|
|||||||
let height = compute(0, &module).unwrap();
|
let height = compute(0, &module).unwrap();
|
||||||
assert_eq!(height, 1);
|
assert_eq!(height, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn breaks() {
|
||||||
|
let module = parse_wat(
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(func $main
|
||||||
|
block (result i32)
|
||||||
|
block (result i32)
|
||||||
|
i32.const 99
|
||||||
|
br 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
drop
|
||||||
|
)
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
let height = compute(0, &module).unwrap();
|
||||||
|
assert_eq!(height, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn if_else_works() {
|
||||||
|
let module = parse_wat(
|
||||||
|
r#"
|
||||||
|
(module
|
||||||
|
(func $main
|
||||||
|
i32.const 7
|
||||||
|
i32.const 1
|
||||||
|
if (result i32)
|
||||||
|
i32.const 42
|
||||||
|
else
|
||||||
|
i32.const 99
|
||||||
|
end
|
||||||
|
i32.const 97
|
||||||
|
drop
|
||||||
|
drop
|
||||||
|
drop
|
||||||
|
)
|
||||||
|
)
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
let height = compute(0, &module).unwrap();
|
||||||
|
assert_eq!(height, 3);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user