-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathparser.js
377 lines (351 loc) · 11.8 KB
/
parser.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
( // Module boilerplate to support browser globals, node.js and AMD.
(typeof module !== "undefined" && function (m) { module.exports = m(); }) ||
(typeof define === "function" && function (m) { define(m); }) ||
(function (m) { window.brozula = window.brozula || {}; window.brozula.parse = m(); })
)(function () {
"use strict";
// For mapping enum integer values to opcode names
var opcodes = [
"ISLT", "ISGE", "ISLE", "ISGT", "ISEQV", "ISNEV", "ISEQS", "ISNES", "ISEQN",
"ISNEN", "ISEQP", "ISNEP", "ISTC", "ISFC", "IST", "ISF", "MOV", "NOT", "UNM",
"LEN", "ADDVN", "SUBVN", "MULVN", "DIVVN", "MODVN", "ADDNV", "SUBNV", "MULNV",
"DIVNV", "MODNV", "ADDVV", "SUBVV", "MULVV", "DIVVV", "MODVV", "POW", "CAT",
"KSTR", "KCDATA", "KSHORT", "KNUM", "KPRI", "KNIL", "UGET", "USETV", "USETS",
"USETN", "USETP", "UCLO", "FNEW", "TNEW", "TDUP", "GGET", "GSET", "TGETV",
"TGETS", "TGETB", "TSETV", "TSETS", "TSETB", "TSETM", "CALLM", "CALL",
"CALLMT", "CALLT", "ITERC", "ITERN", "VARG", "ISNEXT", "RETM", "RET", "RET0",
"RET1", "FORI", "JFORI", "FORL", "IFORL", "JFORL", "ITERL", "IITERL",
"JITERL", "LOOP", "ILOOP", "JLOOP", "JMP", "FUNCF", "IFUNCF", "JFUNCF",
"FUNCV", "IFUNCV", "JFUNCV", "FUNCC", "FUNCCW"
];
// For mapping opcode names to parse instructions
var bcdef = {
ISLT: {ma: "var", md: "var", mt: "lt"},
ISGE: {ma: "var", md: "var", mt: "lt"},
ISLE: {ma: "var", md: "var", mt: "le"},
ISGT: {ma: "var", md: "var", mt: "le"},
ISEQV: {ma: "var", md: "var", mt: "eq"},
ISNEV: {ma: "var", md: "var", mt: "eq"},
ISEQS: {ma: "var", md: "str", mt: "eq"},
ISNES: {ma: "var", md: "str", mt: "eq"},
ISEQN: {ma: "var", md: "num", mt: "eq"},
ISNEN: {ma: "var", md: "num", mt: "eq"},
ISEQP: {ma: "var", md: "pri", mt: "eq"},
ISNEP: {ma: "var", md: "pri", mt: "eq"},
ISTC: {ma: "dst", md: "var"},
ISFC: {ma: "dst", md: "var"},
IST: {md: "var"},
ISF: {md: "var"},
MOV: {ma: "dst", md: "var"},
NOT: {ma: "dst", md: "var"},
UNM: {ma: "dst", md: "var", mt: "unm"},
LEN: {ma: "dst", md: "var", mt: "len"},
ADDVN: {ma: "dst", mb: "var", mc: "num", mt: "add"},
SUBVN: {ma: "dst", mb: "var", mc: "num", mt: "sub"},
MULVN: {ma: "dst", mb: "var", mc: "num", mt: "mul"},
DIVVN: {ma: "dst", mb: "var", mc: "num", mt: "div"},
MODVN: {ma: "dst", mb: "var", mc: "num", mt: "mod"},
ADDNV: {ma: "dst", mb: "var", mc: "num", mt: "add"},
SUBNV: {ma: "dst", mb: "var", mc: "num", mt: "sub"},
MULNV: {ma: "dst", mb: "var", mc: "num", mt: "mul"},
DIVNV: {ma: "dst", mb: "var", mc: "num", mt: "div"},
MODNV: {ma: "dst", mb: "var", mc: "num", mt: "mod"},
ADDVV: {ma: "dst", mb: "var", mc: "var", mt: "add"},
SUBVV: {ma: "dst", mb: "var", mc: "var", mt: "sub"},
MULVV: {ma: "dst", mb: "var", mc: "var", mt: "mul"},
DIVVV: {ma: "dst", mb: "var", mc: "var", mt: "div"},
MODVV: {ma: "dst", mb: "var", mc: "var", mt: "mod"},
POW: {ma: "dst", mb: "var", mc: "var", mt: "pow"},
CAT: {ma: "dst", mb: "rbase", mc: "rbase", mt: "concat"},
KSTR: {ma: "dst", md: "str"},
KCDATA: {ma: "dst", md: "cdata"},
KSHORT: {ma: "dst", md: "lits"},
KNUM: {ma: "dst", md: "num"},
KPRI: {ma: "dst", md: "pri"},
KNIL: {ma: "base", md: "base"},
UGET: {ma: "dst", md: "uv"},
USETV: {ma: "uv", md: "var"},
USETS: {ma: "uv", md: "str"},
USETN: {ma: "uv", md: "num"},
USETP: {ma: "uv", md: "pri"},
UCLO: {ma: "rbase", md: "jump"},
FNEW: {ma: "dst", md: "func", mt: "gc"},
TNEW: {ma: "dst", md: "lit", mt: "gc"},
TDUP: {ma: "dst", md: "tab", mt: "gc"},
GGET: {ma: "dst", md: "str", mt: "index"},
GSET: {ma: "var", md: "str", mt: "newindex"},
TGETV: {ma: "dst", mb: "var", mc: "var", mt: "index"},
TGETS: {ma: "dst", mb: "var", mc: "str", mt: "index"},
TGETB: {ma: "dst", mb: "var", mc: "lit", mt: "index"},
TSETV: {ma: "var", mb: "var", mc: "var", mt: "newindex"},
TSETS: {ma: "var", mb: "var", mc: "str", mt: "newindex"},
TSETB: {ma: "var", mb: "var", mc: "lit", mt: "newindex"},
TSETM: {ma: "base", md: "num", mt: "newindex"},
CALLM: {ma: "base", mb: "lit", mc: "lit", mt: "call"},
CALL: {ma: "base", mb: "lit", mc: "lit", mt: "call"},
CALLMT: {ma: "base", md: "lit", mt: "call"},
CALLT: {ma: "base", md: "lit", mt: "call"},
ITERC: {ma: "base", mb: "lit", mc: "lit", mt: "call"},
ITERN: {ma: "base", mb: "lit", mc: "lit", mt: "call"},
VARG: {ma: "base", mb: "lit", mc: "lit"},
ISNEXT: {ma: "base", md: "jump"},
RETM: {ma: "base", md: "lit"},
RET: {ma: "rbase", md: "lit"},
RET0: {ma: "rbase", md: "lit"},
RET1: {ma: "rbase", md: "lit"},
FORI: {ma: "base", md: "jump"},
JFORI: {ma: "base", md: "jump"},
FORL: {ma: "base", md: "jump"},
IFORL: {ma: "base", md: "jump"},
JFORL: {ma: "base", md: "lit"},
ITERL: {ma: "base", md: "jump"},
IITERL: {ma: "base", md: "jump"},
JITERL: {ma: "base", md: "lit"},
LOOP: {ma: "rbase", md: "jump"},
ILOOP: {ma: "rbase", md: "jump"},
JLOOP: {ma: "rbase", md: "lit"},
JMP: {ma: "rbase", md: "jump"},
FUNCF: {ma: "rbase"},
IFUNCF: {ma: "rbase"},
JFUNCF: {ma: "rbase", md: "lit"},
FUNCV: {ma: "rbase"},
IFUNCV: {ma: "rbase"},
JFUNCV: {ma: "rbase", md: "lit"},
FUNCC: {ma: "rbase"},
FUNCCW: {ma: "rbase"}
};
var kgctypes = ["CHILD", "TAB", "I64", "U64", "COMPLEX", "STR"];
var ktabtypes = ["NIL", "FALSE", "TRUE", "INT", "NUM", "STR"];
var kgcdecs = {
TAB: function (parser) {
var narray = parser.U();
var nhash = parser.U();
if (narray) {
if (nhash) {
throw new Error("TODO: implement mixed tables");
}
var tab = new Array(narray);
for (var i = 0; i < narray; i++) {
tab[i] = readktabk(parser);
}
return tab;
}
if (nhash) {
var tab = {};
for (var i = 0; i < nhash; i++) {
var key = readktabk(parser);
if (typeof key !== "string") throw new Error("TODO: Implement non-string keys");
tab[key] = readktabk(parser);
}
return tab;
}
return {};
},
I64: function (parser) {
throw new Error("TODO: Implement I64 kgc decoder");
},
U64: function (parser) {
throw new Error("TODO: Implement U64 kgc decoder");
},
COMPLEX: function (parser) {
throw new Error("TODO: Implement COMPLEX kgc decoder");
},
STR: function (parser, len) {
len -= 5; // Offset for STR enum
var value = parser.buffer.slice(parser.index, parser.index + len).toString();
parser.index += len;
return value;
}
};
var ktabdecs = {
NIL: function (parser) {
return null;
},
FALSE: function (parser) {
return false;
},
TRUE: function (parser) {
return true;
},
INT: function (parser) {
return parser.U();
},
NUM: readknum,
STR: kgcdecs.STR
};
// Opcodes that consume a jump
var conditionals = {ISLT: true, ISGE: true, ISLE: true, ISGT: true, ISEQV: true,
ISNEV: true, ISEQS: true, ISNES: true, ISEQN: true, ISNEN: true, ISEQP: true,
ISNEP: true, ISTC: true, ISFC: true, IST: true, ISF: true};
// Used to consume bytes from a bytecode stream
function Parser(buffer) {
this.buffer = buffer;
this.index = 0;
this.mark = 0;
}
Parser.prototype.B = function () {
return this.buffer[this.index++];
};
// Consume 16 bit value from stream and move pointer
Parser.prototype.H = function () {
var value = this.buffer.readUInt16LE(this.index);
this.index += 2;
return value;
};
// Consume 32 bit value from stream and move pointer
Parser.prototype.W = function () {
var value = this.buffer.readUInt32LE(this.index);
this.index += 4;
return value;
};
// Decode ULEB128 from the stream
// http://en.wikipedia.org/wiki/LEB128
Parser.prototype.U = function () {
var value = 0;
var shift = 0;
var byte;
do {
byte = this.buffer[this.index++];
value |= (byte & 0x7f) << shift;
shift += 7;
} while (byte >= 0x80);
return value >>> 0;
};
function parse(buffer) {
var parser = new Parser(buffer);
// header = ESC 'L' 'J' versionB flagsU [namelenU nameB*]
if (parser.B() !== 0x1b) throw new Error("Expected ESC in first byte");
if (parser.B() !== 0x4c) throw new Error("Expected L in second byte");
if (parser.B() !== 0x4a) throw new Error("Expected J in third byte");
if (parser.B() !== 1) throw new Error("Only version 1 supported");
var flags = parser.U();
if (flags & 1) throw new Error("Big endian encoding not supported yet");
if (!(flags & 2)) throw new Error("Non stripped bytecode not supported yet");
if (flags & 4) throw new Error("FFI bytecode not supported");
// proto+
var protoBuffers = [];
do {
var len = parser.U();
protoBuffers.push(buffer.slice(parser.index, parser.index + len));
parser.index += len;
} while (buffer[parser.index]);
// 0U and EOF
if (parser.U() !== 0) throw new Error("Missing 0U at end of file");
if (parser.index < buffer.length) throw new Error((buffer.length - parser.index) + " bytes leftover");
var l = protoBuffers.length;
var protos = new Array(l);
for (var i = 0; i < l; i++) {
readproto(protoBuffers[i], protos, i);
}
return protos;
}
function readproto(buffer, protos, protoIndex) {
var parser = new Parser(buffer);
// flagsB numparamsB framesizeB numuvB numkgcU numknU numbcU [debuglenU [firstlineU numlineU]]
var flags = parser.B();
var numparams = parser.B();
var framesize = parser.B();
var numuv = parser.B();
var numkgc = parser.U();
var numkn = parser.U();
var numbc = parser.U();
var bcins = new Array(numbc);
var uvdata = new Array(numuv);
var proto = protos[protoIndex] = {
index: protoIndex,
flags: flags,
numparams: numparams,
framesize: framesize,
bcins: bcins,
uvdata: uvdata
};
// bcinsW* uvdataH* kgc* knum*
var i;
for (i = 0; i < numbc; i++) {
bcins[i] = parser.W();
}
for (i = 0; i < numuv; i++) {
var uv = parser.H();
uvdata[i] = {
local: !!(uv & 0x8000),
immutable: !!(uv & 0x4000),
uv: uv & 0x3fff
};
}
var constants = new Array(numkgc + numkn);
var childc = protoIndex;
for (i = 0; i < numkgc; i++) {
var kgctype = parser.U();
var type = kgctypes[kgctype] || "STR";
if (type === "CHILD") {
constants[i + numkn] = protos[--childc];
}
else {
constants[i + numkn] = kgcdecs[type](parser, kgctype);
}
}
for (i = 0; i < numkn; i++) {
constants[i] = readknum(parser);
}
for (i = 0; i < numbc; i++) {
bcins[i] = parseOpcode(bcins[i], i);
}
// Make sure we consumed all the bytes properly
if (parser.index !== buffer.length) throw new Error((buffer.length - parser.index) + " bytes leftover");
function parseArg(type, val, i) {
switch (type) {
case "lit": return val >>> 0;
case "lits": return val & 0x8000 ? val - 0x10000 : val;
case "pri": return val === 0 ? null : val === 1 ? false : true;
case "num": return constants[val];
case "str": case "tab": case "func": case "cdata":
return constants[constants.length - val - 1];
case "jump": return val - 0x8000;
default: return val;
}
}
function parseOpcode(word, i) {
var opcode = opcodes[word & 0xff];
var def = bcdef[opcode];
var args = [];
var op = {
op: opcode,
args: args
};
if (def.ma) {
args.push(parseArg(def.ma, (word >>> 8) & 0xff, i));
}
if (def.mb) {
args.push(parseArg(def.mb, word >>> 24, i));
}
if (def.mc) {
args.push(parseArg(def.mc, (word >>> 16) & 0xff, i));
}
if (def.md) {
args.push(parseArg(def.md, word >>> 16, i));
}
if (def.mt) {
args.push(def.mt);
}
return op;
}
}
function readknum(parser) {
var isnum = parser.buffer[parser.index] & 1;
var lo = parser.U() >> 1;
if (isnum) {
var buf = new Buffer(8);
buf.writeUInt32LE(lo >>> 0, 0);
buf.writeUInt32LE(parser.U(), 4);
var num = buf.readDoubleLE(0);
return num;
}
return lo;
}
// Read a constant table key or value
function readktabk(parser) {
var ktabtype = parser.U();
return ktabdecs[ktabtypes[ktabtype] || "STR"](parser, ktabtype);
}
return parse;
});