userdata metatable is loaded smoothly
This commit is contained in:
+24
-1
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user