Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Static analysis of Lua 5.1/5.2/5.3 source, finding all global references and making them local
# Based on, but extended to automate the code generation.
# Lua 5.1
for f in *.lua; do luac-5.1 -p -l "$f" | grep ETGLOBAL | cut -d ';' -f 2 | sort | uniq | sed -E 's/^ (.+)$/local \1 = \1;/' > "_globals.${f}.txt"; done
# Lua 5.2 and 5.3
for f in *.lua; do luac-5.3 -p -l "$f" | grep 'ETTABUP.*_ENV' | cut -d ';' -f 2 | cut -d ' ' -f 1-3 | sort | uniq | sed -E 's/^ _ENV "(.+)"\s*$/local \1 = \1;/' > "_globals.${f}.txt"; done
# The resulting log files will be overzealous (any non-"local"-declared vars from the same file will be seen as globals),
# so you'll still have to do manual work to remove the things that aren't necessary. But it's a lot better than doing
# all of this work manually, trudging through thousands of lines of code to find all external function calls... hehe!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.