Skip to content

Commit 416ce84

Browse files
committed
Add check cast
1 parent 67fb4f3 commit 416ce84

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

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

4-
use PHPJava\Exceptions\NotImplementedException;
4+
use PHPJava\Packages\java\lang\ClassCastException;
5+
use PHPJava\Utilities\Formatter;
6+
use PHPJava\Utilities\TypeResolver;
57

68
final class _checkcast implements OperationInterface
79
{
@@ -10,6 +12,28 @@ final class _checkcast implements OperationInterface
1012

1113
public function execute(): void
1214
{
13-
throw new NotImplementedException(__CLASS__);
15+
$cp = $this->getConstantPool();
16+
$index = $this->readUnsignedShort();
17+
$objectref = $this->popFromOperandStack();
18+
19+
if ($objectref === null) {
20+
return;
21+
}
22+
23+
$castTo = $cp[$cp[$index]->getClassIndex()]->getString();
24+
25+
$fromObjectClass = Formatter::convertPHPNamespacesToJava(get_class($objectref));
26+
27+
[$classes, $interfaces] = TypeResolver::getExtendedClasses(
28+
'L' . str_replace('/', '.', $castTo)
29+
)[0] ?? [[], []];
30+
31+
if (in_array($fromObjectClass, $classes, true)) {
32+
return;
33+
}
34+
35+
throw new ClassCastException(
36+
'class \\' . get_class($objectref) . ' cannot be cast to class ' . Formatter::convertJavaNamespaceToPHP($castTo)[1]
37+
);
1438
}
1539
}

src/Kernel/Mnemonics/_ldc.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPJava\Kernel\Structures\_Integer;
66
use PHPJava\Kernel\Structures\_String;
77
use PHPJava\Kernel\Structures\_Utf8;
8+
use PHPJava\Kernel\Types\_Int;
89

910
final class _ldc implements OperationInterface
1011
{
@@ -14,7 +15,6 @@ final class _ldc implements OperationInterface
1415
public function execute(): void
1516
{
1617
$cpInfo = $this->getConstantPool();
17-
1818
$data = $cpInfo[$this->readUnsignedByte()];
1919

2020
$value = null;
@@ -25,8 +25,10 @@ public function execute(): void
2525
if ($value instanceof _Utf8) {
2626
$value = $value->getStringObject();
2727
}
28-
} elseif (($data instanceof _Integer) || ($data instanceof _Float)) {
29-
$value = $data->getBytes();
28+
} elseif (($data instanceof _Integer)) {
29+
$value = _Int::get($data->getBytes());
30+
} elseif ($data instanceof _Float) {
31+
$value = _Float::get($data->getBytes());
3032
} else {
3133
$value = $cpInfo[$data->getStringIndex()];
3234
}

src/Kernel/Mnemonics/_ldc2_w.php

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

4+
use PHPJava\Kernel\Types\_Double;
5+
use PHPJava\Kernel\Types\_Long;
6+
47
final class _ldc2_w implements OperationInterface
58
{
69
use \PHPJava\Kernel\Core\Accumulator;
@@ -9,9 +12,15 @@ final class _ldc2_w implements OperationInterface
912
public function execute(): void
1013
{
1114
$cpInfo = $this->getConstantPool();
12-
1315
$data = $cpInfo[$this->readUnsignedShort()];
1416

15-
$this->pushToOperandStack($data->getBytes());
17+
$value = null;
18+
if (($data instanceof \PHPJava\Kernel\Structures\_Long)) {
19+
$value = _Long::get($data->getBytes());
20+
} elseif ($data instanceof \PHPJava\Kernel\Structures\_Double) {
21+
$value = _Double::get($data->getBytes());
22+
}
23+
24+
$this->pushToOperandStack($value);
1625
}
1726
}

0 commit comments

Comments
 (0)