Refactor to StackValue

This commit is contained in:
VegOwOtenks 2024-09-02 17:46:00 +02:00
parent 6c0fbd179a
commit 4ee673d5ff
4 changed files with 58 additions and 59 deletions

View file

@ -12,7 +12,7 @@ use crate::classstore::ClassStore;
use crate::constantpool::{ ConstantPoolInfo, ConstantClassInfo, ConstantUtf8Info, ConstantMethodRefInfo, ConstantNameAndTypeInfo};
use crate::heap_area::HeapArea;
use crate::stackframe;
use crate::stackframe::{ StackFrame, Value, OperandStack };
use crate::stackframe::{ StackFrame, StackValue, OperandStack };
#[derive(Debug)]
pub enum Error {
@ -63,7 +63,7 @@ impl JVM {
}
}
pub fn entrypoint(&mut self, class_name: &String, method_name: &String, arguments: &[Value]) -> Result<(), Error> {
pub fn entrypoint(&mut self, class_name: &String, method_name: &String, arguments: &[StackValue]) -> Result<(), Error> {
let entry_class = JavaClassFile {
minor_version: 0,
major_version: 0,
@ -184,7 +184,7 @@ impl JVM {
Ok(())
}
fn prepare_invoke_static(&mut self, class_index: usize, method_name: &String, arguments: &[Value]) -> Result<(), Error> {
fn prepare_invoke_static(&mut self, class_index: usize, method_name: &String, arguments: &[StackValue]) -> Result<(), Error> {
let class_file = self.class_store.class_file_from_idx(class_index).unwrap();
@ -283,7 +283,7 @@ impl JVM {
// sign extend into int
let i8_int = i8::from_be_bytes([byte]);
let frame_result = frame.operand_stack.push(Value::Int(i8_int as i32));
let frame_result = frame.operand_stack.push(StackValue::Int(i8_int as i32));
match frame_result {
Ok(_) => (),
Err(err) => return Err(Error::StackFrameError(err, format!("in '{}', in class '{}'", method.name, class.get_classname().unwrap()))),
@ -306,11 +306,11 @@ impl JVM {
Instruction::MultiplyInt() => {
let factor_1 = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
let factor_2 = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
wrap_stackframe_error(class, method, frame.operand_stack.push(Value::Int(factor_1 * factor_2)))?;
wrap_stackframe_error(class, method, frame.operand_stack.push(StackValue::Int(factor_1 * factor_2)))?;
}
Instruction::PushConstInt5() => {
let frame_result = frame.operand_stack.push(Value::Int(5));
let frame_result = frame.operand_stack.push(StackValue::Int(5));
match frame_result {
Ok(_) => (),
Err(err) => return Err(Error::StackFrameError(err, format!("in '{}', in class '{}'", method.name, class.get_classname().unwrap()))),
@ -328,7 +328,7 @@ impl JVM {
let int = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
return Ok(JVMCallbackOperation::ReturnFrame(Value::Int(int)));
return Ok(JVMCallbackOperation::ReturnFrame(StackValue::Int(int)));
}
Instruction::ReturnVoid() => {
let expected_type = AbstractTypeDescription {
@ -346,22 +346,22 @@ impl JVM {
Instruction::StoreLocalInt0() => {
let int = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(0))?;
wrap_stackframe_error(class, method, frame.store_local(0, Value::Int(int)))?;
wrap_stackframe_error(class, method, frame.store_local(0, StackValue::Int(int)))?;
},
Instruction::StoreLocalInt1() => {
let int = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(1))?;
wrap_stackframe_error(class, method, frame.store_local(0, Value::Int(int)))?;
wrap_stackframe_error(class, method, frame.store_local(0, StackValue::Int(int)))?;
},
Instruction::StoreLocalInt2() => {
let int = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(2))?;
wrap_stackframe_error(class, method, frame.store_local(0, Value::Int(int)))?;
wrap_stackframe_error(class, method, frame.store_local(0, StackValue::Int(int)))?;
},
Instruction::StoreLocalInt3() => {
let int = wrap_stackframe_error(class, method, frame.operand_stack.pop_int(3))?;
wrap_stackframe_error(class, method, frame.store_local(0, Value::Int(int)))?;
wrap_stackframe_error(class, method, frame.store_local(0, StackValue::Int(int)))?;
},
_ => {
@ -379,7 +379,7 @@ impl JVM {
enum JVMCallbackOperation {
PopFrame(),
ReturnFrame(Value),
ReturnFrame(StackValue),
PushFrame(StackFrame),
LoadClass(String),
InitClass(String),
@ -394,19 +394,19 @@ fn load_local_int(class: &JavaClassFile, method: &MethodInfo, frame: &mut StackF
Err(err) => return Err(Error::StackFrameError(err, format!("in '{}', in class '{}'", method.name, class.get_classname().unwrap()))),
};
let frame_result = frame.operand_stack.push(Value::Int(local_int));
let frame_result = frame.operand_stack.push(StackValue::Int(local_int));
match frame_result {
Ok(_) => Ok(()),
Err(err) => return Err(Error::StackFrameError(err, format!("in '{}', in class '{}'", method.name, class.get_classname().unwrap()))),
}
}
fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut VecDeque<Value>, argument_types: &Box<[AbstractTypeDescription]>, stack: &mut OperandStack) -> Result<(), Error> {
fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut VecDeque<StackValue>, argument_types: &Box<[AbstractTypeDescription]>, stack: &mut OperandStack) -> Result<(), Error> {
for argument_type in argument_types {
if argument_type.array_level != 0 {
// TODO: Type checking
arguments.push_front(
Value::Reference(wrap_stackframe_error(class, method, stack.pop_reference(0))?),
StackValue::Reference(wrap_stackframe_error(class, method, stack.pop_reference(0))?),
)
} else {
match argument_type.kind {
@ -414,7 +414,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
// TODO: Add better description
AbstractTypeKind::Byte() => {
arguments.push_front(
Value::Byte(
StackValue::Byte(
wrap_stackframe_error(
class,
method,
@ -425,7 +425,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
},
AbstractTypeKind::Char() => {
arguments.push_front(
Value::Char(
StackValue::Char(
wrap_stackframe_error(
class,
method,
@ -436,7 +436,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
},
AbstractTypeKind::Double() => {
arguments.push_front(
Value::Double1(
StackValue::Double1(
wrap_stackframe_error(
class,
method,
@ -445,7 +445,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
)
);
arguments.push_front(
Value::Double0(
StackValue::Double0(
wrap_stackframe_error(
class,
method,
@ -456,7 +456,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
},
AbstractTypeKind::Float() => {
arguments.push_front(
Value::Float(
StackValue::Float(
wrap_stackframe_error(
class,
method,
@ -467,7 +467,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
},
AbstractTypeKind::Int() => {
arguments.push_front(
Value::Int(
StackValue::Int(
wrap_stackframe_error(
class,
method,
@ -478,7 +478,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
},
AbstractTypeKind::Long() => {
arguments.push_front(
Value::Long1(
StackValue::Long1(
wrap_stackframe_error(
class,
method,
@ -487,7 +487,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
)
);
arguments.push_front(
Value::Long0(
StackValue::Long0(
wrap_stackframe_error(
class,
method,
@ -499,7 +499,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
AbstractTypeKind::Classname(ref name) => {
// TODO: Type checking
arguments.push_front(
Value::Reference(
StackValue::Reference(
wrap_stackframe_error(
class,
method,
@ -510,7 +510,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
},
AbstractTypeKind::Short() => {
arguments.push_front(
Value::Short(
StackValue::Short(
wrap_stackframe_error(
class,
method,
@ -521,7 +521,7 @@ fn fill_arguments(class: &JavaClassFile, method: &MethodInfo, arguments: &mut Ve
},
AbstractTypeKind::Boolean() => {
arguments.push_front(
Value::Boolean(
StackValue::Boolean(
wrap_stackframe_error(
class,
method,