all cp indices u16
This commit is contained in:
parent
8dd60c0866
commit
9688b6a0c5
5 changed files with 35 additions and 35 deletions
26
src/class.rs
26
src/class.rs
|
|
@ -11,7 +11,7 @@ use crate::io::read_u16;
|
|||
pub struct Class {
|
||||
pub minor_version: u16,
|
||||
pub major_version: u16,
|
||||
pub constant_pool: Rc<HashMap<usize, CpEntry>>,
|
||||
pub constant_pool: Rc<HashMap<u16, CpEntry>>,
|
||||
pub access_flags: u16,
|
||||
pub this_class: u16,
|
||||
pub super_class: u16,
|
||||
|
|
@ -35,10 +35,10 @@ impl Class {
|
|||
}
|
||||
|
||||
pub struct Method {
|
||||
pub(crate) constant_pool: Rc<HashMap<usize, CpEntry>>,
|
||||
pub(crate) constant_pool: Rc<HashMap<u16, CpEntry>>,
|
||||
access_flags: u16,
|
||||
name_index: usize,
|
||||
descriptor_index: usize,
|
||||
name_index: u16,
|
||||
descriptor_index: u16,
|
||||
pub(crate) attributes: HashMap<String, AttributeType>,
|
||||
}
|
||||
|
||||
|
|
@ -50,10 +50,10 @@ impl fmt::Debug for Method {
|
|||
}
|
||||
|
||||
impl Method {
|
||||
pub fn new(constant_pool: Rc<HashMap<usize, CpEntry>>,
|
||||
pub fn new(constant_pool: Rc<HashMap<u16, CpEntry>>,
|
||||
access_flags: u16,
|
||||
name_index: usize,
|
||||
descriptor_index: usize,
|
||||
name_index: u16,
|
||||
descriptor_index: u16,
|
||||
attributes: HashMap<String, AttributeType>, ) -> Self {
|
||||
Method { constant_pool, access_flags, name_index, descriptor_index, attributes }
|
||||
}
|
||||
|
|
@ -73,10 +73,10 @@ impl Method {
|
|||
}
|
||||
|
||||
pub struct Field {
|
||||
constant_pool: Rc<HashMap<usize, CpEntry>>,
|
||||
constant_pool: Rc<HashMap<u16, CpEntry>>,
|
||||
access_flags: u16,
|
||||
name_index: usize,
|
||||
descriptor_index: usize,
|
||||
name_index: u16,
|
||||
descriptor_index: u16,
|
||||
attributes: HashMap<String, AttributeType>,
|
||||
}
|
||||
|
||||
|
|
@ -88,10 +88,10 @@ impl fmt::Debug for Field {
|
|||
}
|
||||
|
||||
impl Field {
|
||||
pub fn new(constant_pool: Rc<HashMap<usize, CpEntry>>,
|
||||
pub fn new(constant_pool: Rc<HashMap<u16, CpEntry>>,
|
||||
access_flags: u16,
|
||||
name_index: usize,
|
||||
descriptor_index: usize,
|
||||
name_index: u16,
|
||||
descriptor_index: u16,
|
||||
attributes: HashMap<String, AttributeType>, ) -> Self {
|
||||
Field { constant_pool, access_flags, name_index, descriptor_index, attributes: attributes }
|
||||
}
|
||||
|
|
|
|||
22
src/lib.rs
22
src/lib.rs
|
|
@ -15,9 +15,9 @@ pub fn get_class(bytecode: Vec<u8>) -> Option<Class> {
|
|||
let constant_pool_count = read_u16(&bytecode, 8);
|
||||
// println!("cp count: {}", constant_pool_count);
|
||||
let mut index = 10;
|
||||
let mut constant_pool: HashMap<usize, CpEntry> = HashMap::with_capacity(constant_pool_count as usize);
|
||||
let mut constant_pool: HashMap<u16, CpEntry> = HashMap::with_capacity(constant_pool_count as usize);
|
||||
let mut cp_index = 1;
|
||||
while cp_index < constant_pool_count as usize {
|
||||
while cp_index < constant_pool_count {
|
||||
// println!("cp#{}", cp_index);
|
||||
constant_pool.insert(cp_index, read_constant_pool_entry(&mut cp_index, &mut index, &bytecode));
|
||||
cp_index += 1;
|
||||
|
|
@ -85,7 +85,7 @@ fn check_magic(bytecode: &[u8]) {
|
|||
}
|
||||
}
|
||||
|
||||
fn read_constant_pool_entry(cp_index: &mut usize, index: &mut usize, bytecode: &[u8]) -> CpEntry {
|
||||
fn read_constant_pool_entry(cp_index: &mut u16, index: &mut usize, bytecode: &[u8]) -> CpEntry {
|
||||
let tag = bytecode[*index];
|
||||
// println!("#{}: {}", cp_index, tag);
|
||||
match tag {
|
||||
|
|
@ -164,10 +164,10 @@ fn read_constant_pool_entry(cp_index: &mut usize, index: &mut usize, bytecode: &
|
|||
}
|
||||
}
|
||||
|
||||
fn read_field(constant_pool: Rc<HashMap<usize, CpEntry>>, index: &mut usize, bytecode: &[u8]) -> Field {
|
||||
fn read_field(constant_pool: Rc<HashMap<u16, CpEntry>>, index: &mut usize, bytecode: &[u8]) -> Field {
|
||||
let access_flags = read_u16(bytecode, *index);
|
||||
let name_index = read_u16(bytecode, *index + 2) as usize;
|
||||
let descriptor_index = read_u16(bytecode, *index + 4) as usize;
|
||||
let name_index = read_u16(bytecode, *index + 2);
|
||||
let descriptor_index = read_u16(bytecode, *index + 4);
|
||||
let attributes_count = read_u16(bytecode, *index + 6);
|
||||
*index += 8;
|
||||
let mut attributes = HashMap::new();
|
||||
|
|
@ -187,10 +187,10 @@ fn read_field(constant_pool: Rc<HashMap<usize, CpEntry>>, index: &mut usize, byt
|
|||
)
|
||||
}
|
||||
|
||||
fn read_method(constant_pool: Rc<HashMap<usize, CpEntry>>, index: &mut usize, bytecode: &[u8]) -> Method {
|
||||
fn read_method(constant_pool: Rc<HashMap<u16, CpEntry>>, index: &mut usize, bytecode: &[u8]) -> Method {
|
||||
let access_flags = read_u16(bytecode, *index);
|
||||
let name_index = read_u16(bytecode, *index + 2) as usize;
|
||||
let descriptor_index = read_u16(bytecode, *index + 4) as usize;
|
||||
let name_index = read_u16(bytecode, *index + 2);
|
||||
let descriptor_index = read_u16(bytecode, *index + 4);
|
||||
let attributes_count = read_u16(bytecode, *index + 6);
|
||||
*index += 8;
|
||||
|
||||
|
|
@ -210,8 +210,8 @@ fn read_method(constant_pool: Rc<HashMap<usize, CpEntry>>, index: &mut usize, by
|
|||
)
|
||||
}
|
||||
|
||||
fn read_attribute(constant_pool: Rc<HashMap<usize, CpEntry>>, bytecode: &[u8], index: &mut usize) -> Option<(String, AttributeType)> {
|
||||
let attribute_name_index = read_u16(bytecode, *index) as usize;
|
||||
fn read_attribute(constant_pool: Rc<HashMap<u16, CpEntry>>, bytecode: &[u8], index: &mut usize) -> Option<(String, AttributeType)> {
|
||||
let attribute_name_index = read_u16(bytecode, *index);
|
||||
*index += 2;
|
||||
let attribute_length = read_u32(bytecode, *index) as usize;
|
||||
*index += 4;
|
||||
|
|
|
|||
10
src/main.rs
10
src/main.rs
|
|
@ -1,9 +1,9 @@
|
|||
fn main() {
|
||||
if let Some(class) = classfile_reader::get_class(classfile_reader::io::read_class_file("./Dummy.class")){
|
||||
println!("{:?}", class);
|
||||
let ret = class.execute("public static get()D");
|
||||
println!("{:?}", ret);
|
||||
}
|
||||
// if let Some(class) = classfile_reader::get_class(classfile_reader::io::read_class_file("./Dummy.class")){
|
||||
// println!("{:?}", class);
|
||||
// let ret = class.execute("public static get()D");
|
||||
// println!("{:?}", ret);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
10
src/vm.rs
10
src/vm.rs
|
|
@ -39,7 +39,7 @@ impl Vm {
|
|||
}
|
||||
|
||||
pub fn new_instance(&self, class: &Class) {
|
||||
for f in class.fields {
|
||||
for f in &class.fields {
|
||||
println!("{}", f.type_of());
|
||||
}
|
||||
// Object::new(Rc::new(class))
|
||||
|
|
@ -60,7 +60,7 @@ impl Vm {
|
|||
pc += 1;
|
||||
}
|
||||
&opcodes::LDC => {
|
||||
let cp_index = read_u8(&code.opcodes, pc) as usize;
|
||||
let cp_index = read_u8(&code.opcodes, pc) as u16;
|
||||
match method.constant_pool.get(&cp_index).unwrap() {
|
||||
CpEntry::Integer(i) => {
|
||||
stack.push(Value::I32(*i));
|
||||
|
|
@ -73,7 +73,7 @@ impl Vm {
|
|||
pc += 1;
|
||||
}
|
||||
&opcodes::LDC_W => {
|
||||
let cp_index = read_u16(&code.opcodes, pc) as usize;
|
||||
let cp_index = read_u16(&code.opcodes, pc);
|
||||
match method.constant_pool.get(&cp_index).unwrap() {
|
||||
CpEntry::Integer(i) => {
|
||||
stack.push(Value::I32(*i));
|
||||
|
|
@ -86,7 +86,7 @@ impl Vm {
|
|||
pc += 2;
|
||||
}
|
||||
&opcodes::LDC2_W => {
|
||||
let cp_index = read_u16(&code.opcodes, pc) as usize;
|
||||
let cp_index = read_u16(&code.opcodes, pc);
|
||||
match method.constant_pool.get(&cp_index).unwrap() {
|
||||
CpEntry::Double(d) => {
|
||||
stack.push(Value::F64(*d));
|
||||
|
|
@ -110,7 +110,7 @@ impl Vm {
|
|||
return stack.pop();
|
||||
}
|
||||
&opcodes::NEW => {
|
||||
let cp_index = read_u16(&code.opcodes, pc) as usize;
|
||||
let cp_index = read_u16(&code.opcodes, pc);
|
||||
if let CpEntry::ClassRef(class_name_index) = method.constant_pool.get(&cp_index).unwrap() {
|
||||
if let CpEntry::Utf8(class) = method.constant_pool.get(class_name_index).unwrap(){
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ mod test {
|
|||
#[test]
|
||||
fn get_constant_foat() {
|
||||
let class = get_class(io::read_class_file("tests/Float.class")).unwrap();
|
||||
Vm::new().new_instance(class);
|
||||
Vm::new().new_instance(&class);
|
||||
// assert_eq!((55, 0), class.get_version());
|
||||
// if let Value::F32(v) = Vm::new().execute(class.methods.get("public static getF()F").unwrap()).unwrap() {
|
||||
// assert_eq!(v, 42.0);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue