From 6b03cab14d28c0a64bff92cbedee219e7741f84b Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Thu, 7 Nov 2024 14:41:00 +0100 Subject: [PATCH] ThrowException .-. --- src/bytecode.rs | 2 ++ src/classfile.rs | 8 ++++---- src/jvm.rs | 7 +++++++ src/native_methods.rs | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/bytecode.rs b/src/bytecode.rs index 42336a9..c188b84 100644 --- a/src/bytecode.rs +++ b/src/bytecode.rs @@ -251,6 +251,7 @@ impl Bytecode { 0xBC => (Instruction::NewPrimitiveArray(self.bytes[offset+1]), 2), 0xBD => (Instruction::NewArray((self.bytes[offset+1] as u16) << 8 | self.bytes[offset+2] as u16), 3), 0xBE => (Instruction::ArrayLength(), 1), + 0xBF => (Instruction::ThrowException(), 1), 0xC0 => (Instruction::CheckCast((self.bytes[offset+1] as u16) << 8 | self.bytes[offset+2] as u16), 3), 0xC1 => (Instruction::InstanceOf((self.bytes[offset+1] as u16) << 8 | self.bytes[offset+2] as u16), 3), @@ -446,6 +447,7 @@ pub enum Instruction { NewPrimitiveArray(u8) = 0xBC, // make a primitive array NewArray(u16) = 0xBD, // Create a new array from a constant-pool component class reference ArrayLength() = 0xBE, // Get length from array reference + ThrowException() = 0xBF, // throw the exception CheckCast(u16) = 0xC0, // throw exception on fail InstanceOf(u16) = 0xC1, // push integer result for success diff --git a/src/classfile.rs b/src/classfile.rs index c2071c1..8e0cf65 100644 --- a/src/classfile.rs +++ b/src/classfile.rs @@ -194,7 +194,7 @@ impl JavaClassFile { pub fn find_method_index(&self, name: &String) -> Option { - for (index, method_info) in self.methods.iter().enumerate() { + for (index, method_info) in (&self.methods).into_iter().enumerate() { if method_info.name == *name { return Some(index); } @@ -377,7 +377,7 @@ impl JavaClassFile { } pub fn sourcefile(&self) -> Result, Error> { - match self.attributes.into_iter() + match (&self.attributes).into_iter() .filter_map(|attribute| match &attribute.data { AttributeData::SourceFile(data) => Some(data), _ => None, @@ -1103,7 +1103,7 @@ impl MethodInfo { pub fn get_bytecode_linenumber(&self, bytecode_index: u16) -> Option { let linenumbertable = match self.attributes - .into_iter() + .iter() .filter_map(|a| match &a.data { AttributeData::LineNumberTable(data) => Some(data), _ => None, @@ -1112,7 +1112,7 @@ impl MethodInfo { Some(a) => a, None => return None, }; - linenumbertable.entries.into_iter() + linenumbertable.entries.iter() .take_while(|entry| entry.start_pc < bytecode_index) .map(|entry| entry.line_number) .last() diff --git a/src/jvm.rs b/src/jvm.rs index 5ee469a..3f24b01 100644 --- a/src/jvm.rs +++ b/src/jvm.rs @@ -2278,6 +2278,13 @@ impl JVM { } } + Instruction::ThrowException() => { + let exception = wrap_stackframe_error(class, method, frame.operand_stack.pop_reference(0))?; + if exception == ObjectReference::NULL { + + } + } + Instruction::XorInt() => { let value_1 = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?; let value_2 = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?; diff --git a/src/native_methods.rs b/src/native_methods.rs index 7413bf8..350fb9d 100644 --- a/src/native_methods.rs +++ b/src/native_methods.rs @@ -422,7 +422,7 @@ impl JavaLangThrowable { let sourcefile = class_file.sourcefile()?; let sourcefile_string = match sourcefile { Some(string) => jvm.heap_area.make_handmade_string(string, &jvm.class_store), - None => jvm.heap_area.static_area.get(&String::from("StackTraceElement"), &String::from("UNKNOWN_SOURCE"), AbstractTypeDescription::class_type("java/lang/String")).unwrap().expect_reference(), + None => jvm.heap_area.static_area.get(&String::from("java/lang/StackTraceElement"), &String::from("UNKNOWN_SOURCE"), AbstractTypeDescription::class_type("java/lang/String")).unwrap().expect_reference(), }; jvm.heap_area.object_area.set_object_field(