lua-users home
lua-l archive

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


Hello Mark,

----- Original Message -----
From: "Mark Hamburg" <mhamburg@adobe.com>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Thursday, December 18, 2003 3:05 PM
Subject: Re: getfenv question


> I've written Lua code that uses getfenv with a series of increasing
indices
> to walk up the call stack looking for a function environment that defines
a
> particular variable. This creates sort of a dynamic scoping effect. The
Lua
> code right now, however, has no way to detect when to stop looking and
> instead just relies on catching the exception when the index gets too
high.
> I'm somewhat loathe to use exceptions for a non-exceptional condition.
>
> I don't have a problem with writing this in C if that will work better,
but
> I figured that I'd try doing it in pure Lua first.
>
> Mark
>
> P.S. My specific usage: When I need to load resources such as images for
> some GUI code, I need to know what directory to look in. What I'm trying
to
> make work is something where when I load a piece of code, I give it a
custom
> environment that includes a variable pointing to the directory it came
from.
> Then I can just search up the call chain looking at directories until I
find
> one containing the named image.

    Unhapply I can't help you, as I'm not know how you are setting this env.
This is because if you are setfenv it to some random table, you will never
know the root, specially if your "search" function are inside one of this
env (as it appear to be).
    Isn't better if you implement something like indexing the function in a
know table instead of using setfenv?
    Or let the enviromment table have a metatable that set the env in
another table, adding directories to it with numerical index.
    Or instead of setfenv(afunc,atable) you can do acommontable[afunc] = {},
and then setfenv it to this table. Then you search in this acommontable for
all the envs, instead of searching the stack.
    This is my little suggestions, of course, I can't says anything more
accurate because I'm not seeing your code.

                                                                God bless
you,


Leandro.
>
> on 12/18/03 12:06 AM, Leandro Candido at enclle@click21.com.br wrote:
>
> > Hello Mark,
> >
> >   Can you better explain what you want? Are you making this in C or in
> > Lua? Are you using the debug api?
> >   What you want is to limit the access for getfenv?
> >
> >                                                                   The
> > God's Peace,
> >
> >
> > Leandro.
> > ----- Original Message -----
> > From: "Mark Hamburg" <mhamburg@adobe.com>
> > To: "Lua list" <lua@bazar2.conectiva.com.br>
> > Sent: Wednesday, December 17, 2003 11:14 PM
> > Subject: getfenv question
> >
> >
> >> Is there a way other than wrapping things in pcall to tell what the
> > maximum
> >> level I can pass to getfenv is?
> >>
> >> I'm trying to implement pseudo-dynamic scoping by searching up the call
> >> chain for the first function environment referencing a particular
> > variable.
> >>
> >> Mark
> >>
> >>
> >
>
>