lua-users home
lua-l archive

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



I may have asked this before, but the issue has come up again, so I apologize if it is redundant.

If I write an __index() metamethod in C, it will get called with a string parameter, so:

    local a = obj.foo

If obj has an __index metamethod, I can go like this in that metamethod:

    const char* prop = lua_tostring(L, -1);

and I will have a pointer to "foo". Now if I go:

    lua_pushstring(L, "foo");
    const char* s = lua_tostring(L, -1);

will the pointer s always be equal to the pointer prop? In all the cases I have tried it is. But can I rely on this?

This comes up because I want to make an atom table. For instance, I want to do something like this to create the atom:

    lua_getfield(L, LUA_GLOBALSINDEX, "__atomtable");
    lua_pushlightuserdata(L, lua_tostring(L, -1));
    lua_setfield(L, -2, "foo");

And I will store the string pointer as the value of the atom. Now I want to go:

    if (lua_tostring(L, -1) == myFooAtom) // the strings match!

As I said, this works today in test cases I have written. But can I reliably use this as an efficient atom mechanism? If not, is there any way I can test strings coming in from the Lua stack without interning a test string every time?

--
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`