Skip to content

Commit da8a02e

Browse files
committed
WIP commit
1 parent cc45500 commit da8a02e

19 files changed

Lines changed: 186 additions & 121 deletions

src/core/JavaClass.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,9 @@ public function getMethods(): array
133133
{
134134
return $this->activeMethods->getEntries();
135135
}
136+
137+
public function getInvoker(): JavaClassInvoker
138+
{
139+
return new JavaClassInvoker($this);
140+
}
136141
}

src/core/JavaClassInvoker.php

Lines changed: 92 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class JavaClassInvoker
2727

2828
private $debugTraces = [];
2929

30+
private static $singleton = [];
31+
3032
public function __construct(JavaClass $javaClass)
3133
{
3234
$this->javaClass = $javaClass;
@@ -104,106 +106,108 @@ public function debug(): void
104106
{
105107
$cpInfo = $this->getJavaClass()->getConstantPool()->getEntries();
106108

107-
$methodAccessFlags = $this->debugTraces['method']->getAccessFlag();
108-
$accessFlags = [];
109-
$accessFlag = new AccessFlag();
110-
foreach ($accessFlag->getValues() as $value) {
111-
if (($methodAccessFlags & $value) !== 0) {
112-
$accessFlags[] = strtolower(str_replace('_', '', $accessFlag->getName($value)));
109+
foreach ($this->debugTraces as $debugTraces) {
110+
$methodAccessFlags = $debugTraces['method']->getAccessFlag();
111+
$accessFlags = [];
112+
$accessFlag = new AccessFlag();
113+
foreach ($accessFlag->getValues() as $value) {
114+
if (($methodAccessFlags & $value) !== 0) {
115+
$accessFlags[] = strtolower(str_replace('_', '', $accessFlag->getName($value)));
116+
}
113117
}
114-
}
115118

116-
$methodName = $cpInfo[$this->debugTraces['method']->getNameIndex()]->getString();
117-
$descriptor = Formatter::parseSignature($cpInfo[$this->debugTraces['method']->getDescriptorIndex()]->getString());
118-
$formattedArguments = str_replace(
119-
'/',
120-
'.',
119+
$methodName = $cpInfo[$debugTraces['method']->getNameIndex()]->getString();
120+
$descriptor = Formatter::parseSignature($cpInfo[$debugTraces['method']->getDescriptorIndex()]->getString());
121+
$formattedArguments = str_replace(
122+
'/',
123+
'.',
121124
implode(
122-
', ',
123-
array_map(
124-
function ($argument) {
125-
$arrayBrackets = str_repeat('[]', $argument['deep_array']);
126-
if ($argument['type'] === 'class') {
127-
return $argument['class_name'] . $arrayBrackets;
128-
}
129-
return $argument['type'] . $arrayBrackets;
130-
},
131-
$descriptor['arguments']
125+
', ',
126+
array_map(
127+
function ($argument) {
128+
$arrayBrackets = str_repeat('[]', $argument['deep_array']);
129+
if ($argument['type'] === 'class') {
130+
return $argument['class_name'] . $arrayBrackets;
131+
}
132+
return $argument['type'] . $arrayBrackets;
133+
},
134+
$descriptor['arguments']
135+
)
132136
)
133-
)
134-
);
137+
);
135138

136139

137-
$type = $descriptor[0]['type'];
138-
if ($type === 'class') {
139-
$type = $descriptor[0]['class_name'];
140-
}
140+
$type = $descriptor[0]['type'];
141+
if ($type === 'class') {
142+
$type = $descriptor[0]['class_name'];
143+
}
141144

142-
$methodAccessibility = implode(' ', $accessFlags);
143-
144-
printf("[method]\n");
145-
printf(ltrim("$methodAccessibility $type $methodName($formattedArguments)\n", ' '));
146-
printf("\n");
147-
printf("[code]\n");
148-
149-
$codeCounter = 0;
150-
printf(
151-
"%s\n",
152-
implode(
153-
"\n",
154-
array_map(
155-
function ($codes) use (&$codeCounter) {
156-
return implode(
157-
' ',
158-
array_map(
159-
function ($code) use (&$codeCounter) {
160-
$isMnemonic = in_array($codeCounter, $this->debugTraces['mnemonic_indexes']);
161-
$codeCounter++;
162-
return ($isMnemonic ? "\e[1m\e[35m" : "") . "<0x{$code}>" . ($isMnemonic ? "\e[m" : "");
163-
},
164-
$codes
165-
)
166-
);
167-
},
168-
array_chunk(str_split(bin2hex($this->debugTraces['raw_code']), 2), 20)
169-
)
170-
)
171-
);
172-
printf("\n");
173-
printf("[executed]\n");
174-
175-
printf(
176-
"% 8s | %-6.6s | %-20.20s | %-10.10s | %-15.15s\n",
177-
"PC",
178-
"OPCODE",
179-
"MNEMONIC",
180-
"OPERANDS",
181-
"LOCAL STORAGE"
182-
);
145+
$methodAccessibility = implode(' ', $accessFlags);
183146

184-
$line = sprintf(
185-
"%8s+%8s+%22s+%12s+%17s\n",
186-
"---------",
187-
"--------",
188-
"----------------------",
189-
"------------",
190-
"-----------------"
191-
);
147+
printf("[method]\n");
148+
printf(ltrim("$methodAccessibility $type $methodName($formattedArguments)\n", ' '));
149+
printf("\n");
150+
printf("[code]\n");
192151

193-
printf($line);
152+
$codeCounter = 0;
153+
printf(
154+
"%s\n",
155+
implode(
156+
"\n",
157+
array_map(
158+
function ($codes) use (&$codeCounter, &$debugTraces) {
159+
return implode(
160+
' ',
161+
array_map(
162+
function ($code) use (&$codeCounter, &$debugTraces) {
163+
$isMnemonic = in_array($codeCounter, $debugTraces['mnemonic_indexes']);
164+
$codeCounter++;
165+
return ($isMnemonic ? "\e[1m\e[35m" : "") . "<0x{$code}>" . ($isMnemonic ? "\e[m" : "");
166+
},
167+
$codes
168+
)
169+
);
170+
},
171+
array_chunk(str_split(bin2hex($debugTraces['raw_code']), 2), 20)
172+
)
173+
)
174+
);
175+
printf("\n");
176+
printf("[executed]\n");
194177

195-
foreach ($this->debugTraces['executed'] as [$opcode, $mnemonic, $localStorage, $stacks, $pointer]) {
196178
printf(
197-
"% 8s | 0x%02X | %-20.20s | %-10.10s | %-15.15s\n",
198-
(int) $pointer,
199-
$opcode,
200-
// Remove prefix
201-
ltrim($mnemonic, '_'),
202-
count($stacks),
203-
count($localStorage)
179+
"% 8s | %-6.6s | %-20.20s | %-10.10s | %-15.15s\n",
180+
"PC",
181+
"OPCODE",
182+
"MNEMONIC",
183+
"OPERANDS",
184+
"LOCAL STORAGE"
204185
);
205-
}
206186

207-
printf($line);
187+
$line = sprintf(
188+
"%8s+%8s+%22s+%12s+%17s\n",
189+
"---------",
190+
"--------",
191+
"----------------------",
192+
"------------",
193+
"-----------------"
194+
);
195+
196+
printf($line);
197+
198+
foreach ($debugTraces['executed'] as [$opcode, $mnemonic, $localStorage, $stacks, $pointer]) {
199+
printf(
200+
"% 8s | 0x%02X | %-20.20s | %-10.10s | %-15.15s\n",
201+
(int) $pointer,
202+
$opcode,
203+
// Remove prefix
204+
ltrim($mnemonic, '_'),
205+
count($stacks),
206+
count($localStorage)
207+
);
208+
}
209+
210+
printf($line);
211+
}
208212
}
209213
}

src/core/jvm/invoker/Invokable.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public function __call($name, $arguments)
6262
rewind($handle);
6363

6464
// debug code attribution with HEX
65-
$this->debugTraces['raw_code'] = $codeAttribute->getCode();
66-
$this->debugTraces['method'] = $method;
67-
$this->debugTraces['mnemonic_indexes'] = [];
68-
$this->debugTraces['executed'] = [];
65+
$debugTraces['raw_code'] = $codeAttribute->getCode();
66+
$debugTraces['method'] = $method;
67+
$debugTraces['mnemonic_indexes'] = [];
68+
$debugTraces['executed'] = [];
6969

7070
$reader = new BinaryReader($handle);
7171
$localStorage = [
@@ -90,8 +90,8 @@ public function __call($name, $arguments)
9090
$pointer = $reader->getOffset() - 1;
9191

9292
$fullName = '\\PHPJava\\Kernel\\Mnemonics\\' . $mnemonic;
93-
$this->debugTraces['executed'][] = [$opcode, $mnemonic, $localStorage, $stacks, $pointer];
94-
$this->debugTraces['mnemonic_indexes'][] = $pointer;
93+
$debugTraces['executed'][] = [$opcode, $mnemonic, $localStorage, $stacks, $pointer];
94+
$debugTraces['mnemonic_indexes'][] = $pointer;
9595

9696
/**
9797
* @var OperationInterface|Accumulator|ConstantPool $executor
@@ -102,10 +102,12 @@ public function __call($name, $arguments)
102102
$returnValue = $executor->execute();
103103

104104
if ($returnValue !== null) {
105+
$this->debugTraces[] = $debugTraces;
105106
return $returnValue;
106107
}
107108
}
108109

110+
$this->debugTraces[] = $debugTraces;
109111
return null;
110112
}
111113
}

src/core/jvm/parameters/Invoker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
final class Invoker
55
{
6-
const MAX_STACK_EXCEEDED = 1000;
6+
const MAX_STACK_EXCEEDED = PHP_INT_MAX - 1;
77
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace PHPJava\Imitation\java\lang;
3+
4+
class ClassNotFoundException extends ReflectiveOperationException
5+
{
6+
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace PHPJava\Imitation\java\lang;
3+
4+
class Integer extends Number
5+
{
6+
public static function parseInt(string $number, int $radix = null)
7+
{
8+
if ($radix !== null) {
9+
$number = base_convert($number, $radix, 10);
10+
}
11+
return (int) $number;
12+
}
13+
}

src/imitation/java/lang/Number.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace PHPJava\Imitation\java\lang;
3+
4+
class Number extends _Object
5+
{
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace PHPJava\Imitation\java\lang;
3+
4+
class ReflectiveOperationException extends Exception
5+
{
6+
7+
}

src/imitation/java/lang/Throwable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44
class Throwable extends \Exception
55
{
66
use \PHPJava\Imitation\PHPJava\Extended\_Object;
7+
8+
public function __toString(): string
9+
{
10+
return $this->getMessage();
11+
}
712
}

src/kernel/core/Accumulator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ 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+
}
147150
return $this->localStorage[(int) $index];
148151
}
149152

0 commit comments

Comments
 (0)