{"action":"opened","issue":{"url":"https://api.github.com/repos/mishoo/UglifyJS2/issues/3180","repository_url":"https://api.github.com/repos/mishoo/UglifyJS2","labels_url":"https://api.github.com/repos/mishoo/UglifyJS2/issues/3180/labels{/name}","comments_url":"https://api.github.com/repos/mishoo/UglifyJS2/issues/3180/comments","events_url":"https://api.github.com/repos/mishoo/UglifyJS2/issues/3180/events","html_url":"https://github.com/mishoo/UglifyJS2/issues/3180","id":330875073,"node_id":"MDU6SXNzdWUzMzA4NzUwNzM=","number":3180,"title":"function.toString() generates wrong results","user":{"login":"Flamefire","id":309017,"node_id":"MDQ6VXNlcjMwOTAxNw==","avatar_url":"https://avatars2.githubusercontent.com/u/309017?v=4","gravatar_id":"","url":"https://api.github.com/users/Flamefire","html_url":"https://github.com/Flamefire","followers_url":"https://api.github.com/users/Flamefire/followers","following_url":"https://api.github.com/users/Flamefire/following{/other_user}","gists_url":"https://api.github.com/users/Flamefire/gists{/gist_id}","starred_url":"https://api.github.com/users/Flamefire/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Flamefire/subscriptions","organizations_url":"https://api.github.com/users/Flamefire/orgs","repos_url":"https://api.github.com/users/Flamefire/repos","events_url":"https://api.github.com/users/Flamefire/events{/privacy}","received_events_url":"https://api.github.com/users/Flamefire/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-06-09T10:35:58Z","updated_at":"2018-06-09T10:35:58Z","closed_at":null,"author_association":"NONE","body":"The toString() evaluation for functions is actually not implemented, it simply returns an empty function.\r\n\r\nThis means any `function foo(){return \"bar\";}.toString()` gets evaluated into `\"function(){}\"`\r\n\r\n**Input**\r\n\r\n```\r\nfunction bar(){\r\n  function foo(){var a=1;return a;}\r\n  return /a=1;return/.test(foo.toString());\r\n}\r\n```\r\n\r\n**Options**\r\n\r\n`bin/uglifyjs test.js --compress unsafe`\r\n\r\n**Output**\r\n\r\n`function bar(){return!1}`\r\n\r\n**Expected output**\r\n\r\n`function bar(){return!0}`\r\n\r\nI tried to implement the correct handling by embedding the unparsed function as a `raw` field into `AST_Lambda` but when the optimizer comes to decide whether to take the evaluated `fn.toString()` or the raw function it decides in favour of the function as it is shorter than the string. This inhibits further constant folding (e.g. by the `test`-call which would produce shorter code)\r\n\r\nFurthermore this decision changes logic:\r\n\r\n```\r\nfunction bar(){\r\n  function foo(){var a=1;  return a;}\r\n  return foo.toString();\r\n}\r\n```\r\n\r\ngets transformed (with beautify) into:\r\n\r\n```\r\nfunction bar() {\r\n  return \"\" +  function foo() {\r\n      return 1;\r\n  };\r\n}\r\n```\r\n\r\nThis changes the result!"}}
{"actor":{"display_login":"Flamefire"}}