Skip to content

Commit 42fe161

Browse files
committed
WIP commit
1 parent f222e99 commit 42fe161

16 files changed

Lines changed: 335 additions & 344 deletions

File tree

β€Žsrc/bridge/java/lang/_String.phpβ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public function equals($object)
2828

2929
public function toString()
3030
{
31-
32-
return $this->object->getString();
31+
return $this->__toString();
3332

3433
}
3534

β€Žsrc/core/JavaClassInvoker.phpβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public function __construct(JavaClass $javaClass)
5555
}
5656

5757
// call <clinit>
58-
$this->getStaticMethods()->{'<clinit>'}();
58+
if (isset($this->staticMethods['<clinit>'])) {
59+
$this->getStaticMethods()->{'<clinit>'}();
60+
}
5961
}
6062

6163
public function getJavaClass(): JavaClass

β€Žsrc/core/jvm/invoker/Invokable.phpβ€Ž

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPJava\Core\JavaClassInvoker;
66
use PHPJava\Core\JVM\Stream\BinaryReader;
77
use PHPJava\Exceptions\IllegalJavaClassException;
8+
use PHPJava\Exceptions\RuntimeException;
89
use PHPJava\Exceptions\UndefinedMethodException;
910
use PHPJava\Exceptions\UndefinedOpCodeException;
1011
use PHPJava\Kernel\Attributes\AttributeInfo;
@@ -29,6 +30,17 @@ public function __construct(JavaClassInvoker $javaClassInvoker, array $methods)
2930

3031
public function __call($name, $arguments)
3132
{
33+
$getCodeAttribute = function ($attributes) {
34+
foreach ($attributes as $attribute) {
35+
/**
36+
* @var AttributeInfo $attribute
37+
*/
38+
if ($attribute->getAttributeData() instanceof CodeAttribute) {
39+
return $attribute->getAttributeData();
40+
}
41+
}
42+
return null;
43+
};
3244
/**
3345
* @var _MethodInfo|null $method
3446
*/
@@ -37,7 +49,7 @@ public function __call($name, $arguments)
3749
throw new UndefinedMethodException('Undefined ' . $name . ' method.');
3850
}
3951

40-
$codeAttribute = $this->getCodeAttribute($method->getAttributes());
52+
$codeAttribute = $getCodeAttribute($method->getAttributes());
4153

4254
if ($codeAttribute === null) {
4355
throw new IllegalJavaClassException('Java class does not having code attribution.');
@@ -47,20 +59,34 @@ public function __call($name, $arguments)
4759
fwrite($handle, $codeAttribute->getCode());
4860
rewind($handle);
4961

62+
// debug code attribution with HEX
63+
// var_dump(implode(' ', str_split(bin2hex($codeAttribute->getCode()), 2)));
64+
5065
$reader = new BinaryReader($handle);
5166

52-
$localStorage = array_slice($arguments, 0, 4);
67+
$localStorage = [
68+
$arguments[0] ?? null,
69+
$arguments[1] ?? null,
70+
$arguments[2] ?? null,
71+
$arguments[3] ?? null,
72+
];
5373

5474
$stacks = [];
5575
$opcodeMap = new OpCode();
76+
$executedCounter = 0;
5677
while ($reader->getOffset() < $codeAttribute->getOpCodeLength()) {
78+
if (++$executedCounter > \PHPJava\Core\JVM\Parameter\Invoker::MAX_STACK_EXCEEDED) {
79+
throw new RuntimeException('Max stack exceeded. PHPJava has been stopped by safety guard. Maybe Java class has illegal program counter, stacks, or OpCode.');
80+
}
5781
$cursor = $reader->readUnsignedByte();
5882
$opcode = $opcodeMap->getName($cursor);
5983
if ($opcode === null) {
6084
throw new UndefinedOpCodeException('Undefined OpCode ' . sprintf('0x%X', $cursor) . '.');
6185
}
6286
$pointer = $reader->getOffset() - 1;
6387

88+
var_dump($cursor, $pointer);
89+
6490
$fullName = '\\PHPJava\\Kernel\\OpCode\\' . $opcode;
6591

6692
echo 'Mnemonic: ' . $opcode . "\n";
@@ -80,17 +106,4 @@ public function __call($name, $arguments)
80106

81107
return null;
82108
}
83-
84-
private function getCodeAttribute(array $attributes): ?CodeAttribute
85-
{
86-
foreach ($attributes as $attribute) {
87-
/**
88-
* @var AttributeInfo $attribute
89-
*/
90-
if ($attribute->getAttributeData() instanceof CodeAttribute) {
91-
return $attribute->getAttributeData();
92-
}
93-
}
94-
return null;
95-
}
96109
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace PHPJava\Core\JVM\Parameter;
3+
4+
final class Invoker
5+
{
6+
const MAX_STACK_EXCEEDED = 1000;
7+
}

β€Žsrc/core/jvm/stream/BinaryReader.phpβ€Ž

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace PHPJava\Core\JVM\Stream;
33

4+
use PHPJava\Utilities\BinaryTool;
5+
46
class BinaryReader
57
{
68
private $handle;
@@ -39,12 +41,12 @@ public function readUnsignedShort()
3941

4042
public function readInt()
4143
{
42-
return hexdec(bin2hex($this->read(4)));
44+
return current(unpack('l', $this->read(4)));
4345
}
4446

4547
public function readShort()
4648
{
47-
$short = (int) sprintf('%u', hexdec(bin2hex($this->read(2))));
49+
$short = $this->readUnsignedShort();
4850
return (($short & 0x8000) > 0) ? ($short - 0xFFFF - 1) : $short ;
4951
}
5052

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace PHPJava\Exceptions;
3+
4+
class RuntimeException extends \Exception
5+
{
6+
}

β€Žsrc/kernel/core/Accumulator.phpβ€Ž

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function setParameters(
3535
$this->javaClassInvoker = $javaClassInvoker;
3636
$this->javaClass = $javaClassInvoker->getJavaClass();
3737
$this->reader = $reader;
38-
$this->localStorage = $localStorage;
38+
$this->localStorage = &$localStorage;
3939
$this->stacks = &$stacks;
4040
$this->pointer = $pointer;
4141
return $this;
@@ -144,9 +144,6 @@ public function setLocalStorage($index, $value)
144144

145145
public function getLocalStorage($index)
146146
{
147-
if (!isset($this->localStorage[(int) $index])) {
148-
$this->localStorage[(int) $index] = null;
149-
}
150147
return $this->localStorage[(int) $index];
151148
}
152149

β€Žsrc/kernel/opcode/_getstatic.phpβ€Ž

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ public function execute(): void
2020

2121
$signature = Formatter::parseSignature($cpInfo[$cpInfo[$cp->getNameAndTypeIndex()]->getDescriptorIndex()]->getString());
2222

23+
if (isset($signature[0]['className'])) {
24+
$javaObjectName = str_replace('/', '\\', $signature[0]['className']);
25+
26+
if ($javaObjectName === 'java\\lang\\String') {
27+
// For PHP
28+
$javaObjectName = 'java\\lang\\_String';
29+
}
30+
$className = '\\PHPJava\\Bridge\\' . $javaObjectName;
31+
$this->pushStack(new $className());
32+
return;
33+
}
34+
2335
foreach ($this->javaClass->getFields() as $field) {
2436
if ($cpInfo[$field->getNameIndex()]->getString() === $cpInfo[$cpInfo[$cp->getNameAndTypeIndex()]->getNameIndex()]->getString()) {
2537

@@ -29,12 +41,6 @@ public function execute(): void
2941
return;
3042
}
3143
}
32-
33-
if (isset($signature[0]['className'])) {
34-
$className = '\\PHPJava\\Bridge\\' . str_replace('/', '\\', $signature[0]['className']);
35-
$this->pushStack(new $className());
36-
return;
37-
}
3844

3945
throw new \Exception('γ«γ‚ƒγ€œγ‚“');
4046
}

β€Žsrc/kernel/opcode/_invokespecial.phpβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function execute(): void
3030
}
3131

3232
$methodName = $cpInfo[$nameAndTypeIndex->getNameIndex()]->getString();
33+
var_dump($methodName);
3334

3435
// $result = call_user_func_array(
3536
// [$this->javaClassInvoker->getDynamicMethods(), $methodName],

β€Žsrc/kernel/opcode/_invokevirtual.phpβ€Ž

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ public function execute(): void
3535
}
3636

3737
$invokerClassName = '\\PHPJava\\Bridge\\' . $javaObjectName;
38-
3938
$result = call_user_func_array([
4039
new $invokerClassName,
4140
$cpInfo[$cpInfo[$cp->getNameAndTypeIndex()]->getNameIndex()]->getString()
42-
4341
], $arguments);
4442

4543
if ($signature[0]['type'] !== 'void') {

0 commit comments

Comments
Β (0)