lua-users home
lua-l archive

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


On Sat, 24 Mar 2012 15:56:17 -0700 (PDT)
codemonkey Monkey <thecodemonkey@rocketmail.com> wrote:

> Hi Steve!  Thanks for your reply.
> 
> > If there's no such thing, what's wrong with every subroutine making
> > a
> call to a quick program to retrieve the intended environment?
> 
> Well, a few things.  The major one is that you have to do it each and
> every time, or it's a bug.  

For practical purposes that's true.

> I had essentially that when I was calling
> "setfenv" in every Lua function.  First it litters up every Lua fn
> with extra lines of code, but more importantly it's fragile against
> programmer errors. I was constantly forgetting to do it, and then I
> had subtle defects in my Lua code where some variable that should
> have gone to the environment-of-interest really went elsewhere.

True enough.

> Since there's no compile-time checking in Lua (already dangerous
> itself), that's a pretty dangerous situation.

Hmmmm...

> 
> 
> Since I'm intending for the game community to be able to write these
> Lua scripts, I don't want everybody to have to understand they must
> keep calling such functions.  The real solution should be robust and
> automatic, leaving no chance to screw it up :).  I screwed it up
> enough myself, and I understood what was happening!  I'd rather have
> the C++ calling functions set something up that "just works".

Obviously I agree. The problem I see is that in Lua, tables are
passed/assigned by reference, not by value. So if a called function
modifies a table value, it remains modified after returning to the
caller, which I don't think is what you wanted. So you can't use any
data structure created from a table.

> 
> 
> Also, performance matters here, second after robustness; I don't want
> extra function calls in every Lua function.  (In fact, I can
> optionally switch in LuaJIT to get more performance, and that's what
> I intend to use for real).

At this point I don't think anyone can tell how much of a performance
hit these extra calls would make. After all, if there were such a thing
as a passed/stacked environment, you'd still need to query it.

> 
> 
> I'm not trying to claim there's no way to do what I want, only that
> I don't know what the way is :).  Seems like there must be one...

I think probably somebody will come forth with a Lua-provided way to do
it. But if not, I'd kinda like to help with the thinking on doing the
best possible workaround. I might learn something.

In another email I'll throw out some off-the-cuff rough ideas.

Thanks

SteveT