Page 1 of 1

[Lib] _L_U - access locals and upvalues via tables

Posted: Wed Mar 30, 2016 12:57 pm
by TheZombieKiller
https://github.com/DaZombieKiller/_L_U

This is another old library of mine, that I decided to completely re-do today.
It adds an _L (locals) and _U (upvalues) table to Lua, similar to _G. It's intended for debugging, but may be useful for other purposes.

Note that _L and _U cannot be used to declare new locals or upvalues, but can be used to modify or access them.
This also allows you to easily add them to the global table, via something like this:

Code: Select all

for k, v in pairs(_L) do _G[k] = v end
for k, v in pairs(_U) do _G[k] = v end
The GitHub page includes an example file with explanations on how it all works.
I dunno if anyone will really find this useful in the long run, but I thought I might as well release it anyway.

Re: [Lib] _L_U - access locals and upvalues via tables

Posted: Wed Mar 30, 2016 3:16 pm
by ivan
Cool idea, but it would be nice if you have some practical examples on why/how I would want to use the lib (I assume it's for debugging).

The implementation is OK, but could be improved in the following way:
Every time we access "_L" a new table is constructed, containing every single "local" variable.
This is convenient for the "pairs" function, but it's not very efficient for access/assignment.
Access/assignments use "_L['foo']" so you probably don't need to fetch all "local" vars every time you access "_L['foo']".
Hope this makes sense.

Good work though, it's an interesting idea. :)

Re: [Lib] _L_U - access locals and upvalues via tables

Posted: Wed Mar 30, 2016 3:31 pm
by TheZombieKiller
Fair point you have there, I'll probably add some optimizations to it later on.
Also yeah, it's intended for debugging, although it has other possible uses I guess.