Skip to content

Commit a844741

Browse files
committed
Fix ifacmpeq/ne null handling
1 parent bc62a15 commit a844741

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

jvm_rust/src/interpreter.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,18 +453,18 @@ impl Interpreter {
453453
stack_frame.operand_stack.push(JavaValue::Int(value1 / value2));
454454
}
455455
Opcode::IfAcmpeq(x) => {
456-
let value2: Box<dyn ClassInstance> = stack_frame.operand_stack.pop().unwrap().into();
457-
let value1: Box<dyn ClassInstance> = stack_frame.operand_stack.pop().unwrap().into();
456+
let value2: Option<Box<dyn ClassInstance>> = stack_frame.operand_stack.pop().unwrap().into();
457+
let value1: Option<Box<dyn ClassInstance>> = stack_frame.operand_stack.pop().unwrap().into();
458458

459-
if value1.equals(&*value2)? {
459+
if value1 == value2 {
460460
return Ok(ExecuteNext::Jump((current_offset as i32 + *x as i32) as u32));
461461
}
462462
}
463463
Opcode::IfAcmpne(x) => {
464-
let value2: Box<dyn ClassInstance> = stack_frame.operand_stack.pop().unwrap().into();
465-
let value1: Box<dyn ClassInstance> = stack_frame.operand_stack.pop().unwrap().into();
464+
let value2: Option<Box<dyn ClassInstance>> = stack_frame.operand_stack.pop().unwrap().into();
465+
let value1: Option<Box<dyn ClassInstance>> = stack_frame.operand_stack.pop().unwrap().into();
466466

467-
if !value1.equals(&*value2)? {
467+
if value1 != value2 {
468468
return Ok(ExecuteNext::Jump((current_offset as i32 + *x as i32) as u32));
469469
}
470470
}

test_data/ReferenceCompare.class

875 Bytes
Binary file not shown.

test_data/ReferenceCompare.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a != b
2+
a != c
3+
b != c
4+
a != d
5+
a == e
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class ReferenceCompare {
2+
public static void main(String[] args) {
3+
ReferenceCompare a = new ReferenceCompare();
4+
ReferenceCompare b = new ReferenceCompare();
5+
ReferenceCompare c = null;
6+
String d = "test";
7+
ReferenceCompare e = a;
8+
9+
if (a == b) {
10+
System.out.println("a == b");
11+
} else {
12+
System.out.println("a != b");
13+
}
14+
15+
if (a == c) {
16+
System.out.println("a == c");
17+
} else {
18+
System.out.println("a != c");
19+
}
20+
21+
if (b == c) {
22+
System.out.println("b == c");
23+
} else {
24+
System.out.println("b != c");
25+
}
26+
27+
if ((Object) a == d) {
28+
System.out.println("a == d");
29+
} else {
30+
System.out.println("a != d");
31+
}
32+
33+
if (a == e) {
34+
System.out.println("a == e");
35+
} else {
36+
System.out.println("a != e");
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)