Skip to content

Commit 4c6a9e5

Browse files
authored
Merge pull request #115 from php-java/optimization-part-3
Optimization part 3
2 parents d066bc2 + 9dbf938 commit 4c6a9e5

19 files changed

Lines changed: 237 additions & 152 deletions

src/Core/JVM/ActiveAttributes.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Core/JVM/ActiveInterface.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/Core/JVM/AttributePool.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace PHPJava\Core\JVM;
3+
4+
use PHPJava\Core\Stream\Reader\ReaderInterface;
5+
use PHPJava\Exceptions\ReadOnlyException;
6+
use PHPJava\Kernel\Attributes\AttributeInfo;
7+
use PHPJava\Utilities\DebugTool;
8+
9+
class AttributePool implements \ArrayAccess, \Countable
10+
{
11+
private $entries = [];
12+
private $reader;
13+
14+
public function __construct(
15+
ReaderInterface $reader,
16+
int $entries,
17+
ConstantPool $constantPool,
18+
DebugTool $debugTool
19+
) {
20+
$this->reader = $reader;
21+
for ($i = 0; $i < $entries; $i++) {
22+
// not implemented, read only
23+
$entry = (new AttributeInfo($reader))
24+
->setConstantPool($constantPool)
25+
->setDebugTool($debugTool);
26+
$entry->execute();
27+
28+
$this->entries[] = $entry;
29+
}
30+
}
31+
32+
public function getEntries()
33+
{
34+
return $this->entries;
35+
}
36+
37+
public function offsetExists($offset)
38+
{
39+
return isset($this->entries[$offset]);
40+
}
41+
42+
public function offsetGet($offset)
43+
{
44+
return $this->entries[$offset];
45+
}
46+
47+
public function count()
48+
{
49+
return count($this->entries);
50+
}
51+
52+
public function offsetSet($offset, $value)
53+
{
54+
throw new ReadOnlyException('You cannot rewrite datum. The Attribute Pool is read-only.');
55+
}
56+
57+
public function offsetUnset($offset)
58+
{
59+
throw new ReadOnlyException('You cannot rewrite datum. The Attribute Pool is read-only.');
60+
}
61+
}

src/Core/JVM/ConstantPool.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use PHPJava\Kernel\Structures\_Utf8;
2121
use PHPJava\Kernel\Structures\StructureInterface;
2222

23-
class ConstantPool implements \ArrayAccess
23+
class ConstantPool implements \ArrayAccess, \Countable
2424
{
2525
private $entries = [];
2626
private $reader;
@@ -102,6 +102,11 @@ public function offsetGet($offset)
102102
return $this->entries[$offset];
103103
}
104104

105+
public function count()
106+
{
107+
return count($this->entries);
108+
}
109+
105110
public function offsetSet($offset, $value)
106111
{
107112
throw new ReadOnlyException('You cannot rewrite datum. The Constant Pool is read-only.');
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
use PHPJava\Core\JavaClass;
55
use PHPJava\Core\Stream\Reader\ReaderInterface;
6+
use PHPJava\Exceptions\ReadOnlyException;
67
use PHPJava\Kernel\Structures\_FieldInfo;
78
use PHPJava\Utilities\DebugTool;
89

9-
class ActiveFields
10+
class FieldPool implements \ArrayAccess, \Countable
1011
{
1112
private $entries = [];
1213
private $reader;
@@ -30,4 +31,29 @@ public function getEntries()
3031
{
3132
return $this->entries;
3233
}
34+
35+
public function offsetExists($offset)
36+
{
37+
return isset($this->entries[$offset]);
38+
}
39+
40+
public function offsetGet($offset)
41+
{
42+
return $this->entries[$offset];
43+
}
44+
45+
public function count()
46+
{
47+
return count($this->entries);
48+
}
49+
50+
public function offsetSet($offset, $value)
51+
{
52+
throw new ReadOnlyException('You cannot rewrite datum. The Field Pool is read-only.');
53+
}
54+
55+
public function offsetUnset($offset)
56+
{
57+
throw new ReadOnlyException('You cannot rewrite datum. The Field Pool is read-only.');
58+
}
3359
}

src/Core/JVM/InterfacePool.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
namespace PHPJava\Core\JVM;
3+
4+
use PHPJava\Core\JavaClass;
5+
use PHPJava\Core\Stream\Reader\ReaderInterface;
6+
use PHPJava\Exceptions\ReadOnlyException;
7+
use PHPJava\Utilities\DebugTool;
8+
9+
class InterfacePool implements \ArrayAccess, \Countable
10+
{
11+
private $entries = [];
12+
private $reader;
13+
14+
public function __construct(
15+
ReaderInterface $reader,
16+
int $entries,
17+
ConstantPool $constantPool,
18+
DebugTool $debugTool
19+
) {
20+
$this->reader = $reader;
21+
for ($i = 0; $i < $entries; $i++) {
22+
$this->entries[$i] = $reader->getBinaryReader()->readUnsignedShort();
23+
}
24+
}
25+
26+
public function getEntries()
27+
{
28+
return $this->entries;
29+
}
30+
31+
public function offsetExists($offset)
32+
{
33+
return isset($this->entries[$offset]);
34+
}
35+
36+
public function offsetGet($offset)
37+
{
38+
return $this->entries[$offset];
39+
}
40+
41+
public function count()
42+
{
43+
return count($this->entries);
44+
}
45+
46+
public function offsetSet($offset, $value)
47+
{
48+
throw new ReadOnlyException('You cannot rewrite datum. The Interface Pool is read-only.');
49+
}
50+
51+
public function offsetUnset($offset)
52+
{
53+
throw new ReadOnlyException('You cannot rewrite datum. The Interface Pool is read-only.');
54+
}
55+
}

src/Core/JVM/Invoker/Invokable.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,10 @@ public function __construct(JavaClassInvoker $javaClassInvoker, array $methods,
5656
* @param mixed ...$arguments
5757
* @return null
5858
* @throws IllegalJavaClassException
59-
* @throws NoSuchMethodException
6059
* @throws RuntimeException
61-
* @throws UndefinedMethodException
6260
* @throws UndefinedOpCodeException
6361
* @throws \PHPJava\Exceptions\TypeException
6462
* @throws \PHPJava\Exceptions\UnableToFindAttributionException
65-
* @throws \ReflectionException
6663
*/
6764
public function call(string $name, ...$arguments)
6865
{
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
namespace PHPJava\Core\JVM;
33

44
use PHPJava\Core\Stream\Reader\ReaderInterface;
5+
use PHPJava\Exceptions\ReadOnlyException;
56
use PHPJava\Kernel\Structures\_MethodInfo;
67
use PHPJava\Utilities\DebugTool;
78

8-
class ActiveMethods
9+
class MethodPool implements \ArrayAccess, \Countable
910
{
1011
private $entries = [];
1112
private $reader;
@@ -29,4 +30,29 @@ public function getEntries()
2930
{
3031
return $this->entries;
3132
}
33+
34+
public function offsetExists($offset)
35+
{
36+
return isset($this->entries[$offset]);
37+
}
38+
39+
public function offsetGet($offset)
40+
{
41+
return $this->entries[$offset];
42+
}
43+
44+
public function count()
45+
{
46+
return count($this->entries);
47+
}
48+
49+
public function offsetSet($offset, $value)
50+
{
51+
throw new ReadOnlyException('You cannot rewrite datum. The Interface Pool is read-only.');
52+
}
53+
54+
public function offsetUnset($offset)
55+
{
56+
throw new ReadOnlyException('You cannot rewrite datum. The Interface Pool is read-only.');
57+
}
3258
}

src/Core/JVM/Parameters/Runtime.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ final class Runtime
2121
const LOG_PATH = 'php://stderr';
2222
const LOG_LEVEL = Logger::EMERGENCY;
2323

24+
const LOAD_ATTRIBUTES = [
25+
'Code',
26+
'Exceptions',
27+
'SourceFile',
28+
];
29+
2430
const PHP_PACKAGES_MAPS = [
2531
'String' => '_String',
2632
'Object' => '_Object',

src/Core/JVM/Stream/BinaryReader.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ public function readByte()
3535

3636
public function readUnsignedByte()
3737
{
38-
return (int) sprintf('%u', ord($this->read(1)));
38+
return current(unpack('C', $this->read(1)));
3939
}
4040

4141
public function readUnsignedInt()
4242
{
43-
return base_convert(bin2hex($this->read(4)), 16, 10);
43+
return current(unpack('N', $this->read(4)));
4444
}
4545

4646
public function readUnsignedShort()
4747
{
48-
return (int) sprintf('%u', hexdec(bin2hex($this->read(2))));
48+
return current(unpack('n', $this->read(2)));
4949
}
5050

5151
public function readInt()
@@ -57,16 +57,12 @@ public function readInt()
5757
public function readShort()
5858
{
5959
$short = $this->readUnsignedShort();
60-
return (($short & 0x8000) > 0) ? ($short - 0xFFFF - 1) : $short ;
60+
return (($short & 0x8000) > 0) ? ($short - 0xFFFF - 1) : $short;
6161
}
6262

6363
public function readUnsignedLong()
6464
{
65-
if (PHP_INT_MAX === 2147483647) {
66-
return base_convert(bin2hex($this->read(8)), 16, 10);
67-
}
68-
69-
return (int) sprintf('%u', hexdec(bin2hex($this->read(8))));
65+
return current(unpack('J', $this->read(8)));
7066
}
7167

7268
public function readLong()

0 commit comments

Comments
 (0)