lua-users home
lua-l archive

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


well I'm totally lost now...

cpBody *has* a metatable (__index is used for the methods) I even tried

char buf[40];
cpBody *bb = (cpBody *)lua_newuserdata(L, sizeof(cpBody));
lua_pushvalue(L,-1);
luaL_getmetatable(L, "cpBody");
lua_setmetatable(L, -2);

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;


How am I missing your previous advice, are you able to
explain in a clearer manner?

how else would I index the cpBody* C pointer with the userdata instance
in a table called __cpBody_ptrs  ?

Thanks
Chris



Jerome Vuarand wrote:
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.


--
Disclaimer:
By sending an email to ANY of my addresses you are agreeing that:

  1. I am by definition, "the intended recipient"

  2. All information in the email is mine to do with as I see fit and
make such financial profit, political mileage, or good joke as it lends
itself to. In particular, I may quote it where I please.

  3. I may take the contents as representing the views of your company.

  4. This overrides any disclaimer or statement of confidentiality that
may be included on your message.