Closed 2dengine closed 2 years ago
Can you post/link the code that becomes incorrect when minified? I don't know what the issue could be without more information. The minify()
function should only rename local variables - not touch any literals or anything else (unless the optimize
argument is set, in which case it might be the optimize()
function doing something bad).
Looking through your list of reported issues in the parser's source, none is an actual issue. The HANDLE_ENV assignment statement is successfully detecting if the running Lua version supports _ENV.
Hello. The code in question is not open source and it doesn't raise any errors. However I get weird behavior after minification.
I am not calling the optimize function explicitly:
local parser = require(lib.."minify")
local minify = function(s)
local ast = parser.parse(s)
parser.minify(ast)
return parser.toLua(ast)
end
SUPERSTRICT reports possible issues and while these may not be "issues" that would raise an error, they are certainly worth looking into. For example, Lua is an interpreted language so things like empty code blocks are not "optimized" by a compiler like in the case of other static languages.
Perhaps, I am missing something, but I don't see how pcall could fail in the following case:
local pcall = pcall
local _ENV = nil
HANDLE_ENV = not pcall(function() local x = _G end)
Are you using LuaJIT-specific syntax for any numbers (like 64-bit ints)? If you just parse() and toLua() the code without minify(), is the problem still there?
Regarding empty blocks, I do this in several places as an optimization:
if x then
-- Do nothing. Don't even check any of the remaining if-conditions.
elseif y then
-- ...
elseif z then
-- ...
end
-- Instead of doing this:
if not x then
if y then
-- ...
elseif z then
-- ...
end
end
If _ENV is supported then x=_G
(which means x=_ENV._G
) becomes x=(nil)._G
which raises an error that pcall catches. Because the value of the local _ENV variable is nil when the function pcall calls is created then the function's environment becomes nil. If _ENV isn't supported then _G is looked up from the main chunk's environment, which is a table like normal, thus no error.
Hello again. Yes, I can confirm that the issues begin only after the call to "parser.minify(ast)". love.physics starts behaving strangely - for example some shapes seem to be way larger than their intended dimensions. In another game, we are getting crashes and other unexpected results. Unfortunately these are large projects so I cannot give you a simple code example that we can debug. Both of my games are using FFI-related features although this is not directly related to love.physics. I never used a version of Lua where "local x = _G" could raise an error unless _G uses custom metatables. It's fine if the code works - all I am saying is that it's a hacky way to do it. Thank you for the reply!
Then I can only assume the minifier renames some variables incorrectly, and that there's probably no parsing/toLua issue here. I'll try to do some more random testing, I guess.
They reworked environments in Lua 5.2. I'm just detecting if a feature exists by trying to use the feature directly and checking if the code succeeds (instead of checking _VERSION or some other value). I don't think it's much different from e.g. checking pcall(require,"bit")
to detect if the bit module exists.
Ok, I believe I found and fixed the problem. It affected declarations/functions/for-loops where multiple declared names were the same (which they often become after getting minified in case some of the variables are unused, like the index variable in ipairs
loops).
Hello and good job! I pulled and ran the latest version of the code and it seems to work fine in my initial tests. I will push the update publicly to our games and promise to report back if anybody finds any other bugs. Thank you for the support, Ivan from 2dengine
Great! :)
Hello, I am using your parser with the following tool for the Love2D engine: https://github.com/2dengine/love.maker and we are getting incorrect output after code minification (difficult to say, but it may be related to parsing literals).
I ran another of my tools called SUPERSTRICT https://github.com/2dengine/sstrict.lua over the source code of the parser and we found some issues:
The "redefinition" warnings are not important, although there are some questionable parts like:
HANDLE_ENV = not pcall(function() local x = _G end)
where "x" is an unused variable and "local x = _G end" does nothing.Any tips on how we can pinpoint what is causing the incorrect output of the minification? Thank you!