Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Script to check that you use local variables inside functions

Script to check that you use local variables inside functions

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Wed 06 Sep 2006 04:34 AM (UTC)

Amended on Wed 06 Sep 2006 07:49 AM (UTC) by Nick Gammon

Message
I found this nifty code example on the Lua mailing list. It was written by Roberto Ierusalimschy, the developer of Lua.


--
-- strict.lua
-- checks uses of undeclared global variables
-- All global variables must be 'declared' through a regular assignment
-- (even assigning nil will do) in a main chunk before being used
-- anywhere or assigned to inside a function.
--

local mt = getmetatable(_G)
if mt == nil then
  mt = {}
  setmetatable(_G, mt)
end

mt.__declared = {}

mt.__newindex = function (t, n, v)
  if not mt.__declared[n] then
    local w = debug.getinfo(2, "S").what
    if w ~= "main" and w ~= "C" then
      error("assign to undeclared variable '"..n.."'", 2)
    end
    mt.__declared[n] = true
  end
  rawset(t, n, v)
end
  
mt.__index = function (t, n)
  if not mt.__declared[n] then
    error("variable '"..n.."' is not declared", 2)
  end
  return rawget(t, n)
end



What this does is use a metatable on the global (_G) table to make sure that setting variables, or reading them, is only done to "declared" variables. That is, variables that are explicitly mentioned on the global script space.

This helps prevent common errors like misspelling variable names in functions, or accidentally assigning to a global variable when a local one would be more sensible.

Let's assume you save that to disk as "strict.lua".

Some example code:


require "strict"

function f ()
 a = 2 --> error: assign to undeclared variable 'a'
 
 print (b)  --> error:  variable 'b' is not declared
 
 function g ()  --> error:  assign to undeclared variable 'g'
 end -- g
 
end -- f

f ()



A sensible strategy in these cases would be to put "local" in front of variables and functions that are really supposed to only exist inside the scope of the local function.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Wed 06 Sep 2006 05:18 PM (UTC)

Amended on Wed 06 Sep 2006 05:19 PM (UTC) by David Haley

Message
Perl programmers out there will recognize this behavior as being very similar to use strict, or, at least, use strict vars. It's very useful for debugging, because it's easy to make a typo in a variable name and cause mysterious and hard-to-find errors.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


9,125 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.