FindGlobals

This is a Lua script that checks your .lua files for excess and/or involuntary global variable access.

It needs a Lua interpreter to run; I recommend Cogwheel's WoW Lua.

THIS IS NOT AN ADDON

PRO'S DIGEST

Yes, it's basically a "Luac | grep ?ETGLOBAL", but with some controls.

In PARTICULAR, it differentiates between set/get global access inside and outside of functions.
It also lets you list globals that you are ok with in comment lines like "--GLOBALS: blah, bleh, bluh".

So, it's a little more run-every-time-you-save-before-reloadui friendly. I've been using it for a bit and thought i'd share.

What do I need to know about globals for?

To optimize an addon's performance, you want to make access to commonly-used functions "local" access rather than global namespace lookups. This most definitely includes functions like type, next, pairs, select that maybe you didn't even realize were functions.

Some globals you may be okay with being global accesses (or in fact NEED them to because they can be hooked or changed), for those you have two options:

  • Add one or more "-- GLOBALS: SomeFunc, SomeOtherFunc, SomeGlobalVariable" lines to the source file
    or
  • Put a "local _G=_G" at the top of the file, and then access them through _G.SomeFunc, etc. This is actually somewhat faster than accessing them directly, believe it or not. (Direct global access involves looking up the global variable table first!)

Then, and this is possibly the biggest boon of the script, there is finding the odd miss-spelled variable name. Or the code snippet that you copy&pasted from one function to another but forgot to rename the variables. This rocks.

How to run

Put globals.lua in some directory. Maybe along with your Lua interpreter. Then:

luac -l -p MyFile.lua | lua globals.lua MyFile.lua

or, more likely:

c:\path\to\luac -l -p MyFile.lua | c:\path\to\lua c:\path\to\globals.lua MyFile.lua

Additionally

... since we're running Luac on the file, it is also checked for syntax errors.

I heartily recommend adding this as a "Tool" in your text editor of choice and binding it to a key that you can mash it to save & test before reloading your UI.

Oh and by the way, if you feel that this deserves a % of the curse points that go to libraries that you use, I'd appreciate a tools-used line in your .pkgmeta file :-)

You must login to post a comment. Don't have an account? Register to get one!

  • 7 comments
  • Avatar of Vandesdelca32 Vandesdelca32 Feb 17, 2013 at 18:09 UTC - 0 likes

    I use Sublime Text 2, and figured I'd share the build thing I set up for each file here, for users that also use this delightful editor:

    Create a new build system with this in it, save, and press CTRL+B to execute

    {
      "cmd": ["C:\\path\\to\\your\\findglobals.bat", "$file"],
      "selector": "source.lua"
    }
    

    This is written in a json constructor so, you have to escape the backslashes in the path with a second backslash (\\)

    I stored globals.lua alongside a batch file with the name 'findglobals.bat' in "My Documents\Subversion" where I keep all the addons I'm working on.

    The batch file is simple with just two lines

    @echo off
    luac -l -p %1 | lua path\to\your\globals.lua %1
    

    Sublime Text will run this batch file and pipe all of the output from it into Sublime's console, so you can read it really easily in the file you're working on.

    Of course, just modify the paths in each of the files to suit your particular needs.

    -- Vandesdelca32

  • Avatar of LightsfuryUther LightsfuryUther Sep 06, 2012 at 23:17 UTC - 0 likes

    For those running Python 3.x, I've written a small script to run the find globals script and display a "human-friendly" report.

    http://www.curseforge.com/paste/6180/

    Sample output:

    Global 'CreateFrame' accessed at:
      ./FactionBroker.lua at line 1198
    Global 'FactionBroker' accessed at:
      ./FactionBroker.lua at line 3
    Global 'io' accessed at:
      ./LocaleBuilder.lua at line 14
      ./LocaleWatcher.lua at line 14
    Global '_G' accessed at:
      ./FactionBroker.lua at line 201
      ./FactionBroker.lua at line 941
    ...
    
    Last edited Sep 06, 2012 by LightsfuryUther
  • Avatar of wutname1 wutname1 Sep 04, 2012 at 23:24 UTC - 0 likes

    @Morsker: Go

    I love the nppExec ability how did i not know about this! Here is my command when using F6:

    C:\lua-wow\global.bat "$(FULL_CURRENT_PATH)"

  • Avatar of Morsker Morsker Jul 21, 2012 at 13:23 UTC - 1 like

    That NppExec is really nice; I was able to get it to color lines and enable clicking on them to jump to the line in the file:

    http://oi47.tinypic.com/2yn2c01.jpg
    http://oi46.tinypic.com/2ibm0px.jpg

    Also if anyone uses my batch file with NppExec, take out the last line with the pause.

  • Avatar of mikk mikk Apr 21, 2012 at 23:37 UTC - 2 likes

    @Morsker: Go I heartily recommend installing NppExec for this. It shows you the results in a pane inside NotePad++ instead of a cmd window.

    Here's an NppExec script that works using Morsker's findglobals.bat

    NPP_SAVE
    CD $(CURRENT_DIRECTORY)
    c:\path\to\findglobals.bat $(FILE_NAME)
    
    Last edited Apr 21, 2012 by mikk
  • Avatar of Morsker Morsker Sep 18, 2010 at 01:10 UTC - 1 like

    Something similar works with Notepad++.

    1. I renamed the file FindGlobals.lua and put it in the WoW Interface\ folder

    2. I made FindGlobals.bat in the same folder:

    1
    2
    3
    4
    5
    6
    7
    @echo off
    @set LUAC="C:\Program Files\lua-wow\luac-wow.exe"
    @set LUA="C:\Program Files\lua-wow\lua-wow.exe"
    @set FINDGLOBALS="C:\Program Files\World of Warcraft\Interface\FindGlobals.lua"
    
    %LUAC% -p -l %1 | %LUA% %FINDGLOBALS% %1
    pause
    

    3. Press F5 for "Run" and give it this:

    "C:\Program Files\World of Warcraft\Interface\FindGlobals.bat" "$(FULL_CURRENT_PATH)"
    

    And the Run interface lets you save a keybind to it.

  • Avatar of Cyprias Cyprias May 27, 2010 at 16:54 UTC - 0 likes

    I've got this working in WoW-SciTE to print out in the output window. First I put Cogwheel's WoW Lua interpreter and globals.lua into a folder named lua-wow on my C drive. Then in that folder I created a run.bat file with this in it.

    @C:\lua-wow\luac-wow.exe -l %1 | C:\lua-wow\lua-wow.exe C:\lua-wow\globals.lua %1
    


    Then in SciTE. I opened lua.properties and added this line

    command.build.*.lua=C:\lua-wow\run.bat "$(FileNameExt)"
    

    Now in SciTE. I can hit F7 and quickly see what's global in the output window.

    Thanks for the great tool mikk!

    Last edited May 27, 2010 by Cyprias

  • 7 comments

Facts

Date created
Oct 01, 2009
Category
Last update
Nov 06, 2009
Development stage
Mature
License
Public Domain
Curse link
FindGlobals
Reverse relationships
4
Downloads
1,100
Recent files
  • B: r3-beta for 3.3.0 Nov 06, 2009
  • A: r3 for 3.2.0 Oct 01, 2009
  • A: r2 for 3.2.0 Oct 01, 2009

Authors

  • Avatar of mikk
    • Manager
    • Author