use crate::attribute::{CodeAttribute, MethodAttribute};
use crate::bytecode::Instruction;
use crate::constant_pool::ConstantPool;
use common::error::ClassFormatErr;
use common::utils::indent_write::Indented;
use std::fmt::Write as _;
impl MethodAttribute {
pub(crate) fn fmt_rns(
&self,
ind: &mut Indented,
cp: &ConstantPool,
) -> Result<(), ClassFormatErr> {
ind.with_indent(|ind| match self {
MethodAttribute::Code(code) => code.fmt_rns(ind, cp),
other => unimplemented!("{:?} is not supported for writing right now", other),
})
}
}
impl CodeAttribute {
pub(super) fn fmt_rns(
&self,
ind: &mut Indented,
cp: &ConstantPool,
) -> Result<(), ClassFormatErr> {
writeln!(
ind,
".code stack {} locals {}",
self.max_stack, self.max_locals
)?;
ind.with_indent(|ind| {
let mut pc = 0;
let code_len = self.code.len();
while pc < code_len {
let inst = Instruction::new_at(&self.code, pc)?;
inst.fmt_rns(ind, cp)?;
pc += inst.byte_size() as usize;
}
Ok(())
})?;
writeln!(ind, ".end code")?;
Ok(())
}
}