lua-users home
lua-l archive

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


I’ve got some code that does this:

 

        lua_pushlightuserdata(l, (void *) mctx);

        lua_setglobal(l, "mctx");

        lua_pushlightuserdata(l, (void *) mctx);

        (void) luaL_newmetatable(l, CHIPOTLE_TYPE_MCTX);

        lua_setmetatable(l, -2);

        lua_pop(l, 1);

 

So this (supposedly) puts my own pointer “mctx” into the state, with global name “mctx” assigned to it, then creates a metatable (if it’s not already there) and assigns that to my pointer.  Should be pretty simple.

 

Later on, I do this:

 

        if (lua_getmetatable(l, 1) != 0)

        {

                lua_getfield(l, LUA_REGISTRYINDEX, CHIPOTLE_TYPE_MCTX);

                if (lua_rawequal(l, -1, -2) == 0)

                {

                        lua_pop(l, top);

                        lua_pushstring(l, "myfunction(): invalid usage");

                        lua_error(l);

                }

        }

        else

        {

                lua_pop(l, top);

                lua_pushstring(l, "myfunction(): internal error");

                lua_error(l);

        }

 

However, no matter what pointer is at index 1, this test appears to pass.  That’s bad.


In fact I find that it only works if a full userdata is tagged with a metatable.  But that’s not what the docs say.

 

Any idea what’s going on here?

 

-MSK