Skip to content

Commit 1471014

Browse files
committed
WIP: Add a multi dimension array generator
1 parent 99a28f5 commit 1471014

2 files changed

Lines changed: 62 additions & 6 deletions

File tree

src/Kernel/Mnemonics/_multianewarray.php

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

4+
use PHPJava\Kernel\Types\_Array\Collection;
5+
use PHPJava\Utilities\Extractor;
6+
use PHPJava\Utilities\Formatter;
7+
48
final class _multianewarray implements OperationInterface
59
{
610
use \PHPJava\Kernel\Core\Accumulator;
@@ -12,9 +16,50 @@ public function execute(): void
1216
$index = $this->readUnsignedShort();
1317
$dimensions = $this->readByte();
1418

15-
$descriptor = $cp[$cp[$index]->getClassIndex()]->getString();
19+
$descriptor = Formatter::parseSignature(
20+
$cp[$cp[$index]->getClassIndex()]->getString()
21+
);
22+
23+
$counts = array_fill(0, $dimensions, 0);
24+
25+
for ($i = $dimensions - 1; $i >= 0; $i--) {
26+
$counts[$i] = Extractor::getRealValue(
27+
$this->popFromOperandStack()
28+
);
29+
}
30+
31+
$data = null;
32+
$multiDimensionArray = $this->makeMultiDimensionArray(
33+
$data,
34+
$counts,
35+
$descriptor[0]['class_name'] ?? $descriptor[0]['type'],
36+
0,
37+
$dimensions
38+
);
1639

17-
var_dump($descriptor, $dimensions);
18-
exit();
40+
$this->pushToOperandStackByReference($multiDimensionArray);
41+
}
42+
43+
/**
44+
* @param $array
45+
* @return $this
46+
*/
47+
private function makeMultiDimensionArray($array, array $counts, string $type, int $currentDimension, int $maxDimension)
48+
{
49+
if ($currentDimension >= $maxDimension) {
50+
return $array;
51+
}
52+
$newArray = (new Collection())->setType($type);
53+
for ($i = 0; $i < $counts[$currentDimension]; $i++) {
54+
$collection = (new Collection())->setType($type);
55+
$newArray[$i] = $this->makeMultiDimensionArray(
56+
$collection,
57+
$counts,
58+
$type,
59+
$currentDimension + 1,
60+
$maxDimension
61+
);
62+
}
63+
return $newArray;
1964
}
2065
}

src/Kernel/Types/_Array/Collection.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,27 @@ class Collection implements \ArrayAccess, \Countable, \IteratorAggregate
99
{
1010
private $data;
1111
private $position = 0;
12+
private $type;
1213

13-
public function __construct(array &$data, string $type = null)
14+
public function __construct(array &$data = [])
1415
{
1516
$this->data = $data;
1617
}
1718

19+
public function setType(string $type = null)
20+
{
21+
$this->type = $type;
22+
return $this;
23+
}
24+
1825
public function getType($default = null): ?string
1926
{
2027
if (!isset($this->data[0])) {
21-
return $default;
28+
return $this->type ?? $default;
2229
}
2330
return TypeResolver::resolveFromPHPType(
2431
Extractor::getRealValue($this->data[0])
25-
) ?? $default;
32+
) ?? $this->type ?? $default;
2633
}
2734

2835
public function __toString()
@@ -55,6 +62,10 @@ public function offsetUnset($offset)
5562

5663
public function offsetSet($offset, $value)
5764
{
65+
if (empty($offset)) {
66+
$this->data[] = $value;
67+
return;
68+
}
5869
$this->data[$offset] = $value;
5970
}
6071

0 commit comments

Comments
 (0)