userdata metatable is loaded smoothly
This commit is contained in:
+1
-11
@@ -80,17 +80,7 @@ void ServerApplication::Init(int argc, char* argv[]) {
|
|||||||
std::cout << "Initialized lua" << std::endl;
|
std::cout << "Initialized lua" << std::endl;
|
||||||
|
|
||||||
//create the userdata metatable
|
//create the userdata metatable
|
||||||
lua_pushlightuserdata(luaState, nullptr); //userdata
|
createUserdataMetatable(luaState);
|
||||||
lua_createtable(luaState, 1, 0); //table
|
|
||||||
|
|
||||||
//__index
|
|
||||||
lua_pushstring(luaState, "__index");
|
|
||||||
lua_pushcfunction(luaState, userdataIndex);
|
|
||||||
lua_settable(luaState, -3);
|
|
||||||
|
|
||||||
//set in the object
|
|
||||||
lua_setmetatable(luaState, -2); //set the metatable for userdata
|
|
||||||
lua_pop(luaState, 1); //pop the userdata
|
|
||||||
|
|
||||||
std::cout << "\tCreated userdata metatable" << std::endl;
|
std::cout << "\tCreated userdata metatable" << std::endl;
|
||||||
|
|
||||||
|
|||||||
+24
-1
@@ -23,7 +23,30 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int userdataIndex(lua_State* L) {
|
static int index(lua_State* L) {
|
||||||
std::cout << "WARNING: userdataIndex called" << std::endl;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
+1
-3
@@ -24,8 +24,6 @@
|
|||||||
|
|
||||||
#include "lua.hpp"
|
#include "lua.hpp"
|
||||||
|
|
||||||
#define TORTUGA_USERDATA_NAME "userdata"
|
int createUserdataMetatable(lua_State* L);
|
||||||
|
|
||||||
int userdataIndex(lua_State* L);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user