Skip to content

Commit 1f0333e

Browse files
authoredMar 5, 2017
stay safe with constants in IE8- (#1547)
- `undefined` etc. can be redefined at top-level for IE8-, so disable related optimisations - fixed `--support-ie8` catch mangle bug
1 parent eb98a7f commit 1f0333e

File tree

5 files changed

+52
-20
lines changed

5 files changed

+52
-20
lines changed
 

‎README.md

-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ The available options are:
7575
--support-ie8 Use this flag to support Internet Explorer 6/7/8.
7676
Equivalent to setting `screw_ie8: false` in `minify()`
7777
for `compress`, `mangle` and `output` options.
78-
Note: `--support-ie8` may generate incorrect code
79-
for `try`/`catch` in ES5 compliant browsers.
8078
--expr Parse a single expression, rather than a
8179
program (for parsing JSON)
8280
-p, --prefix Skip prefix for original filenames that appear

‎bin/uglifyjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mangling you need to use `-c` and `-m`.\
2626
.describe("source-map-include-sources", "Pass this flag if you want to include the content of source files in the source map as sourcesContent property.")
2727
.describe("in-source-map", "Input source map, useful if you're compressing JS that was generated from some other original code.")
2828
.describe("screw-ie8", "Do not support Internet Explorer 6/7/8. This flag is enabled by default.")
29-
.describe("support-ie8", "Support non-standard Internet Explorer 6/7/8 javascript. Note: may generate incorrect code for try/catch in ES5 compliant browsers.")
29+
.describe("support-ie8", "Support non-standard Internet Explorer 6/7/8 javascript.")
3030
.describe("expr", "Parse a single expression, rather than a program (for parsing JSON)")
3131
.describe("p", "Skip prefix for original filenames that appear in source maps. \
3232
For example -p 3 will drop 3 directories from file names and ensure they are relative paths. \

‎lib/compress.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3348,7 +3348,9 @@ merge(Compressor.prototype, {
33483348
return def;
33493349
}
33503350
// testing against !self.scope.uses_with first is an optimization
3351-
if (self.undeclared() && !isLHS(self, compressor.parent())
3351+
if (compressor.option("screw_ie8")
3352+
&& self.undeclared()
3353+
&& !isLHS(self, compressor.parent())
33523354
&& (!self.scope.uses_with || !compressor.find_parent(AST_With))) {
33533355
switch (self.name) {
33543356
case "undefined":

‎lib/scope.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
9797
var labels = new Dictionary();
9898
var defun = null;
9999
var tw = new TreeWalker(function(node, descend){
100-
if (options.screw_ie8 && node instanceof AST_Catch) {
100+
if (node instanceof AST_Catch) {
101101
var save_scope = scope;
102102
scope = new AST_Scope(node);
103103
scope.init_scope_vars();
@@ -158,8 +158,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
158158
def.init = tw.parent().value;
159159
}
160160
else if (node instanceof AST_SymbolCatch) {
161-
(options.screw_ie8 ? scope : defun)
162-
.def_variable(node);
161+
scope.def_variable(node);
163162
}
164163
else if (node instanceof AST_LabelRef) {
165164
var sym = labels.get(node.name);
@@ -209,6 +208,23 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
209208
});
210209
self.walk(tw);
211210

211+
// pass 3: fix up any scoping issue with IE8
212+
if (!options.screw_ie8) {
213+
self.walk(new TreeWalker(function(node, descend) {
214+
if (node instanceof AST_SymbolCatch) {
215+
var name = node.name;
216+
var scope = node.thedef.scope.parent_scope;
217+
var def = scope.find_variable(name) || self.globals.get(name) || scope.def_variable(node);
218+
node.thedef.references.forEach(function(ref) {
219+
ref.thedef = def;
220+
ref.reference(options);
221+
});
222+
node.thedef = def;
223+
return true;
224+
}
225+
}));
226+
}
227+
212228
if (options.cache) {
213229
this.cname = options.cache.cname;
214230
}

‎test/compress/screw-ie8.js

+29-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ dont_screw: {
1717
expect_exact: 'f("\\x0B");';
1818
}
1919

20+
do_screw_constants: {
21+
options = {
22+
screw_ie8: true,
23+
}
24+
input: {
25+
f(undefined, Infinity);
26+
}
27+
expect_exact: "f(void 0,1/0);"
28+
}
29+
30+
dont_screw_constants: {
31+
options = {
32+
screw_ie8: false,
33+
}
34+
input: {
35+
f(undefined, Infinity);
36+
}
37+
expect_exact: "f(undefined,Infinity);"
38+
}
39+
2040
do_screw_try_catch: {
2141
options = { screw_ie8: true };
2242
mangle = { screw_ie8: true };
@@ -46,8 +66,6 @@ do_screw_try_catch: {
4666
}
4767

4868
dont_screw_try_catch: {
49-
// This test is known to generate incorrect code for screw_ie8=false.
50-
// Update expected result in the event this bug is ever fixed.
5169
options = { screw_ie8: false };
5270
mangle = { screw_ie8: false };
5371
beautify = { screw_ie8: false };
@@ -64,11 +82,11 @@ dont_screw_try_catch: {
6482
}
6583
expect: {
6684
bad = function(n){
67-
return function(n){
85+
return function(t){
6886
try{
69-
t()
70-
} catch(t) {
71-
n(t)
87+
n()
88+
} catch(n) {
89+
t(n)
7290
}
7391
}
7492
};
@@ -104,8 +122,6 @@ do_screw_try_catch_undefined: {
104122
}
105123

106124
dont_screw_try_catch_undefined: {
107-
// This test is known to generate incorrect code for screw_ie8=false.
108-
// Update expected result in the event this bug is ever fixed.
109125
options = { screw_ie8: false };
110126
mangle = { screw_ie8: false };
111127
beautify = { screw_ie8: false };
@@ -121,14 +137,14 @@ dont_screw_try_catch_undefined: {
121137
};
122138
}
123139
expect: {
124-
function a(o){
140+
function a(n){
125141
try{
126142
throw "Stuff"
127-
} catch (n) {
128-
console.log("caught: "+n)
143+
} catch (undefined) {
144+
console.log("caught: " + undefined)
129145
}
130-
console.log("undefined is " + n);
131-
return o === n
146+
console.log("undefined is " + undefined);
147+
return n === undefined
132148
}
133149
}
134150
}

0 commit comments

Comments
 (0)
Please sign in to comment.