straighten out execute signatures

This commit is contained in:
Shautvast 2023-10-22 15:22:18 +02:00
parent 922006da1a
commit ff5c41fc8d
3 changed files with 19 additions and 21 deletions

View file

@ -61,8 +61,9 @@ pub fn get_class(
Class::initialize_fields(clone3);
let clinit = clone2.borrow().methods.contains_key("<clinit>()V");
let name = &clone2.borrow().name.to_owned();
if clinit {
vm.execute_class(clone2, "<clinit>()V", vec![]).unwrap();
vm.execute(name, "<clinit>()V", vec![]).unwrap();
}
}
Ok(clone)

View file

@ -9,9 +9,7 @@ use log::{info};
use Value::*;
use crate::class::Value::{Null, Void};
use crate::class::{
get_class, unsafe_ref, unsafe_val, AttributeType, Class, Modifier, UnsafeValue, Value,
};
use crate::class::{get_class, unsafe_ref, unsafe_val, AttributeType, Class, Modifier, UnsafeValue, Value, Method};
use crate::classloader::CpEntry;
use crate::heap::{Heap, Object, ObjectRef};
use crate::io::*;
@ -97,19 +95,20 @@ impl Vm {
args: Vec<UnsafeValue>,
) -> Result<UnsafeValue, Error> {
let class = get_class(self, class_name)?;
self.execute_class(class, method_name, args)
let method = class.clone().borrow().get_method(method_name)?.clone();
//TODO implement dynamic dispatch -> get method from instance
self.execute_class(class, method, args)
}
pub fn execute_class(
&mut self,
class: Arc<RefCell<Class>>,
method_name: &str,
method: Rc<Method>,
args: Vec<UnsafeValue>,
) -> Result<UnsafeValue, Error> {
let this_class = class;
println!("execute {}.{}", this_class.borrow().name, method_name);
println!("execute {}.{}", this_class.borrow().name, method.name());
let method = this_class.clone().borrow().get_method(method_name)?.clone();
//TODO implement dynamic dispatch -> get method from instance
let mut local_params: Vec<Option<UnsafeValue>> =
@ -152,25 +151,25 @@ impl Vm {
self.current_frame().push(I32(5));
}
LCONST_0 => {
self.current_frame().push(Value::I64(0));
self.current_frame().push(I64(0));
}
LCONST_1 => {
self.current_frame().push(Value::I64(1));
self.current_frame().push(I64(1));
}
FCONST_0 => {
self.current_frame().push(Value::F32(0.0));
self.current_frame().push(F32(0.0));
}
FCONST_1 => {
self.current_frame().push(Value::F32(1.0));
self.current_frame().push(F32(1.0));
}
FCONST_2 => {
self.current_frame().push(Value::F32(2.0));
self.current_frame().push(F32(2.0));
}
DCONST_0 => {
self.current_frame().push(Value::F64(0.0));
self.current_frame().push(F64(0.0));
}
DCONST_1 => {
self.current_frame().push(Value::F64(1.0));
self.current_frame().push(F64(1.0));
}
SIPUSH => {
let s = read_u16(&code.opcodes, pc) as i32;
@ -212,8 +211,8 @@ impl Vm {
.as_bytes()
.into();
self.execute_class(
stringclass,
self.execute(
"java/lang/String",
"<init>([B)V",
vec![
stringinstance.clone(),

View file

@ -5,8 +5,7 @@ mod test {
#[test]
fn if_cmp() {
let mut vm = Vm::new("tests");
let c = get_class(&mut vm, "testclasses.IfCmp").unwrap();
let ret = vm.execute_class(c, "i_is_1()Z", vec![]).unwrap();
let ret = vm.execute("testclasses.IfCmp", "i_is_1()Z", vec![]).unwrap();
unsafe {
if let Value::I32(b) = *ret.get() {
// internally a boolean is an int
@ -21,9 +20,8 @@ mod test {
#[test]
fn consts() {
let mut vm = Vm::new("tests");
let c = get_class(&mut vm, "testclasses.Const").unwrap();
let ret = vm
.execute_class(c, "hello()Ljava/lang/String;", vec![])
.execute("testclasses.Const", "hello()Ljava/lang/String;", vec![])
.unwrap();
unsafe {
if let Value::Ref(s) = &*ret.get() {