Skip to content

Commit c2f264f

Browse files
committed
Fix getfield
1 parent 7159b6f commit c2f264f

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

src/Kernel/Mnemonics/_getfield.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ final class _getfield implements OperationInterface
1212
public function execute(): void
1313
{
1414
$cpInfo = $this->getConstantPool();
15-
1615
$cp = $cpInfo[$this->readUnsignedShort()];
16+
$class = $cpInfo[$cp->getNameAndTypeIndex()];
1717

18-
$get = $this->popFromOperandStack();
18+
$name = $cpInfo[$class->getNameIndex()]->getString();
19+
$objectref = $this->popFromOperandStack();
1920

20-
$return = $get->getInstance($cpInfo[$cpInfo[$cp->getNameAndTypeIndex()]->getNameIndex()]->getString());
21+
$return = $objectref->getInvoker()->getDynamic()->getFields()->get($name);
2122

2223
if ($return !== null) {
2324
$this->pushToOperandStack($return);
2425
return;
2526
}
26-
throw new Exception('Cannot get to undefined Field ' . $cpInfo[$cpInfo[$cp->getNameAndTypeIndex()]->getNameIndex()]->getString() . '');
27+
28+
throw new Exception('Cannot get to undefined Field ' . $name);
2729
}
2830
}

tests/GetFieldTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace PHPJava\Tests;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
class GetFieldTest extends Base
7+
{
8+
protected $fixtures = [
9+
'GetFieldTest',
10+
];
11+
12+
public function testGetField()
13+
{
14+
$actual = $this->initiatedJavaClasses['GetFieldTest']
15+
->getInvoker()
16+
->getStatic()
17+
->getMethods()
18+
->call('getField')
19+
->getValue();
20+
21+
$this->assertEquals(1, $actual);
22+
}
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class GetFieldTest
2+
{
3+
public int value = 1;
4+
5+
public static int getField() {
6+
GetFieldTest instance = new GetFieldTest();
7+
return instance.value;
8+
}
9+
}

0 commit comments

Comments
 (0)