-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathdeserialize.js.erb
More file actions
448 lines (376 loc) · 11.8 KB
/
Copy pathdeserialize.js.erb
File metadata and controls
448 lines (376 loc) · 11.8 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import * as nodes from "./nodes.js";
const MAJOR_VERSION = 1;
const MINOR_VERSION = 5;
const PATCH_VERSION = 0;
// The DataView getFloat64 function takes an optional second argument that
// specifies whether the number is little-endian or big-endian. It does not
// appear to have a native endian mode, so we need to determine the endianness
// of the system at runtime.
const LITTLE_ENDIAN = (() => {
let uint32 = new Uint32Array([0x11223344]);
let uint8 = new Uint8Array(uint32.buffer);
if (uint8[0] === 0x44) {
return true;
} else if (uInt8[0] === 0x11) {
return false;
} else {
throw new Error("Mixed endianness");
}
})();
class SerializationBuffer {
FORCED_UTF8_ENCODING_FLAG = 1 << 2;
FORCED_BINARY_ENCODING_FLAG = 1 << 3;
DECODER_MAP = new Map([
["ascii-8bit", "ascii"]
]);
constructor(source, array) {
this.source = source;
this.array = array;
this.index = 0;
this.fileEncoding = "utf-8";
this.decoders = new Map();
}
readByte() {
const result = this.array[this.index];
this.index += 1;
return result;
}
readBytes(length) {
const result = this.array.slice(this.index, this.index + length);
this.index += length;
return result;
}
readString(length, flags) {
return this.decodeString(this.readBytes(length), flags).value;
}
// Read a 32-bit unsigned integer in little-endian format.
readUint32() {
const result = this.scanUint32(this.index);
this.index += 4;
return result;
}
scanUint32(offset) {
const bytes = this.array.slice(offset, offset + 4);
return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
}
readVarInt() {
let result = 0;
let shift = 0;
while (true) {
const byte = this.readByte();
result += (byte & 0x7f) << shift;
shift += 7;
if ((byte & 0x80) === 0) {
break;
}
}
return result;
}
readLocation() {
return { startOffset: this.readVarInt(), length: this.readVarInt() };
}
readOptionalLocation() {
if (this.readByte() != 0) {
return this.readLocation();
} else {
return null;
}
}
readStringField(flags) {
if (flags === undefined) flags = 0;
const type = this.readByte();
switch (type) {
case 1: {
const startOffset = this.readVarInt();
const length = this.readVarInt();
return this.decodeString(this.source.slice(startOffset, startOffset + length), flags);
}
case 2:
return this.decodeString(this.readBytes(this.readVarInt()), flags);
default:
throw new Error(`Unknown serialized string type: ${type}`);
}
}
scanConstant(constantPoolOffset, constantIndex) {
const offset = constantPoolOffset + constantIndex * 8;
let startOffset = this.scanUint32(offset);
const length = this.scanUint32(offset + 4);
if (startOffset & (1 << 31)) {
startOffset &= (1 << 31) - 1;
return new TextDecoder().decode(this.array.slice(startOffset, startOffset + length));
} else {
return new TextDecoder().decode(this.source.slice(startOffset, startOffset + length));
}
}
readDouble() {
const view = new DataView(new ArrayBuffer(8));
for (let index = 0; index < 8; index++) {
view.setUint8(index, this.readByte());
}
return view.getFloat64(0, LITTLE_ENDIAN);
}
decodeString(bytes, flags) {
const forcedBin = (flags & this.FORCED_BINARY_ENCODING_FLAG) !== 0;
const forcedUtf8 = (flags & this.FORCED_UTF8_ENCODING_FLAG) !== 0;
if (forcedBin) {
// just use raw bytes
return {
encoding: "ascii",
validEncoding: true,
value: this.asciiDecoder.decode(bytes)
};
} else {
const encoding = forcedUtf8 ? "utf-8" : this.fileEncoding.toLowerCase();
const decoder = this.getDecoder(encoding);
try {
// decode with encoding
return {
encoding,
validEncoding: true,
value: decoder.decode(bytes)
};
} catch(e) {
// just use raw bytes, capture what the encoding should be, set flag saying encoding is invalid
if (e.code === "ERR_ENCODING_INVALID_ENCODED_DATA") {
return {
encoding,
validEncoding: false,
value: this.asciiDecoder.decode(bytes)
};
}
throw e;
}
}
}
getDecoder(encoding) {
encoding = this.DECODER_MAP.get(encoding) || encoding;
if (!this.decoders.has(encoding)) {
this.decoders.set(encoding, new TextDecoder(encoding, {fatal: true}));
}
return this.decoders.get(encoding);
}
get asciiDecoder() {
if (!this._asciiDecoder) {
this._asciiDecoder = new TextDecoder("ascii");
}
return this._asciiDecoder;
}
}
/**
* A location in the source code.
*
* @typedef {{ startOffset: number, length: number }} Location
*/
/**
* A comment in the source code.
*
* @typedef {{ type: number, location: Location }} Comment
*/
/**
* A magic comment in the source code.
*
* @typedef {{ startLocation: Location, endLocation: Location }} MagicComment
*/
/**
* An error in the source code.
*
* @typedef {{ type: string, message: string, location: Location, level: string }} ParseError
*/
/**
* A warning in the source code.
*
* @typedef {{ type: string, message: string, location: Location, level: string }} ParseWarning
*/
/**
* The result of parsing the source code.
*
* @typedef {{ value: ProgramNode, comments: Comment[], magicComments: MagicComment[], errors: ParseError[], warnings: ParseWarning[] }} ParseResult
*/
/**
* The result of calling parse.
*/
export class ParseResult {
/**
* @type {nodes.ProgramNode}
*/
value;
/**
* @type {Comment[]}
*/
comments;
/**
* @type {MagicComment[]}
*/
magicComments;
/**
* @type {Location | null}
*/
/**
* @type {ParseError[]}
*/
errors;
/**
* @type {ParseWarning[]}
*/
warnings;
/**
* @param {nodes.ProgramNode} value
* @param {Comment[]} comments
* @param {MagicComment[]} magicComments
* @param {ParseError[]} errors
* @param {ParseWarning[]} warnings
*/
constructor(value, comments, magicComments, dataLoc, errors, warnings) {
this.value = value;
this.comments = comments;
this.magicComments = magicComments;
this.dataLoc = dataLoc;
this.errors = errors;
this.warnings = warnings;
}
}
const errorLevels = ["syntax", "argument", "load"];
const errorTypes = [
<%- errors.each do |error| -%>
"<%= error.name.downcase %>",
<%- end -%>
];
const warningLevels = ["default", "verbose"];
const warningTypes = [
<%- warnings.each do |warning| -%>
"<%= warning.name.downcase %>",
<%- end -%>
];
/**
* Accept two Uint8Arrays, one for the source and one for the serialized format.
* Return the AST corresponding to the serialized form.
*
* @param {Uint8Array} source
* @param {Uint8Array} array
* @returns {ParseResult}
* @throws {Error}
*/
export function deserialize(source, array) {
const buffer = new SerializationBuffer(source, array);
if (buffer.readString(5) !== "PRISM") {
throw new Error("Invalid serialization");
}
if ((buffer.readByte() != MAJOR_VERSION) || (buffer.readByte() != MINOR_VERSION) || (buffer.readByte() != PATCH_VERSION)) {
throw new Error("Invalid serialization");
}
if (buffer.readByte() != 0) {
throw new Error("Invalid serialization (location fields must be included but are not)");
}
// Read the file's encoding.
buffer.fileEncoding = buffer.readString(buffer.readVarInt());
// Skip past the start line, as we don't support that option yet in
// JavaScript.
buffer.readVarInt();
// Skip past the line offsets, as there is no Source object yet in JavaScript.
// const lineOffsets = Array.from({ length: buffer.readVarInt() }, () => buffer.readVarInt());
const lineOffsetsCount = buffer.readVarInt();
for (let i = 0; i < lineOffsetsCount; i ++) {
buffer.readVarInt();
}
const comments = Array.from({ length: buffer.readVarInt() }, () => ({
type: buffer.readVarInt(),
location: buffer.readLocation()
}));
const magicComments = Array.from({ length: buffer.readVarInt() }, () => ({
startLocation: buffer.readLocation(),
endLocation: buffer.readLocation()
}));
const dataLoc = buffer.readOptionalLocation();
const errors = Array.from({ length: buffer.readVarInt() }, () => ({
type: errorTypes[buffer.readVarInt()],
message: buffer.readString(buffer.readVarInt()),
location: buffer.readLocation(),
level: errorLevels[buffer.readByte()]
}));
const warnings = Array.from({ length: buffer.readVarInt() }, () => ({
type: warningTypes[buffer.readVarInt()],
message: buffer.readString(buffer.readVarInt()),
location: buffer.readLocation(),
level: warningLevels[buffer.readByte()]
}));
const constantPoolOffset = buffer.readUint32();
const constants = Array.from({ length: buffer.readVarInt() }, () => null);
return new ParseResult(readRequiredNode(), comments, magicComments, dataLoc, errors, warnings);
function readRequiredNode() {
const type = buffer.readByte();
const nodeID = buffer.readVarInt();
const location = buffer.readLocation();
let flags;
switch (type) {
<%- nodes.each.with_index(1) do |node, index| -%>
case <%= index %>:
<%- if node.needs_serialized_length? -%>
buffer.readUint32();
<%- end -%>
return new nodes.<%= node.name %>(<%= ["nodeID", "location", "flags = buffer.readVarInt()", *node.fields.map { |field|
case field
when Prism::Template::NodeField then "readRequiredNode()"
when Prism::Template::OptionalNodeField then "readOptionalNode()"
when Prism::Template::StringField then "buffer.readStringField(flags)"
when Prism::Template::NodeListField then "Array.from({ length: buffer.readVarInt() }, readRequiredNode)"
when Prism::Template::ConstantField then "readRequiredConstant()"
when Prism::Template::OptionalConstantField then "readOptionalConstant()"
when Prism::Template::ConstantListField then "Array.from({ length: buffer.readVarInt() }, readRequiredConstant)"
when Prism::Template::LocationField then "buffer.readLocation()"
when Prism::Template::OptionalLocationField then "buffer.readOptionalLocation()"
when Prism::Template::UInt8Field then "buffer.readByte()"
when Prism::Template::UInt32Field then "buffer.readVarInt()"
when Prism::Template::IntegerField then "readInteger()"
when Prism::Template::DoubleField then "buffer.readDouble()"
end
}].join(", ") -%>);
<%- end -%>
default:
throw new Error(`Unknown node type: ${type}`);
}
}
function readOptionalNode() {
if (buffer.readByte() != 0) {
buffer.index -= 1;
return readRequiredNode();
} else {
return null;
}
}
function scanConstant(constantIndex) {
if (constants[constantIndex] === null) {
constants[constantIndex] = buffer.scanConstant(constantPoolOffset, constantIndex);
}
return constants[constantIndex];
}
function readRequiredConstant() {
return scanConstant(buffer.readVarInt() - 1);
}
function readOptionalConstant() {
const index = buffer.readVarInt();
if (index === 0) {
return null;
} else {
return scanConstant(index - 1);
}
}
function readInteger() {
const negative = buffer.readByte() != 0;
const length = buffer.readVarInt();
const firstWord = buffer.readVarInt();
if (length == 1) {
if (negative && firstWord >= 0x80000000) {
return -BigInt(firstWord);
} else if (negative) {
return -firstWord;
} else {
return firstWord;
}
}
let result = BigInt(firstWord);
for (let index = 1; index < length; index++) {
result |= (BigInt(buffer.readVarInt()) << BigInt(index * 32));
}
return negative ? -result : result;
}
}