Skip to content

Commit 072342e

Browse files
committed
Update for ambiguous types
1 parent 2adea3e commit 072342e

20 files changed

Lines changed: 135 additions & 24 deletions

src/Core/JVM/DynamicAccessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class DynamicAccessor implements AccessorInterface
1919
*/
2020
private $methodAccessor;
2121

22-
public function __construct(JavaClassInvoker $invoker, array $methods)
22+
public function __construct(JavaClassInvoker $invoker, array $methods, array $options = [])
2323
{
24-
$this->methodAccessor = new DynamicMethodInvoker($invoker, $methods);
24+
$this->methodAccessor = new DynamicMethodInvoker($invoker, $methods, $options);
2525
$this->fieldAccessor = new DynamicField($invoker, []);
2626
}
2727

src/Core/JVM/Invoker/Invokable.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ trait Invokable
2626
{
2727
private $javaClassInvoker;
2828
private $methods = [];
29+
private $options = [];
2930

30-
public function __construct(JavaClassInvoker $javaClassInvoker, array $methods)
31+
public function __construct(JavaClassInvoker $javaClassInvoker, array $methods, array $options = [])
3132
{
3233
$this->javaClassInvoker = $javaClassInvoker;
3334
$this->methods = $methods;
35+
$this->options = $options;
3436
}
3537

3638
/**
@@ -104,15 +106,36 @@ function ($argument) {
104106
break;
105107
}
106108
$constantPool = ($currentConstantPool = $methodReference->getConstantPool())->getEntries();
109+
$formattedArguments = Formatter::parseSignature(
110+
$constantPool[$methodReference->getDescriptorIndex()]->getString()
111+
)['arguments'];
112+
113+
// does not strict mode can be PHP types
114+
if ((!$this->options['strict'] ?? false)) {
115+
$formattedArguments = Formatter::signatureConvertToAmbiguousForPHP($formattedArguments);
116+
}
117+
107118
/**
108119
* @var _MethodInfo $methodReference
109120
*/
110-
$methodSignature = Formatter::buildArgumentsSignature(
111-
Formatter::parseSignature($constantPool[$methodReference->getDescriptorIndex()]->getString())['arguments']
112-
);
113-
if ($methodSignature === $convertedPassedArguments) {
114-
$method = $methodReference;
115-
break;
121+
$methodSignature = Formatter::buildArgumentsSignature($formattedArguments);
122+
123+
if ((!$this->options['validation']['method']['arguments_count_only'] ?? false)) {
124+
if ($methodSignature === $convertedPassedArguments) {
125+
$method = $methodReference;
126+
break;
127+
}
128+
}
129+
if (($this->options['validation']['method']['arguments_count_only'] ?? false) === true) {
130+
$size = count($formattedArguments);
131+
$passedArgumentsSize = count(
132+
$arguments
133+
);
134+
135+
if ($size === $passedArgumentsSize) {
136+
$method = $methodReference;
137+
break;
138+
}
116139
}
117140
}
118141

@@ -157,8 +180,11 @@ function ($argument) {
157180
$mnemonicMap = new OpCode();
158181
$executedCounter = 0;
159182
while ($reader->getOffset() < $codeAttribute->getOpCodeLength()) {
160-
if (++$executedCounter > \PHPJava\Core\JVM\Parameters\Invoker::MAX_STACK_EXCEEDED) {
161-
throw new RuntimeException('Max stack exceeded. PHPJava has been stopped by safety guard. Maybe Java class has illegal program counter, stacks, or OpCode.');
183+
if (++$executedCounter > ($this->options['max_stack_exceeded'] ?? \PHPJava\Core\JVM\Parameters\Invoker::MAX_STACK_EXCEEDED)) {
184+
throw new RuntimeException(
185+
'Max stack exceeded. PHPJava has been stopped by safety guard.' .
186+
' Maybe Java class has illegal program counter, stacks, or OpCode.'
187+
);
162188
}
163189
$opcode = $reader->readUnsignedByte();
164190
$mnemonic = $mnemonicMap->getName($opcode);

src/Core/JVM/StaticAccessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class StaticAccessor implements AccessorInterface
1919
*/
2020
private $methodAccessor;
2121

22-
public function __construct(JavaClassInvoker $invoker, array $methods)
22+
public function __construct(JavaClassInvoker $invoker, array $methods, array $options = [])
2323
{
24-
$this->methodAccessor = new StaticMethodInvoker($invoker, $methods);
24+
$this->methodAccessor = new StaticMethodInvoker($invoker, $methods, $options);
2525
$this->fieldAccessor = new StaticField($invoker, []);
2626
}
2727

src/Core/JavaClass.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ class JavaClass
7171

7272
private $superClass;
7373

74+
private $options = [];
75+
7476
/**
7577
* JavaClass constructor.
7678
* @param JavaClassReaderInterface $reader
79+
* @param array $options
7780
* @throws ValidatorException
7881
* @throws \PHPJava\Exceptions\ReadEntryException
7982
* @throws \PHPJava\Imitation\java\lang\ClassNotFoundException
8083
*/
81-
public function __construct(JavaClassReaderInterface $reader)
84+
public function __construct(JavaClassReaderInterface $reader, array $options = [])
8285
{
8386
// Validate Java file
8487
if (!(new MagicByte($reader->getBinaryReader()->readUnsignedInt()))->isValid()) {
@@ -162,7 +165,10 @@ public function __construct(JavaClassReaderInterface $reader)
162165
}
163166
}
164167

165-
$this->invoker = new JavaClassInvoker($this);
168+
$this->invoker = new JavaClassInvoker(
169+
$this,
170+
$options
171+
);
166172
}
167173

168174
public function __debugInfo()

src/Core/JavaClassInvoker.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ class JavaClassInvoker
4141

4242
private $specialInvoked = [];
4343

44+
private $options = [];
45+
4446
/**
45-
* JavaClassInvoker constructor.
4647
* @param JavaClass $javaClass
48+
* @param array $options
4749
*/
48-
public function __construct(JavaClass $javaClass)
50+
public function __construct(JavaClass $javaClass, array $options)
4951
{
5052
$this->javaClass = $javaClass;
53+
$this->options = $options;
5154
$cpInfo = $javaClass->getConstantPool()->getEntries();
5255

5356
foreach ($javaClass->getMethods() as $methodInfo) {
@@ -78,12 +81,14 @@ public function __construct(JavaClass $javaClass)
7881

7982
$this->dynamicAccessor = new DynamicAccessor(
8083
$this,
81-
$this->dynamicMethods
84+
$this->dynamicMethods,
85+
$this->options
8286
);
8387

8488
$this->staticAccessor = new StaticAccessor(
8589
$this,
86-
$this->staticMethods
90+
$this->staticMethods,
91+
$this->options
8792
);
8893

8994
// call <clinit>
@@ -100,7 +105,8 @@ public function construct(...$arguments): self
100105
{
101106
$this->dynamicAccessor = new DynamicAccessor(
102107
$this,
103-
$this->dynamicMethods
108+
$this->dynamicMethods,
109+
$this->options
104110
);
105111

106112
if (isset($this->dynamicMethods['<init>'])) {

src/Kernel/Mnemonics/_lload_0.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ final class _lload_0 implements OperationInterface
1111

1212
public function execute(): void
1313
{
14-
throw new NotImplementedException(__CLASS__);
14+
$this->pushStack($this->getLocalStorage(0));
1515
}
1616
}

src/Kernel/Mnemonics/_lload_1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ final class _lload_1 implements OperationInterface
1111

1212
public function execute(): void
1313
{
14-
throw new NotImplementedException(__CLASS__);
14+
$this->pushStack($this->getLocalStorage(1));
1515
}
1616
}

src/Kernel/Mnemonics/_lload_2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ final class _lload_2 implements OperationInterface
1111

1212
public function execute(): void
1313
{
14-
throw new NotImplementedException(__CLASS__);
14+
$this->pushStack($this->getLocalStorage(2));
1515
}
1616
}

src/Kernel/Mnemonics/_lload_3.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ final class _lload_3 implements OperationInterface
1111

1212
public function execute(): void
1313
{
14-
throw new NotImplementedException(__CLASS__);
14+
$this->pushStack($this->getLocalStorage(3));
1515
}
1616
}

src/Kernel/Types/Type.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
class Type
77
{
88
private $value = null;
9+
protected $nameInJava = null;
10+
protected $nameInPHP = null;
911

1012
public function __construct($value)
1113
{
@@ -27,6 +29,17 @@ public function getValue()
2729
return $this->value;
2830
}
2931

32+
public function getTypeNameInJava()
33+
{
34+
return $this->nameInJava;
35+
}
36+
37+
38+
public function getTypeNameInPHP()
39+
{
40+
return $this->nameInPHP;
41+
}
42+
3043
public function __toString()
3144
{
3245
return (string) $this->getValue();

0 commit comments

Comments
 (0)