no base54.reset()
slightly less improved gzip results
This commit is contained in:
parent
13062a4758
commit
423d0146be
104
lib/scope.js
104
lib/scope.js
|
|
@ -458,17 +458,14 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
||||||
this.walk(tw);
|
this.walk(tw);
|
||||||
to_mangle.forEach(function(def){ def.mangle(options) });
|
to_mangle.forEach(function(def){ def.mangle(options) });
|
||||||
|
|
||||||
if (options.group_voids) {
|
if (!options.group_voids) return;
|
||||||
base54.reset();
|
if (options.toplevel) this.group_voids(options);
|
||||||
base54.sort();
|
else this.walk(new TreeWalker(function(node) {
|
||||||
if (options.toplevel) this.group_voids(options);
|
if (node instanceof AST_Scope && !(node instanceof AST_Toplevel)) {
|
||||||
else this.walk(new TreeWalker(function(node) {
|
node.group_voids(options);
|
||||||
if (node instanceof AST_Scope && !(node instanceof AST_Toplevel)) {
|
return true;
|
||||||
node.group_voids(options);
|
}
|
||||||
return true;
|
}));
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
function collect(symbol) {
|
function collect(symbol) {
|
||||||
if (!member(symbol.name, options.reserved)) {
|
if (!member(symbol.name, options.reserved)) {
|
||||||
|
|
@ -477,11 +474,11 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Scope.DEFMETHOD("find_colliding_names", function(options, all) {
|
AST_Toplevel.DEFMETHOD("find_colliding_names", function(options) {
|
||||||
var cache = options.cache && options.cache.props;
|
var cache = options.cache && options.cache.props;
|
||||||
var avoid = Object.create(null);
|
var avoid = Object.create(null);
|
||||||
options.reserved.forEach(to_avoid);
|
options.reserved.forEach(to_avoid);
|
||||||
if (this.globals) this.globals.each(add_def);
|
this.globals.each(add_def);
|
||||||
this.walk(new TreeWalker(function(node) {
|
this.walk(new TreeWalker(function(node) {
|
||||||
if (node instanceof AST_Scope) node.variables.each(add_def);
|
if (node instanceof AST_Scope) node.variables.each(add_def);
|
||||||
if (node instanceof AST_SymbolCatch) add_def(node.definition());
|
if (node instanceof AST_SymbolCatch) add_def(node.definition());
|
||||||
|
|
@ -493,9 +490,9 @@ AST_Scope.DEFMETHOD("find_colliding_names", function(options, all) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function add_def(def) {
|
function add_def(def) {
|
||||||
var name = def.mangled_name || def.name;
|
var name = def.name;
|
||||||
if (def.global && cache && cache.has(name)) name = cache.get(name);
|
if (def.global && cache && cache.has(name)) name = cache.get(name);
|
||||||
else if (!all && !def.unmangleable(options)) return;
|
else if (!def.unmangleable(options)) return;
|
||||||
to_avoid(name);
|
to_avoid(name);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -536,39 +533,56 @@ AST_Toplevel.DEFMETHOD("expand_names", function(options) {
|
||||||
});
|
});
|
||||||
|
|
||||||
AST_Scope.DEFMETHOD("group_voids", function(options) {
|
AST_Scope.DEFMETHOD("group_voids", function(options) {
|
||||||
var avoid = this.find_colliding_names(options, true);
|
var self = this, enclosed = [];
|
||||||
var cname = 0;
|
var scopes = [], count = 0;
|
||||||
var name;
|
self.walk(new TreeWalker(function(node, descend) {
|
||||||
do {
|
if (node instanceof AST_Scope && node !== self) {
|
||||||
name = base54(cname++);
|
scopes.unshift(node);
|
||||||
} while (avoid[name] || !is_identifier(name));
|
descend();
|
||||||
var count = 0;
|
if (scopes[0] === node) scopes.shift();
|
||||||
this.transform(new TreeTransformer(function(node) {
|
return true;
|
||||||
if (node instanceof AST_Undefined
|
|
||||||
|| node instanceof AST_UnaryPrefix
|
|
||||||
&& node.operator == "void"
|
|
||||||
&& node.expression.is_constant()) {
|
|
||||||
count++;
|
|
||||||
return new AST_SymbolRef({
|
|
||||||
name: name,
|
|
||||||
start: node.start,
|
|
||||||
end: node.end
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}));
|
if (count > 0 && scopes.length == 0) return;
|
||||||
if (count) {
|
if (is_undefined(node)) {
|
||||||
for (var i = 0, len = this.body.length; i < len; i++) {
|
count++;
|
||||||
var stat = this.body[i];
|
var scope;
|
||||||
if (stat instanceof AST_Var) {
|
while (scope = scopes.shift()) {
|
||||||
stat.definitions.push(make_var_def(stat));
|
scope.enclosed.forEach(function(def) {
|
||||||
return;
|
push_uniq(enclosed, def);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.body.push(new AST_Var({
|
}));
|
||||||
definitions: [ make_var_def(this) ],
|
if (count < 1) return;
|
||||||
start: this.start,
|
var save = self.enclosed;
|
||||||
end: this.end
|
self.enclosed = enclosed;
|
||||||
}));
|
var name = next_mangled(self, options);
|
||||||
|
self.enclosed = save;
|
||||||
|
this.transform(new TreeTransformer(function(node) {
|
||||||
|
if (is_undefined(node)) return new AST_SymbolRef({
|
||||||
|
name: name,
|
||||||
|
start: node.start,
|
||||||
|
end: node.end
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
for (var i = 0, len = this.body.length; i < len; i++) {
|
||||||
|
var stat = this.body[i];
|
||||||
|
if (stat instanceof AST_Var) {
|
||||||
|
stat.definitions.push(make_var_def(stat));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.body.push(new AST_Var({
|
||||||
|
definitions: [ make_var_def(this) ],
|
||||||
|
start: this.start,
|
||||||
|
end: this.end
|
||||||
|
}));
|
||||||
|
|
||||||
|
function is_undefined(node) {
|
||||||
|
return node instanceof AST_Undefined
|
||||||
|
|| node instanceof AST_UnaryPrefix
|
||||||
|
&& node.operator == "void"
|
||||||
|
&& node.expression.is_constant();
|
||||||
}
|
}
|
||||||
|
|
||||||
function make_var_def(node) {
|
function make_var_def(node) {
|
||||||
|
|
|
||||||
|
|
@ -26,13 +26,13 @@ group_voids: {
|
||||||
console.log(void 0);
|
console.log(void 0);
|
||||||
for (var a = 4;;);
|
for (var a = 4;;);
|
||||||
var b = 4;
|
var b = 4;
|
||||||
function f5() {
|
function g() {
|
||||||
var c = 5;
|
var c = 5;
|
||||||
var d = 5;
|
var d = 5;
|
||||||
console.log(void 0);
|
console.log(void 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function f6() {
|
function f5() {
|
||||||
try {
|
try {
|
||||||
var a = 6;
|
var a = 6;
|
||||||
console.log(void 0);
|
console.log(void 0);
|
||||||
|
|
@ -47,35 +47,35 @@ group_voids: {
|
||||||
if (void 0 === b)
|
if (void 0 === b)
|
||||||
c = void 0;
|
c = void 0;
|
||||||
function f1() {
|
function f1() {
|
||||||
var o = 1, a;
|
var o = 1, n;
|
||||||
console.log(a);
|
console.log(n);
|
||||||
}
|
}
|
||||||
function f2(o) {
|
function f2(o) {
|
||||||
var n = 2, a;
|
var n = 2, v;
|
||||||
console.log(a);
|
console.log(v);
|
||||||
}
|
}
|
||||||
function f3() {
|
function f3() {
|
||||||
var o = 3, a;
|
var o = 3, n;
|
||||||
console.log(a);
|
console.log(n);
|
||||||
}
|
}
|
||||||
function f4() {
|
function f4() {
|
||||||
console.log(a);
|
console.log(i);
|
||||||
for(var o = 4;;);
|
for(var o = 4;;);
|
||||||
var n = 4, a;
|
var n = 4, i;
|
||||||
function v() {
|
function v() {
|
||||||
var o = 5;
|
var o = 5;
|
||||||
var n = 5;
|
var n = 5;
|
||||||
console.log(a);
|
console.log(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function f6() {
|
function f5() {
|
||||||
try {
|
try {
|
||||||
var o = 6;
|
var o = 6;
|
||||||
console.log(a);
|
console.log(n);
|
||||||
} catch (o) {
|
} catch (o) {
|
||||||
console.log(a);
|
console.log(n);
|
||||||
}
|
}
|
||||||
var a;
|
var n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -108,13 +108,13 @@ group_voids_toplevel: {
|
||||||
console.log(void 0);
|
console.log(void 0);
|
||||||
for (var a = 4;;);
|
for (var a = 4;;);
|
||||||
var b = 4;
|
var b = 4;
|
||||||
function f5() {
|
function g() {
|
||||||
var c = 5;
|
var c = 5;
|
||||||
var d = 5;
|
var d = 5;
|
||||||
console.log(void 0);
|
console.log(void 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function f6() {
|
function f5() {
|
||||||
try {
|
try {
|
||||||
var a = 6;
|
var a = 6;
|
||||||
console.log(void 0);
|
console.log(void 0);
|
||||||
|
|
@ -168,8 +168,7 @@ group_voids_catch: {
|
||||||
group_voids: true,
|
group_voids: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
f();
|
(function() {
|
||||||
function f() {
|
|
||||||
var a = 1;
|
var a = 1;
|
||||||
console.log(void 0);
|
console.log(void 0);
|
||||||
try {
|
try {
|
||||||
|
|
@ -177,19 +176,18 @@ group_voids_catch: {
|
||||||
} catch (undefined) {
|
} catch (undefined) {
|
||||||
console.log(void 0);
|
console.log(void 0);
|
||||||
}
|
}
|
||||||
}
|
})();
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
f();
|
(function() {
|
||||||
function f() {
|
var o = 1, c;
|
||||||
var o = 1, a;
|
console.log(c);
|
||||||
console.log(a);
|
|
||||||
try {
|
try {
|
||||||
throw "FAIL";
|
throw "FAIL";
|
||||||
} catch (o) {
|
} catch (o) {
|
||||||
console.log(a);
|
console.log(c);
|
||||||
}
|
}
|
||||||
}
|
})();
|
||||||
}
|
}
|
||||||
expect_stdout: [
|
expect_stdout: [
|
||||||
"undefined",
|
"undefined",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user