-
Notifications
You must be signed in to change notification settings - Fork 12
/
decode.js
executable file
·59 lines (45 loc) · 1.52 KB
/
decode.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
#!/usr/bin/node
require('./astutil.js');
require('./node-augh.js');
require('./third_party/encoding/encoding.js');
var common = require("./ast-common.js");
var Configuration = require("./configuration.js");
var astDecoder = require('./ast-decoder.js');
var fs = require('fs');
if (process.argv.length < 4) {
console.log("USAGE: decode input.bast [astOutput.json] [configuration.json]");
process.exit(1);
}
var inputFile = process.argv[2];
var outputAstFile = process.argv[3];
var configurationPath = process.argv[4];
if (!configurationPath)
throw new Error("Configuration file required");
var configuration = Configuration.FromJson(
fs.readFileSync(configurationPath, { encoding: "utf8" })
);
var inputBuffer = fs.readFileSync(inputFile), inputBytes;
if (inputBuffer.toArrayBuffer)
inputBytes = inputBuffer.toArrayBuffer;
else
inputBytes = new Uint8Array(inputBuffer);
var shapes = astDecoder.ShapeTable.fromJson(
fs.readFileSync("shapes-jsontree.json", { encoding: "utf8" })
);
console.time("bytesToModule");
var inputModule = astDecoder.bytesToModule(configuration, shapes, inputBytes);
console.timeEnd("bytesToModule");
inputBytes = null;
if ((typeof (global) !== "undefined") && global.gc) {
global.gc();
}
var outputAst = inputModule.root;
console.log("heapUsed " + process.memoryUsage().heapUsed);
if (common.DumpJson && outputAstFile) {
var json;
if (astDecoder.PrettyJson)
json = JSON.stringify(outputAst, null, 2)
else
json = JSON.stringify(outputAst);
fs.writeFileSync(outputAstFile, json);
}