-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
encoder.js
46 lines (36 loc) · 1000 Bytes
/
encoder.js
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
import { DEBUG } from '@glimmer/env';
class InstructionEncoderImpl {
constructor(buffer) {
this.buffer = buffer;
this.size = 0;
}
encode(type, machine) {
if (type > 255
/* TYPE_SIZE */
) {
throw new Error(`Opcode type over 8-bits. Got ${type}.`);
}
let first = type | machine | arguments.length - 2 << 8
/* ARG_SHIFT */
;
this.buffer.push(first);
for (let i = 2; i < arguments.length; i++) {
let op = arguments[i];
if (DEBUG && typeof op === 'number' && op > 2147483647
/* MAX_SIZE */
) {
throw new Error(`Operand over 32-bits. Got ${op}.`);
}
this.buffer.push(op);
}
this.size = this.buffer.length;
}
patch(position, target) {
if (this.buffer[position + 1] === -1) {
this.buffer[position + 1] = target;
} else {
throw new Error('Trying to patch operand in populated slot instead of a reserved slot.');
}
}
}
export { InstructionEncoderImpl };