Blitz Lua Keeper

lua-users home
wiki

This page briefly describes BlitzLua "Keepers", a mechanism provided by BlitzLua to allow explicit finalization of (pure) Lua objects.

Introduction

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.

Theory of Operation

"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?":

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:

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.

References

--AndreasRozek


RecentChanges · preferences
edit · history
Last edited May 5, 2007 9:13 pm GMT (diff)