[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: setfenv on userdata???
- From: Rici Lake <lua@...>
- Date: Mon, 22 Aug 2005 10:54:29 -0500
On 22-Aug-05, at 10:35 AM, Chris Marrin wrote:
Mike Pall wrote:
Chris Marrin wrote:
I see in 5.1-work6 that you can now set environments not only on
both C and Lua functions, but also on userdatas. But I don't see
anywhere in the code where this environment is being used. What can
you do with this environment? Does it get used when you make
metamethod calls with this userdata? Or is it just there so it can
be retrieved or something?
Thanks for the pointer. This brings up a question:
- I assume that luaopen_queue() is what gets implicitly called when
you do the require "queue", right? If so, then it looks like it sets
the environment for your test program to the environment for the
queue, as evidenced by lua_replace(L, LUA_ENVIRONINDEX). Looking at
the code for this, it gets the current function (which is the main lua
program at this point?) and sets its environment to the passed
table(which is the metatable you are constructing for queue). Doesn't
that change the environment for the whole program?
Yeah, that threw me as well. However, the idea is that you don't just
call luaopen_queue; in 5.1, you lua_call library open functions. So
your code would probably have looked like:
lua_call(L, 0, 0);
in which case the instance of the luaopen_queue function which is
setfenv'd is promptly discarded.
It still seems a little odd to me but I'm sure I'll get used to it.