Skip to content

Commit

Permalink
enhance unsafe evaluate (#3564)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Nov 1, 2019
1 parent dcbf223 commit 1c0defd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
12 changes: 8 additions & 4 deletions lib/compress.js
Expand Up @@ -2995,6 +2995,7 @@ merge(Compressor.prototype, {
],
};
convert_to_predicate(static_values);
var regexp_props = makePredicate("global ignoreCase multiline source");
def(AST_PropAccess, function(compressor, cached, depth) {
if (compressor.option("unsafe")) {
var key = this.property;
Expand All @@ -3010,9 +3011,12 @@ merge(Compressor.prototype, {
val = global_objs[exp.name];
} else {
val = exp._eval(compressor, cached, depth + 1);
if (!val || val === exp) return this;
if (typeof val == "object" && !HOP(val, key)) return this;
if (typeof val == "function") switch (key) {
if (val == null || val === exp) return this;
if (val instanceof RegExp) {
if (!regexp_props[key]) return this;
} else if (typeof val == "object") {
if (!HOP(val, key)) return this;
} else if (typeof val == "function") switch (key) {
case "name":
return val.node.name ? val.node.name.name : "";
case "length":
Expand Down Expand Up @@ -3060,7 +3064,7 @@ merge(Compressor.prototype, {
val = global_objs[e.name];
} else {
val = e._eval(compressor, cached, depth + 1);
if (val === e || !val) return this;
if (val == null || val === e) return this;
var native_fn = native_fns[val.constructor.name];
if (!native_fn || !native_fn[key]) return this;
}
Expand Down
43 changes: 30 additions & 13 deletions test/compress/evaluate.js
Expand Up @@ -237,22 +237,39 @@ unsafe_constant: {
unsafe: true,
}
input: {
console.log(
true.a,
false.a,
null.a,
undefined.a
);
console.log(true.a, false.a);
console.log(true.valueOf(), false.valueOf());
try {
console.log(null.a);
} catch (e) {
console.log("PASS");
}
try {
console.log(undefined.a);
} catch (e) {
console.log("PASS");
}
}
expect: {
console.log(
void 0,
false.a,
null.a,
(void 0).a
);
console.log(void 0, void 0);
console.log(true, false);
try {
console.log(null.a);
} catch (e) {
console.log("PASS");
}
try {
console.log((void 0).a);
} catch (e) {
console.log("PASS");
}
}
expect_stdout: true
expect_stdout: [
"undefined undefined",
"true false",
"PASS",
"PASS",
]
}

unsafe_object: {
Expand Down
14 changes: 14 additions & 0 deletions test/compress/regexp.js
Expand Up @@ -36,6 +36,20 @@ regexp_2: {
expect_stdout: '["PASS","pass"]'
}

regexp_properties: {
options = {
evaluate: true,
unsafe: true,
}
input: {
console.log(/abc/g.source, /abc/g.global, /abc/g.ignoreCase, /abc/g.lastIndex, /abc/g.multiline);
}
expect: {
console.log("abc", true, false, /abc/g.lastIndex, false);
}
expect_stdout: "abc true false 0 false"
}

issue_3434_1: {
options = {
evaluate: true,
Expand Down

0 comments on commit 1c0defd

Please sign in to comment.