lua-users home
lua-l archive

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


On 25/02/2010, at 7:41 PM, steve donovan wrote:

> On Wed, Feb 24, 2010 at 9:25 PM, Geoff Leyland
> <geoff_leyland@fastmail.fm> wrote:
>> How does strict work with modules?
>> 
>> As far as I can tell, strict.lua (pl's and lua's) both change the metatable of _G.  But inside a module the global environment is _M.  How is _M affected by what you did to _G?
> 
> Modules aren't affected currently, although the same principles should
> apply. In any large application, most of the code is in modules
> anyway, so use of unassigned module scope variables is just as much a
> problem.
> 
> strict.lua had to fool with _G's __index; pl.strict.lua does so via
> setting a handler on the load-by-demand global __index;  _M already
> has an __index (often pointing at _G) so I'd have to be careful.

It turns out this has been thought about before: http://lua-users.org/wiki/DetectingUndefinedVariables
So, in the spirit of Lua, I read everyone else's and wrote my own.  It's extra feature is that it overrides "module" and "setfenv".  I didn't seem to run into trouble replacing _M's __index - is that because I don't use package.seeall?

Anyway, if anyone wants a look, it's at: http://code.google.com/p/rima/source/browse/trunk/lua/test/strict.lua

Because I'm a bit careless with modules and use this trick

module(...)
class_this_module_defines = _M

to get the classes returned from modules, I had to be a bit less strict when checking globals in modules.  It caught a few bugs in rima though.

Cheers,
Geoff