Skip to content

Commit 7b749f1

Browse files
committed
WIP: Pass all tests
1 parent a2e663e commit 7b749f1

File tree

9 files changed

+85
-70
lines changed

9 files changed

+85
-70
lines changed

src/Core/JVM/Intern/StringIntern.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,4 @@ public function offsetUnset($offset)
4040
{
4141
unset($this->entries[$offset]);
4242
}
43-
4443
}

src/Core/JVM/Invoker/Invokable.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPJava\Exceptions\RuntimeException;
1515
use PHPJava\Exceptions\UndefinedMethodException;
1616
use PHPJava\Exceptions\UndefinedOpCodeException;
17+
use PHPJava\Kernel\Provider\DependencyInjectionProvider;
1718
use PHPJava\Packages\java\lang\NoSuchMethodException;
1819
use PHPJava\Kernel\Attributes\AttributeInfo;
1920
use PHPJava\Kernel\Attributes\AttributeInterface;
@@ -149,7 +150,11 @@ function () use ($name, $arguments) {
149150
$executionTime = microtime(true);
150151
$maxExecutionTime = ($this->options['max_execution_time'] ?? GlobalOptions::get('max_execution_time') ?? Runtime::MAX_EXECUTION_TIME);
151152

152-
$methodBeautified = Formatter::beatifyMethodFromConstantPool($method, $currentConstantPool);
153+
$methodBeautified = Formatter::beatifyMethodFromConstantPool(
154+
$method,
155+
$currentConstantPool
156+
);
157+
153158
$this->debugTool->getLogger()->info('Start operations: ' . $methodBeautified);
154159

155160
$localStorage = array_map(
@@ -159,17 +164,33 @@ function ($item) {
159164
$localStorage
160165
);
161166

162-
$this->debugTool->getLogger()->debug(
163-
vsprintf(
164-
'Used Memory: %s, Used Memory Peak: %s',
165-
[
166-
Metric::bytes(memory_get_usage())->format(),
167-
Metric::bytes(memory_get_peak_usage())->format(),
168-
]
167+
$dependencyInjectionProvider = (new DependencyInjectionProvider())
168+
->add(
169+
'ConstantPool',
170+
$currentConstantPool
169171
)
170-
);
172+
->add(
173+
'JavaClass',
174+
$this->javaClassInvoker->getJavaClass()
175+
)
176+
->add(
177+
'JavaClassInvoker',
178+
$this->javaClassInvoker
179+
)
180+
->add(
181+
'Attributes',
182+
$method->getAttributes()
183+
)
184+
->add(
185+
'Options',
186+
$this->options
187+
);
171188

172189
while ($reader->getOffset() < $codeAttribute->getOpCodeLength()) {
190+
$dependencyInjectionProvider
191+
->add('OperandStacks', $stacks)
192+
->add('LocalStorages', $localStorage);
193+
173194
if (++$executedCounter > ($this->options['max_stack_exceeded'] ?? GlobalOptions::get('max_stack_exceeded') ?? Runtime::MAX_STACK_EXCEEDED)) {
174195
throw new RuntimeException(
175196
'Max stack exceeded. PHPJava has been stopped by safety guard.' .
@@ -241,7 +262,8 @@ function () use ($fullName) {
241262
$reader,
242263
$localStorage,
243264
$stacks,
244-
$pointer
265+
$pointer,
266+
$dependencyInjectionProvider
245267
)
246268
->execute();
247269

src/Core/JavaClass.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,8 @@ public function __construct(ReaderInterface $reader, array $options = [])
222222
}
223223
$this->debugTool->getLogger()->info('End of Class');
224224

225-
// Intern provider registration.
226-
$internProvider = new InternProvider();
227-
$internProvider->add(
228-
StringIntern::class,
229-
new StringIntern()
230-
);
231-
232225
$this->invoker = new JavaClassInvoker(
233226
$this,
234-
$internProvider,
235227
$options
236228
);
237229

src/Core/JavaClassInvoker.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PHPJava\Core\JVM\DynamicAccessor;
55
use PHPJava\Core\JVM\Field\FieldInterface;
6+
use PHPJava\Core\JVM\Intern\StringIntern;
67
use PHPJava\Core\JVM\Invoker\Invokable;
78
use PHPJava\Core\JVM\Invoker\InvokerInterface;
89
use PHPJava\Core\JVM\Parameters\GlobalOptions;
@@ -48,21 +49,19 @@ class JavaClassInvoker
4849

4950
private $options = [];
5051

51-
private $internProvider = null;
52+
private $providers = [];
5253

5354
/**
5455
* JavaClassInvoker constructor.
5556
* @param JavaClass $javaClass
56-
* @param InternProvider $internProvider
5757
* @param array $options
5858
*/
5959
public function __construct(
6060
JavaClass $javaClass,
61-
InternProvider $internProvider,
6261
array $options
6362
) {
6463
$this->javaClass = $javaClass;
65-
$this->internProvider = $internProvider;
64+
6665
$this->options = $options;
6766
$cpInfo = $javaClass->getConstantPool();
6867

@@ -92,6 +91,15 @@ public function __construct(
9291
}
9392
}
9493

94+
// Intern provider registration.
95+
$this->providers = [
96+
'InternProvider' => (new InternProvider())
97+
->add(
98+
StringIntern::class,
99+
new StringIntern()
100+
),
101+
];
102+
95103
$this->dynamicAccessor = new DynamicAccessor(
96104
$this,
97105
$this->dynamicMethods,
@@ -176,15 +184,10 @@ public function addToSpecialInvokedList(string $name, string $signature): self
176184
*/
177185
public function getProvider($providerName): ProviderInterface
178186
{
179-
// TODO: Change to be accepted any provider.
180-
$providers = [
181-
'InternProvider' => $this->internProvider,
182-
];
183-
184-
if (!isset($providers[$providerName])) {
187+
if (!isset($this->providers[$providerName])) {
185188
throw new IllegalJavaClassException($providerName . ' not provided.');
186189
}
187190

188-
return $providers[$providerName];
191+
return $this->providers[$providerName];
189192
}
190193
}

src/Kernel/Core/Accumulator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PHPJava\Core\JavaClass;
55
use PHPJava\Core\JavaClassInvoker;
6+
use PHPJava\Kernel\Provider\DependencyInjectionProvider;
67

78
trait Accumulator
89
{
@@ -31,13 +32,19 @@ trait Accumulator
3132
private $stacks = [];
3233
private $options;
3334

35+
/**
36+
* @var DependencyInjectionProvider
37+
*/
38+
private $dependencyInjectionProvider;
39+
3440
public function setParameters(
3541
array $attributes,
3642
JavaClassInvoker $javaClassInvoker,
3743
\PHPJava\Core\JVM\Stream\BinaryReader $reader,
3844
array &$localStorage,
3945
array &$stacks,
40-
int $pointer
46+
int $pointer,
47+
DependencyInjectionProvider $dependencyInjectionProvider
4148
): self {
4249
$this->attributes = $attributes;
4350
$this->javaClassInvoker = $javaClassInvoker;
@@ -47,6 +54,7 @@ public function setParameters(
4754
$this->localStorage = &$localStorage;
4855
$this->stacks = &$stacks;
4956
$this->pointer = $pointer;
57+
$this->dependencyInjectionProvider = $dependencyInjectionProvider;
5058
return $this;
5159
}
5260

src/Kernel/Core/DependencyInjector.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,7 @@ private function getNativeAnnotateInjections(string $phpDocument): array
3030
/**
3131
* @var \phpDocumentor\Reflection\DocBlock\Tags\Generic $native
3232
*/
33-
switch ($type = trim($native->getDescription())) {
34-
case 'ConstantPool':
35-
$injections[] = $this->getConstantPool();
36-
break;
37-
case 'JavaClass':
38-
$injections[] = $this->javaClass;
39-
break;
40-
case 'JavaClassInvoker':
41-
$injections[] = $this->javaClassInvoker;
42-
break;
43-
case 'Attributes':
44-
$injections[] = $this->getAttributes();
45-
break;
46-
case 'OperandStacks':
47-
$injections[] = $this->getStacks();
48-
break;
49-
case 'LocalStorages':
50-
$injections[] = $this->getLocalStorages();
51-
break;
52-
case 'Options':
53-
$injections[] = $this->options;
54-
break;
55-
default:
56-
throw new NotSupportedInjectionTypesException('Not supported injection type: "' . $type . '"');
57-
}
33+
$injections[] = $this->dependencyInjectionProvider->get($native);
5834
}
5935
return $injections;
6036
}

src/Kernel/Mnemonics/_if_acmpeq.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace PHPJava\Kernel\Mnemonics;
33

4+
use PHPJava\Core\JavaClass;
45
use PHPJava\Exceptions\NotImplementedException;
56
use PHPJava\Kernel\Structures\_String;
67
use PHPJava\Kernel\Structures\_Utf8;
@@ -16,13 +17,21 @@ public function execute(): void
1617
$offset = $this->readShort();
1718

1819
/**
19-
* @var $rightOperand _String|_Utf8
20-
* @var $leftOperand _String|_Utf8
20+
* @var $rightOperand _String|_Utf8|JavaClass
21+
* @var $leftOperand _String|_Utf8|JavaClass
2122
*/
22-
$rightOperand = $this->popFromOperandStack();
23-
$leftOperand = $this->popFromOperandStack();
23+
$rightComparingValue = $rightOperand = $this->popFromOperandStack();
24+
$leftComparingValue = $leftOperand = $this->popFromOperandStack();
2425

25-
if ($leftOperand->hashCode() === $rightOperand->hashCode()) {
26+
if (method_exists($rightComparingValue, 'hashCode')) {
27+
$rightComparingValue = $rightComparingValue->hashCode();
28+
}
29+
30+
if (method_exists($leftComparingValue, 'hashCode')) {
31+
$leftComparingValue = $leftComparingValue->hashCode();
32+
}
33+
34+
if ($leftComparingValue === $rightComparingValue) {
2635
$this->setOffset($this->getProgramCounter() + $offset);
2736
}
2837
}

src/Kernel/Mnemonics/_if_acmpne.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace PHPJava\Kernel\Mnemonics;
33

4+
use PHPJava\Core\JavaClass;
45
use PHPJava\Exceptions\NotImplementedException;
56
use PHPJava\Kernel\Structures\_Utf8;
67
use PHPJava\Packages\java\lang\_String;
@@ -16,13 +17,21 @@ public function execute(): void
1617
$offset = $this->readShort();
1718

1819
/**
19-
* @var $rightOperand _String|_Utf8
20-
* @var $leftOperand _String|_Utf8
20+
* @var $rightOperand _String|_Utf8|JavaClass
21+
* @var $leftOperand _String|_Utf8|JavaClass
2122
*/
22-
$rightOperand = $this->popFromOperandStack();
23-
$leftOperand = $this->popFromOperandStack();
23+
$rightComparingValue = $rightOperand = $this->popFromOperandStack();
24+
$leftComparingValue = $leftOperand = $this->popFromOperandStack();
2425

25-
if ($leftOperand->hashCode() !== $rightOperand->hashCode()) {
26+
if (method_exists($rightComparingValue, 'hashCode')) {
27+
$rightComparingValue = $rightComparingValue->hashCode();
28+
}
29+
30+
if (method_exists($leftComparingValue, 'hashCode')) {
31+
$leftComparingValue = $leftComparingValue->hashCode();
32+
}
33+
34+
if ($leftComparingValue !== $rightComparingValue) {
2635
$this->setOffset($this->getProgramCounter() + $offset);
2736
}
2837
}

src/Kernel/Mnemonics/_invokevirtual.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ public function execute(): void
5353
...$arguments
5454
);
5555
} else {
56-
$reflectionClass = new \ReflectionClass(
57-
// $realInvokerClass = TypeResolver::convertPHPTypeToJavaType($invokerClass)
58-
$invokerClass
59-
);
56+
$reflectionClass = new \ReflectionClass($invokerClass);
6057
$methodAccessor = $reflectionClass->getMethod($methodName);
6158

6259
if ($document = $methodAccessor->getDocComment()) {

0 commit comments

Comments
 (0)