Skip to content

Commit aa66f92

Browse files
authored
Rewrite MinifyLocals from Uglify to Acorn. NFC (#13621)
Basically a simple rewrite from one AST to another, but also using slightly more modern JS while doing so (Set, Map). Some parts differ though as the ASTs are not compatible, so the algorithm is not 100% identical, but the results should be. Test changes are just minor whitespace, caused by the different whitespace style that Uglify used to emit vs the new one. Verified on wasm2js2, 3, s, z which we do not run on CI here. After this we have just one remaining pass using Uglify, MinifyGlobals, after which we can remove it entirely.
1 parent b3b0cb4 commit aa66f92

7 files changed

+200
-153
lines changed

tests/optimizer/constructor-output.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ function instantiate(o, p, q) {
2424
function i() {
2525
return g(10, 20) + h(30);
2626
}
27-
28-
29-
3027
function g(a, b) {
3128
return h(a + b);
3229
}
30+
3331
function h(a) {
3432
return a + 1;
3533
}
3634

35+
3736
// EMSCRIPTEN_END_FUNCS
3837

3938
return {

tests/optimizer/test-js-optimizer-minifyLocals-output.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ function d(f, g, h) {
1919
}
2020

2121
function l() {
22-
a : while (1) {
23-
b : while (1) {
22+
a: while (1) {
23+
b: while (1) {
2424
if (c()) {
2525
if (d()) {
2626
break a;
@@ -36,5 +36,5 @@ function l() {
3636
}
3737
}
3838
}
39-
a : while (1) {}
39+
a: while (1) {}
4040
}

tests/optimizer/wasm2js-output.js

+25-15
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ function E(a, b, c) {
4040
return;
4141
}
4242
e = a + c | 0;
43-
a : {
43+
a: {
4444
if (!((a ^ b) & 3)) {
45-
b : {
45+
b: {
4646
if ((c | 0) < 1) {
4747
c = a;
4848
break b;
@@ -66,7 +66,7 @@ function E(a, b, c) {
6666
}
6767
}
6868
a = e & -4;
69-
c : {
69+
c: {
7070
if (a >>> 0 < 64) {
7171
break c;
7272
}
@@ -148,6 +148,7 @@ function E(a, b, c) {
148148
}
149149
}
150150
}
151+
151152
function H(a, b, c) {
152153
a = a | 0;
153154
b = b | 0;
@@ -165,9 +166,9 @@ function H(a, b, c) {
165166
e = b + c | 0;
166167
i = 2;
167168
b = d + 16 | 0;
168-
a : {
169-
b : {
170-
c : {
169+
a: {
170+
b: {
171+
c: {
171172
if (!D(x(f[a + 60 >> 2], d + 16 | 0, 2, d + 12 | 0) | 0)) {
172173
while (1) {
173174
g = f[d + 12 >> 2];
@@ -216,12 +217,13 @@ function H(a, b, c) {
216217
z = d + 32 | 0;
217218
return a | 0;
218219
}
220+
219221
function B(a, b) {
220222
var e = 0, h = 0, i = 0, j = 0, k = 0;
221223
P();
222224
j = 1024;
223225
e = f[b + 16 >> 2];
224-
a : {
226+
a: {
225227
if (!e) {
226228
if (C(b)) {
227229
break a;
@@ -232,7 +234,7 @@ function B(a, b) {
232234
if (e - i >>> 0 < a >>> 0) {
233235
return c[f[b + 36 >> 2]](b, 1024, a) | 0;
234236
}
235-
b : {
237+
b: {
236238
if (d[b + 75 | 0] < 0) {
237239
break b;
238240
}
@@ -263,13 +265,14 @@ function B(a, b) {
263265
}
264266
return h;
265267
}
268+
266269
function L(a) {
267270
var b = 0, e = 0, g = 0;
268271
b = z - 16 | 0;
269272
z = b;
270273
d[b + 15 | 0] = 10;
271274
e = f[a + 16 >> 2];
272-
a : {
275+
a: {
273276
if (!e) {
274277
if (C(a)) {
275278
break a;
@@ -288,6 +291,7 @@ function L(a) {
288291
}
289292
z = b + 16 | 0;
290293
}
294+
291295
function J() {
292296
var a = 0, b = 0, c = 0;
293297
b = 1024;
@@ -314,16 +318,17 @@ function J() {
314318
}
315319
return b - 1024 | 0;
316320
}
321+
317322
function K() {
318323
var a = 0, b = 0;
319324
a = f[260];
320325
a;
321326
b = J();
322-
a : {
327+
a: {
323328
if ((((M(b, a) | 0) != (b | 0) ? -1 : 0) | 0) < 0) {
324329
break a;
325330
}
326-
b : {
331+
b: {
327332
if (g[a + 75 | 0] == 10) {
328333
break b;
329334
}
@@ -338,6 +343,7 @@ function K() {
338343
L(a);
339344
}
340345
}
346+
341347
function C(a) {
342348
var b = 0;
343349
b = g[a + 74 | 0];
@@ -355,11 +361,12 @@ function C(a) {
355361
f[a + 16 >> 2] = b + f[a + 48 >> 2];
356362
return 0;
357363
}
364+
358365
function M(a, b) {
359366
var c = 0, d = 0;
360367
c = a;
361368
d = c;
362-
a : {
369+
a: {
363370
if (f[b + 76 >> 2] <= -1) {
364371
b = B(c, b);
365372
break a;
@@ -371,6 +378,7 @@ function M(a, b) {
371378
}
372379
return b;
373380
}
381+
374382
function F(a, b, c, d) {
375383
a = a | 0;
376384
b = b | 0;
@@ -379,31 +387,33 @@ function F(a, b, c, d) {
379387
A = 0;
380388
return 0;
381389
}
390+
382391
function D(a) {
383392
if (!a) {
384393
return 0;
385394
}
386395
f[300] = a;
387396
return -1;
388397
}
398+
389399
function N(a, b) {
390400
a = a | 0;
391401
b = b | 0;
392402
K();
393403
return 0;
394404
}
405+
395406
function I(a) {
396407
a = a | 0;
397408
return u() | 0;
398409
}
410+
399411
function G(a) {
400412
a = a | 0;
401413
return 0;
402414
}
403-
function O() {}
404-
405-
406415

416+
function O() {}
407417

408418
// EMSCRIPTEN_END_FUNCS
409419

tests/test_other.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,8 @@ def test_js_optimizer(self):
18601860
'growableHeap',
18611861
'unsignPointers',
18621862
'asanify',
1863-
'safeHeap'
1863+
'safeHeap',
1864+
'minifyLocals',
18641865
]
18651866
for input, expected, passes in [
18661867
(path_from_root('tests', 'optimizer', 'test-js-optimizer-minifyGlobals.js'), open(path_from_root('tests', 'optimizer', 'test-js-optimizer-minifyGlobals-output.js')).read(),

0 commit comments

Comments
 (0)