Skip to content

Commit 82b2897

Browse files
committed
jvm: More array opcodes
1 parent f92d35b commit 82b2897

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

example/Array.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,18 @@ public static int sum(int[] array) {
5050
}
5151
return total;
5252
}
53+
54+
public static String loopMultipleArray() {
55+
StringBuilder result = new StringBuilder();
56+
57+
String[] a = {"a","b","c"};
58+
String[] b = {"A","B","C"};
59+
int[] c = {1,2,3};
60+
61+
for(int i = 0;i < a.length;i++){
62+
result.append(a[i] + b[i] + c[i] + '\n');
63+
}
64+
65+
return result.toString();
66+
}
5367
}

pyjvm/Machine.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class Inst(Enum):
3737
ALOAD_0 = 0x2A
3838
ALOAD_1 = 0x2B
3939
ALOAD_2 = 0x2C
40+
ALOAD_3 = 0x2D
4041
IALOAD = 0x2E
42+
AALOAD = 0x32
4143
ISTORE = 0x36
4244
LSTORE = 0x37
4345
DSTORE = 0x39
@@ -55,6 +57,7 @@ class Inst(Enum):
5557
ASTORE_2 = 0x4D
5658
ASTORE_3 = 0x4E
5759
IASTORE = 0x4F
60+
AASTORE = 0x53
5861
POP = 0x57
5962
DUP = 0x59
6063
IADD = 0x60
@@ -92,6 +95,8 @@ class Inst(Enum):
9295
INVOKESPECIAL = 0xB7
9396
INVOKESTATIC = 0xB8
9497
NEW = 0xBB
98+
NEWARRAY = 0xBC
99+
ANEWARRAY = 0xBD
95100
ARRAYLENGTH = 0xBE
96101

97102
def argumentCount(desc):
@@ -230,6 +235,10 @@ def iload_1(frame):
230235
def iload_2(frame):
231236
frame.push(frame.get_local(2))
232237

238+
@opcode(Inst.ALOAD_3)
239+
def aload_3(frame):
240+
frame.push(frame.get_local(3))
241+
233242
@opcode(Inst.IALOAD)
234243
def iaload(frame):
235244
index = frame.pop()
@@ -339,6 +348,28 @@ def arraylength(frame):
339348
array = frame.pop()
340349
frame.push(len(array))
341350

351+
@opcode(Inst.ANEWARRAY)
352+
def anewarray(frame):
353+
index = read_unsigned_short(frame)
354+
ref = frame.current_class.const_pool[index - 1]
355+
356+
count = frame.pop()
357+
frame.push(['' for _ in range(count)])
358+
359+
@opcode(Inst.NEWARRAY)
360+
def newarray(frame):
361+
array_type = read_byte(frame)
362+
count = frame.pop()
363+
364+
frame.push([0 for _ in range(count)])
365+
366+
@opcode(Inst.AALOAD)
367+
def aaload(frame):
368+
index = frame.pop()
369+
array = frame.pop()
370+
371+
frame.push(array[index])
372+
342373
class Machine:
343374
def __init__(self):
344375
self.class_files = {}
@@ -380,7 +411,10 @@ def execute_code(self, frame):
380411
elif inst == Inst.ASTORE_2:
381412
obj = frame.stack.pop()
382413
frame.set_local(2, obj)
383-
elif inst == Inst.IASTORE:
414+
elif inst == Inst.ASTORE_3:
415+
obj = frame.stack.pop()
416+
frame.set_local(3, obj)
417+
elif inst == Inst.IASTORE or inst == Inst.AASTORE:
384418
val = frame.stack.pop()
385419
index = frame.stack.pop()
386420
array = frame.stack.pop()

pyjvm/jstdlib/StringBuilder.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ def canHandleMethod(self, name, desc):
1717
def handleMethod(self, name, desc, frame):
1818
super().handleMethod(name, desc, frame)
1919
if name == 'append':
20-
v2 = str(frame.stack.pop())
20+
if desc.startswith('(C)'):
21+
v2 = frame.stack.pop()
22+
if type(v2) == int:
23+
v2 = chr(v2)
24+
else:
25+
v2 = str(frame.stack.pop())
2126
v1 = frame.stack.pop()
2227
v1.string += v2
2328
return v1

run_unittest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,9 @@ def test_sum(self):
105105
r = self.jvm.call_function('jvmtest/ArrayTest/sum', l)
106106
self.assertEqual(r, sum(l))
107107

108+
def test_loop_multiple(self):
109+
r = self.jvm.call_function('jvmtest/ArrayTest/loopMultipleArray')
110+
self.assertEqual(r, 'aA1\nbB2\ncC3\n')
111+
108112
if __name__ == '__main__':
109113
unittest.main()

0 commit comments

Comments
 (0)