-
Notifications
You must be signed in to change notification settings - Fork 0
/
em2app.ts
88 lines (85 loc) · 2.14 KB
/
em2app.ts
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
import {Descriptor, Grit} from '../src/';
import {readFileSync} from 'fs';
import {argv} from 'process';
function parseEmJs(js: string): Descriptor {
// Metadata storage.
let grit: Grit = {
staticBump: NaN,
tableSize: NaN,
};
let descriptor: Descriptor = {
grit,
main: '',
title: '',
};
// Prep handlers.
interface Handler {
pattern: RegExp;
process: (match: RegExpMatchArray) => void;
};
let handlers: Handler[] = [
{
pattern: /var wasmBinaryFile = .*'((.*).wasm)';/,
process(match) {
descriptor.main = match[1];
descriptor.title = match[2];
},
},
{
pattern: /__ATINIT__\.push\((.*)\);/,
process(match) {
let items = match[1].split(',');
items = items.filter(item => item.trim());
if (items.length) {
grit.atInits = items.map((item) => {
return item.match(/function\(\) \{ (\w+)\(\) }/)![1];
});
}
},
},
{
pattern: /var STATIC_BUMP = (\d+);/,
process(match) {
grit.staticBump = Number(match[1]);
},
},
{
// For, presume wasmTableSize and wasmMaxTableSize are the same.
pattern: /Module\['wasmMaxTableSize'] = (\d+);/,
process(match) {
grit.tableSize = Number(match[1]);
},
}
];
// Parse.
let lines = js.split('\n');
for (let line of lines) {
handlers.some((handler, index) => {
let match = line.match(handler.pattern);
if (match) {
handler.process(match);
// No need to keep this handler around anymore.
// TODO Could make keys on handlers to remove all matching keys if we
// TODO need different handlers for different versions of output.
handlers.splice(index, 1);
return true;
}
return false;
});
if (!handlers.length) {
// Got everything we needed.
break;
}
}
// All done.
return descriptor;
}
function main() {
let jsName = argv[2];
let js = readFileSync(jsName).toString();
let descriptor = parseEmJs(js);
let json = JSON.stringify(descriptor);
// console.log(descriptor);
console.log(json);
}
main();