Skip to content
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

Add support for MULTIANEWARRAY ops #143

Merged
merged 1 commit into from
Dec 9, 2024
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
10 changes: 8 additions & 2 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn should_convert_to_string_and_back() {

#[test]
fn should_do_3d_arrays() {
assert_success("samples.arrays.array3d.Array3D", "780\n");
assert_success("samples.arrays.array3d.Array3D", "780\n360\n");
}

#[test]
Expand Down Expand Up @@ -442,7 +442,13 @@ fn should_print_to_stdout() {
fn should_return_class_name() {
assert_success(
"samples.reflection.trivial.classgetname.ClassGetNameExample",
"1\n",
r#"java.lang.String
java.lang.Character$UnicodeBlock
byte
[Ljava.lang.Object;
[[[[[[[I
[[[[[[I
"#,
);
}

Expand Down
16 changes: 13 additions & 3 deletions tests/test_data/Array3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

public class Array3D {
public static void main(String[] args) {
int[][][] array = new int[][][]{
int[][][] initialization = new int[][][]{
{{10, 20}, {30, 40}, {50, 60}},
{{70, 80}, {90, 100}, {110, 120}}
};

int result = sum(array);
System.out.println(result);
int[][][] dimensionExpression = new int[2][1][4];
dimensionExpression[0][0][0] = 10;
dimensionExpression[0][0][1] = 20;
dimensionExpression[0][0][2] = 30;
dimensionExpression[0][0][3] = 40;
dimensionExpression[1][0][0] = 50;
dimensionExpression[1][0][1] = 60;
dimensionExpression[1][0][2] = 70;
dimensionExpression[1][0][3] = 80;

System.out.println(sum(initialization));
System.out.println(sum(dimensionExpression));
}

private static int sum(int[][][] array3d) {
Expand Down
18 changes: 9 additions & 9 deletions tests/test_data/ClassGetNameExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public static void main(String[] args) {
String unicodeBlockName = Character.UnicodeBlock.class.getName();
String byteName = byte.class.getName();
String objectsArrayName = (new Object[3]).getClass().getName();
//String intArrayName = (new int[3][4][5][6][7][8][9]).getClass().getName(); //todo: https://github.com/hextriclosan/rusty-jvm/issues/125
var intArr = new int[3][4][5][6][7][8][9];
String intArrayName = intArr.getClass().getName();
String intSubArrayName = intArr[0].getClass().getName();

int result = "java.lang.String".equals(stringName) &&
"java.lang.Character$UnicodeBlock".equals(unicodeBlockName) &&
"byte".equals(byteName) &&
"[Ljava.lang.Object;".equals(objectsArrayName) /*&&
"[[[[[[[I".equals(intArrayName)*/
? 1
: 0;
System.out.println(result);
System.out.println(stringName);
System.out.println(unicodeBlockName);
System.out.println(byteName);
System.out.println(objectsArrayName);
System.out.println(intArrayName);
System.out.println(intSubArrayName);
}
}
Binary file modified tests/test_data/samples/arrays/array3d/Array3D.class
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion vm/src/execution_engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Engine {
ops_reference_processor::process(code, &class, &mut stack_frames)?;
}
196u8..=201u8 => {
ops_extended_processor::process(code, &mut stack_frames)?;
ops_extended_processor::process(code, &class, &mut stack_frames)?;
}
_ => unreachable!("{}", format! {"xxx = {}", code}),
}
Expand Down
45 changes: 44 additions & 1 deletion vm/src/execution_engine/ops_extended_processor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use tracing::trace;
use crate::method_area::method_area::with_method_area;
use crate::error::Error;
use crate::execution_engine::common::last_frame_mut;
use crate::execution_engine::opcode::*;
Expand All @@ -8,8 +10,9 @@ use crate::execution_engine::ops_store_processor::handle_store;
use crate::stack::sack_value::StackValue;
use crate::stack::stack_frame::{StackFrame, StackFrames};
use std::fmt::Display;
use crate::heap::heap::with_heap_write_lock;

pub(crate) fn process(code: u8, stack_frames: &mut StackFrames) -> crate::error::Result<()> {
pub(crate) fn process(code: u8, current_class_name: &str, stack_frames: &mut StackFrames) -> crate::error::Result<()> {
let stack_frame = last_frame_mut(stack_frames)?;
match code {
WIDE => {
Expand Down Expand Up @@ -38,6 +41,31 @@ pub(crate) fn process(code: u8, stack_frames: &mut StackFrames) -> crate::error:
}
}
}
MULTIANEWARRAY => {
let class_index = stack_frame.extract_two_bytes();
let dimension_number = stack_frame.extract_one_byte();

let rc = with_method_area(|method_area| method_area.get(current_class_name))?;
let cpool_helper = rc.cpool_helper();
let class_name = cpool_helper.get_class_name(class_index as u16).ok_or_else(|| {
Error::new_execution(&format!(
"Error getting class name by index {class_index}"
))
})?;

let dimentions = (0..dimension_number)
.map(|_| stack_frame.pop())
.collect::<Vec<_>>()
.into_iter()
.rev()
.collect::<Vec<_>>();

let root_array_ref = create_n_array(&dimentions, &class_name, 0)?;
stack_frame.push(root_array_ref);

stack_frame.incr_pc();
trace!("MULTIANEWARRAY -> type={class_name}, dimension_number={dimension_number}, root_array_ref={root_array_ref}");
}
IFNULL => branch1arg(|a| a == 0, stack_frame, "IFNULL"),
IFNONNULL => branch1arg(|a| a != 0, stack_frame, "IFNONNULL"),
_ => {
Expand Down Expand Up @@ -66,3 +94,18 @@ fn handle_pos_and_store<T: StackValue + Display + Copy>(
let pos = stack_frame.extract_two_bytes();
handle_store::<T, _>(stack_frame, pos as usize, name_starts);
}

fn create_n_array(dimensions: &[i32], signature: &str, current_level: usize) -> crate::error::Result<i32> {
let current_length = dimensions[current_level];
let current_signature = &signature[current_level..];
let arrayref = with_heap_write_lock(|heap| heap.create_array(current_signature, current_length))?;

if current_level < dimensions.len() - 1 {
for i in 0..current_length {
let next_ref = create_n_array(dimensions, signature, current_level + 1)?;
with_heap_write_lock(|heap| heap.set_array_value(arrayref, i, vec![next_ref]))?;
}
}

Ok(arrayref)
}
Loading