lua-users home
lua-l archive

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


2009/8/18 Chris Camacho <c.camacho@ntlworld.com>:
>> You can use lua_pushvalue to duplicate the reference to the userdata
>> on the stack. Then you can save one in the weak table (with the light
>> userdata key), and do whatever you want with the other one.
>
>  char buf[40];
>  cpBody *bb = (cpBody *)lua_newuserdata(L, sizeof(cpBody));
>  lua_pushvalue(L,-1);
>  lua_pushstring(L,"__cpBody_ptrs");
>  sprintf(buf,"%ld\0",(long)bb);
>  lua_setfield(L,-2,buf);
>
>  luaL_getmetatable(L, "cpBody");
>  lua_setmetatable(L, -2);
>  return bb;
>
> what on earth does attempt to index a userdata value mean?

You're calling lua_setfield (which is an indexing operation), with the
parameter -2 pointing to the stack slot where you created the 'bb'
userdata. Since that userdata has no metatable the indexing operation
fails.

I don't know if that lua_pushvalue call is supposed to reflect the
previous advices I gave you, but if it is you are clearly misusing it.