diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-01-26 19:14:18 +0800 |
---|---|---|
committer | Richard van Velzen <rvanvelzen1@gmail.com> | 2017-01-26 12:14:18 +0100 |
commit | 0d7d4918eb6fb73c3cf9863479b3e66d38fad6df (patch) | |
tree | aba146adb5371e19935493b132fc605abbe081ca /test/compress | |
parent | 48284844a461e6113bb9911cdcdad7ab8a3d85de (diff) | |
download | tracifyjs-0d7d4918eb6fb73c3cf9863479b3e66d38fad6df.tar.gz tracifyjs-0d7d4918eb6fb73c3cf9863479b3e66d38fad6df.zip |
augment evaluate to extract within objects (#1425)
- gated by `unsafe`
- replaces previous optimisation specific to String.length
- "123"[0] => 1
- [1, 2, 3][0] => 1
- [1, 2, 3].length => 3
- does not apply to objects with overridden prototype functions
Diffstat (limited to 'test/compress')
-rw-r--r-- | test/compress/arrays.js | 49 | ||||
-rw-r--r-- | test/compress/evaluate.js | 377 | ||||
-rw-r--r-- | test/compress/properties.js | 51 | ||||
-rw-r--r-- | test/compress/reduce_vars.js | 183 |
4 files changed, 658 insertions, 2 deletions
diff --git a/test/compress/arrays.js b/test/compress/arrays.js index e636347f..807ba206 100644 --- a/test/compress/arrays.js +++ b/test/compress/arrays.js @@ -72,3 +72,52 @@ constant_join_2: { var f = "strstr" + variable + "foobarmoo" + foo; } } + +for_loop: { + options = { + unsafe : true, + evaluate : true, + reduce_vars : true + }; + input: { + function f0() { + var a = [1, 2, 3]; + for (var i = 0; i < a.length; i++) { + console.log(a[i]); + } + } + + function f1() { + var a = [1, 2, 3]; + for (var i = 0, len = a.length; i < len; i++) { + console.log(a[i]); + } + } + + function f2() { + var a = [1, 2, 3]; + for (var i = 0; i < a.length; i++) { + a[i]++; + } + } + } + expect: { + function f0() { + var a = [1, 2, 3]; + for (var i = 0; i < 3; i++) + console.log(a[i]); + } + + function f1() { + var a = [1, 2, 3]; + for (var i = 0, len = 3; i < len; i++) + console.log(a[i]); + } + + function f2() { + var a = [1, 2, 3]; + for (var i = 0; i < a.length; i++) + a[i]++; + } + } +} diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js index d27582f3..c74c7b24 100644 --- a/test/compress/evaluate.js +++ b/test/compress/evaluate.js @@ -37,3 +37,380 @@ positive_zero: { ); } } + +unsafe_constant: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + true.a, + false.a, + null.a, + undefined.a + ); + } + expect: { + console.log( + true.a, + false.a, + null.a, + (void 0).a + ); + } +} + +unsafe_object: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + ({a:1}) + 1, + ({a:1}).a + 1, + ({a:1}).b + 1, + ({a:1}).a.b + 1 + ); + } + expect: { + console.log( + ({a:1}) + 1, + 2, + ({a:1}).b + 1, + 1..b + 1 + ); + } +} + +unsafe_object_nested: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + ({a:{b:1}}) + 1, + ({a:{b:1}}).a + 1, + ({a:{b:1}}).b + 1, + ({a:{b:1}}).a.b + 1 + ); + } + expect: { + console.log( + ({a:{b:1}}) + 1, + ({a:{b:1}}).a + 1, + ({a:{b:1}}).b + 1, + 2 + ); + } +} + +unsafe_object_complex: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + ({a:{b:1},b:1}) + 1, + ({a:{b:1},b:1}).a + 1, + ({a:{b:1},b:1}).b + 1, + ({a:{b:1},b:1}).a.b + 1 + ); + } + expect: { + console.log( + ({a:{b:1},b:1}) + 1, + ({a:{b:1},b:1}).a + 1, + 2, + 2 + ); + } +} + +unsafe_object_repeated: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + ({a:{b:1},a:1}) + 1, + ({a:{b:1},a:1}).a + 1, + ({a:{b:1},a:1}).b + 1, + ({a:{b:1},a:1}).a.b + 1 + ); + } + expect: { + console.log( + ({a:{b:1},a:1}) + 1, + 2, + ({a:{b:1},a:1}).b + 1, + 1..b + 1 + ); + } +} + +unsafe_function: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + ({a:{b:1},b:function(){}}) + 1, + ({a:{b:1},b:function(){}}).a + 1, + ({a:{b:1},b:function(){}}).b + 1, + ({a:{b:1},b:function(){}}).a.b + 1 + ); + } + expect: { + console.log( + ({a:{b:1},b:function(){}}) + 1, + ({a:{b:1},b:function(){}}).a + 1, + ({a:{b:1},b:function(){}}).b + 1, + ({a:{b:1},b:function(){}}).a.b + 1 + ); + } +} + +unsafe_integer_key: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + ({0:1}) + 1, + ({0:1})[0] + 1, + ({0:1})["0"] + 1, + ({0:1})[1] + 1, + ({0:1})[0][1] + 1, + ({0:1})[0]["1"] + 1 + ); + } + expect: { + console.log( + ({0:1}) + 1, + 2, + 2, + ({0:1})[1] + 1, + 1[1] + 1, + 1["1"] + 1 + ); + } +} + +unsafe_integer_key_complex: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + ({0:{1:1},1:1}) + 1, + ({0:{1:1},1:1})[0] + 1, + ({0:{1:1},1:1})["0"] + 1, + ({0:{1:1},1:1})[1] + 1, + ({0:{1:1},1:1})[0][1] + 1, + ({0:{1:1},1:1})[0]["1"] + 1 + ); + } + expect: { + console.log( + ({0:{1:1},1:1}) + 1, + "[object Object]1", + "[object Object]1", + 2, + 2, + 2 + ); + } +} + +unsafe_float_key: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + ({2.72:1}) + 1, + ({2.72:1})[2.72] + 1, + ({2.72:1})["2.72"] + 1, + ({2.72:1})[3.14] + 1, + ({2.72:1})[2.72][3.14] + 1, + ({2.72:1})[2.72]["3.14"] + 1 + ); + } + expect: { + console.log( + ({2.72:1}) + 1, + 2, + 2, + ({2.72:1})[3.14] + 1, + 1[3.14] + 1, + 1["3.14"] + 1 + ); + } +} + +unsafe_float_key_complex: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + ({2.72:{3.14:1},3.14:1}) + 1, + ({2.72:{3.14:1},3.14:1})[2.72] + 1, + ({2.72:{3.14:1},3.14:1})["2.72"] + 1, + ({2.72:{3.14:1},3.14:1})[3.14] + 1, + ({2.72:{3.14:1},3.14:1})[2.72][3.14] + 1, + ({2.72:{3.14:1},3.14:1})[2.72]["3.14"] + 1 + ); + } + expect: { + console.log( + "[object Object]1", + "[object Object]1", + "[object Object]1", + 2, + 2, + 2 + ); + } +} + +unsafe_array: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + [1, , 3][1], + [1, 2, 3, a] + 1, + [1, 2, 3, 4] + 1, + [1, 2, 3, a][0] + 1, + [1, 2, 3, 4][0] + 1, + [1, 2, 3, 4][6 - 5] + 1, + [1, , 3, 4][6 - 5] + 1, + [[1, 2], [3, 4]][0] + 1, + [[1, 2], [3, 4]][6 - 5][1] + 1, + [[1, 2], , [3, 4]][6 - 5][1] + 1 + ); + } + expect: { + console.log( + void 0, + [1, 2, 3, a] + 1, + "1,2,3,41", + [1, 2, 3, a][0] + 1, + 2, + 3, + NaN, + "1,21", + 5, + (void 0)[1] + 1 + ); + } +} + +unsafe_string: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + "1234" + 1, + "1234"[0] + 1, + "1234"[6 - 5] + 1, + ("12" + "34")[0] + 1, + ("12" + "34")[6 - 5] + 1, + [1, 2, 3, 4].join("")[0] + 1 + ); + } + expect: { + console.log( + "12341", + "11", + "21", + "11", + "21", + "11" + ); + } +} + +unsafe_array_bad_index: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + [1, 2, 3, 4].a + 1, + [1, 2, 3, 4]["a"] + 1, + [1, 2, 3, 4][3.14] + 1 + ); + } + expect: { + console.log( + [1, 2, 3, 4].a + 1, + [1, 2, 3, 4]["a"] + 1, + [1, 2, 3, 4][3.14] + 1 + ); + } +} + +unsafe_string_bad_index: { + options = { + evaluate : true, + unsafe : true + } + input: { + console.log( + "1234".a + 1, + "1234"["a"] + 1, + "1234"[3.14] + 1 + ); + } + expect: { + console.log( + "1234".a + 1, + "1234"["a"] + 1, + "1234"[3.14] + 1 + ); + } +} + +unsafe_prototype_function: { + options = { + evaluate : true, + unsafe : true + } + input: { + var a = ({valueOf: 0}) < 1; + var b = ({toString: 0}) < 1; + var c = ({valueOf: 0}) + ""; + var d = ({toString: 0}) + ""; + var e = (({valueOf: 0}) + "")[2]; + var f = (({toString: 0}) + "")[2]; + var g = ({valueOf: 0}).valueOf(); + var h = ({toString: 0}).toString(); + } + expect: { + var a = ({valueOf: 0}) < 1; + var b = ({toString: 0}) < 1; + var c = ({valueOf: 0}) + ""; + var d = ({toString: 0}) + ""; + var e = (({valueOf: 0}) + "")[2]; + var f = (({toString: 0}) + "")[2]; + var g = ({valueOf: 0}).valueOf(); + var h = "" + ({toString: 0}); + } +} diff --git a/test/compress/properties.js b/test/compress/properties.js index 22f2c339..7ad54ebe 100644 --- a/test/compress/properties.js +++ b/test/compress/properties.js @@ -54,7 +54,56 @@ dot_properties_es5: { } } -evaluate_length: { +sub_properties: { + options = { + evaluate: true, + properties: true + }; + input: { + a[0] = 0; + a["0"] = 1; + a[3.14] = 2; + a["3" + ".14"] = 3; + a["i" + "f"] = 4; + a["foo" + " bar"] = 5; + a[0 / 0] = 6; + a[null] = 7; + a[undefined] = 8; + } + expect: { + a[0] = 0; + a[0] = 1; + a[3.14] = 2; + a[3.14] = 3; + a.if = 4; + a["foo bar"] = 5; + a[NaN] = 6; + a[null] = 7; + a[void 0] = 8; + } +} + +evaluate_array_length: { + options = { + properties: true, + unsafe: true, + evaluate: true + }; + input: { + a = [1, 2, 3].length; + a = [1, 2, 3].join()["len" + "gth"]; + a = [1, 2, b].length; + a = [1, 2, 3].join(b).length; + } + expect: { + a = 3; + a = 5; + a = [1, 2, b].length; + a = [1, 2, 3].join(b).length; + } +} + +evaluate_string_length: { options = { properties: true, unsafe: true, diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index a1d05012..c401ac66 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -168,4 +168,185 @@ modified: { console.log(B ? 'yes' : 'no'); } } -}
\ No newline at end of file +} + +unsafe_evaluate: { + options = { + evaluate : true, + reduce_vars : true, + unsafe : true, + unused : true + } + input: { + function f0(){ + var a = { + b:1 + }; + console.log(a.b + 3); + } + + function f1(){ + var a = { + b:{ + c:1 + }, + d:2 + }; + console.log(a.b + 3, a.d + 4, a.b.c + 5, a.d.c + 6); + } + } + expect: { + function f0(){ + console.log(4); + } + + function f1(){ + var a = { + b:{ + c:1 + }, + d:2 + }; + console.log(a.b + 3, 6, 6, 2..c + 6); + } + } +} + +unsafe_evaluate_object: { + options = { + evaluate : true, + reduce_vars : true, + unsafe : true + } + input: { + function f0(){ + var a = 1; + var b = {}; + b[a] = 2; + console.log(a + 3); + } + + function f1(){ + var a = { + b:1 + }; + a.b = 2; + console.log(a.b + 3); + } + } + expect: { + function f0(){ + var a = 1; + var b = {}; + b[a] = 2; + console.log(4); + } + + function f1(){ + var a = { + b:1 + }; + a.b = 2; + console.log(a.b + 3); + } + } +} + +unsafe_evaluate_array: { + options = { + evaluate : true, + reduce_vars : true, + unsafe : true + } + input: { + function f0(){ + var a = 1; + var b = []; + b[a] = 2; + console.log(a + 3); + } + + function f1(){ + var a = [1]; + a[2] = 3; + console.log(a.length); + } + + function f2(){ + var a = [1]; + a.push(2); + console.log(a.length); + } + } + expect: { + function f0(){ + var a = 1; + var b = []; + b[a] = 2; + console.log(4); + } + + function f1(){ + var a = [1]; + a[2] = 3; + console.log(a.length); + } + + function f2(){ + var a = [1]; + a.push(2); + console.log(a.length); + } + } +} + +unsafe_evaluate_equality: { + options = { + evaluate : true, + reduce_vars : true, + unsafe : true, + unused : true + } + input: { + function f0(){ + var a = {}; + console.log(a === a); + } + + function f1(){ + var a = []; + console.log(a === a); + } + + function f2(){ + var a = {a:1, b:2}; + var b = a; + var c = a; + console.log(b === c); + } + + function f3(){ + var a = [1, 2, 3]; + var b = a; + var c = a; + console.log(b === c); + } + } + expect: { + function f0(){ + console.log(true); + } + + function f1(){ + console.log(true); + } + + function f2(){ + console.log(true); + } + + function f3(){ + console.log(true); + } + } +} |