From 86ea38a25954ba4cda017fbfee5e0ef537b24bf1 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 24 Oct 2017 02:58:30 +0800 Subject: enhance `unsafe` `evaluate` of arrays & objects (#2394) --- test/compress/reduce_vars.js | 290 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 288 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 1274024f..f516b9db 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -211,7 +211,91 @@ unsafe_evaluate: { } } -unsafe_evaluate_object: { +unsafe_evaluate_side_effect_free: { + options = { + evaluate: true, + reduce_vars: true, + unsafe: true, + unused: true, + } + input: { + console.log(function(){ var o={p:1}; console.log(o.p); return o.p; }()); + console.log(function(){ var o={p:2}; console.log(o.p); return o; }()); + } + expect: { + console.log(function(){ console.log(1); return 1; }()); + console.log(function(){ var o={p:2}; console.log(2); return o; }()); + } + expect_stdout: true +} + +unsafe_evaluate_escaped: { + options = { + evaluate: true, + reduce_vars: true, + unsafe: true, + unused: true, + } + input: { + console.log(function(){ var o={p:1}; console.log(o, o.p); return o.p; }()); + console.log(function(){ var o={p:2}; console.log(o.p, o); return o.p; }()); + } + expect: { + console.log(function(){ var o={p:1}; console.log(o, o.p); return o.p; }()); + console.log(function(){ var o={p:2}; console.log(o.p, o); return o.p; }()); + } + expect_stdout: true +} + +unsafe_evaluate_modified: { + options = { + evaluate: true, + reduce_vars: true, + unsafe: true, + unused: true, + } + input: { + console.log(function(){ var o={p:1}; o.p++; console.log(o.p); return o.p; }()); + console.log(function(){ var o={p:2}; --o.p; console.log(o.p); return o.p; }()); + console.log(function(){ var o={p:3}; o.p += ""; console.log(o.p); return o.p; }()); + console.log(function(){ var o={p:4}; o = {}; console.log(o.p); return o.p; }()); + console.log(function(){ var o={p:5}; o.p = -9; console.log(o.p); return o.p; }()); + function inc() { this.p++; } + console.log(function(){ var o={p:6}; inc.call(o); console.log(o.p); return o.p; }()); + } + expect: { + console.log(function(){ var o={p:1}; o.p++; console.log(o.p); return o.p; }()); + console.log(function(){ var o={p:2}; --o.p; console.log(o.p); return o.p; }()); + console.log(function(){ var o={p:3}; o.p += ""; console.log(o.p); return o.p; }()); + console.log(function(){ var o={p:4}; o = {}; console.log(o.p); return o.p; }()); + console.log(function(){ var o={p:5}; o.p = -9; console.log(o.p); return o.p; }()); + function inc() { this.p++; } + console.log(function(){ var o={p:6}; inc.call(o); console.log(o.p); return o.p; }()); + } + expect_stdout: true +} + +unsafe_evaluate_unknown: { + options = { + evaluate: true, + reduce_vars: true, + unsafe: true, + unused: true, + } + input: { + console.log(function(){ var o={p:1}; console.log(o.not_present); return o.p; }()); + console.log(function(){ var o={p:2}; console.log(o.prototype); return o.p; }()); + console.log(function(){ var o={p:3}; console.log(o.hasOwnProperty); return o.p; }()); + } + expect: { + console.log(function(){ var o={p:1}; console.log(o.not_present); return o.p; }()); + console.log(function(){ var o={p:2}; console.log(o.prototype); return o.p; }()); + console.log(function(){ var o={p:3}; console.log(o.hasOwnProperty); return o.p; }()); + } + expect_stdout: true +} + +unsafe_evaluate_object_1: { options = { evaluate : true, reduce_vars : true, @@ -251,7 +335,83 @@ unsafe_evaluate_object: { } } -unsafe_evaluate_array: { +unsafe_evaluate_object_2: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unsafe: true, + } + input: { + var obj = { + foo: 1, + bar: 2, + square: function(x) { + return x * x; + }, + cube: function(x) { + return x * x * x; + }, + }; + console.log(obj.foo, obj.bar, obj.square(2), obj.cube); + } + expect: { + var obj = { + foo: 1, + bar: 2, + square: function(x) { + return x * x; + }, + cube: function(x) { + return x * x * x; + }, + }; + console.log(1, 2, obj.square(2), obj.cube); + } + expect_stdout: true +} + +unsafe_evaluate_object_3: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unsafe: true, + } + input: { + var obj = { + get foo() { + return 1; + }, + bar: 2, + square: function(x) { + return x * x; + }, + cube: function(x) { + return x * x * x; + }, + }; + console.log(obj.foo, obj.bar, obj.square(2), obj.cube); + } + expect: { + var obj = { + get foo() { + return 1; + }, + bar: 2, + square: function(x) { + return x * x; + }, + cube: function(x) { + return x * x * x; + }, + }; + console.log(obj.foo, obj.bar, obj.square(2), obj.cube); + } + expect_stdout: true +} + +unsafe_evaluate_array_1: { options = { evaluate : true, reduce_vars : true, @@ -299,6 +459,132 @@ unsafe_evaluate_array: { } } +unsafe_evaluate_array_2: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unsafe: true, + } + input: { + var arr = [ + 1, + 2, + function(x) { + return x * x; + }, + function(x) { + return x * x * x; + }, + ]; + console.log(arr[0], arr[1], arr[2](2), arr[3]); + } + expect: { + var arr = [ + 1, + 2, + function(x) { + return x * x; + }, + function(x) { + return x * x * x; + }, + ]; + console.log(1, 2, arr[2](2), arr[3]); + } + expect_stdout: true +} + +unsafe_evaluate_array_3: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unsafe: true, + } + input: { + var arr = [ + 1, + 2, + function() { + return ++arr[0]; + }, + ]; + console.log(arr[0], arr[1], arr[2](), arr[0]); + } + expect: { + var arr = [ + 1, + 2, + function() { + return ++arr[0]; + }, + ]; + console.log(arr[0], arr[1], arr[2](), arr[0]); + } + expect_stdout: "1 2 2 2" +} + +unsafe_evaluate_array_4: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unsafe: true, + } + input: { + var arr = [ + 1, + 2, + function() { + return ++this[0]; + }, + ]; + console.log(arr[0], arr[1], arr[2], arr[0]); + } + expect: { + var arr = [ + 1, + 2, + function() { + return ++this[0]; + }, + ]; + console.log(1, 2, arr[2], 1); + } + expect_stdout: true +} + +unsafe_evaluate_array_5: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unsafe: true, + } + input: { + var arr = [ + 1, + 2, + function() { + return ++this[0]; + }, + ]; + console.log(arr[0], arr[1], arr[2](), arr[0]); + } + expect: { + var arr = [ + 1, + 2, + function() { + return ++this[0]; + }, + ]; + console.log(arr[0], arr[1], arr[2](), arr[0]); + } + expect_stdout: "1 2 2 2" +} + unsafe_evaluate_equality_1: { options = { evaluate : true, -- cgit v1.2.3