lua-users home
lua-l archive

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



Here is how to do it:

I ended up with something different (I'm not using weak ref's
as with this particular lib I've decided its more appropriate to
"hand back" the object from the lib)

static cpBody *push_cpBody (lua_State *L) {
  cpBody *bb=cpBodyNew(0,0); // initialise later
cpBody **pbb = (cpBody **)lua_newuserdata(L, sizeof(cpBody*)); // ptr to ptr
  *pbb = bb;  // Lua is only holding a pointer to the object pointer

  luaL_getmetatable(L, "cpBody");
  lua_setmetatable(L, -2);

lua_getglobal(L,"__cpBody_ptrs"); // this table is created when cpBody UD is created
  lua_pushlightuserdata(L, bb);   // table index
lua_pushvalue(L,-3); // previously created table of *cpBody pointers to userdata
  lua_rawset(L, -3); // update the table
  lua_pop(L,1); // restore stack

 return bb;
}

What do you think any obvious bloopers?



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