From 74ec18e2e3af05993cde8b9ba919a093ac6819c2 Mon Sep 17 00:00:00 2001 From: Igor Rudenko Date: Fri, 20 Sep 2024 00:07:41 +0300 Subject: [PATCH] Add proper static initialization --- tests/integration_test.rs | 1 - vm/src/execution_engine/engine.rs | 16 ++++++++++++++++ vm/src/vm.rs | 9 --------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 620896b1..80d1a5e7 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -154,7 +154,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( diff --git a/vm/src/execution_engine/engine.rs b/vm/src/execution_engine/engine.rs index ae2ea31b..40bcd94b 100644 --- a/vm/src/execution_engine/engine.rs +++ b/vm/src/execution_engine/engine.rs @@ -1,3 +1,4 @@ +use std::collections::HashSet; use crate::error::Error; use crate::execution_engine::opcode::*; use crate::heap::heap::Heap; @@ -20,6 +21,7 @@ impl<'a> Engine<'a> { let mut stack_frames = vec![method.new_stack_frame()]; let mut last_value: Option> = None; let mut current_class_name: String; + let mut static_set: HashSet = HashSet::new(); while !stack_frames.is_empty() { let stack_frame = stack_frames @@ -688,6 +690,20 @@ impl<'a> Engine<'a> { fieldref_constpool_index, )?; + //todo!! + //try call invoke static `":()V"` on `class_name` (it should be done only once) + if !static_set.contains(class_name.as_str()) { + static_set.insert(class_name.clone()); + if let Ok(clinit) = self.method_area.get_method_by_name_signature(class_name.as_str(), ":()V") { + stack_frame.advance_pc(-2); + let next_frame = clinit.new_stack_frame(); + //stack_frame.incr_pc(); //incr here because of borrowing problem + stack_frames.push(next_frame); + println!("!!!invoke -> {class_name}.:()V"); + continue; + } + } + let field = self .method_area .loaded_classes diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 029a4850..19238108 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -20,15 +20,6 @@ impl VM { 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(":()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) } }