Skip to content

Commit ff2a987

Browse files
committed
Merge branch 'master' of github.com:memory-agape/php-java
2 parents 333f581 + 1b1c8b3 commit ff2a987

4 files changed

Lines changed: 83 additions & 4 deletions

File tree

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22
namespace PHPJava\Kernel\Mnemonics;
33

4-
use PHPJava\Exceptions\NotImplementedException;
4+
use PHPJava\Kernel\Types\_Int;
5+
use PHPJava\Utilities\Formatter;
56

67
final class _instanceof implements OperationInterface
78
{
@@ -10,6 +11,22 @@ final class _instanceof implements OperationInterface
1011

1112
public function execute(): void
1213
{
13-
throw new NotImplementedException(__CLASS__);
14+
$cp = $this->getConstantPool();
15+
$objectref = $this->popFromOperandStack();
16+
17+
$targetClass = $cp[$cp[$this->readUnsignedShort()]->getClassIndex()];
18+
19+
[, $className] = Formatter::convertJavaNamespaceToPHP($targetClass);
20+
21+
if ($objectref instanceof $className) {
22+
$this->pushToOperandStack(
23+
_Int::get(1)
24+
);
25+
return;
26+
}
27+
28+
$this->pushToOperandStack(
29+
_Int::get(0)
30+
);
1431
}
1532
}

src/Utilities/Formatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
class Formatter
1010
{
11-
const BUILT_IN_PACKAGE = 0;
12-
const USER_DEFINED_PACKAGE = 1;
11+
const BUILT_IN_PACKAGE = 'BUILT_IN_PACKAGE';
12+
const USER_DEFINED_PACKAGE = 'USER_DEFINED_PACKAGE';
1313

1414
/**
1515
* @param $signature

tests/InstanceOfTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace PHPJava\Tests;
3+
4+
class InstanceOfTest extends Base
5+
{
6+
protected $fixtures = [
7+
'InstanceOfTest',
8+
];
9+
10+
private function call($method)
11+
{
12+
return $this->initiatedJavaClasses['InstanceOfTest']
13+
->getInvoker()
14+
->getStatic()
15+
->getMethods()
16+
->call($method);
17+
}
18+
19+
public function testInstanceOfString()
20+
{
21+
ob_start();
22+
$this->call('instanceOfString');
23+
$result = ob_get_clean();
24+
$this->assertEquals(
25+
"Hello World\n",
26+
$result
27+
);
28+
}
29+
30+
public function testInstanceOfObject()
31+
{
32+
ob_start();
33+
$this->call('instanceOfObject');
34+
$result = ob_get_clean();
35+
$this->assertEquals(
36+
"Hello World\n",
37+
$result
38+
);
39+
}
40+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class InstanceOfTest
2+
{
3+
public static void instanceOfString()
4+
{
5+
String a = "Hello World";
6+
if (a instanceof String) {
7+
System.out.println(a);
8+
return;
9+
}
10+
System.out.println("Unreachable here.");
11+
}
12+
13+
public static void instanceOfObject()
14+
{
15+
String a = "Hello World";
16+
if (a instanceof Object) {
17+
System.out.println(a);
18+
return;
19+
}
20+
System.out.println("Unreachable here.");
21+
}
22+
}

0 commit comments

Comments
 (0)