Skip to content

Commit

Permalink
Add support for string concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
hextriclosan committed Oct 16, 2024
1 parent ca7d631 commit 2021a90
Show file tree
Hide file tree
Showing 4 changed files with 39 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 @@ -349,6 +349,15 @@ fn should_do_strings_cpool_advanced() {
assert_eq!(29, get_int(last_frame_value))
}

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

#[test]
fn should_do_trivial_util_arrays() {
let mut vm = VM::new("std");
Expand Down
22 changes: 22 additions & 0 deletions tests/test_data/StringConcatInline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// javac -XDstringConcat=inline -d . StringConcatInline.java

package samples.javacore.strings.concat.trivial;

public class StringConcatInline {
public static void main(String[] args) {
int result = concat();
}

private static int concat() {
String abc = "abc";
String def = "def";
String abcdef = abc + def;

int sum = 0;
for (char c : abcdef.toCharArray()) {
sum += c;
}

return sum;
}
}
Binary file not shown.
8 changes: 8 additions & 0 deletions vm/src/execution_engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,14 @@ impl Engine {
stack_frame.incr_pc();
println!("I2B -> {value}B");
}
I2C => {
let value = stack_frame.pop() as u16;

stack_frame.push(value as i32);

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

0 comments on commit 2021a90

Please sign in to comment.