lua-users home
lua-l archive

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


Rici Lake wrote:


On 21-Aug-05, at 2:24 PM, 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?


I figured the easiest way to answer this question (for myself) was to write a binding library using the feature. I'm generally happy with the results, but it makes me wonder about the semantics of userdata environments.

Instead of dumping several pages of rumination into the mailing list, I've put it all on the Wiki at http://lua-users.org/wiki/UserDataRefinement

Thanks a lot for this missive. It is incredibly close to what I am trying to do. Some questions to help (sorry if some are pedantic):

- CodeSnippetThree: in getenvfield() you do this:

    lua_getfenv(L, index);
    lua_getfield(L, -1, fieldname);
    if (lua_isnil(L, -1)
        && !lua_rawequal(L, -1, LUA_ENVIRONINDEX)) {
        ...

This gets the desired field out of the env table. If it is nil that means it does not exist in that table, which makes sense. But then you test to see if that value is the same as the environment. Is that right? I don't see how that can work. Wouldn't this test actually want to test -2 against the LUA_ENVIRONINDEX?

- CodeSnippetThree: It looks like you leave a value on the stack. Is that intended?

After reading all this I am still confused about what the environment table in a userdata does. Is it just a place to store a table that can be retrieved and manipulated, or does it actually come to play in some API calls somewhere? As I mentioned, I don't see it being used anywhere in the 5.1 API. Sorry about being dense, but I really think this will solve a lot of problems for me, so I really want to understand it well.

Thanks again...


--
chris marrin              ,""$, "As a general rule,don't solve puzzles
chris@marrin.com        b`    $  that open portals to Hell" ,,.
        ,.`           ,b`    ,`                            , 1$'
     ,|`             mP    ,`                              :$$'     ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`