Skip to content

Commit 7922198

Browse files
committed
Fix long/double variable slot crash
1 parent dbf2490 commit 7922198

4 files changed

Lines changed: 23 additions & 3 deletions

File tree

jvm_rust/src/interpreter.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloc::{boxed::Box, format, vec::Vec};
1+
use alloc::{boxed::Box, format, vec, vec::Vec};
22
use core::iter;
33

44
use classfile::{AttributeInfoCode, ConstantPoolReference, Opcode};
@@ -18,7 +18,19 @@ impl Interpreter {
1818
pub async fn run(jvm: &Jvm, code_attribute: &AttributeInfoCode, args: Box<[JavaValue]>, return_type: &JavaType) -> Result<JavaValue> {
1919
let mut stack_frame = StackFrame::new();
2020

21-
stack_frame.local_variables = args.into_vec().into_iter().map(Self::to_stack_frame_type).collect();
21+
stack_frame.local_variables = args
22+
.into_iter()
23+
.flat_map(|x| {
24+
let stack_value = Self::to_stack_frame_type(x);
25+
match stack_value {
26+
// long and double take two slots in local variables
27+
JavaValue::Long(_) | JavaValue::Double(_) => {
28+
vec![stack_value, JavaValue::Void]
29+
}
30+
_ => vec![stack_value],
31+
}
32+
})
33+
.collect::<Vec<_>>();
2234

2335
stack_frame
2436
.local_variables

test_data/LongDouble.class

84 Bytes
Binary file not shown.

test_data/LongDouble.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
5
22
2.0
3+
10

test_data/src/LongDouble.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ public static void main(String[] args) {
1313

1414
System.out.println(test1);
1515
System.out.println(test2);
16+
17+
long test5 = testtesttest(test1, test4);
18+
System.out.println(test5);
1619
}
1720

1821
static long testtest(long arg) {
1922
return arg + 1;
2023
}
2124

2225
static long testtesttest(long arg) {
23-
return arg --;
26+
return arg--;
27+
}
28+
29+
static long testtesttest(long arg1, long arg2) {
30+
return arg1 + arg2;
2431
}
2532
}

0 commit comments

Comments
 (0)