Skip to content

Commit

Permalink
Use ESTree-based optimizer for core JS passes, to support ES6+ inputs (
Browse files Browse the repository at this point in the history
…#7973)

Fixes #6000

The key change here is to rewrite the JS optimizer passes that run in a normal `-O3` etc. build from the Uglify1 AST to ESTree. With ESTree we can use modern parsers etc. so that we support ES6+ inputs to js libraries, pre-jses, EM_ASM, etc.

Aside from that rewrite, the other changes are less critical and can be altered later. Specifically, this uses acorn for parsing and terser for outputting, but we could switch to anything using ESTree very easily. Acorn is nice for parsing since it's small and standalone. For outputting I experimented with astring, which is small and nice, and escodegen, which looks very robust, but neither could output compact-enough JS to not regress our JS code sizes. This is not truly critical since for minimal code size people should use closure anyhow, however, it's nice for default builds to be small (and we don't run closure by default), and I didn't want to regress anything. Using the terser outputter achieves that. (Since it uses the Uglify2 AST internally, this means using their tool to convert ESTree to Uglify2.) They may be some minor code size changes with this PR, just because we use a different outputter now, but nothing major in either direction. Most changes seem positive actually. Sizes after closure are unchanged.

This uses almost unmodified versions of acorn and terser, but they are stripped down to what we need, and I had to make two modifications, see these PRs: [acornjs/acorn#793](acornjs/acorn#793) (quote the error on parse exceptions) and [mishoo/UglifyJS#3323](mishoo/UglifyJS#3323) (preserve quoted properties).

This may very slightly regress compile times when using those passes, as Uglify1 was just very fast. However, the change should be very small.

This does _not_ rewrite every single JS optimizer pass. In particular the asm.js passes don't need to support ES6, and so don't need to be rewritten. There are also optional passes that do not run by default, that we can convert later depending on priority.
  • Loading branch information
kripken authored Feb 8, 2019
1 parent c3e6c60 commit 59f08fb
Show file tree
Hide file tree
Showing 25 changed files with 17,441 additions and 601 deletions.
14 changes: 10 additions & 4 deletions tests/optimizer/AJSDCE-output.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@

var z = fleefl();

var zz = fleefl();

function g(a) {
return a + 1;
}

Module["g"] = g;

function h(a) {
return a + 1;
}

print(h(123));
((function() {

(function() {
var z = fleefl();
var zz = fleefl();
function g(a) {
Expand All @@ -20,8 +25,9 @@ print(h(123));
return a + 1;
}
print(hh(123));
}))();
})();

function glue() {
}
glue();

glue();
3 changes: 3 additions & 0 deletions tests/optimizer/JSDCE-fors-output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (var i in x) {}

for (var j = 0; ;) {}
4 changes: 4 additions & 0 deletions tests/optimizer/JSDCE-fors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

for (var i in x) {}
for (var j = 0;;) {}

1 change: 1 addition & 0 deletions tests/optimizer/JSDCE-hasOwnProperty-output.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}

if (hasOwnProperty({}, "prop_name")) {
console.log("yeah");
}
26 changes: 20 additions & 6 deletions tests/optimizer/JSDCE-output.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@

var z = fleefl();

var zz = fleefl();

var keeperObj = {
x: fleefl()
};

var keeperArray = [ 1, 2, "3", four() ];

function g(a) {
return a + 1;
}

Module["g"] = g;

function h(a) {
return a + 1;
}

print(h(123));
((function() {

(function() {
var z = fleefl();
var zz = fleefl();
function g(a) {
Expand All @@ -24,31 +31,38 @@ print(h(123));
return a + 1;
}
print(hh(123));
}))();
})();

function glue() {
function lookup() {
throw 1;
}
}

glue();

function _glCreateShader() {
return 1;
}

function emulate() {
_glCreateShader = function _glCreateShader(shaderType) {
return glCreateShader();
};
}

emulate();

___cxa_find_matching_catch_before();

function ___cxa_find_matching_catch_before() {
if (!___cxa_find_matching_catch_before.buffer) ___cxa_find_matching_catch_before.buffer = {};
}

function ___cxa_find_matching_catch_after() {
if (!___cxa_find_matching_catch_after.buffer) ___cxa_find_matching_catch_after.buffer = {};
}
___cxa_find_matching_catch_after();
var dotOther = Side.effect;


___cxa_find_matching_catch_after();

var dotOther = Side.effect;
54 changes: 0 additions & 54 deletions tests/optimizer/JSDCE-uglifyjsNodeTypes-output.js

This file was deleted.

47 changes: 0 additions & 47 deletions tests/optimizer/JSDCE-uglifyjsNodeTypes.js

This file was deleted.

32 changes: 23 additions & 9 deletions tests/optimizer/applyDCEGraphRemovals-output.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
var name;

var asmLibraryArg = {
"save1": 1,
"save2": 2
};

var expD1 = Module["expD1"] = asm["expD1"];

var expD2 = Module["expD2"] = asm["expD2"];

var expD3 = Module["expD3"] = asm["expD3"];
var expD4 = undefined;
var expI1 = Module["expI1"] = (function() {

var expD4;

var expI1 = Module["expI1"] = function() {
return Module["asm"]["expI1"].apply(null, arguments);
});
var expI2 = Module["expI2"] = (function() {
};

var expI2 = Module["expI2"] = function() {
return Module["asm"]["expI2"].apply(null, arguments);
});
var expI3 = Module["expI3"] = (function() {
};

var expI3 = Module["expI3"] = function() {
return Module["asm"]["expI3"].apply(null, arguments);
});
var expI4 = undefined;
};

var expI4;

expD1;

Module["expD2"];

asm["expD3"];

expI1;

Module["expI2"];
asm["expI3"];

asm["expI3"];
10 changes: 5 additions & 5 deletions tests/optimizer/applyImportAndExportNameChanges-output.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var name;

var asmLibraryArg = {
"a": 1,
"A": 33,
Expand All @@ -12,10 +13,9 @@ var asmLibraryArg = {
"d": ___syscall140,
"q": ___syscall146
};
var expD1 = Module["expD1"] = asm["c"];
var expI1 = Module["expI1"] = (function() {
return Module["asm"]["d"].apply(null, arguments);
});


var expD1 = Module["expD1"] = asm["c"];

var expI1 = Module["expI1"] = function() {
return Module["asm"]["d"].apply(null, arguments);
};
Loading

0 comments on commit 59f08fb

Please sign in to comment.