userdata metatable is loaded smoothly

This commit is contained in:
Kayne Ruse
2015-03-27 20:42:36 +11:00
parent 29ccb982e6
commit 9a5c447d0c
3 changed files with 26 additions and 15 deletions
+24 -1
View File
@@ -23,7 +23,30 @@
#include <iostream>
int userdataIndex(lua_State* L) {
static int index(lua_State* L) {
std::cout << "WARNING: userdataIndex called" << std::endl;
return 0;
}
static luaL_Reg metatable[] = {
{"__index", index},
{nullptr, nullptr}
};
int createUserdataMetatable(lua_State* L) {
//create the userdata metatable
lua_pushlightuserdata(L, nullptr); //anon userdata
lua_createtable(L, 0, 0); //table
for (luaL_Reg* it = metatable; it->name; it++) {
lua_pushstring(L, it->name);
lua_pushcfunction(L, it->func);
lua_settable(L, -3);
}
//set in the object & pop the anon userdata
lua_setmetatable(L, -2); //set the metatable for userdata
lua_pop(L, 1); //pop the anon userdata
return 0;
}