| Andreas Rozek |
|
FunctionProtection ModuleLua is a highly dynamic language insofar as, at run-time, any part of a script (even its implementation) may be modified by the script itself. Thus, since vital components may be changed as well (and undesired modifications might become appearant only long after they have been applied) there is some need for "protection" of certain items of a running Lua program. The aim of the FunctionProtection module is to "protect" globally defined functions from being accidentially overwritten. While there is still the possibility to explicitly redefine a global function, a simple assignment to the corresponding global variable will just issue an error message and terminate the program (at the location where this problem occurred). Technical DescriptionThe idea for the module and its implementation came from a section found in the Lua FaQ ("How do I create read-only variables?", [2]) and has been assumed almost without any modification. Basically, the FunctionProtection module registers a tag method for global functions and the event "setglobal" which terminates the actual script with an error message as soon as it detects an attempt to modify a global function. Please note, that there is still the possibility to "rawset" global variables (which bypasses the mentioned tag method) and, thus, to redefine global functions despite an active "function protection". Because of this behaviour, the FunctionProtection module should be "imported" not before all global functions of the running script have been defined - this includes the import of any other modules. Usage NotesThe script file for this module may just be executed from within an application by means of the built-in Lua function dofile (the script safely handles any attempts to run it multiple times). With the assistance of the author's LibrarySupport module, you may, however, prefer to import it:
--**** load any definitions required by this script ****
if (type(import) ~= "function") then
dofile("LibrarySupport.lua"); -- provides a simple library import mechanism
end;
import("FunctionProtection"); -- "protects" functions from being overwritten
In that case, please keep in mind that
Source CodeThe source code of this module is available for download:
References
|
| http://www.Andreas-Rozek.de/Lua/Modules/FunctionProtection.html | (last Modification: 22.04.2002) |