Add property name mangler

We only touch properties that are present in an object literal, or which are
assigned to.  Example:

    x = { foo: 1 };
    x.bar = 2;
    x["baz"] = 3;
    x[cond ? "qwe" : "asd"] = 4;
    console.log(x.stuff);

The names "foo", "bar", "baz", "qwe" and "asd" will be mangled, and the
resulting mangled names will be used for the same properties throughout the
code.  The "stuff" will not be, since it's just referenced but never
assigned to.

This *will* break most of the code out there, but could work on carefully
written code: do not use eval, do not define methods or properties by
walking an array of names, etc.  Also, a comprehensive list of exclusions
needs to be passed, to avoid mangling properties that are standard in
JavaScript, DOM, used in external libraries etc.
This commit is contained in:
Mihai Bazon
2015-03-14 11:22:28 +02:00
parent 9de7199b88
commit ea3430102c
4 changed files with 267 additions and 3 deletions

View File

@@ -106,10 +106,12 @@ function defaults(args, defs, croak) {
};
function merge(obj, ext) {
var count = 0;
for (var i in ext) if (ext.hasOwnProperty(i)) {
obj[i] = ext[i];
count++;
}
return obj;
return count;
};
function noop() {};
@@ -298,5 +300,11 @@ Dictionary.prototype = {
for (var i in this._values)
ret.push(f(this._values[i], i.substr(1)));
return ret;
}
},
toObject: function() { return this._values }
};
Dictionary.fromObject = function(obj) {
var dict = new Dictionary();
dict._size = merge(dict._values, obj);
return dict;
};