Skip to content

Commit 6b1ade4

Browse files
committed
Fix bug
1 parent c6c1ae3 commit 6b1ade4

File tree

14 files changed

+287
-22
lines changed

14 files changed

+287
-22
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
"friendsofphp/php-cs-fixer": "^2.14"
3131
},
3232
"scripts": {
33-
"tests": "phpunit tests && phpcs --standard=phpcs.xml src"
33+
"tests": "phpunit tests --stop-on-failure && phpcs --standard=phpcs.xml src"
3434
}
3535
}

src/Core/JVM/Invoker/Invokable.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PHPJava\Kernel\Maps\OpCode;
2222
use PHPJava\Kernel\Mnemonics\OperationInterface;
2323
use PHPJava\Kernel\Structures\_MethodInfo;
24+
use PHPJava\Kernel\Types\_Char;
2425
use PHPJava\Utilities\DebugTool;
2526
use PHPJava\Utilities\Formatter;
2627
use PHPJava\Utilities\SuperClassResolver;
@@ -89,6 +90,13 @@ public function call(string $name, ...$arguments)
8990
->getJavaClass()
9091
->getConstantPool();
9192

93+
// Wrap _Char
94+
foreach ($arguments as &$argument) {
95+
if (is_string($argument) && strlen($argument) === 1) {
96+
$argument = new _Char($argument);
97+
}
98+
}
99+
92100
$constantPool = $currentConstantPool->getEntries();
93101

94102
if ($name === '<init>' && $this->javaClassInvoker->getJavaClass()->hasParentClass()) {
@@ -125,7 +133,7 @@ function ($argument) {
125133
)['arguments'];
126134

127135
// does not strict mode can be PHP types
128-
if (!($this->options['strict'] ?? Runtime::STRICT)) {
136+
if (!($this->options['strict'] ?? GlobalOptions::get('strict') ?? Runtime::STRICT)) {
129137
$formattedArguments = Formatter::signatureConvertToAmbiguousForPHP($formattedArguments);
130138
}
131139

@@ -209,6 +217,14 @@ function ($argument) {
209217

210218
$methodBeautified = Formatter::beatifyMethodFromConstantPool($method, $currentConstantPool);
211219
$this->debugTool->getLogger()->info('Start operations: ' . $methodBeautified);
220+
221+
$localStorage = array_map(
222+
function ($item) {
223+
return TypeResolver::convertPHPTypeToJavaType($item);
224+
},
225+
$localStorage
226+
);
227+
212228
while ($reader->getOffset() < $codeAttribute->getOpCodeLength()) {
213229
if (++$executedCounter > ($this->options['max_stack_exceeded'] ?? GlobalOptions::get('max_stack_exceeded') ?? Runtime::MAX_STACK_EXCEEDED)) {
214230
throw new RuntimeException(

src/Kernel/Mnemonics/_aaload.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace PHPJava\Kernel\Mnemonics;
33

44
use PHPJava\Exceptions\NotImplementedException;
5+
use PHPJava\Kernel\Types\_Array\Collection;
56
use PHPJava\Utilities\BinaryTool;
67
use PHPJava\Utilities\Extractor;
78

@@ -16,11 +17,10 @@ final class _aaload implements OperationInterface
1617
public function execute(): void
1718
{
1819
$index = Extractor::realValue($this->popFromOperandStack());
19-
$arrayref = Extractor::realValue($this->popFromOperandStack());
20+
$arrayref = $this->popFromOperandStack();
2021

21-
if (!isset($arrayref[$index])) {
22-
throw new \PHPJava\Imitation\java\lang\ArrayIndexOutOfBoundsException('Array index ' . $index . ' out of bounds. (Program Counter: ' . $this->getProgramCounter() . ')');
23-
}
24-
$this->pushToOperandStack($arrayref[$index]);
22+
$this->pushToOperandStack(
23+
$arrayref[$index]
24+
);
2525
}
2626
}

src/Kernel/Mnemonics/_arraylength.php

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

44
use PHPJava\Exceptions\NotImplementedException;
55
use PHPJava\Utilities\BinaryTool;
6+
use PHPJava\Utilities\Extractor;
67

78
final class _arraylength implements OperationInterface
89
{
@@ -13,6 +14,6 @@ public function execute(): void
1314
{
1415
$arrayref = $this->popFromOperandStack();
1516

16-
$this->pushToOperandStack(sizeof($arrayref));
17+
$this->pushToOperandStack(count($arrayref->toArray()));
1718
}
1819
}

src/Kernel/Mnemonics/_iaload.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace PHPJava\Kernel\Mnemonics;
33

44
use PHPJava\Exceptions\NotImplementedException;
5+
use PHPJava\Kernel\Types\_Array\Collection;
56
use PHPJava\Utilities\BinaryTool;
67
use PHPJava\Utilities\Extractor;
78

@@ -14,7 +15,9 @@ public function execute(): void
1415
{
1516
$index = Extractor::realValue($this->popFromOperandStack());
1617
$arrayref = $this->popFromOperandStack();
17-
18-
$this->pushToOperandStack($arrayref[$index]);
18+
19+
$this->pushToOperandStack(
20+
$arrayref[$index]
21+
);
1922
}
2023
}

src/Kernel/Mnemonics/_iastore.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
namespace PHPJava\Kernel\Mnemonics;
33

44
use PHPJava\Exceptions\NotImplementedException;
5+
use PHPJava\Kernel\Types\_Array\Collection;
6+
use PHPJava\Kernel\Types\Type;
57
use PHPJava\Utilities\BinaryTool;
68
use PHPJava\Utilities\Extractor;
79

@@ -14,9 +16,17 @@ public function execute(): void
1416
{
1517
$data = Extractor::realValue($this->popFromOperandStack());
1618
$arrayref = Extractor::realValue($this->popFromOperandStack());
19+
20+
/**
21+
* @var Type $value
22+
*/
1723
$value = $this->popFromOperandStack();
1824

25+
// The value is a ref.
1926
$value[$arrayref] = $data;
20-
$this->pushToOperandStack($value);
27+
28+
$this->pushToOperandStack(
29+
$value
30+
);
2131
}
2232
}

src/Kernel/Mnemonics/_newarray.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
namespace PHPJava\Kernel\Mnemonics;
33

44
use PHPJava\Exceptions\NotImplementedException;
5+
use PHPJava\Exceptions\RuntimeException;
6+
use PHPJava\Kernel\Types\_Array\Collection;
7+
use PHPJava\Kernel\Types\_Boolean;
8+
use PHPJava\Kernel\Types\_Byte;
9+
use PHPJava\Kernel\Types\_Char;
10+
use PHPJava\Kernel\Types\_Double;
11+
use PHPJava\Kernel\Types\_Float;
12+
use PHPJava\Kernel\Types\_Int;
13+
use PHPJava\Kernel\Types\_Long;
14+
use PHPJava\Kernel\Types\_Short;
515
use PHPJava\Utilities\BinaryTool;
616
use PHPJava\Utilities\Extractor;
717

@@ -14,9 +24,49 @@ public function execute(): void
1424
{
1525
$atype = $this->readUnsignedByte();
1626
$count = Extractor::realValue($this->popFromOperandStack());
17-
27+
28+
$array = array_fill(0, $count, null);
29+
1830
// need reference
19-
$ref = new \ArrayIterator(array_fill(0, $count, null));
31+
$className = null;
32+
33+
switch ($atype) {
34+
case 4: // T_BOOLEAN
35+
$className = '_Boolean';
36+
break;
37+
case 5: // T_CHAR
38+
$className = '_Char';
39+
break;
40+
case 6: // T_FLOAT
41+
$className = '_Float';
42+
break;
43+
case 7: // T_DOUBLE
44+
$className = '_Double';
45+
break;
46+
case 8: // T_BYTE
47+
$className = '_Byte';
48+
break;
49+
case 9: // T_SHORT
50+
$className = '_Short';
51+
break;
52+
case 10: // T_INT
53+
$className = '_Int';
54+
break;
55+
case 11: // T_LONG
56+
$className = '_Long';
57+
break;
58+
default:
59+
throw new RuntimeException('Unknown array type ' . $atype);
60+
break;
61+
}
62+
63+
$className = '\\PHPJava\\Kernel\\Types\\' . $className;
64+
foreach ($array as &$item) {
65+
$item = new $className(null);
66+
}
67+
68+
$ref = new Collection($array);
69+
2070
$this->pushToOperandStackByReference($ref);
2171
}
2272
}

src/Kernel/Types/Type.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,32 @@ public function __construct($value)
1616
!is_float($value) &&
1717
!is_string($value) &&
1818
!is_bool($value) &&
19+
!is_array($value) &&
20+
!is_null($value) &&
1921
!($value instanceof self)
2022
) {
23+
$type = gettype($value);
2124
throw new TypeException(
22-
'Passed value is not scalar. The value is "' . gettype($value) . '"'
25+
'Passed value is not scalar or miss match type. The value is "' . ($type === 'object' ? get_class($value) : $type) . '"'
2326
);
2427
}
2528
$this->value = ($value instanceof self)
2629
? $value->getValue()
2730
: $value;
2831
}
2932

33+
public static function ensure($value)
34+
{
35+
return ($value instanceof self)
36+
? $value
37+
: new self($value);
38+
}
39+
40+
public function setValue($value)
41+
{
42+
$this->value = $value;
43+
}
44+
3045
public function getValue()
3146
{
3247
return $this->value;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
namespace PHPJava\Kernel\Types\_Array;
3+
4+
use PHPJava\Exceptions\TypeException;
5+
use PHPJava\Kernel\Types\Type;
6+
use PHPJava\Utilities\TypeResolver;
7+
8+
class Collection implements \ArrayAccess
9+
{
10+
private $data;
11+
12+
public function __construct(array &$data, string $type = null)
13+
{
14+
$this->data = $data;
15+
}
16+
17+
public function getType($default = null): ?string
18+
{
19+
if (!isset($this->data[0])) {
20+
return $default;
21+
}
22+
return TypeResolver::resolveFromPHPType($this->data[0]) ?? $default;
23+
}
24+
25+
public function toArray()
26+
{
27+
return $this->data;
28+
}
29+
30+
public function offsetExists($offset)
31+
{
32+
return isset($this->data[$offset]);
33+
}
34+
35+
public function offsetGet($offset)
36+
{
37+
return $this->data[$offset];
38+
}
39+
40+
public function offsetUnset($offset)
41+
{
42+
unset($this->data[$offset]);
43+
}
44+
45+
public function offsetSet($offset, $value)
46+
{
47+
$this->data[$offset] = $value;
48+
}
49+
}

src/Kernel/Types/_Char.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
class _Char extends Type
66
{
77
protected $nameInJava = 'char';
8-
protected $nameInPHP = 'string';
8+
protected $nameInPHP = 'int';
99
}

0 commit comments

Comments
 (0)