-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathAccumulator.php
More file actions
218 lines (180 loc) Β· 4.72 KB
/
Accumulator.php
File metadata and controls
218 lines (180 loc) Β· 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
declare(strict_types=1);
namespace PHPJava\Kernel\Core;
use PHPJava\Core\JavaClass;
use PHPJava\Core\JVM\ClassInvokerInterface;
use PHPJava\Exceptions\IllegalOperationException;
use PHPJava\Kernel\Provider\DependencyInjectionProvider;
use PHPJava\Kernel\Structures\MethodInfo;
trait Accumulator
{
/**
* @var null|JavaClass
*/
protected $javaClass;
/**
* @var \PHPJava\Kernel\Attributes\AttributeInfo[]
*/
protected $attributes = [];
/**
* @var ClassInvokerInterface
*/
protected $javaClassInvoker;
/**
* @var \PHPJava\Core\JVM\Stream\BinaryReader
*/
protected $reader;
/**
* @var array
*/
protected $localStorage;
/**
* @var int
*/
protected $pointer;
/**
* @var array
*/
protected $stacks = [];
/**
* @var array
*/
protected $options;
/**
* @var MethodInfo $method
*/
protected $method;
/**
* @var DependencyInjectionProvider
*/
protected $dependencyInjectionProvider;
protected $poppedOperandStacks = [];
protected $pushedOperandStacks = [];
public function setParameters(
MethodInfo $method,
ClassInvokerInterface $javaClassInvoker,
\PHPJava\Core\JVM\Stream\BinaryReader $reader,
array &$localStorage,
array &$stacks,
int $pointer,
DependencyInjectionProvider $dependencyInjectionProvider
): self {
$this->method = $method;
$this->attributes = $method->getAttributes();
$this->javaClassInvoker = $javaClassInvoker;
$this->javaClass = $javaClassInvoker->getJavaClass();
$this->options = $this->javaClass->getOptions();
$this->reader = $reader;
$this->localStorage = &$localStorage;
$this->stacks = &$stacks;
$this->pointer = $pointer;
$this->dependencyInjectionProvider = $dependencyInjectionProvider;
return $this;
}
final public function read(int $bytes = 1): string
{
return $this->reader->read($bytes);
}
public function readByte(): int
{
return $this->reader->readByte();
}
public function readUnsignedByte(): int
{
return $this->reader->readUnsignedByte();
}
public function readUnsignedInt(): int
{
return $this->reader->readUnsignedInt();
}
public function readUnsignedShort(): int
{
return $this->reader->readUnsignedShort();
}
public function readInt(): int
{
return $this->reader->readInt();
}
public function readShort(): int
{
return $this->reader->readShort();
}
public function readUnsignedLong(): int
{
return $this->reader->readUnsignedLong();
}
public function readLong(): int
{
return current(unpack('q', $this->read(8)));
}
public function seek(int $bytes): void
{
$this->reader->seek($bytes);
}
public function setOffset(int $pointer): void
{
$this->reader->setOffset($pointer);
}
public function getOffset(): int
{
return $this->reader->getOffset();
}
public function pushToOperandStack($value): void
{
$this->pushedOperandStacks[] = $this->stacks[] = $value;
}
public function pushToOperandStackByReference(&$value): void
{
$this->pushedOperandStacks[] = $this->stacks[] = &$value;
}
public function popFromOperandStack()
{
if (empty($this->stacks)) {
throw new IllegalOperationException('Cannot pop an item from stack.');
}
return $this->poppedOperandStacks[] = array_pop($this->stacks);
}
public function getCurrentStackIndex(): int
{
return count($this->stacks) - 1;
}
public function popStack(): self
{
$this->poppedOperandStacks[] = array_pop($this->stacks);
return $this;
}
public function getStacks()
{
return $this->stacks;
}
public function setLocalStorage($index, $value): void
{
$this->localStorage[(int) $index] = $value;
}
public function getLocalStorage($index)
{
if (!isset($this->localStorage[(int) $index])) {
$this->localStorage[(int) $index] = null;
}
return $this->localStorage[(int) $index];
}
public function getLocalStorages(): array
{
return $this->localStorage;
}
public function getProgramCounter(): int
{
return $this->pointer;
}
public function getOptions($key)
{
return $this->options[$key] ?? null;
}
/**
* @return \PHPJava\Kernel\Attributes\AttributeInfo[]
*/
public function getAttributes(): array
{
return $this->attributes;
}
}