--****************************************************************************** --* * --* File: ExplicitGlobals.lua Revision: 1.0 * --* * --* Purpose: requires global variables to be explicitly declared * --* * --* Creation: 13.03.2002 Last Modification: 04.04.2002 * --* * --* Platform: IBM-compatible PC running Windows 98SE * --* * --* Environment: Lua 4.0, TkLua 4.0 * --* * --* Author: Andreas Rozek Phone: ++49 (711) 6770682 * --* Kirschblütenweg 15 Fax: - * --* D-70569 Stuttgart EMail: Andreas.Rozek@T-Online.De * --* Germany * --* * --* URL: http://www.Andreas-Rozek.de/ * --* * --* Copyright: the software is published under the "GNU Lesser General Pub- * --* lic License" (see "http://www.fsf.org/copyleft/lesser.html" * --* for additional information) * --* * --* Comments: (none) * --* * --****************************************************************************** --**** do not execute this script more often than once **** if (type(global) == "function") then return nil; end; --**** load any definitions required by this script **** if (type(import) ~= "function") then dofile("LibrarySupport.lua"); -- provides a simple library import mechanism end; import("BasicDefinitions"); -- provides basic definitions for Lua scripts --**** maintain a list of declared globals **** local declaredGlobals = globals(); -- automatically "declare" existing globals for Key,Value in declaredGlobals do declaredGlobals[Key] = 1; -- keep the "name" rather than the "content" end; --****************************************************************************** --* * --* global registers the given name(s) for global variables * --* * --****************************************************************************** function global (...) local ArgCount = getn(arg); if (ArgCount == 1) and isList(arg[1]) then call(global,arg); else for i = 1,ArgCount do assert( -- also rejects 'nil' values type(arg[i]) == "string", "invalid type of argument (expected \"string\", got \""..type(arg[i]).."\")" ); %declaredGlobals[arg[i]] = 1; end; end; end; --****************************************************************************** --* * --* getglobal replacement for the homonymous built-in function * --* * --****************************************************************************** local getglobal = function (Name) assert( type(Name) == "string", -- also rejects 'nil' values "invalid type of argument \"Name\" (expected \"string\", got \""..type(Name).."\")" ); --**** check for a declaration of 'Name' **** assert(%declaredGlobals[Name],"undeclared global \""..encodedString(Name).."\""); --**** and then actually retrieve the given global **** return nil; -- the predictable result "rawget(globals(), Name)" end; --****************************************************************************** --* * --* setglobal replacement for the homonymous built-in function * --* * --****************************************************************************** local setglobal = function (Name, Value) assert( type(Name) == "string", -- also rejects 'nil' values "invalid type of argument \"Name\" (expected \"string\", got \""..type(Name).."\")" ); --**** check for a declaration of 'Name' **** assert(%declaredGlobals[Name],"undeclared global \""..encodedString(Name).."\""); --**** and then actually set the given global **** rawset(globals(), Name, Value); end; --****************************************************************************** --* * --* main program * --* * --****************************************************************************** --**** install accessor functions for global variables **** settagmethod(tag(nil), "getglobal", getglobal); settagmethod(tag(nil), "setglobal", setglobal);