lua-users home
lua-l archive

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


>Sorry, I should have been clearer - by "looks up local variables",  I meant a lookups any variable reference in a function, not just variables declared as local.
>
>So if you wanted to look up variables like this:
>1. in the locals (I'm assuming arguments are locals too)
>2. then in self table (if there is a local self variable)
>3. then in the globals table
>
>Is there an easy way to implement this in Lua?

No. Locals are really stack objects, accessed by number, not name.
Local names are now (4.0) stored in the functions, but only for error reporting.

>If there we some sort of abstract table that lua put arguments and locals in while executing a function that it did variable lookups on, then a index tag on this table would do the trick.

Yes, that would be the way to go, now that globals are in an ordinary table.
But, like I said, performance would suffer (and I don't know how much...)

Moving globals to an ordinary table did cost some performance but Lua 4.0
ws already some much faster than 3.2 that it did not matter much and it
simplified the code a lot.
In the distant past (2.4?), globals were once accessed by number, but it was
complicated when we added loading binaries produced by luac.

What I'm saying is that in principle we could store locals in a table, but
that would require changing the virtual machine (yet again!) and would
probably cost performance, but this would have to be measured.
It all boils down to whether we want to have this complication for perhaps
a slight increase of expressive power. I don't know the answer.
--lhf