Blitz Lua Keeper |
|
BlitzLua is a Lua runtime environment written in BlitzMAX, a fast cross-platform BASIC compiler and run-time system for Windows 98/ME/2000/XP, MacOS X and Linux. BlitzMAX objects are passed to Lua as "light userdata" and manipulated using functions (with the underlying object as their first parameter) instead of "methods". An additional layer (above the underlying "function interface") directly maps any BlitzMAX objects to a corresponding Lua table. By means of a "keeper", such a table can perform final "house-keeping" operations (such as the explicit removal of the associated BlitzMAX object) when it is about to be removed by the Lua garbage collector.
"Keeper"s implement a mechanism described in the [Lua FaQ] as an answer to the question "Why doesn't the __gc metamethod work on tables?":
blitzmax.newKeeper(LuaTable)
creates a new (full) userdata object which uses the given LuaTable
as its environment
LuaTable
should store a reference to the new "keeper" in its field "_Keeper" (with leading underbar and capital "K")
Both the LuaTable
and the "keeper" now reference each other - and there should be no other reference to the "keeper" within Lua. As soon as the LuaTable
is no longer referenced from another object besides its "keeper", the Lua garbage collector starts collecting both objects:
LuaTable
still uses the userdata object as its "keeper" (if not, it silently aborts). Afterwards, it looks for a field "destroy" within LuaTable
(which should hold a Lua function) and invokes it with LuaTable
as its first and only argument
The destructor may now perform any operations required to clean-up the LuaTable
and its associated BlitzMAX object. As soon as the destructor finishes (or fails), both objects will be removed by the Lua garbage collector.