Skip to content

Commit 496722c

Browse files
committed
WIP commit
1 parent da8a02e commit 496722c

5 files changed

Lines changed: 130 additions & 124 deletions

File tree

src/core/JavaClass.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use PHPJava\Core\JVM\ConstantPool;
99
use PHPJava\Core\JVM\Validations\MagicByte;
1010
use PHPJava\Exceptions\ValidatorException;
11+
use PHPJava\Kernel\Maps\AccessFlag;
1112
use PHPJava\Kernel\Structures\_Utf8;
13+
use PHPJava\Utilities\Formatter;
1214

1315
class JavaClass
1416
{
@@ -53,6 +55,8 @@ class JavaClass
5355
*/
5456
private $className = null;
5557

58+
private $debugTraces = [];
59+
5660
/**
5761
* JavaClass constructor.
5862
* @param JavaClassReader $reader
@@ -138,4 +142,121 @@ public function getInvoker(): JavaClassInvoker
138142
{
139143
return new JavaClassInvoker($this);
140144
}
145+
146+
public function appendDebug($log)
147+
{
148+
$this->debugTraces[] = $log;
149+
return $this;
150+
}
151+
152+
public function debug(): void
153+
{
154+
$cpInfo = $this->getConstantPool()->getEntries();
155+
foreach ($this->debugTraces as $debugTraces) {
156+
$methodAccessFlags = $debugTraces['method']->getAccessFlag();
157+
$accessFlags = [];
158+
$accessFlag = new AccessFlag();
159+
foreach ($accessFlag->getValues() as $value) {
160+
if (($methodAccessFlags & $value) !== 0) {
161+
$accessFlags[] = strtolower(str_replace('_', '', $accessFlag->getName($value)));
162+
}
163+
}
164+
165+
$methodName = $cpInfo[$debugTraces['method']->getNameIndex()]->getString();
166+
$descriptor = Formatter::parseSignature($cpInfo[$debugTraces['method']->getDescriptorIndex()]->getString());
167+
$formattedArguments = str_replace(
168+
'/',
169+
'.',
170+
implode(
171+
', ',
172+
array_map(
173+
function ($argument) {
174+
$arrayBrackets = str_repeat('[]', $argument['deep_array']);
175+
if ($argument['type'] === 'class') {
176+
return $argument['class_name'] . $arrayBrackets;
177+
}
178+
return $argument['type'] . $arrayBrackets;
179+
},
180+
$descriptor['arguments']
181+
)
182+
)
183+
);
184+
185+
186+
$type = $descriptor[0]['type'];
187+
if ($type === 'class') {
188+
$type = $descriptor[0]['class_name'];
189+
}
190+
191+
$type = str_replace('/', '.', $type);
192+
193+
$methodAccessibility = implode(' ', $accessFlags);
194+
195+
printf("[method]\n");
196+
printf(ltrim("$methodAccessibility $type $methodName($formattedArguments)\n", ' '));
197+
printf("\n");
198+
printf("[code]\n");
199+
200+
$codeCounter = 0;
201+
printf(
202+
"%s\n",
203+
implode(
204+
"\n",
205+
array_map(
206+
function ($codes) use (&$codeCounter, &$debugTraces) {
207+
return implode(
208+
' ',
209+
array_map(
210+
function ($code) use (&$codeCounter, &$debugTraces) {
211+
$isMnemonic = in_array($codeCounter, $debugTraces['mnemonic_indexes']);
212+
$codeCounter++;
213+
return ($isMnemonic ? "\e[1m\e[35m" : "") . "<0x{$code}>" . ($isMnemonic ? "\e[m" : "");
214+
},
215+
$codes
216+
)
217+
);
218+
},
219+
array_chunk(str_split(bin2hex($debugTraces['raw_code']), 2), 20)
220+
)
221+
)
222+
);
223+
printf("\n");
224+
printf("[executed]\n");
225+
226+
printf(
227+
"% 8s | %-6.6s | %-20.20s | %-10.10s | %-15.15s\n",
228+
"PC",
229+
"OPCODE",
230+
"MNEMONIC",
231+
"OPERANDS",
232+
"LOCAL STORAGE"
233+
);
234+
235+
$line = sprintf(
236+
"%8s+%8s+%22s+%12s+%17s\n",
237+
"---------",
238+
"--------",
239+
"----------------------",
240+
"------------",
241+
"-----------------"
242+
);
243+
244+
printf($line);
245+
246+
foreach ($debugTraces['executed'] as [$opcode, $mnemonic, $localStorage, $stacks, $pointer]) {
247+
printf(
248+
"% 8s | 0x%02X | %-20.20s | %-10.10s | %-15.15s\n",
249+
(int) $pointer,
250+
$opcode,
251+
// Remove prefix
252+
ltrim($mnemonic, '_'),
253+
count($stacks),
254+
count($localStorage)
255+
);
256+
}
257+
258+
printf($line);
259+
printf("\n");
260+
}
261+
}
141262
}

src/core/JavaClassInvoker.php

Lines changed: 4 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ class JavaClassInvoker
2525
private $dynamicFields = [];
2626
private $staticFields = [];
2727

28-
private $debugTraces = [];
29-
30-
private static $singleton = [];
28+
private $debugTraces ;
3129

3230
public function __construct(JavaClass $javaClass)
3331
{
@@ -75,25 +73,22 @@ public function getDynamicMethods(): InvokerInterface
7573
{
7674
return new JVM\Invoker\DynamicMethodInvoker(
7775
$this,
78-
$this->dynamicMethods,
79-
$this->debugTraces
76+
$this->dynamicMethods
8077
);
8178
}
8279

8380
public function getStaticMethods(): InvokerInterface
8481
{
8582
return new JVM\Invoker\StaticMethodInvoker(
8683
$this,
87-
$this->staticMethods,
88-
$this->debugTraces
84+
$this->staticMethods
8985
);
9086
}
9187

9288
public function getDynamicFields(): FieldInterface
9389
{
9490
return new JVM\Field\DynamicField(
95-
$this,
96-
$this->dynamicFields
91+
$this
9792
);
9893
}
9994

@@ -102,112 +97,4 @@ public function getStaticFields(): JVM\Field\StaticField
10297
return new JVM\Field\StaticField();
10398
}
10499

105-
public function debug(): void
106-
{
107-
$cpInfo = $this->getJavaClass()->getConstantPool()->getEntries();
108-
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-
}
117-
}
118-
119-
$methodName = $cpInfo[$debugTraces['method']->getNameIndex()]->getString();
120-
$descriptor = Formatter::parseSignature($cpInfo[$debugTraces['method']->getDescriptorIndex()]->getString());
121-
$formattedArguments = str_replace(
122-
'/',
123-
'.',
124-
implode(
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-
)
136-
)
137-
);
138-
139-
140-
$type = $descriptor[0]['type'];
141-
if ($type === 'class') {
142-
$type = $descriptor[0]['class_name'];
143-
}
144-
145-
$methodAccessibility = implode(' ', $accessFlags);
146-
147-
printf("[method]\n");
148-
printf(ltrim("$methodAccessibility $type $methodName($formattedArguments)\n", ' '));
149-
printf("\n");
150-
printf("[code]\n");
151-
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");
177-
178-
printf(
179-
"% 8s | %-6.6s | %-20.20s | %-10.10s | %-15.15s\n",
180-
"PC",
181-
"OPCODE",
182-
"MNEMONIC",
183-
"OPERANDS",
184-
"LOCAL STORAGE"
185-
);
186-
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-
}
212-
}
213100
}

src/core/jvm/invoker/Invokable.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ trait Invokable
2121
{
2222
private $javaClassInvoker;
2323
private $methods = [];
24-
private $debugTraces;
2524

26-
public function __construct(JavaClassInvoker $javaClassInvoker, array $methods, array &$debugTraces)
25+
public function __construct(JavaClassInvoker $javaClassInvoker, array $methods)
2726
{
2827
$this->javaClassInvoker = $javaClassInvoker;
2928
$this->methods = $methods;
30-
$this->debugTraces = &$debugTraces;
3129
}
3230

3331
public function __call($name, $arguments)
@@ -102,12 +100,12 @@ public function __call($name, $arguments)
102100
$returnValue = $executor->execute();
103101

104102
if ($returnValue !== null) {
105-
$this->debugTraces[] = $debugTraces;
103+
$this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces);
106104
return $returnValue;
107105
}
108106
}
109107

110-
$this->debugTraces[] = $debugTraces;
108+
$this->javaClassInvoker->getJavaClass()->appendDebug($debugTraces);
111109
return null;
112110
}
113111
}

src/core/jvm/invoker/InvokerInterface.php

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

66
interface InvokerInterface
77
{
8-
public function __construct(JavaClassInvoker $javaClassInvoker, array $methods, array &$debugTraces);
8+
public function __construct(JavaClassInvoker $javaClassInvoker, array $methods);
99
public function __call($name, $arguments);
1010
}

tools/test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
);
1111
$javaClass->getInvoker()->getStaticMethods()->main([111, 222, 333]);
1212

13-
$javaClass->getInvoker()->debug();
13+
$javaClass->debug();

0 commit comments

Comments
 (0)