|
21 | 21 | use PHPJava\Utilities\Formatter; |
22 | 22 | use PHPJava\Utilities\SuperClassResolver; |
23 | 23 | use PHPJava\Utilities\TypeResolver; |
| 24 | +use PHPJava\Core\JVM\Parameters\Runtime; |
24 | 25 |
|
25 | 26 | trait Invokable |
26 | 27 | { |
27 | 28 | private $javaClassInvoker; |
28 | 29 | private $methods = []; |
| 30 | + private $options = []; |
29 | 31 |
|
30 | | - public function __construct(JavaClassInvoker $javaClassInvoker, array $methods) |
| 32 | + public function __construct(JavaClassInvoker $javaClassInvoker, array $methods, array $options = []) |
31 | 33 | { |
32 | 34 | $this->javaClassInvoker = $javaClassInvoker; |
33 | 35 | $this->methods = $methods; |
| 36 | + $this->options = $options; |
34 | 37 | } |
35 | 38 |
|
36 | 39 | /** |
@@ -104,15 +107,36 @@ function ($argument) { |
104 | 107 | break; |
105 | 108 | } |
106 | 109 | $constantPool = ($currentConstantPool = $methodReference->getConstantPool())->getEntries(); |
| 110 | + $formattedArguments = Formatter::parseSignature( |
| 111 | + $constantPool[$methodReference->getDescriptorIndex()]->getString() |
| 112 | + )['arguments']; |
| 113 | + |
| 114 | + // does not strict mode can be PHP types |
| 115 | + if (!($this->options['strict'] ?? Runtime::STRICT)) { |
| 116 | + $formattedArguments = Formatter::signatureConvertToAmbiguousForPHP($formattedArguments); |
| 117 | + } |
| 118 | + |
107 | 119 | /** |
108 | 120 | * @var _MethodInfo $methodReference |
109 | 121 | */ |
110 | | - $methodSignature = Formatter::buildArgumentsSignature( |
111 | | - Formatter::parseSignature($constantPool[$methodReference->getDescriptorIndex()]->getString())['arguments'] |
112 | | - ); |
113 | | - if ($methodSignature === $convertedPassedArguments) { |
114 | | - $method = $methodReference; |
115 | | - break; |
| 122 | + $methodSignature = Formatter::buildArgumentsSignature($formattedArguments); |
| 123 | + |
| 124 | + if (!($this->options['validation']['method']['arguments_count_only'] ?? Runtime::VALIDATION_METHOD_ARGUMENTS_COUNT_ONLY)) { |
| 125 | + if ($methodSignature === $convertedPassedArguments) { |
| 126 | + $method = $methodReference; |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + if (($this->options['validation']['method']['arguments_count_only'] ?? Runtime::VALIDATION_METHOD_ARGUMENTS_COUNT_ONLY) === true) { |
| 131 | + $size = count($formattedArguments); |
| 132 | + $passedArgumentsSize = count( |
| 133 | + $arguments |
| 134 | + ); |
| 135 | + |
| 136 | + if ($size === $passedArgumentsSize) { |
| 137 | + $method = $methodReference; |
| 138 | + break; |
| 139 | + } |
116 | 140 | } |
117 | 141 | } |
118 | 142 |
|
@@ -157,8 +181,11 @@ function ($argument) { |
157 | 181 | $mnemonicMap = new OpCode(); |
158 | 182 | $executedCounter = 0; |
159 | 183 | 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.'); |
| 184 | + if (++$executedCounter > ($this->options['max_stack_exceeded'] ?? Runtime::MAX_STACK_EXCEEDED)) { |
| 185 | + throw new RuntimeException( |
| 186 | + 'Max stack exceeded. PHPJava has been stopped by safety guard.' . |
| 187 | + ' Maybe Java class has illegal program counter, stacks, or OpCode.' |
| 188 | + ); |
162 | 189 | } |
163 | 190 | $opcode = $reader->readUnsignedByte(); |
164 | 191 | $mnemonic = $mnemonicMap->getName($opcode); |
|
0 commit comments