From cfebeb2f63929a9e1a73a606ff2bc9a75e380b28 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 9 Aug 2018 17:34:28 +0800 Subject: [PATCH] fix corner case in `mangle` workaround for Safari (#3230) fixes #3227 --- lib/compress.js | 3 ++- test/compress/webkit.js | 49 +++++++++++++++++++++++++++++++++++++++++ test/sandbox.js | 47 ++++++++++++++++++--------------------- 3 files changed, 73 insertions(+), 26 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 08a22c54..9b6e3369 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5758,9 +5758,10 @@ merge(Compressor.prototype, { if (fixed instanceof AST_Defun) { fixed._squeezed = true; fixed = make_node(AST_Function, fixed, fixed); + fixed.name = make_node(AST_SymbolLambda, fixed.name, fixed.name); } var value; - if (d.recursive_refs > 0 && fixed.name instanceof AST_SymbolDefun) { + if (d.recursive_refs > 0) { value = fixed.clone(true); var defun_def = value.name.definition(); var lambda_def = value.variables.get(value.name.name); diff --git a/test/compress/webkit.js b/test/compress/webkit.js index a554fddf..f67a7b7f 100644 --- a/test/compress/webkit.js +++ b/test/compress/webkit.js @@ -58,3 +58,52 @@ lambda_name_mangle: { expect_exact: "console.log(typeof function o(n){});" expect_stdout: "function" } + +lambda_name_mangle_ie8: { + mangle = { + ie8: true, + toplevel: true, + } + input: { + console.log(typeof function foo(bar) {}); + } + expect_exact: "console.log(typeof function n(o){});" + expect_stdout: "function" +} + +function_name_mangle: { + options = { + keep_fnames: true, + reduce_vars: true, + unused: true, + } + mangle = {} + input: { + (function() { + function foo(bar) {} + console.log(typeof foo); + })(); + } + expect_exact: "(function(){console.log(typeof function o(n){})})();" + expect_stdout: "function" +} + +function_name_mangle_ie8: { + options = { + keep_fnames: true, + reduce_vars: true, + unused: true, + } + mangle = { + ie8: true, + toplevel: true, + } + input: { + (function() { + function foo(bar) {} + console.log(typeof foo); + })(); + } + expect_exact: "(function(){console.log(typeof function n(o){})})();" + expect_stdout: "function" +} diff --git a/test/sandbox.js b/test/sandbox.js index e85ebd79..23fb4fc9 100644 --- a/test/sandbox.js +++ b/test/sandbox.js @@ -1,21 +1,6 @@ var semver = require("semver"); var vm = require("vm"); -function createContext() { - return vm.createContext(Object.defineProperty({}, "console", { - value: { - log: function(msg) { - if (arguments.length == 1 && typeof msg == "string") { - return console.log("%s", msg); - } - return console.log.apply(console, [].map.call(arguments, function(arg) { - return safe_log(arg, 3); - })); - } - } - })); -} - function safe_log(arg, level) { if (arg) switch (typeof arg) { case "function": @@ -25,21 +10,21 @@ function safe_log(arg, level) { arg.constructor.toString(); if (level--) for (var key in arg) { var desc = Object.getOwnPropertyDescriptor(arg, key); - if (!desc || !desc.get) { - arg[key] = safe_log(arg[key], level); - } + if (!desc || !desc.get) arg[key] = safe_log(arg[key], level); } } return arg; } -function strip_func_ids(text) { - return ("" + text).replace(/F[0-9]{6}N/g, "N>"); +function log(msg) { + if (arguments.length == 1 && typeof msg == "string") return console.log("%s", msg); + return console.log.apply(console, [].map.call(arguments, function(arg) { + return safe_log(arg, 3); + })); } -var context; -var FUNC_TOSTRING = [ - "[ Array, Boolean, Error, Function, Number, Object, RegExp, String].forEach(function(f) {", +var func_toString = new vm.Script([ + "[ Array, Boolean, Error, Function, Number, Object, RegExp, String ].forEach(function(f) {", " f.toString = Function.prototype.toString;", "});", "Function.prototype.toString = function() {", @@ -59,7 +44,15 @@ var FUNC_TOSTRING = [ ' return "function(){}";', " };", "}();", -]).join("\n"); +]).join("\n")); + +function createContext() { + var ctx = vm.createContext(Object.defineProperty({}, "console", { value: { log: log } })); + func_toString.runInContext(ctx); + return ctx; +} + +var context; exports.run_code = function(code, reuse) { var stdout = ""; var original_write = process.stdout.write; @@ -69,7 +62,6 @@ exports.run_code = function(code, reuse) { try { if (!reuse || !context) context = createContext(); vm.runInContext([ - FUNC_TOSTRING, "!function() {", code, "}();", @@ -86,6 +78,11 @@ exports.run_code = function(code, reuse) { } } }; + +function strip_func_ids(text) { + return ("" + text).replace(/F[0-9]{6}N/g, "N>"); +} + exports.same_stdout = semver.satisfies(process.version, "0.12") ? function(expected, actual) { if (typeof expected != typeof actual) return false; if (typeof expected != "string") { -- 2.34.1