Lines
100 %
Functions
Branches
//! A runtime-clause `COND` lowers to one flat block, so wasm control nesting
//! does not grow with the clause count.
use wasmparser::{FunctionBody, Operator, Parser, Payload};
use super::common::{compile_and_validate, wrap_with_runtime_i32};
fn body_control_depth(body: &FunctionBody) -> usize {
let reader = body.get_operators_reader().expect("operators reader");
let mut depth = 0usize;
let mut max = 0usize;
for op in reader {
match op.expect("operator") {
Operator::Block { .. }
| Operator::Loop { .. }
| Operator::If { .. }
| Operator::TryTable { .. } => {
depth += 1;
max = max.max(depth);
}
Operator::End => depth = depth.saturating_sub(1),
_ => {}
max
fn max_control_depth(wasm: &[u8]) -> usize {
Parser::new(0)
.parse_all(wasm)
.filter_map(Result::ok)
.filter_map(|payload| match payload {
Payload::CodeSectionEntry(body) => Some(body_control_depth(&body)),
_ => None,
})
.max()
.unwrap_or(0)
fn stack_position_cond(clauses: u32) -> Vec<u8> {
let body: String = (0..clauses)
.map(|i| format!("((= IDX {i}) {i}) "))
.collect();
compile_and_validate(&wrap_with_runtime_i32(&format!(
"(+ 1 (cond {body}(t -1)))"
)))
#[test]
fn runtime_cond_nesting_does_not_grow_with_the_clause_count() {
let small = max_control_depth(&stack_position_cond(4));
let large = max_control_depth(&stack_position_cond(256));
assert!(small > 0, "a runtime COND must emit control structure");
assert_eq!(small, large);