Make registertype/extend type a macro;

Just a nice-to-have.
This commit is contained in:
bjorn 2019-04-05 03:48:36 -07:00
parent 6fdeda9e61
commit ae470d8804
9 changed files with 42 additions and 47 deletions

View File

@ -248,8 +248,8 @@ static const luaL_Reg lovrAudio[] = {
int luaopen_lovr_audio(lua_State* L) { int luaopen_lovr_audio(lua_State* L) {
lua_newtable(L); lua_newtable(L);
luaL_register(L, NULL, lovrAudio); luaL_register(L, NULL, lovrAudio);
luax_registertype(L, "Microphone", lovrMicrophone); luax_registertype(L, Microphone);
luax_registertype(L, "Source", lovrSource); luax_registertype(L, Source);
if (lovrAudioInit()) { if (lovrAudioInit()) {
luax_atexit(L, lovrAudioDestroy); luax_atexit(L, lovrAudioDestroy);
} }

View File

@ -132,11 +132,11 @@ static const luaL_Reg lovrData[] = {
int luaopen_lovr_data(lua_State* L) { int luaopen_lovr_data(lua_State* L) {
lua_newtable(L); lua_newtable(L);
luaL_register(L, NULL, lovrData); luaL_register(L, NULL, lovrData);
luax_registertype(L, "Blob", lovrBlob); luax_registertype(L, Blob);
luax_registertype(L, "AudioStream", lovrAudioStream); luax_registertype(L, AudioStream);
luax_registertype(L, "ModelData", lovrModelData); luax_registertype(L, ModelData);
luax_registertype(L, "Rasterizer", lovrRasterizer); luax_registertype(L, Rasterizer);
luax_extendtype(L, "Blob", "SoundData", lovrBlob, lovrSoundData); luax_extendtype(L, Blob, SoundData);
luax_extendtype(L, "Blob", "TextureData", lovrBlob, lovrTextureData); luax_extendtype(L, Blob, TextureData);
return 1; return 1;
} }

View File

@ -1508,15 +1508,15 @@ static const luaL_Reg lovrGraphics[] = {
int luaopen_lovr_graphics(lua_State* L) { int luaopen_lovr_graphics(lua_State* L) {
lua_newtable(L); lua_newtable(L);
luaL_register(L, NULL, lovrGraphics); luaL_register(L, NULL, lovrGraphics);
luax_registertype(L, "Animator", lovrAnimator); luax_registertype(L, Animator);
luax_registertype(L, "Font", lovrFont); luax_registertype(L, Font);
luax_registertype(L, "Material", lovrMaterial); luax_registertype(L, Material);
luax_registertype(L, "Mesh", lovrMesh); luax_registertype(L, Mesh);
luax_registertype(L, "Model", lovrModel); luax_registertype(L, Model);
luax_registertype(L, "Shader", lovrShader); luax_registertype(L, Shader);
luax_registertype(L, "ShaderBlock", lovrShaderBlock); luax_registertype(L, ShaderBlock);
luax_registertype(L, "Texture", lovrTexture); luax_registertype(L, Texture);
luax_registertype(L, "Canvas", lovrCanvas); luax_registertype(L, Canvas);
luax_pushconf(L); luax_pushconf(L);

View File

@ -331,7 +331,7 @@ static const luaL_Reg lovrHeadset[] = {
int luaopen_lovr_headset(lua_State* L) { int luaopen_lovr_headset(lua_State* L) {
lua_newtable(L); lua_newtable(L);
luaL_register(L, NULL, lovrHeadset); luaL_register(L, NULL, lovrHeadset);
luax_registertype(L, "Controller", lovrController); luax_registertype(L, Controller);
luax_pushconf(L); luax_pushconf(L);
lua_getfield(L, -1, "headset"); lua_getfield(L, -1, "headset");

View File

@ -283,12 +283,12 @@ static int l_lovrLightUserdataOp(lua_State* L) {
int luaopen_lovr_math(lua_State* L) { int luaopen_lovr_math(lua_State* L) {
lua_newtable(L); lua_newtable(L);
luaL_register(L, NULL, lovrMath); luaL_register(L, NULL, lovrMath);
luax_registertype(L, "Curve", lovrCurve); luax_registertype(L, Curve);
luax_registertype(L, "Pool", lovrPool); luax_registertype(L, Pool);
luax_registertype(L, "RandomGenerator", lovrRandomGenerator); luax_registertype(L, RandomGenerator);
for (int i = 0; i < MAX_MATH_TYPES; i++) { for (int i = 0; i < MAX_MATH_TYPES; i++) {
luax_registertype(L, lovrMathTypeNames[i], lovrMathTypes[i]); _luax_registertype(L, lovrMathTypeNames[i], lovrMathTypes[i]);
luaL_getmetatable(L, lovrMathTypeNames[i]); luaL_getmetatable(L, lovrMathTypeNames[i]);
// Remove usual __gc handler // Remove usual __gc handler

View File

@ -150,16 +150,16 @@ static const luaL_Reg lovrPhysics[] = {
int luaopen_lovr_physics(lua_State* L) { int luaopen_lovr_physics(lua_State* L) {
lua_newtable(L); lua_newtable(L);
luaL_register(L, NULL, lovrPhysics); luaL_register(L, NULL, lovrPhysics);
luax_registertype(L, "World", lovrWorld); luax_registertype(L, World);
luax_registertype(L, "Collider", lovrCollider); luax_registertype(L, Collider);
luax_extendtype(L, "Joint", "BallJoint", lovrJoint, lovrBallJoint); luax_extendtype(L, Joint, BallJoint);
luax_extendtype(L, "Joint", "DistanceJoint", lovrJoint, lovrDistanceJoint); luax_extendtype(L, Joint, DistanceJoint);
luax_extendtype(L, "Joint", "HingeJoint", lovrJoint, lovrHingeJoint); luax_extendtype(L, Joint, HingeJoint);
luax_extendtype(L, "Joint", "SliderJoint", lovrJoint, lovrSliderJoint); luax_extendtype(L, Joint, SliderJoint);
luax_extendtype(L, "Shape", "SphereShape", lovrShape, lovrSphereShape); luax_extendtype(L, Shape, SphereShape);
luax_extendtype(L, "Shape", "BoxShape", lovrShape, lovrBoxShape); luax_extendtype(L, Shape, BoxShape);
luax_extendtype(L, "Shape", "CapsuleShape", lovrShape, lovrCapsuleShape); luax_extendtype(L, Shape, CapsuleShape);
luax_extendtype(L, "Shape", "CylinderShape", lovrShape, lovrCylinderShape); luax_extendtype(L, Shape, CylinderShape);
if (lovrPhysicsInit()) { if (lovrPhysicsInit()) {
luax_atexit(L, lovrPhysicsDestroy); luax_atexit(L, lovrPhysicsDestroy);
} }

View File

@ -67,8 +67,8 @@ static const luaL_Reg lovrThreadModule[] = {
int luaopen_lovr_thread(lua_State* L) { int luaopen_lovr_thread(lua_State* L) {
lua_newtable(L); lua_newtable(L);
luaL_register(L, NULL, lovrThreadModule); luaL_register(L, NULL, lovrThreadModule);
luax_registertype(L, "Thread", lovrThread); luax_registertype(L, Thread);
luax_registertype(L, "Channel", lovrChannel); luax_registertype(L, Channel);
if (lovrThreadModuleInit()) { if (lovrThreadModuleInit()) {
luax_atexit(L, lovrThreadModuleDestroy); luax_atexit(L, lovrThreadModuleDestroy);
} }

View File

@ -100,7 +100,7 @@ void luax_registerloader(lua_State* L, lua_CFunction loader, int index) {
lua_pop(L, 1); lua_pop(L, 1);
} }
void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions) { void _luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions) {
// Push metatable // Push metatable
luaL_newmetatable(L, name); luaL_newmetatable(L, name);
@ -131,17 +131,10 @@ void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions
lua_pop(L, 1); lua_pop(L, 1);
} }
void luax_extendtype(lua_State* L, const char* base, const char* name, const luaL_Reg* baseFunctions, const luaL_Reg* functions) { void _luax_extendtype(lua_State* L, const char* name, const luaL_Reg* baseFunctions, const luaL_Reg* functions) {
luax_registertype(L, name, functions); _luax_registertype(L, name, functions);
luaL_getmetatable(L, name); luaL_getmetatable(L, name);
luaL_register(L, NULL, baseFunctions);
lua_pushstring(L, base);
lua_setfield(L, -2, "super");
if (baseFunctions) {
luaL_register(L, NULL, baseFunctions);
}
lua_pop(L, 1); lua_pop(L, 1);
} }

View File

@ -11,6 +11,8 @@
#endif #endif
#define luax_len(L, i) (int) lua_objlen(L, i) #define luax_len(L, i) (int) lua_objlen(L, i)
#define luax_registertype(L, T) _luax_registertype(L, #T, lovr ## T)
#define luax_extendtype(L, S, T) _luax_extendtype(L, #T, lovr ## S, lovr ## T)
#define luax_totype(L, i, T) ((T*) _luax_totype(L, i, T_ ## T)) #define luax_totype(L, i, T) ((T*) _luax_totype(L, i, T_ ## T))
#define luax_checktype(L, i, T) ((T*) _luax_checktype(L, i, T_ ## T, #T)) #define luax_checktype(L, i, T) ((T*) _luax_checktype(L, i, T_ ## T, #T))
#define luax_checkfloat(L, i) (float) luaL_checknumber(L, i) #define luax_checkfloat(L, i) (float) luaL_checknumber(L, i)
@ -24,8 +26,8 @@ int luax_print(lua_State* L);
void luax_setmainthread(lua_State* L); void luax_setmainthread(lua_State* L);
void luax_atexit(lua_State* L, luax_destructor destructor); void luax_atexit(lua_State* L, luax_destructor destructor);
void luax_registerloader(lua_State* L, lua_CFunction loader, int index); void luax_registerloader(lua_State* L, lua_CFunction loader, int index);
void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions); 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* baseFunctions, const luaL_Reg* functions); void _luax_extendtype(lua_State* L, const char* name, const luaL_Reg* baseFunctions, const luaL_Reg* functions);
void* _luax_totype(lua_State* L, int index, Type type); void* _luax_totype(lua_State* L, int index, Type type);
void* _luax_checktype(lua_State* L, int index, Type type, const char* debug); void* _luax_checktype(lua_State* L, int index, Type type, const char* debug);
void luax_pushobject(lua_State* L, void* object); void luax_pushobject(lua_State* L, void* object);