Last active
August 29, 2015 14:15
-
-
Save yorung/f7ac5b81f48a87713262 to your computer and use it in GitHub Desktop.
a very simple class binder for Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int CreateCppClassInstance(lua_State* L) | |
{ | |
const char* className = lua_tostring(L, lua_upvalueindex(1)); | |
lua_CFunction actualInstanceCreator = lua_tocfunction(L, lua_upvalueindex(2)); | |
actualInstanceCreator(L); | |
luaL_getmetatable(L, className); | |
lua_setmetatable(L, -2); | |
return 1; | |
} | |
static void CreateClassMetatable(lua_State* L, const char* className, luaL_Reg methods[]) | |
{ | |
luaL_newmetatable(L, className); | |
lua_pushstring(L, "__index"); | |
lua_pushvalue(L, -2); | |
lua_settable(L, -3); | |
luaL_setfuncs(L, methods, 0); | |
lua_pop(L, 1); | |
} | |
static void CreateClassInstanceCreator(lua_State* L, const char* className, lua_CFunction creator) | |
{ | |
lua_pushstring(L, className); | |
lua_pushcfunction(L, creator); | |
lua_pushcclosure(L, CreateCppClassInstance, 2); | |
lua_setglobal(L, className); | |
} | |
void BindClass(lua_State* L, const char* className, luaL_Reg methods[], lua_CFunction creator) | |
{ | |
CreateClassMetatable(L, className, methods); | |
CreateClassInstanceCreator(L, className, creator); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment