straighten out execute signatures
This commit is contained in:
parent
922006da1a
commit
ff5c41fc8d
3 changed files with 19 additions and 21 deletions
|
|
@ -61,8 +61,9 @@ pub fn get_class(
|
||||||
|
|
||||||
Class::initialize_fields(clone3);
|
Class::initialize_fields(clone3);
|
||||||
let clinit = clone2.borrow().methods.contains_key("<clinit>()V");
|
let clinit = clone2.borrow().methods.contains_key("<clinit>()V");
|
||||||
|
let name = &clone2.borrow().name.to_owned();
|
||||||
if clinit {
|
if clinit {
|
||||||
vm.execute_class(clone2, "<clinit>()V", vec![]).unwrap();
|
vm.execute(name, "<clinit>()V", vec![]).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(clone)
|
Ok(clone)
|
||||||
|
|
|
||||||
31
src/vm.rs
31
src/vm.rs
|
|
@ -9,9 +9,7 @@ use log::{info};
|
||||||
use Value::*;
|
use Value::*;
|
||||||
|
|
||||||
use crate::class::Value::{Null, Void};
|
use crate::class::Value::{Null, Void};
|
||||||
use crate::class::{
|
use crate::class::{get_class, unsafe_ref, unsafe_val, AttributeType, Class, Modifier, UnsafeValue, Value, Method};
|
||||||
get_class, unsafe_ref, unsafe_val, AttributeType, Class, Modifier, UnsafeValue, Value,
|
|
||||||
};
|
|
||||||
use crate::classloader::CpEntry;
|
use crate::classloader::CpEntry;
|
||||||
use crate::heap::{Heap, Object, ObjectRef};
|
use crate::heap::{Heap, Object, ObjectRef};
|
||||||
use crate::io::*;
|
use crate::io::*;
|
||||||
|
|
@ -97,19 +95,20 @@ impl Vm {
|
||||||
args: Vec<UnsafeValue>,
|
args: Vec<UnsafeValue>,
|
||||||
) -> Result<UnsafeValue, Error> {
|
) -> Result<UnsafeValue, Error> {
|
||||||
let class = get_class(self, class_name)?;
|
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(
|
pub fn execute_class(
|
||||||
&mut self,
|
&mut self,
|
||||||
class: Arc<RefCell<Class>>,
|
class: Arc<RefCell<Class>>,
|
||||||
method_name: &str,
|
method: Rc<Method>,
|
||||||
args: Vec<UnsafeValue>,
|
args: Vec<UnsafeValue>,
|
||||||
) -> Result<UnsafeValue, Error> {
|
) -> Result<UnsafeValue, Error> {
|
||||||
let this_class = class;
|
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
|
//TODO implement dynamic dispatch -> get method from instance
|
||||||
|
|
||||||
let mut local_params: Vec<Option<UnsafeValue>> =
|
let mut local_params: Vec<Option<UnsafeValue>> =
|
||||||
|
|
@ -152,25 +151,25 @@ impl Vm {
|
||||||
self.current_frame().push(I32(5));
|
self.current_frame().push(I32(5));
|
||||||
}
|
}
|
||||||
LCONST_0 => {
|
LCONST_0 => {
|
||||||
self.current_frame().push(Value::I64(0));
|
self.current_frame().push(I64(0));
|
||||||
}
|
}
|
||||||
LCONST_1 => {
|
LCONST_1 => {
|
||||||
self.current_frame().push(Value::I64(1));
|
self.current_frame().push(I64(1));
|
||||||
}
|
}
|
||||||
FCONST_0 => {
|
FCONST_0 => {
|
||||||
self.current_frame().push(Value::F32(0.0));
|
self.current_frame().push(F32(0.0));
|
||||||
}
|
}
|
||||||
FCONST_1 => {
|
FCONST_1 => {
|
||||||
self.current_frame().push(Value::F32(1.0));
|
self.current_frame().push(F32(1.0));
|
||||||
}
|
}
|
||||||
FCONST_2 => {
|
FCONST_2 => {
|
||||||
self.current_frame().push(Value::F32(2.0));
|
self.current_frame().push(F32(2.0));
|
||||||
}
|
}
|
||||||
DCONST_0 => {
|
DCONST_0 => {
|
||||||
self.current_frame().push(Value::F64(0.0));
|
self.current_frame().push(F64(0.0));
|
||||||
}
|
}
|
||||||
DCONST_1 => {
|
DCONST_1 => {
|
||||||
self.current_frame().push(Value::F64(1.0));
|
self.current_frame().push(F64(1.0));
|
||||||
}
|
}
|
||||||
SIPUSH => {
|
SIPUSH => {
|
||||||
let s = read_u16(&code.opcodes, pc) as i32;
|
let s = read_u16(&code.opcodes, pc) as i32;
|
||||||
|
|
@ -212,8 +211,8 @@ impl Vm {
|
||||||
.as_bytes()
|
.as_bytes()
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
self.execute_class(
|
self.execute(
|
||||||
stringclass,
|
"java/lang/String",
|
||||||
"<init>([B)V",
|
"<init>([B)V",
|
||||||
vec![
|
vec![
|
||||||
stringinstance.clone(),
|
stringinstance.clone(),
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,7 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn if_cmp() {
|
fn if_cmp() {
|
||||||
let mut vm = Vm::new("tests");
|
let mut vm = Vm::new("tests");
|
||||||
let c = get_class(&mut vm, "testclasses.IfCmp").unwrap();
|
let ret = vm.execute("testclasses.IfCmp", "i_is_1()Z", vec![]).unwrap();
|
||||||
let ret = vm.execute_class(c, "i_is_1()Z", vec![]).unwrap();
|
|
||||||
unsafe {
|
unsafe {
|
||||||
if let Value::I32(b) = *ret.get() {
|
if let Value::I32(b) = *ret.get() {
|
||||||
// internally a boolean is an int
|
// internally a boolean is an int
|
||||||
|
|
@ -21,9 +20,8 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn consts() {
|
fn consts() {
|
||||||
let mut vm = Vm::new("tests");
|
let mut vm = Vm::new("tests");
|
||||||
let c = get_class(&mut vm, "testclasses.Const").unwrap();
|
|
||||||
let ret = vm
|
let ret = vm
|
||||||
.execute_class(c, "hello()Ljava/lang/String;", vec![])
|
.execute("testclasses.Const", "hello()Ljava/lang/String;", vec![])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
unsafe {
|
unsafe {
|
||||||
if let Value::Ref(s) = &*ret.get() {
|
if let Value::Ref(s) = &*ret.get() {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue