java_asm/impls/jvms/r/
util.rs1use crate::constants::Constants;
2use crate::err::{AsmErr, AsmResult};
3use crate::impls::{mutf8_to_string};
4use crate::jvms::element::{Const, CPInfo};
5use crate::StrRef;
6
7pub fn read_utf8_from_cp(index: usize, cp: &Vec<CPInfo>) -> AsmResult<StrRef> {
8 let cp_info = &cp[index];
9 let CPInfo { tag, info } = cp_info;
10 let tag = *tag;
11 if tag != Constants::CONSTANT_Utf8 {
12 return AsmErr::IllegalFormat(
13 format!("cannot read utf8 from constant pool, current cp tag: {}, index: {}", tag, index)
14 ).e();
15 };
16 if let Const::Utf8 { bytes, .. } = info {
17 return mutf8_to_string(bytes);
18 };
19 AsmErr::IllegalFormat(
20 format!("cannot read utf8 from constant pool, index: {}", index)
21 ).e()
22}