Skip to content

Commit ecb76c2

Browse files
committed
WIP
1 parent c8092f7 commit ecb76c2

58 files changed

Lines changed: 250 additions & 35 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
],
1212
"require": {
1313
"php": ">=7.2",
14-
"ext-zip": "*"
14+
"ext-zip": "*",
15+
"monolog/monolog": "^1.24"
1516
},
1617
"autoload": {
1718
"psr-4": {

src/Core/JVM/ActiveAttributes.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@
33

44
use PHPJava\Core\JavaClassReaderInterface;
55
use PHPJava\Kernel\Attributes\AttributeInfo;
6+
use PHPJava\Utilities\DebugTool;
67

78
class ActiveAttributes
89
{
910
private $entries = [];
1011
private $reader;
1112

12-
public function __construct(JavaClassReaderInterface $reader, int $entries, ConstantPool $constantPool)
13-
{
13+
public function __construct(
14+
JavaClassReaderInterface $reader,
15+
int $entries,
16+
ConstantPool $constantPool,
17+
DebugTool $debugTool
18+
) {
1419
$this->reader = $reader;
1520
for ($i = 0; $i < $entries; $i++) {
1621
// not implemented, read only
1722
$this->entries[$i] = new AttributeInfo($reader);
1823
$this->entries[$i]->setConstantPool($constantPool);
24+
$this->entries[$i]->setDebugTool($debugTool);
1925
$this->entries[$i]->execute();
2026
}
2127
}

src/Core/JVM/ActiveFields.php

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

4+
use PHPJava\Core\JavaClass;
45
use PHPJava\Core\JavaClassReaderInterface;
56
use PHPJava\Kernel\Structures\_FieldInfo;
7+
use PHPJava\Utilities\DebugTool;
68

79
class ActiveFields
810
{
911
private $entries = [];
1012
private $reader;
1113

12-
public function __construct(JavaClassReaderInterface $reader, int $entries, ConstantPool $constantPool)
13-
{
14+
public function __construct(
15+
JavaClassReaderInterface $reader,
16+
int $entries,
17+
ConstantPool $constantPool,
18+
DebugTool $debugTool
19+
) {
1420
$this->reader = $reader;
1521
for ($i = 0; $i < $entries; $i++) {
1622
$this->entries[$i] = new _FieldInfo($reader);
1723
$this->entries[$i]->setConstantPool($constantPool);
24+
$this->entries[$i]->setDebugTool($debugTool);
1825
$this->entries[$i]->execute();
1926
}
2027
}

src/Core/JVM/ActiveInterface.php

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

4+
use PHPJava\Core\JavaClass;
45
use PHPJava\Core\JavaClassReaderInterface;
6+
use PHPJava\Utilities\DebugTool;
57

68
class ActiveInterface
79
{
810
private $entries = [];
911
private $reader;
1012

11-
public function __construct(JavaClassReaderInterface $reader, int $entries, ConstantPool $constantPool)
12-
{
13+
public function __construct(
14+
JavaClassReaderInterface $reader,
15+
int $entries,
16+
ConstantPool $constantPool,
17+
DebugTool $debugTool
18+
) {
1319
$this->reader = $reader;
1420
for ($i = 0; $i < $entries; $i++) {
15-
// not implemented, read only
1621
$this->entries[$i] = $reader->getBinaryReader()->readUnsignedShort();
1722
}
1823
}

src/Core/JVM/ActiveMethods.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@
33

44
use PHPJava\Core\JavaClassReaderInterface;
55
use PHPJava\Kernel\Structures\_MethodInfo;
6+
use PHPJava\Utilities\DebugTool;
67

78
class ActiveMethods
89
{
910
private $entries = [];
1011
private $reader;
1112

12-
public function __construct(JavaClassReaderInterface $reader, int $entries, ConstantPool $constantPool)
13-
{
13+
public function __construct(
14+
JavaClassReaderInterface $reader,
15+
int $entries,
16+
ConstantPool $constantPool,
17+
DebugTool $debugTool
18+
) {
1419
$this->reader = $reader;
1520
for ($i = 0; $i < $entries; $i++) {
1621
$this->entries[$i] = new _MethodInfo($reader);
1722
$this->entries[$i]->setConstantPool($constantPool);
23+
$this->entries[$i]->setDebugTool($debugTool);
1824
$this->entries[$i]->execute();
1925
}
2026
}

src/Core/JVM/Invoker/Invokable.php

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

4+
use Monolog\Handler\StreamHandler;
5+
use Monolog\Logger;
46
use PHPJava\Core\JavaClass;
57
use PHPJava\Core\JavaClassInvoker;
68
use PHPJava\Core\JVM\FlexibleMethod;
@@ -191,7 +193,9 @@ function ($argument) {
191193
$mnemonic = $mnemonicMap->getName($opcode);
192194

193195
if ($mnemonic === null) {
194-
throw new UndefinedOpCodeException('Call to undefined OpCode ' . sprintf('0x%X', $opcode) . '.');
196+
throw new UndefinedOpCodeException(
197+
'Call to undefined OpCode ' . sprintf('0x%X', $opcode) . '.'
198+
);
195199
}
196200
$pointer = $reader->getOffset() - 1;
197201

src/Core/JVM/Parameters/Runtime.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ final class Runtime
99
const STRICT = true;
1010

1111
const VALIDATION_METHOD_ARGUMENTS_COUNT_ONLY = false;
12+
13+
const LOG_PATH = 'php://stdout';
14+
const LOG_LEVEL = 0;
1215
}

src/Core/JavaArchive.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ function ($fileName) {
8383
if (in_array($className, static::IGNORE_FILES)) {
8484
continue;
8585
}
86-
echo "Load " . str_replace('/', '.', $className) . "\n";
8786
$this->classes[str_replace('/', '.', $className)] = new JavaClass(
8887
new JavaClassInlineReader(
8988
$className,

src/Core/JavaClass.php

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPJava\Kernel\Maps\AccessFlag;
1414
use PHPJava\Kernel\Structures\_Utf8;
1515
use PHPJava\Utilities\ClassResolver;
16+
use PHPJava\Utilities\DebugTool;
1617
use PHPJava\Utilities\Formatter;
1718

1819
class JavaClass
@@ -73,6 +74,8 @@ class JavaClass
7374

7475
private $options = [];
7576

77+
private $debugTool;
78+
7679
/**
7780
* JavaClass constructor.
7881
* @param JavaClassReaderInterface $reader
@@ -88,26 +91,43 @@ public function __construct(JavaClassReaderInterface $reader, array $options = [
8891
throw new ValidatorException($reader . ' has broken or not Java class.');
8992
}
9093

94+
// options
95+
$this->options = $options;
96+
97+
// Debug tool
98+
$this->debugTool = new DebugTool(
99+
$reader->getJavaPathName(),
100+
$options
101+
);
102+
103+
$this->debugTool->getLogger()->debug('Start emulation');
104+
91105
// read minor version
92106
$this->versions['minor'] = $reader->getBinaryReader()->readUnsignedShort();
93107

108+
$this->debugTool->getLogger()->debug('Minor version: ' . $this->versions['minor']);
109+
94110
// read major version
95111
$this->versions['major'] = $reader->getBinaryReader()->readUnsignedShort();
96112

113+
$this->debugTool->getLogger()->debug('Major version: ' . $this->versions['minor']);
114+
97115
// read constant pool size
98116
$this->constantPool = new ConstantPool(
99117
$reader,
100118
$reader->getBinaryReader()->readUnsignedShort()
101119
);
102120

121+
$constantPoolEntries = $this->constantPool->getEntries();
122+
123+
$this->debugTool->getLogger()->debug('Constant Pools: ' . count($constantPoolEntries));
124+
103125
// read access flag
104126
$this->accessFlag = $reader->getBinaryReader()->readUnsignedShort();
105127

106128
// read this class
107129
$this->thisClass = $reader->getBinaryReader()->readUnsignedShort();
108130

109-
$constantPoolEntries = $this->constantPool->getEntries();
110-
111131
$this->className = $constantPoolEntries[$constantPoolEntries[$this->thisClass]->getClassIndex()];
112132

113133
// read super class
@@ -132,30 +152,42 @@ public function __construct(JavaClassReaderInterface $reader, array $options = [
132152
$this->activeInterfaces = new ActiveInterface(
133153
$reader,
134154
$reader->getBinaryReader()->readUnsignedShort(),
135-
$this->constantPool
155+
$this->constantPool,
156+
$this->debugTool
136157
);
137158

159+
$this->debugTool->getLogger()->debug('Extracted interfaces: ' . count($this->activeInterfaces->getEntries()));
160+
138161
// read fields
139162
$this->activeFields = new ActiveFields(
140163
$reader,
141164
$reader->getBinaryReader()->readUnsignedShort(),
142-
$this->constantPool
165+
$this->constantPool,
166+
$this->debugTool
143167
);
144168

169+
$this->debugTool->getLogger()->debug('Extracted fields: ' . count($this->activeFields->getEntries()));
170+
145171
// read methods
146172
$this->activeMethods = new ActiveMethods(
147173
$reader,
148174
$reader->getBinaryReader()->readUnsignedShort(),
149-
$this->constantPool
175+
$this->constantPool,
176+
$this->debugTool
150177
);
151178

179+
$this->debugTool->getLogger()->debug('Extracted methods: ' . count($this->activeMethods->getEntries()));
180+
152181
// read Attributes
153182
$this->activeAttributes = new ActiveAttributes(
154183
$reader,
155184
$reader->getBinaryReader()->readUnsignedShort(),
156-
$this->constantPool
185+
$this->constantPool,
186+
$this->debugTool
157187
);
158188

189+
$this->debugTool->getLogger()->debug('Extracted attributes: ' . count($this->activeAttributes->getEntries()));
190+
159191
foreach ($this->activeAttributes->getEntries() as $entry) {
160192
if ($entry->getAttributeData() instanceof InnerClassesAttribute) {
161193
$this->innerClasses = array_merge(
@@ -171,17 +203,17 @@ public function __construct(JavaClassReaderInterface $reader, array $options = [
171203
);
172204
}
173205

174-
public function __debugInfo()
175-
{
176-
return [
177-
'className' => $this->getClassName(),
178-
'superClass' => get_class($this->getSuperClass()),
179-
'methods' => [
180-
'static' => array_keys($this->invoker->getStatic()->getMethods()->getList()),
181-
'dynamic' => array_keys($this->invoker->getDynamic()->getMethods()->getList()),
182-
],
183-
];
184-
}
206+
// public function __debugInfo()
207+
// {
208+
// return [
209+
// 'className' => $this->getClassName(),
210+
// 'superClass' => get_class($this->getSuperClass()),
211+
// 'methods' => [
212+
// 'static' => array_keys($this->invoker->getStatic()->getMethods()->getList()),
213+
// 'dynamic' => array_keys($this->invoker->getDynamic()->getMethods()->getList()),
214+
// ],
215+
// ];
216+
// }
185217

186218
public function getClassName(bool $shortName = false): string
187219
{

src/Core/JavaClassFileReader.php

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

66
class JavaClassFileReader implements JavaClassReaderInterface
77
{
8+
private $fileName;
89
private $handle;
910
private $binaryReader;
1011

@@ -31,8 +32,22 @@ public function getBinaryReader(): JVM\Stream\BinaryReader
3132
return $this->binaryReader;
3233
}
3334

34-
public function __toString(): string
35+
public function getJavaPathName(): string
36+
{
37+
return preg_replace(
38+
'/\.class$/',
39+
'',
40+
basename($this->getFileName())
41+
);
42+
}
43+
44+
public function getFileName(): string
3545
{
3646
return stream_get_meta_data($this->handle)['uri'];
3747
}
48+
49+
public function __toString(): string
50+
{
51+
return $this->getFileName();
52+
}
3853
}

0 commit comments

Comments
 (0)