Skip to content

implement invokespecial and invokeinterface #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"dev": "cd web && npm run dev",
"start": "npm run build && cd web && npm run dev",
"build:dev": "clear && wasm-pack build --target web --out-dir web/pkg",
"build": "wasm-pack build --target web --out-dir web/pkg",
"login": "wasm-pack login",
"pack": "wasm-pack pack web/pkg",
Expand Down
2 changes: 0 additions & 2 deletions src/classloader/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use bytes::{Buf, BytesMut};
use crate::classloader::constant_pool::{ConstantPoolInfo, utf8_info_as_string};
use crate::classloader::java_class_file::{AccessFlag, decode_access_flags};

use wasm_rs_dbg::dbg;

#[derive(Debug)]
pub enum AttributeInfo {
ConstantValue(ConstantValueAttribute),
Expand Down
7 changes: 3 additions & 4 deletions src/classloader/bootstrap_class_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use bytes::{Buf, BytesMut};
use web_sys::console::{time, time_end};
use crate::classloader::constant_pool::{decode_constant_pool, utf8_info_as_string, ConstantPoolInfo};
use crate::classloader::java_class_file::{decode_access_flags, decode_fields, decode_interfaces, decode_methods, decode_this_or_super_class, JavaClassFile};
use wasm_rs_dbg::dbg;
use crate::classloader::attributes::decode_attributes;

const MAGIC: u32 = 0xCAFEBABE;
Expand All @@ -15,7 +14,7 @@ const SUPPORTED_MAJOR_VERSION: f32 = 61.0;
use thiserror::Error;
use crate::classloader::constant_pool::ConstantPoolInfo::Class;
use crate::{error, STORAGE};
use crate::core::method_area::{MethodArea, RuntimeClass};
use crate::core::method_area::{RuntimeClass};
use crate::core::runtime_data_area::RuntimeDataArea;

#[derive(Debug, Error)]
Expand Down Expand Up @@ -108,7 +107,7 @@ impl BootstrapClassLoader {
})
}

pub fn load_class(&mut self, class_name: String) -> Option<Rc<Mutex<RuntimeClass>>> {
pub fn load_class(&mut self, class_name: String) -> Option<Rc<RuntimeClass>> {
let clazz = self.rda.lock().unwrap().method_area.lock().unwrap().get_class(&class_name);
return match clazz {
Some(class) => {
Expand All @@ -132,7 +131,7 @@ impl BootstrapClassLoader {
match data {
Some(bytes) => {
let class = self.define_class(&class_name, bytes.as_slice()).unwrap();
let runtime_class = Rc::new(Mutex::new(RuntimeClass::new(self, class)));
let runtime_class = Rc::new(RuntimeClass::new(self, class));
self.rda.lock().unwrap().method_area.lock().unwrap().add_class(&class_name, Rc::clone(&runtime_class));
Some(Rc::clone(&runtime_class))
},
Expand Down
1 change: 0 additions & 1 deletion src/classloader/constant_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use num_traits::FromPrimitive;
use crate::{error};

use thiserror::Error;
use wasm_rs_dbg::dbg;

#[derive(Debug, Error)]
enum ErrorType {
Expand Down
1 change: 0 additions & 1 deletion src/classloader/java_class_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::HashMap;
use anyhow::anyhow;
use bytes::{Buf, BytesMut};
use crate::classloader::constant_pool::{ConstantPoolInfo};
use wasm_rs_dbg::dbg;
use crate::classloader::attributes::{AttributeInfo, decode_attributes};

use thiserror::Error;
Expand Down
4 changes: 4 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#[allow(dead_code)]
pub mod method_area;
#[allow(dead_code)]
pub mod runtime_data_area;
#[allow(dead_code)]
pub mod heap;
#[allow(dead_code)]
pub mod stack_area;
pub mod native_method_stack;
12 changes: 6 additions & 6 deletions src/core/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub struct RuntimeField {

#[derive(Debug, Clone)]
pub struct JavaInstance {
pub class: Rc<Mutex<RuntimeClass>>,
pub class: Rc<RuntimeClass>,
pub fields: HashMap<String, RuntimeField>,
}

impl JavaInstance {
pub fn new(runtime_object: Rc<Mutex<RuntimeClass>>) -> JavaInstance {
pub fn new(runtime_object: Rc<RuntimeClass>) -> JavaInstance {
let mut fields: HashMap<String, RuntimeField> = HashMap::new();
let mut enumerate_fields = |class: Rc<Mutex<RuntimeClass>>| -> () {
for (key, value) in &class.lock().unwrap().fields {
let mut enumerate_fields = |class: Rc<RuntimeClass>| -> () {
for (key, value) in &class.fields {
let value = value.lock().unwrap();
fields.insert(key.to_string(), RuntimeField {
descriptor: value.descriptor.clone(),
Expand Down Expand Up @@ -59,13 +59,13 @@ impl JavaInstance {

#[derive(Debug, Clone)]
pub struct JavaArray {
pub class: Option<Rc<Mutex<RuntimeClass>>>,
pub class: Option<Rc<RuntimeClass>>,
pub length: usize,
pub value: Vec<Value>,
}

impl JavaArray {
pub fn new(class: Rc<Mutex<RuntimeClass>>, length: usize) -> Self {
pub fn new(class: Rc<RuntimeClass>, length: usize) -> Self {
let mut value = Vec::with_capacity(length);
value.fill(Value::new(ValueType::Reference));

Expand Down
33 changes: 22 additions & 11 deletions src/core/method_area.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use std::collections::HashMap;
use std::marker::PhantomData;
use std::rc::Rc;
use std::sync::Mutex;
use crate::classloader::attributes::AttributeInfo;
use crate::classloader::attributes::AttributeInfo::Code;
use crate::classloader::bootstrap_class_loader::{BootstrapClassLoader};
use crate::classloader::constant_pool::{class_name_as_string, ConstantPoolInfo, utf8_info_as_string};
use crate::classloader::java_class_file::{AccessFlag, Descriptor, FieldInfo, JavaClassFile, MethodInfo};
use crate::{error, log};

use wasm_rs_dbg::dbg;
use crate::execution::interpreter::Value;
use crate::execution::utils::parse_descriptor;

Expand Down Expand Up @@ -42,10 +39,10 @@ pub struct RuntimeClass /*<'a>*/ {
// todo will be removed
pub java_class_file: JavaClassFile,
// pub this_class: Box<Rc<RuntimeClass>>,
pub super_class: Option<Rc<Mutex<RuntimeClass>>>,
pub interfaces: Vec<Rc<Mutex<RuntimeClass>>>,
pub super_class: Option<Rc<RuntimeClass>>,
pub interfaces: Vec<Rc<RuntimeClass>>,
pub name: String,
pub initialized: bool,
pub initialized: Mutex<bool>,
}

impl RuntimeClass {
Expand Down Expand Up @@ -81,7 +78,7 @@ impl RuntimeClass {
interfaces,
java_class_file,
name,
initialized: false,
initialized: Mutex::new(false),
}
}

Expand Down Expand Up @@ -158,7 +155,7 @@ impl RuntimeClass {
for _ in 0..java_class_file.fields.len() {
let field_info = java_class_file.fields.pop().unwrap();
let name_index = field_info.name_index;
let descriptor_index = field_info.descriptor_index;
// let descriptor_index = field_info.descriptor_index;
let field = Self::build_field(java_class_file, field_info);

let name = utf8_info_as_string!(
Expand Down Expand Up @@ -186,11 +183,25 @@ impl RuntimeClass {
static_value: None,
}
}

pub fn lookup_method(class: Rc<RuntimeClass>, name: String) -> Option<Rc<Method>> {
let method = class.methods.get(&name);

match method {
Some(method) => {
Some(Rc::clone(method))
}
_ => {
let Some(super_class) = &class.super_class else { return None };
Self::lookup_method(Rc::clone(super_class), name)
}
}
}
}

#[derive(Debug)]
pub struct MethodArea {
pub indexes: HashMap<String, Rc<Mutex<RuntimeClass>>>,
pub indexes: HashMap<String, Rc<RuntimeClass>>,
}

impl MethodArea {
Expand All @@ -200,11 +211,11 @@ impl MethodArea {
}
}

pub fn add_class(&mut self, class_name: &String, class: Rc<Mutex<RuntimeClass>>) {
pub fn add_class(&mut self, class_name: &String, class: Rc<RuntimeClass>) {
self.indexes.insert(class_name.to_string(), class);
}

pub fn get_class(&self, class_name: &String) -> Option<Rc<Mutex<RuntimeClass>>> {
pub fn get_class(&self, class_name: &String) -> Option<Rc<RuntimeClass>> {
match self.indexes.get(class_name) {
Some(value) => Some(Rc::clone(value)),
_ => None
Expand Down
24 changes: 10 additions & 14 deletions src/core/stack_area.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::rc::Rc;
use std::sync::{Mutex, RwLock};
use std::sync::{Mutex};
use crate::classloader::attributes::AttributeInfo::{LocalVariableTable, LocalVariableTypeTable};
use crate::classloader::attributes::{LocalVariableTableEntry, LocalVariableTypeTableEntry};
use crate::core::method_area::{Method, RuntimeClass};
use crate::execution::interpreter::{interpret, Value};
use crate::{error, log};
use crate::classloader::constant_pool::utf8_info_as_string;
use crate::classloader::constant_pool::ConstantPoolInfo;

use wasm_rs_dbg::dbg;
use crate::jvm::JVM;

#[derive(Debug)]
Expand All @@ -25,15 +23,15 @@ pub struct StackFrame {
}

impl StackFrame {
pub fn new(class: Rc<Mutex<RuntimeClass>>, method: Rc<Method>) -> Self {
pub fn new(class: Rc<RuntimeClass>, method: Rc<Method>) -> Self {
let operand_stack = Vec::with_capacity(method.max_stack as usize);
Self {
local_variables: Self::build_local_variables(class, method),
operand_stack,
}
}

fn build_local_variables(class: Rc<Mutex<RuntimeClass>>, method: Rc<Method>) -> Vec<LocalVariable> {
fn build_local_variables(class: Rc<RuntimeClass>, method: Rc<Method>) -> Vec<LocalVariable> {
let mut result = Vec::with_capacity(method.max_locals as usize);
let descriptor_length = method.descriptor.len() as u16;

Expand Down Expand Up @@ -68,19 +66,19 @@ impl StackFrame {
_ => None,
};

result.push(Self::build_local_variable(Rc::clone(&class), local_variable_table_entry, local_variable_type_table_entry, i));
result.push(Self::build_local_variable(Rc::clone(&class), local_variable_table_entry, local_variable_type_table_entry));
}
}

result
}

fn build_local_variable(class: Rc<Mutex<RuntimeClass>>, local_variable_table: Option<&LocalVariableTableEntry>,
local_variable_type_table: Option<&LocalVariableTypeTableEntry>, index: u16) -> LocalVariable {
fn build_local_variable(class: Rc<RuntimeClass>, local_variable_table: Option<&LocalVariableTableEntry>,
local_variable_type_table: Option<&LocalVariableTypeTableEntry>) -> LocalVariable {
return match (local_variable_table, local_variable_type_table) {
(Some(local_variable_table), Some(local_variable_type_table)) => {
let name = utf8_info_as_string!(&class.lock().unwrap().java_class_file.constant_pool, local_variable_table.name_index).to_string();
let type_name = utf8_info_as_string!(&class.lock().unwrap().java_class_file.constant_pool, local_variable_type_table.name_index).to_string();
let name = utf8_info_as_string!(&class.java_class_file.constant_pool, local_variable_table.name_index).to_string();
let type_name = utf8_info_as_string!(&class.java_class_file.constant_pool, local_variable_type_table.name_index).to_string();

Self::_build_local_variable(name, type_name)
}
Expand Down Expand Up @@ -123,15 +121,13 @@ impl Thread {
}
}

pub fn invoke_method(&mut self, class: Rc<Mutex<RuntimeClass>>, method_name: &str, args: Vec<Value>) {
let binding = class.lock().unwrap();
let method = Rc::clone(binding.methods.get(method_name).unwrap());
pub fn invoke_method(&mut self, class: Rc<RuntimeClass>, method_name: &str, args: Vec<Value>) {
let method = Rc::clone(class.methods.get(method_name).unwrap());

// let method = class.lock().unwrap().methods.get(method_name).unwrap();
let frame = Rc::new(Mutex::new(StackFrame::new(Rc::clone(&class), Rc::clone(&method))));
self.frame_stack.push(Rc::clone(&frame));

drop(binding); // unlock the class binding
interpret(self, Rc::clone(&frame), Rc::clone(&class), Rc::clone(&method), args);

let popped_frame = self.frame_stack.pop().unwrap();
Expand Down
Loading
Loading