Skip to content

Commit

Permalink
Add trivial class static initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
hextriclosan committed Sep 7, 2024
1 parent 58fd980 commit b71cd33
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,30 @@ fn should_do_arrays() {
let last_frame_value = vm.run("Array").unwrap();
assert_eq!(740, last_frame_value.unwrap())
}

#[test]
fn should_do_class_static_initialization() {
let vm = VM::new(
vec!["tests/test_data/StaticInitialization.class"],
"tests/test_data/std",
)
.unwrap();
let last_frame_value = vm.run("StaticInitialization").unwrap();
assert_eq!(257, last_frame_value.unwrap())
}

#[ignore]
#[test]
fn should_do_class_static_initialization_multiple_classes() {
let vm = VM::new(
vec![
"tests/test_data/Dependable.class",
"tests/test_data/DependsOnDependable.class",
"tests/test_data/StaticInitializationUser.class",
],
"tests/test_data/std",
)
.unwrap();
let last_frame_value = vm.run("StaticInitializationUser").unwrap();
assert_eq!(350, last_frame_value.unwrap())
}
Binary file added tests/test_data/Dependable.class
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/test_data/Dependable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

public class Dependable {
static int valueA = 100;

static {
valueA = 200;
}
}
Binary file added tests/test_data/DependsOnDependable.class
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/test_data/DependsOnDependable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

public class DependsOnDependable {
static int valueB = Dependable.valueA + 50;

static {
valueB += 100;
}
}
Binary file added tests/test_data/StaticInitialization.class
Binary file not shown.
35 changes: 35 additions & 0 deletions tests/test_data/StaticInitialization.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

public class StaticInitialization {
private static final int STATIC_FINAL_FIELD = init();
private static int staticField1;
private static int staticField2;
private static int[] staticArray;

// Static initializer block
static {
staticField1 = 5; // Direct static field initialization
staticArray = new int[staticField1]; // Initialize array in static block
for (int i = 0; i < staticArray.length; i++) {
staticArray[i] = i * staticField1;
}
}

private static int init() {
return 42;
}

public static void main(String[] args) {
int arraySum = 0;
for (int i = 0; i < staticArray.length; i++) {
arraySum += staticArray[i];
}

int result = STATIC_FINAL_FIELD + staticField1 + staticField2 + arraySum;
}

// Another static block (executed in order of appearance)
static {
staticField1 += 50; // Modify staticField1
staticField2 = staticField1 * 2; // Initialize staticField2 based on staticField1
}
}
Binary file added tests/test_data/StaticInitializationUser.class
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/test_data/StaticInitializationUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

public class StaticInitializationUser {
public static void main(String[] args) {
int result = DependsOnDependable.valueB;
}
}
8 changes: 8 additions & 0 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ impl VM {
.get_method_by_name_signature(main_class_name, "main:([Ljava/lang/String;)V")?;

let mut engine = Engine::new(&self.class_loader.method_area());

for (class_name, java_class) in self.class_loader.method_area().loaded_classes.iter() {
if let Some(java_method) = java_class.methods.method_by_signature.get("<clinit>:()V") {
println!("About to initialize java class {class_name} java_method={java_method:?}");
engine.execute(java_method)?; //todo implement multiclass correct initialization order
}
}

engine.execute(main_method)
}
}

0 comments on commit b71cd33

Please sign in to comment.