Skip to content

Commit cb737a0

Browse files
committed
Implement operand getter
1 parent cb74904 commit cb737a0

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace PHPJava\Exceptions;
3+
4+
class OperationException extends \Exception
5+
{
6+
}

‎src/Kernel/Mnemonics/Operands.php‎

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

4+
use PHPJava\Exceptions\OperationException;
5+
46
final class Operands implements \ArrayAccess
57
{
68
private $operands = [];
79

8-
public function __construct(array $operands = [])
10+
public function __construct(...$operands)
911
{
10-
$this->operands = $operands;
12+
foreach ($operands as [$alias, $value, $operandNames]) {
13+
$this->operands[$alias] = [$value, $operandNames];
14+
}
1115
}
1216

1317
public function count()
1418
{
15-
return count($this->operands);
19+
return count($this->getInfo());
20+
}
21+
22+
public function getInfo(): array
23+
{
24+
$operands = [];
25+
foreach ($this->operands as [, $operandParameters]) {
26+
array_push($operands, ...$operandParameters);
27+
}
28+
return $operands;
29+
}
30+
31+
public function getElements(): array
32+
{
33+
return $this->operands;
1634
}
1735

1836
public function offsetExists($offset)
@@ -22,17 +40,16 @@ public function offsetExists($offset)
2240

2341
public function offsetGet($offset)
2442
{
25-
return $this->operands[$offset];
43+
return $this->operands[$offset][0];
2644
}
2745

2846
public function offsetSet($offset, $value)
2947
{
30-
$this->operands[$offset] = $value;
48+
throw new OperationException('Operands class is not supported overwrite.');
3149
}
3250

3351
public function offsetUnset($offset)
3452
{
35-
unset($this->operands[$offset]);
36-
$this->operands = array_values($this->operands);
53+
throw new OperationException('Operands class is not supported unset an element.');
3754
}
3855
}

‎src/Kernel/Mnemonics/_invokevirtual.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ public function getOperands(): ?Operands
2121
if ($this->operands !== null) {
2222
return $this->operands;
2323
}
24-
return $this->operands = new Operands();
24+
25+
$indexbyte = $this->readUnsignedShort();
26+
27+
return $this->operands = new Operands(
28+
['indexbyte', $indexbyte, ['indexbyte1', 'indexbyte2']]
29+
);
2530
}
2631

2732
/**
@@ -36,7 +41,7 @@ public function execute(): void
3641
{
3742
parent::execute();
3843
$cpInfo = $this->getConstantPool();
39-
$cp = $cpInfo[$this->readUnsignedShort()];
44+
$cp = $cpInfo[$this->getOperands()['indexbyte']];
4045
$class = $cpInfo[$cpInfo[$cp->getClassIndex()]->getClassIndex()]->getString();
4146
$nameAndTypeIndex = $cpInfo[$cp->getNameAndTypeIndex()];
4247

0 commit comments

Comments
 (0)