improve js optimizer removal of assigns of undefined

This commit is contained in:
Alon Zakai 2011-11-20 00:11:47 -08:00
Родитель b27d8da1c7
Коммит a1dce8a341
2 изменённых файлов: 22 добавлений и 10 удалений

Просмотреть файл

@ -26,6 +26,11 @@ eval(read('utility.js'));
var FUNCTION = set('defun', 'function');
var NULL_NODE = ['name', 'null'];
var UNDEFINED_NODE = ['unary-prefix', 'void', ['num', 0]];
var TRUE_NODE = ['unary-prefix', '!', ['num', 0]];
var FALSE_NODE = ['unary-prefix', '!', ['num', 1]];
// Traverses a JavaScript syntax tree rooted at the given node calling the given
// callback for each node.
// @arg node: The root of the AST.
@ -112,10 +117,10 @@ function unGlobalize(ast) {
var value = varItem[1];
if (!value) return true;
var possible = false;
if (jsonCompare(value, ['name', 'null']) ||
jsonCompare(value, ['unary-prefix', 'void', ['num', 0]]) ||
jsonCompare(value, ['unary-prefix', '!', ['num', 0]]) ||
jsonCompare(value, ['unary-prefix', '!', ['num', 1]])) {
if (jsonCompare(value, NULL_NODE) ||
jsonCompare(value, UNDEFINED_NODE) ||
jsonCompare(value, TRUE_NODE) ||
jsonCompare(value, FALSE_NODE)) {
possible = true;
}
if (!possible) return true;
@ -159,6 +164,13 @@ function removeAssignsToUndefined(ast) {
traverse(ast, function(node, type) {
if (type == 'assign' && jsonCompare(node[3], ['unary-prefix', 'void', ['num', 0]])) {
return emptyNode();
} else if (type == 'var') {
node[1] = node[1].map(function(varItem, j) {
var ident = varItem[0];
var value = varItem[1];
if (jsonCompare(value, UNDEFINED_NODE)) return [ident];
return [ident, value];
});
}
});
}

Просмотреть файл

@ -6,11 +6,11 @@ function abc() {
var cheez = [ void 0 ];
var fleefl;
cheez = 10;
var waka = void 0, flake = void 0, marfoosh = void 0;
var waka2 = 5, flake2 = void 0, marfoosh2 = void 0;
var waka3 = void 0, flake3 = 5, marfoosh3 = void 0;
var waka4 = void 0, flake4 = void 0, marfoosh4 = 5;
var test1 = !0, test2 = !1, test3 = null, test4 = z, test5 = b, test6 = void 0;
var waka, flake, marfoosh;
var waka2 = 5, flake2, marfoosh2;
var waka3, flake3 = 5, marfoosh3;
var waka4, flake4, marfoosh4 = 5;
var test1 = !0, test2 = !1, test3 = null, test4 = z, test5 = b, test6;
s3 = 9;
}
function xyz() {
@ -31,5 +31,5 @@ function xyz2(x) {
var cheez = x;
}
zzz = (function(nada) {
var cheez = void 0;
var cheez;
});