Skip to content

Commit

Permalink
Add support of trivial strings operations
Browse files Browse the repository at this point in the history
  • Loading branch information
hextriclosan committed Oct 10, 2024
1 parent 320dd29 commit ef2b413
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,15 @@ fn should_do_trivial_treemaps() {
assert_eq!(84, get_int(last_frame_value))
}

#[test]
fn should_do_trivial_strings() {
let mut vm = VM::new("std");
let last_frame_value = vm
.run("samples.javacore.strings.trivial.TrivialStrings")
.unwrap();
assert_eq!(8, get_int(last_frame_value))
}

fn get_int(locals: Option<Vec<i32>>) -> i32 {
*locals.unwrap().last().unwrap()
}
Expand Down
12 changes: 12 additions & 0 deletions tests/test_data/TrivialStrings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package samples.javacore.strings.trivial;

public class TrivialStrings {
public static void main(String[] args) {
char[] textArray = {'J', 'a', 'v', 'a', ' ', 'i', 's', ' ', 'f', 'l', 'e', 'x', 'i', 'b', 'l', 'e'};
String text = new String(textArray);
char[] searchArray = {'f', 'l', 'e', 'x', 'i', 'b', 'l', 'e'};
String search = new String(searchArray);

int index = text.indexOf(search);
}
}
Binary file not shown.
60 changes: 60 additions & 0 deletions vm/src/execution_engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,34 @@ impl Engine {
objref[0]
);
}
BALOAD => {
let index = stack_frame.pop();
let arrayref = stack_frame.pop();
let value = with_heap_read_lock(|heap| {
heap.get_array_value(arrayref, index).cloned()
})?;

stack_frame.push(value[0]);
stack_frame.incr_pc();
println!(
"BALOAD -> arrayref={arrayref}, index={index}, value={}",
value[0]
);
}
CALOAD => {
let index = stack_frame.pop();
let arrayref = stack_frame.pop();
let value = with_heap_read_lock(|heap| {
heap.get_array_value(arrayref, index).cloned()
})?;

stack_frame.push(value[0]);
stack_frame.incr_pc();
println!(
"CALOAD -> arrayref={arrayref}, index={index}, value={}",
value[0]
);
}
ISTORE => {
stack_frame.incr_pc();
let pos = stack_frame.get_bytecode_byte() as usize;
Expand Down Expand Up @@ -497,6 +525,30 @@ impl Engine {
stack_frame.incr_pc();
println!("AASTORE -> arrayref={arrayref}, index={index}, objref={objref}");
}
BASTORE => {
let value = stack_frame.pop();
let index = stack_frame.pop();
let arrayref = stack_frame.pop();

with_heap_write_lock(|heap| {
heap.set_array_value(arrayref, index, vec![value])
})?;

stack_frame.incr_pc();
println!("BASTORE -> arrayref={arrayref}, index={index}, value={value}");
}
CASTORE => {
let value = stack_frame.pop();
let index = stack_frame.pop();
let arrayref = stack_frame.pop();

with_heap_write_lock(|heap| {
heap.set_array_value(arrayref, index, vec![value])
})?;

stack_frame.incr_pc();
println!("CASTORE -> arrayref={arrayref}, index={index}, value={value}");
}
POP => {
stack_frame.pop();

Expand Down Expand Up @@ -692,6 +744,14 @@ impl Engine {
stack_frame.incr_pc();
println!("I2F -> {value}F");
}
I2B => {
let value = stack_frame.pop() as i8;

stack_frame.push(value as i32);

stack_frame.incr_pc();
println!("I2B -> {value}B");
}
LCMP => {
let b = stack_frame.pop_i64();
let a = stack_frame.pop_i64();
Expand Down

0 comments on commit ef2b413

Please sign in to comment.