Andreas Rozek Hints for Reading List of Recent Changes Guestbook Entry Contact the Author  Deutsche Version  HomePage Previous Topic Next Topic  First Page of Current Topic Next Page Previous Page

FunctionProtection Module

Lua 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 Description

The 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 Notes

The 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

  • the LibrarySupport script file must be copied into the same directory where the application resides;

  • the FunctionProtection script file must reside in a directory where it can be found by the import function, i.e., a directory which is part of the LuaLibList;

Source Code

The source code of this module is available for download:

References

[1] Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
Reference Manual of the Programming Language Lua 4.0
(see http://www.lua.org/manual)
the reference manual contains any relevant information about the language itself, the set of standard libraries and its interface to the run-time environment;
[2] Lua FAQ
(see http://www.lua.org/faq.html)
the given web page contains answers to frequently asked questions about Lua;

Disclaimer

Please, consider also the author's Disclaimer!

http://www.Andreas-Rozek.de/Lua/Modules/FunctionProtection.html    (last Modification: 22.04.2002)