lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Thomas Lauer writes:
> > So, can anyone tell me why this not better than the other approaches?
> the granularity (one check per function) makes using
> it difficult. I just can't see me calling checkglobals() for all
> functions that might or might not use undefined variables.

That shouldn't be necessary.  checkglobals() validates not just one function but
also all functions lexically nested inside that function.  Typically
checkglobals would be applied to the function chunk that defines a module to
validate the entire module (i.e. one checkglobals call per module).  Example:

  -- file: mymodule.lua
  local M = {}
    return function()
      print(x)   -- x is detected by checkglobals
  return M

  -- file: main.lua
  require 'mymodule'.foo()
  $ lua main.lua
  lua: .\mymodule.lua:2: accessed undefined variable "x" at line 5
  stack traceback:
          [C]: in function 'error'
          etc/checkglobals.lua:77: in function 'checkglobals'
          .\mymodule.lua:2: in main chunk
          [C]: in function 'require'
          main.lua:2: in main chunk
          [C]: ?

As an alternative, one could move checkglobals into require (or similar module
loading function) so that each module requires zero calls of its own to
checkglobals().  However, I think it's best to let each module declare whether
it uses undefined globals or not.

> ...strict.lua or my strict module
> I have used the latter for about a year now and it works quite well,
> although I agree that all such solutions require either some sort of
> code coverage tool or *extensive* testing.

True, code coverage was a main complaint about that approach, and checkglobals,
I believe, avoids that.