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

Lazy static initialization #17

Merged
merged 2 commits into from
Sep 20, 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
4 changes: 2 additions & 2 deletions jdescriptor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn default_value(type_descriptor: &TypeDescriptor) -> Vec<i32> {
Double => todo!(),
Void => panic!("field can't be a void type"),
Array(_, _) => vec![0],
Object(_) => todo!(),
Object(_) => vec![0],
}
}

Expand All @@ -52,7 +52,7 @@ pub fn get_length(type_descriptor: &TypeDescriptor) -> usize {
Long | Double => 2,
Void => panic!("field can't be a void type"),
Array(_, _) => 1,
Object(_) => todo!(),
Object(_) => 1,
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,5 @@ fn main() {
}
};

println!(
"\nresult={:?}",
result.map_or_else(|| vec![], |v| v)
);
println!("\nresult={:?}", result.map_or_else(|| vec![], |v| v));
}
58 changes: 53 additions & 5 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn should_do_adding_with_negative_longs() {
vec!["tests/test_data/AdderNegativeLong.class"],
"tests/test_data/std",
)
.unwrap();
.unwrap();
let last_frame_value = vm.run("AdderNegativeLong").unwrap();
assert_eq!(-1990000000000000, get_long(last_frame_value))
}
Expand Down Expand Up @@ -66,7 +66,7 @@ fn should_write_read_instance_fields_with_longs() {
],
"tests/test_data/std",
)
.unwrap();
.unwrap();
let last_frame_value = vm.run("InstanceFieldsUserLong").unwrap();
assert_eq!(4_380_866_642_760, get_long(last_frame_value))
}
Expand Down Expand Up @@ -102,7 +102,7 @@ fn should_do_extreme_stack_operations_with_longs() {
vec!["tests/test_data/ExtremeStackTestLong.class"],
"tests/test_data/std",
)
.unwrap();
.unwrap();
let last_frame_value = vm.run("ExtremeStackTestLong").unwrap();
assert_eq!(454, get_long(last_frame_value))
}
Expand Down Expand Up @@ -138,7 +138,11 @@ fn should_do_arrays() {

#[test]
fn should_do_arrays_with_longs() {
let vm = VM::new(vec!["tests/test_data/ArrayLong.class"], "tests/test_data/std").unwrap();
let vm = VM::new(
vec!["tests/test_data/ArrayLong.class"],
"tests/test_data/std",
)
.unwrap();
let last_frame_value = vm.run("ArrayLong").unwrap();
assert_eq!(233646220932000, get_long(last_frame_value))
}
Expand All @@ -154,7 +158,6 @@ fn should_do_class_static_initialization() {
assert_eq!(257, get_int(last_frame_value))
}

#[ignore]
#[test]
fn should_do_class_static_initialization_multiple_classes() {
let vm = VM::new(
Expand All @@ -170,6 +173,51 @@ fn should_do_class_static_initialization_multiple_classes() {
assert_eq!(350, get_int(last_frame_value))
}

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

#[test]
fn should_do_class_static_initialization_advanced() {
let vm = VM::new(
vec![
"tests/test_data/StaticInitializationAdvanced.class",
"tests/test_data/ClassA.class",
"tests/test_data/ClassB.class",
"tests/test_data/ClassC.class",
"tests/test_data/ClassD.class",
"tests/test_data/ClassE.class",
"tests/test_data/Helper.class",
],
"tests/test_data/std",
)
.unwrap();
let last_frame_value = vm.run("StaticInitializationAdvanced").unwrap();
assert_eq!(826, get_int(last_frame_value))
}

#[test]
fn should_do_class_static_initialization_circular() {
let vm = VM::new(
vec![
"tests/test_data/StaticInitializationCircular.class",
"tests/test_data/ClassACircular.class",
"tests/test_data/ClassBCircular.class",
],
"tests/test_data/std",
)
.unwrap();
let last_frame_value = vm.run("StaticInitializationCircular").unwrap();
assert_eq!(700, get_int(last_frame_value))
}

fn get_int(locals: Option<Vec<i32>>) -> i32 {
*locals.unwrap().last().unwrap()
}
Expand Down
Binary file added tests/test_data/ClassA.class
Binary file not shown.
Binary file added tests/test_data/ClassACircular.class
Binary file not shown.
Binary file added tests/test_data/ClassB.class
Binary file not shown.
Binary file added tests/test_data/ClassBCircular.class
Binary file not shown.
Binary file added tests/test_data/ClassC.class
Binary file not shown.
Binary file added tests/test_data/ClassD.class
Binary file not shown.
Binary file added tests/test_data/ClassE.class
Binary file not shown.
Binary file added tests/test_data/Helper.class
Binary file not shown.
Binary file added tests/test_data/StaticInitializationAdvanced.class
Binary file not shown.
56 changes: 56 additions & 0 deletions tests/test_data/StaticInitializationAdvanced.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
public class StaticInitializationAdvanced {
public static void main(String[] args) {
int classCWithHelperNonStaticGetter = ClassC.staticField;

int classDWithHelperStaticGetter = ClassD.staticField;

ClassC.staticField = 600;
int classCIsModified = ClassC.staticField;

int classEAsSumOfCAndD = ClassE.staticField;

int result = classCWithHelperNonStaticGetter + classDWithHelperStaticGetter * classCIsModified / classEAsSumOfCAndD;
}
}

class ClassC {
static Helper helper;
static int staticField;

static {
helper = new Helper(500);
staticField = 150 + helper.calculateNonStaticFieldValue();
}
}

class ClassD {
static int staticField;

static {
staticField = Helper.calculateStaticFieldValue();
}
}

class ClassE {
static int staticField;

static {
staticField = ClassC.staticField + ClassD.staticField;
}
}

class Helper {
private final int value;

Helper(int value) {
this.value = value;
}

static int calculateStaticFieldValue() {
return 250;
}

public int calculateNonStaticFieldValue() {
return value;
}
}
Binary file added tests/test_data/StaticInitializationCircular.class
Binary file not shown.
25 changes: 25 additions & 0 deletions tests/test_data/StaticInitializationCircular.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@


public class StaticInitializationCircular {
public static void main(String[] args) {
int classA = ClassACircular.staticField;
int classB = ClassBCircular.staticField;
int result = classA + classB;
}
}

class ClassACircular {
static int staticField;

static {
staticField = 100 + ClassBCircular.staticField;
}
}

class ClassBCircular {
static int staticField;

static {
staticField = 300 + ClassACircular.staticField;
}
}
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/test_data/StaticInitializationWithinOneClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


public class StaticInitializationWithinOneClass {
static int staticField;

static {
int xxx = 1337;
staticField = xxx;
}

public static void main(String[] args) {
staticField = 100;
int result = staticField;
}
}
Loading