#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use crate::Program;
use crate::bytecode::{Instruction, InstructionStream};
use super::error::VerifierError;
use super::phase::Phase;
use super::scan::stream_err;
pub struct LoopNestingPhase;
impl Phase for LoopNestingPhase {
type Error = VerifierError;
fn run(&self, program: &Program) -> Result<(), VerifierError> {
let mut loop_offsets: Vec<usize> = Vec::new();
let mut stream = InstructionStream::new(program.code());
while let Some(item) = stream.next_instruction() {
let (pos, _label, instr) = item.map_err(|e| stream_err(&e))?;
match instr {
Instruction::Range {} | Instruction::Iter { .. } => {
loop_offsets.push(pos);
}
Instruction::Next {} => {
let _ = loop_offsets
.pop()
.ok_or(VerifierError::NoActiveLoop { offset: pos })?;
}
Instruction::Lidx { .. } | Instruction::LVal { .. } if loop_offsets.is_empty() => {
return Err(VerifierError::NoActiveLoop { offset: pos });
}
_ => {}
}
}
match loop_offsets.first().copied() {
Some(outermost) => Err(VerifierError::UnmatchedLoop {
offset: outermost,
depth: loop_offsets.len(),
}),
None => Ok(()),
}
}
}