I am using Lua in a very resource constrained system, in conjunction with C.
A library that I am using creates some references (as pointers) to the objects it creates which are useful for later access to these objects. In order to expose this library's functionality to Lua, when such an object is created, this reference is returned to the Lua script.
The user has the ability to store this reference to their liking, as this makes later calls very convenient.
Example cases:
ref = MyLib.createObject("some", arguments)
local ref = MyLib.createObject("some", arguments)
table_of_refs[45] = MyLib.createObject("some", arguments)
-- etc...
Unfortunately these references may be destroyed outside of the Lua script's scope (from within C). Thus these references may become invalid.
For the time, my code can handle these invalid references with no problem. All these pointers are validated before actual use in the library, so the code is safe.
However it seems a bit confusing on the Lua user's perspective, since there is no way to tell if this reference is still valid or not (this is not very important but still I want to improve it).
What I want is the following. I want to iterate from C all lightuserdata that Lua has stored. If the lightuserdatum is not valid anymore, set it to nil. This way, on next usage the variable will be either valid or nil, providing a much better API to users.
Is there any way to achieve this? Can I iterate from C all lightuserdata that Lua knows about (regardless of where they are stored, local / global / table etc), and modify them?