[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: detecting undefined variables--a new mixed approach
- From: Thomas Lauer <thomas.lauer@...>
- Date: Thu, 27 Mar 2008 16:37:37 +0000
David Manura <dm.lua@math2.org> wrote:
> That shouldn't be necessary. checkglobals() validates not just one function but
> also all functions lexically nested inside that function.
Hm... with this module
-- file: mymodule.lua
module('mymodule',package.seeall)
print(mymodule)
function foo(x) print(_G.testxxyx) end
print("_G:")
checkglobals(1,_G)
print("mymodule:")
checkglobals(1,mymodule)
I get some weird results. (I've changed checkglobals() so that it
doesn't terminate with the first undefined global but just keeps
printing.)
This is printed:
table: 008244E0
_G:
undefined variable "foo" at line 4
mymodule:
undefined variable "module" at line 2
undefined variable "package" at line 2
undefined variable "print" at line 3
undefined variable "mymodule" at line 3
undefined variable "print" at line 6
undefined variable "_G" at line 7
undefined variable "print" at line 8
undefined variable "mymodule" at line 9
undefined variable "print" at line 4
undefined variable "_G" at line 4
nil
The rawget() call in checkglobals() is too restrictive, methinks. How
about changing this call to env[name]:
if env[name]==nil then print('undefined variable "'..name..'"'..(linenum
and ' at line '..linenum or '')) end
This "less strict" mode could easily be triggered by an optional third
boolean parameter to checkglobals().
Another problem I ran into is that checkglobals() doesn't work well if
two or more modules are defined in one file. This may not be the
standard case but I have a few files which create more than one module
and then all hell breaks loose as all globals defined in module 1 are
flagged invalid if checkglobals() is called for module 2 and vice versa.
--
cheers thomasl
web: http://thomaslauer.com/start