Skip to content

Commit

Permalink
close #524: convert predicate sets into Maps instead of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Dec 1, 2019
1 parent 79c02fc commit 846b8d2
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions lib/compress/index.js
Expand Up @@ -2695,17 +2695,19 @@ function best_of(compressor, ast1, ast2) {
}

function convert_to_predicate(obj) {
const out = new Map();
for (var key of Object.keys(obj)) {
obj[key] = makePredicate(obj[key]);
out.set(key, makePredicate(obj[key]));
}
return out;
}

var object_fns = [
"constructor",
"toString",
"valueOf",
];
var native_fns = {
var native_fns = convert_to_predicate({
Array: [
"indexOf",
"join",
Expand Down Expand Up @@ -2741,9 +2743,8 @@ var native_fns = {
"toUpperCase",
"trim",
].concat(object_fns),
};
convert_to_predicate(native_fns);
var static_fns = {
});
var static_fns = convert_to_predicate({
Array: [
"isArray",
],
Expand Down Expand Up @@ -2783,8 +2784,7 @@ var static_fns = {
String: [
"fromCharCode",
],
};
convert_to_predicate(static_fns);
});

// methods to evaluate a constant expression
(function(def_eval) {
Expand Down Expand Up @@ -2989,7 +2989,7 @@ convert_to_predicate(static_fns);
Object: Object,
String: String,
};
var static_values = {
var static_values = convert_to_predicate({
Math: [
"E",
"LN10",
Expand All @@ -3007,8 +3007,7 @@ convert_to_predicate(static_fns);
"NEGATIVE_INFINITY",
"POSITIVE_INFINITY",
],
};
convert_to_predicate(static_values);
});
def_eval(AST_PropAccess, function(compressor, depth) {
if (compressor.option("unsafe")) {
var key = this.property;
Expand All @@ -3032,7 +3031,7 @@ convert_to_predicate(static_fns);
if (first_arg == null || first_arg.thedef && first_arg.thedef.undeclared) {
return this.clone();
}
var static_value = static_values[exp.name];
var static_value = static_values.get(exp.name);
if (!static_value || !static_value.has(key)) return this;
val = global_objs[exp.name];
} else {
Expand Down Expand Up @@ -3072,13 +3071,13 @@ convert_to_predicate(static_fns);
if ((first_arg == null || first_arg.thedef && first_arg.thedef.undeclared)) {
return this.clone();
}
var static_fn = static_fns[e.name];
var static_fn = static_fns.get(e.name);
if (!static_fn || !static_fn.has(key)) return this;
val = global_objs[e.name];
} else {
val = e._eval(compressor, depth + 1);
if (val === e || !val) return this;
var native_fn = native_fns[val.constructor.name];
var native_fn = native_fns.get(val.constructor.name);
if (!native_fn || !native_fn.has(key)) return this;
}
var args = [];
Expand Down Expand Up @@ -3198,10 +3197,11 @@ AST_Call.DEFMETHOD("is_expr_pure", function(compressor) {
return false;
}
if (is_undeclared_ref(expr) && global_pure_fns.has(expr.name)) return true;
let static_fn;
if (expr instanceof AST_Dot
&& is_undeclared_ref(expr.expression)
&& static_fns.hasOwnProperty(expr.expression.name)
&& static_fns[expr.expression.name].has(expr.property)) {
&& (static_fn = static_fns.get(expr.expression.name))
&& static_fn.has(expr.property)) {
return true;
}
}
Expand All @@ -3213,17 +3213,17 @@ AST_Dot.DEFMETHOD("is_call_pure", function(compressor) {
const expr = this.expression;
let map;
if (expr instanceof AST_Array) {
map = native_fns.Array;
map = native_fns.get("Array");
} else if (expr.is_boolean()) {
map = native_fns.Boolean;
map = native_fns.get("Boolean");
} else if (expr.is_number(compressor)) {
map = native_fns.Number;
map = native_fns.get("Number");
} else if (expr instanceof AST_RegExp) {
map = native_fns.RegExp;
map = native_fns.get("RegExp");
} else if (expr.is_string(compressor)) {
map = native_fns.String;
map = native_fns.get("String");
} else if (!this.may_throw_on_access(compressor)) {
map = native_fns.Object;
map = native_fns.get("Object");
}
return map && map.has(this.property);
});
Expand Down

0 comments on commit 846b8d2

Please sign in to comment.