lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Is there a way to learn what functions are defined when I call
> lua_dofile() / lua_dostring()? And using this information, is there a
> way to programmatically undefine these functions?

Undefining functions is pretty straight-forward: simply assign nil to the
corresponding function names. So, something like "func1 = nil" undefines
function "func1".

To determine what functions were actually redefined in a particular chunk,
it might help to remember the global environment is actually a table. You
can iterate the table to see what's there, run the chunk, and then iterate
the globals table again to see what's changed.

Another approach would be to set up a new (empty) globals table that
delegates to your original environment [see the reference manual on how you
can delegate lookup from one table to another, using tag/meta-methods].
Running your code would then set up its entries in the new globals table,
enabling you to see exactly what was changed.

Undefining all code from the first script in your example would thus simply
become a matter of setting up a new globals table for your next script (and
forgetting about the first one, obviously). I believe such a
"sandbox"-approach may well be what you're looking for.

I hope this helps.

Ashwin.