lua-users home
lua-l archive

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


On Fri, Jun 04, 1999 at 11:42:05AM -0300, Luiz Henrique de Figueiredo wrote:

>>>lhf wrote:
>>>there's a game you can play with the set/getglobal tag methods and with
>>>the callhook so that global variables seem local to a chunk.
>>
>>Would you elaborate on how this can be done?  Thanks...

> Use the call hook to see when a chunk starts and end.  A chunk
> starts when line==0. It ends when file=="(return)".  (Since line==0
> when a chunk ends, you should test file first.)  See etc/trace.c in
> the distribution.
>
> Now, when a chunk starts, set the setglobal and the getglobal tag
> methods for nil to use a table to store values. Something similar is
> described in the FAQ: 3.3 - How do I create read-only
> variables?. See also some code in test/trace.lua.
>
> When a chunk ends, simply restore the defaults set/getglobal tag
> methods.

I'm confused about how this works. Correct me if I'm mistaken, but
functions do not inherit their tag methods when they are defined. They
use the tag methods which are setup when they are called. I believe
the above solution will only work if the execution of code in the
chunk begins and ends completely within the chunk. If you define a
function within the chunk which uses global variables, and you call
that function after the chunk is loaded, the variables are no longer
chunk local.

Is some of my analysis incorrect? Can you think of a way to get
chunk-local variables which work even when you call functions after
the chunk is loaded?

The only method I can think of involves overriding the global set and
table set tag methods to detect function assignments and have them
create wrapper functions which set the tag methods appropriately. This
definetly won't have good performance consequences (doubling the
number of function invocations to do anything), however, I think it
would give the desired effect (i.e. a chunk/module variable scope).

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + jeske@chat.net