#include #include #include /* push metatable to stack, create if not yet reg, the metatable of the table is its self */ int lua_opentablemt(lua_State *L, const char * libname, const luaL_reg * reg, int upval){ /*STACK:...#*/ if(luaL_newmetatable(L, libname)){ /*STACK:...#*/ luaL_openlib(L, NULL, reg, upval); lua_pushliteral(L, "__index"); /*STACK:...<"__index">#*/ lua_pushvalue(L, -2); /*STACK:...<"__index">#*/ lua_rawset(L, -3); /*STACK:...*/ return 1;//created } return 0;//opened, all ready created } /* new user data with metamethods table */ void * lua_newuserdatamt(lua_State *L, size_t cdata, const char * mtname, const luaL_reg * mtreg){ void* pdata = lua_newuserdata(L, cdata);/*STACK:...# */ lua_opentablemt(L, mtname, mtreg, 0); /*STACK:...# */ lua_setmetatable(L, -2); /*STACK:...# */ return pdata; } /* new user data with metamethods table and upvalues */ void * lua_newuserdatamtuv(lua_State *L, size_t cdata, const char * mtname, const luaL_reg * mtreg, int upval){ void* pdata; /*STACK:...#*/ lua_opentablemt(L, mtname, mtreg, upval); /*STACK:...#*/ pdata = lua_newuserdata(L, cdata); /*STACK:...#*/ lua_insert(L,-2); /*STACK:...#*/ lua_setmetatable(L, -2); /*STACK:...#*/ return pdata; }