luax_extendtype; luax_checktypeof;

This commit is contained in:
bjorn 2017-05-16 11:38:49 -06:00
parent 1115929583
commit 4ac72a54d6
2 changed files with 21 additions and 0 deletions

View File

@ -71,6 +71,16 @@ void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions
lua_pop(L, 1);
}
void luax_extendtype(lua_State* L, const char* base, const char* name, const luaL_Reg* functions) {
luax_registertype(L, name, functions);
luaL_getmetatable(L, name);
lua_pushstring(L, base);
lua_setfield(L, -2, "super");
lua_pop(L, 1);
}
int luax_releasetype(lua_State* L) {
lovrRelease(*(Ref**) lua_touserdata(L, 1));
return 0;

View File

@ -5,7 +5,17 @@
#pragma once
#define STRINGIFY(x) #x
#define luax_checktype(L, i, T) *(T**) luaL_checkudata(L, i, #T)
#define luax_checktypeof(L, i, T) \
*(T**) (luaL_argcheck(L, lua_touserdata(L, i), i, "Expected " STRINGIFY(T)), \
lua_getmetatable(L, i), \
lua_getfield(L, -1, "super"), \
lua_pushstring(L, #T), \
luaL_argcheck(L, lua_equal(L, -1, -2), i, "Expected " STRINGIFY(T)), \
lua_pop(L, 3), \
lua_touserdata(L, i))
#define luax_newobject(L, T, x) \
T** u = (T**) lua_newuserdata(L, sizeof(T**)); \
luax_registerobject(L, x); \
@ -16,6 +26,7 @@
int luax_preloadmodule(lua_State* L, const char* key, lua_CFunction f);
void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions);
void luax_extendtype(lua_State* L, const char* base, const char* name, const luaL_Reg* functions);
int luax_releasetype(lua_State* L);
int luax_getobject(lua_State* L, void* object);
void luax_registerobject(lua_State* L, void* object);