-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathlexer.js
executable file
·98 lines (85 loc) · 2.84 KB
/
lexer.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
let wasm;
const isLE = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
export function parse (source, name = '@') {
if (!wasm)
throw new Error('Not initialized');
const len = source.length + 1;
// need 2 bytes per code point plus analysis space so we double again
const extraMem = (wasm.__heap_base.value || wasm.__heap_base) + len * 4 - wasm.memory.buffer.byteLength;
if (extraMem > 0)
wasm.memory.grow(Math.ceil(extraMem / 65536));
const addr = wasm.sa(len);
(isLE ? copyLE : copyBE)(source, new Uint16Array(wasm.memory.buffer, addr, len));
if (!wasm.parseCJS(addr, source.length, 0, 0, 0))
throw Object.assign(new Error(`Parse error ${name}${wasm.e()}:${source.slice(0, wasm.e()).split('\n').length}:${wasm.e() - source.lastIndexOf('\n', wasm.e() - 1)}`), { idx: wasm.e() });
let exports = new Set(), reexports = new Set(), unsafeGetters = new Set();
while (wasm.rre()) {
const reexptStr = decode(source.slice(wasm.res(), wasm.ree()));
if (reexptStr)
reexports.add(reexptStr);
}
while (wasm.ru())
unsafeGetters.add(decode(source.slice(wasm.us(), wasm.ue())));
while (wasm.re()) {
let exptStr = decode(source.slice(wasm.es(), wasm.ee()));
if (exptStr !== undefined && !unsafeGetters.has(exptStr))
exports.add(exptStr);
}
return { exports: [...exports], reexports: [...reexports] };
}
function decode (str) {
if (str[0] === '"' || str[0] === '\'') {
try {
const decoded = (0, eval)(str);
// Filter to exclude non-matching UTF-16 surrogate strings
for (let i = 0; i < decoded.length; i++) {
const surrogatePrefix = decoded.charCodeAt(i) & 0xFC00;
if (surrogatePrefix < 0xD800) {
// Not a surrogate
continue;
}
else if (surrogatePrefix === 0xD800) {
// Validate surrogate pair
if ((decoded.charCodeAt(++i) & 0xFC00) !== 0xDC00)
return;
}
else {
// Out-of-range surrogate code (above 0xD800)
return;
}
}
return decoded;
}
catch {}
}
else {
return str;
}
}
function copyBE (src, outBuf16) {
const len = src.length;
let i = 0;
while (i < len) {
const ch = src.charCodeAt(i);
outBuf16[i++] = (ch & 0xff) << 8 | ch >>> 8;
}
}
function copyLE (src, outBuf16) {
const len = src.length;
let i = 0;
while (i < len)
outBuf16[i] = src.charCodeAt(i++);
}
let initPromise;
export function init () {
if (initPromise)
return initPromise;
return initPromise = (async () => {
const compiled = await WebAssembly.compile(
(binary => typeof window !== 'undefined' && typeof atob === 'function' ? Uint8Array.from(atob(binary), x => x.charCodeAt(0)) : Buffer.from(binary, 'base64'))
('WASM_BINARY')
)
const { exports } = await WebAssembly.instantiate(compiled);
wasm = exports;
})();
}