diff --git a/CMakeLists.txt b/CMakeLists.txt index fa17bc20..e43b6f85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -317,12 +317,12 @@ endif() set(LOVR_SRC src/main.c src/core/arr.c - src/core/luax.c src/core/maf.c src/core/platform.c src/core/ref.c src/core/utf.c src/core/util.c + src/api/api.c src/api/l_lovr.c src/lib/map/map.c src/lib/sds/sds.c diff --git a/src/core/luax.c b/src/api/api.c similarity index 99% rename from src/core/luax.c rename to src/api/api.c index ab2f96a6..0f1c4709 100644 --- a/src/core/luax.c +++ b/src/api/api.c @@ -1,7 +1,7 @@ -#include "luax.h" -#include "core/ref.h" -#include "platform.h" +#include "api.h" #include "util.h" +#include "core/ref.h" +#include "core/platform.h" #include "lib/sds/sds.h" #include #include diff --git a/src/api/api.h b/src/api/api.h index 0efa7e34..0e274215 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -1,18 +1,22 @@ -#include "luax.h" -#include "util.h" +#include +#include +#include +#include + +#pragma once // Modules -LOVR_EXPORT int luaopen_lovr(lua_State* L); -LOVR_EXPORT int luaopen_lovr_audio(lua_State* L); -LOVR_EXPORT int luaopen_lovr_data(lua_State* L); -LOVR_EXPORT int luaopen_lovr_event(lua_State* L); -LOVR_EXPORT int luaopen_lovr_filesystem(lua_State* L); -LOVR_EXPORT int luaopen_lovr_graphics(lua_State* L); -LOVR_EXPORT int luaopen_lovr_headset(lua_State* L); -LOVR_EXPORT int luaopen_lovr_math(lua_State* L); -LOVR_EXPORT int luaopen_lovr_physics(lua_State* L); -LOVR_EXPORT int luaopen_lovr_thread(lua_State* L); -LOVR_EXPORT int luaopen_lovr_timer(lua_State* L); +int luaopen_lovr(lua_State* L); +int luaopen_lovr_audio(lua_State* L); +int luaopen_lovr_data(lua_State* L); +int luaopen_lovr_event(lua_State* L); +int luaopen_lovr_filesystem(lua_State* L); +int luaopen_lovr_graphics(lua_State* L); +int luaopen_lovr_headset(lua_State* L); +int luaopen_lovr_math(lua_State* L); +int luaopen_lovr_physics(lua_State* L); +int luaopen_lovr_thread(lua_State* L); +int luaopen_lovr_timer(lua_State* L); extern const luaL_Reg lovrModules[]; // Objects @@ -86,7 +90,54 @@ extern const char* VerticalAligns[]; extern const char* Windings[]; extern const char* WrapModes[]; -// Helpers +// General helpers + +struct Color; + +typedef struct { + uint32_t hash; + void* object; +} Proxy; + +static inline uint32_t hash(const char* str) { + uint32_t x = 0; + while (*str) { + x = (x * 65599) + *str++; + } + return x; +} + +#ifndef LUA_RIDX_MAINTHERAD +#define LUA_RIDX_MAINTHREAD 1 +#endif + +#define luax_len(L, i) (int) lua_objlen(L, i) +#define luax_registertype(L, T) _luax_registertype(L, #T, lovr ## T, lovr ## T ## Destroy) +#define luax_totype(L, i, T) (T*) _luax_totype(L, i, hash(#T)) +#define luax_checktype(L, i, T) (T*) _luax_checktype(L, i, hash(#T), #T) +#define luax_pushtype(L, T, o) _luax_pushtype(L, #T, hash(#T), o) +#define luax_checkfloat(L, i) (float) luaL_checknumber(L, i) +#define luax_optfloat(L, i, x) (float) luaL_optnumber(L, i, x) +#define luax_geterror(L) lua_getfield(L, LUA_REGISTRYINDEX, "_lovrerror") +#define luax_seterror(L) lua_setfield(L, LUA_REGISTRYINDEX, "_lovrerror") +#define luax_clearerror(L) lua_pushnil(L), luax_seterror(L) + +void _luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions, void (*destructor)(void*)); +void* _luax_totype(lua_State* L, int index, uint32_t hash); +void* _luax_checktype(lua_State* L, int index, uint32_t hash, const char* debug); +void _luax_pushtype(lua_State* L, const char* name, uint32_t hash, void* object); +void luax_registerloader(lua_State* L, lua_CFunction loader, int index); +void luax_vthrow(lua_State* L, const char* format, va_list args); +void luax_traceback(lua_State* L, lua_State* T, const char* message, int level); +int luax_getstack(lua_State* L); +int luax_print(lua_State* L); +void luax_pushconf(lua_State* L); +int luax_setconf(lua_State* L); +void luax_setmainthread(lua_State* L); +void luax_atexit(lua_State* L, void (*destructor)(void)); +void luax_readcolor(lua_State* L, int index, struct Color* color); + +// Module helpers #ifdef LOVR_ENABLE_DATA struct Blob; @@ -109,7 +160,6 @@ void luax_readattachments(lua_State* L, int index, struct Attachment* attachment #endif #ifdef LOVR_ENABLE_MATH -#include #include "math/pool.h" // TODO #include "math/randomGenerator.h" // TODO float* luax_tovector(lua_State* L, int index, VectorType* type); diff --git a/src/api/l_audio.c b/src/api/l_audio.c index b5e4dbf2..8575c370 100644 --- a/src/api/l_audio.c +++ b/src/api/l_audio.c @@ -9,8 +9,6 @@ #include "core/ref.h" #include -struct Blob* luax_readblob(lua_State* L, int index, const char* debug); - const char* SourceTypes[] = { [SOURCE_STATIC] = "static", [SOURCE_STREAM] = "stream", @@ -251,7 +249,7 @@ static const luaL_Reg lovrAudio[] = { { NULL, NULL } }; -int luaopen_lovr_audio(lua_State* L) { +LOVR_EXPORT int luaopen_lovr_audio(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrAudio); luax_registertype(L, Microphone); diff --git a/src/api/l_curve.c b/src/api/l_curve.c index b226dcdb..543628ab 100644 --- a/src/api/l_curve.c +++ b/src/api/l_curve.c @@ -1,4 +1,5 @@ #include "api.h" +#include "util.h" #include "math/curve.h" #include diff --git a/src/api/l_data.c b/src/api/l_data.c index eda8fa2e..2ddba15e 100644 --- a/src/api/l_data.c +++ b/src/api/l_data.c @@ -132,7 +132,7 @@ static const luaL_Reg lovrData[] = { { NULL, NULL } }; -int luaopen_lovr_data(lua_State* L) { +LOVR_EXPORT int luaopen_lovr_data(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrData); luax_registertype(L, Blob); diff --git a/src/api/l_event.c b/src/api/l_event.c index 06bcd823..e6674f73 100644 --- a/src/api/l_event.c +++ b/src/api/l_event.c @@ -1,4 +1,5 @@ #include "api.h" +#include "util.h" #include "event/event.h" #include "thread/thread.h" #include "core/platform.h" @@ -195,7 +196,7 @@ static const luaL_Reg lovrEvent[] = { { NULL, NULL } }; -int luaopen_lovr_event(lua_State* L) { +LOVR_EXPORT int luaopen_lovr_event(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrEvent); diff --git a/src/api/l_filesystem.c b/src/api/l_filesystem.c index 47649265..fde20f90 100644 --- a/src/api/l_filesystem.c +++ b/src/api/l_filesystem.c @@ -1,4 +1,5 @@ #include "api.h" +#include "util.h" #include "filesystem/filesystem.h" #include "filesystem/file.h" #include "data/blob.h" @@ -443,7 +444,7 @@ static int libLoader(lua_State* L) { return 0; } -int luaopen_lovr_filesystem(lua_State* L) { +LOVR_EXPORT int luaopen_lovr_filesystem(lua_State* L) { lua_getglobal(L, "arg"); if (lua_istable(L, -1)) { lua_getfield(L, -1, "exe"); diff --git a/src/api/l_graphics.c b/src/api/l_graphics.c index dc5041c3..79d2b432 100644 --- a/src/api/l_graphics.c +++ b/src/api/l_graphics.c @@ -1689,7 +1689,7 @@ static const luaL_Reg lovrGraphics[] = { { NULL, NULL } }; -int luaopen_lovr_graphics(lua_State* L) { +LOVR_EXPORT int luaopen_lovr_graphics(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrGraphics); luax_registertype(L, Canvas); diff --git a/src/api/l_headset.c b/src/api/l_headset.c index 8010e075..af92a0f2 100644 --- a/src/api/l_headset.c +++ b/src/api/l_headset.c @@ -592,7 +592,7 @@ static const luaL_Reg lovrHeadset[] = { { NULL, NULL } }; -int luaopen_lovr_headset(lua_State* L) { +LOVR_EXPORT int luaopen_lovr_headset(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrHeadset); diff --git a/src/api/l_lovr.c b/src/api/l_lovr.c index 1f703116..67b57aa6 100644 --- a/src/api/l_lovr.c +++ b/src/api/l_lovr.c @@ -64,7 +64,7 @@ static const luaL_Reg lovr[] = { { NULL, NULL } }; -int luaopen_lovr(lua_State* L) { +LOVR_EXPORT int luaopen_lovr(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovr); return 1; diff --git a/src/api/l_math.c b/src/api/l_math.c index d4af6551..f9d62964 100644 --- a/src/api/l_math.c +++ b/src/api/l_math.c @@ -343,7 +343,7 @@ static int l_lovrLightUserdataOp(lua_State* L) { return 1; } -int luaopen_lovr_math(lua_State* L) { +LOVR_EXPORT int luaopen_lovr_math(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrMath); luax_registertype(L, Curve); diff --git a/src/api/l_physics.c b/src/api/l_physics.c index 26e5030d..46ae6157 100644 --- a/src/api/l_physics.c +++ b/src/api/l_physics.c @@ -1,4 +1,5 @@ #include "api.h" +#include "util.h" #include "physics/physics.h" #include "core/ref.h" @@ -148,7 +149,7 @@ static const luaL_Reg lovrPhysics[] = { { NULL, NULL } }; -int luaopen_lovr_physics(lua_State* L) { +LOVR_EXPORT int luaopen_lovr_physics(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrPhysics); luax_registertype(L, World); diff --git a/src/api/l_thread_module.c b/src/api/l_thread_module.c index ff3ff7b4..f92ed885 100644 --- a/src/api/l_thread_module.c +++ b/src/api/l_thread_module.c @@ -1,4 +1,5 @@ #include "api.h" +#include "util.h" #include "event/event.h" #include "filesystem/filesystem.h" #include "thread/thread.h" @@ -89,7 +90,7 @@ static const luaL_Reg lovrThreadModule[] = { { NULL, NULL } }; -int luaopen_lovr_thread(lua_State* L) { +LOVR_EXPORT int luaopen_lovr_thread(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrThreadModule); luax_registertype(L, Thread); diff --git a/src/api/l_timer.c b/src/api/l_timer.c index a5fab33d..7bd9cdc2 100644 --- a/src/api/l_timer.c +++ b/src/api/l_timer.c @@ -1,4 +1,5 @@ #include "api.h" +#include "util.h" #include "timer/timer.h" static int l_lovrTimerGetDelta(lua_State* L) { @@ -42,7 +43,7 @@ static const luaL_Reg lovrTimer[] = { { NULL, NULL } }; -int luaopen_lovr_timer(lua_State* L) { +LOVR_EXPORT int luaopen_lovr_timer(lua_State* L) { lua_newtable(L); luaL_register(L, NULL, lovrTimer); if (lovrTimerInit()) { diff --git a/src/core/luax.h b/src/core/luax.h deleted file mode 100644 index f03b9f32..00000000 --- a/src/core/luax.h +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include -#include -#include - -#pragma once - -struct Color; - -typedef struct { - uint32_t hash; - void* object; -} Proxy; - -#ifndef LUA_RIDX_MAINTHERAD -#define LUA_RIDX_MAINTHREAD 1 -#endif - -#define luax_len(L, i) (int) lua_objlen(L, i) -#define luax_registertype(L, T) _luax_registertype(L, #T, lovr ## T, lovr ## T ## Destroy) -#define luax_totype(L, i, T) (T*) _luax_totype(L, i, hash(#T)) -#define luax_checktype(L, i, T) (T*) _luax_checktype(L, i, hash(#T), #T) -#define luax_pushtype(L, T, o) _luax_pushtype(L, #T, hash(#T), o) -#define luax_checkfloat(L, i) (float) luaL_checknumber(L, i) -#define luax_optfloat(L, i, x) (float) luaL_optnumber(L, i, x) -#define luax_geterror(L) lua_getfield(L, LUA_REGISTRYINDEX, "_lovrerror") -#define luax_seterror(L) lua_setfield(L, LUA_REGISTRYINDEX, "_lovrerror") -#define luax_clearerror(L) lua_pushnil(L), luax_seterror(L) - -void _luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions, void (*destructor)(void*)); -void* _luax_totype(lua_State* L, int index, uint32_t hash); -void* _luax_checktype(lua_State* L, int index, uint32_t hash, const char* debug); -void _luax_pushtype(lua_State* L, const char* name, uint32_t hash, void* object); -void luax_registerloader(lua_State* L, lua_CFunction loader, int index); -void luax_vthrow(lua_State* L, const char* format, va_list args); -void luax_traceback(lua_State* L, lua_State* T, const char* message, int level); -int luax_getstack(lua_State* L); -int luax_print(lua_State* L); -void luax_pushconf(lua_State* L); -int luax_setconf(lua_State* L); -void luax_setmainthread(lua_State* L); -void luax_atexit(lua_State* L, void (*destructor)(void)); -void luax_readcolor(lua_State* L, int index, struct Color* color); diff --git a/src/core/util.h b/src/core/util.h index 4f9ce0ab..a1315a8f 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -1,6 +1,5 @@ #include #include -#include #pragma once @@ -44,11 +43,3 @@ void lovrSetErrorCallback(errorFn* callback, void* context); void LOVR_NORETURN lovrThrow(const char* format, ...); #define lovrAssert(c, ...) if (!(c)) { lovrThrow(__VA_ARGS__); } - -static inline uint32_t hash(const char* str) { - uint32_t x = 0; - while (*str) { - x = (x * 65599) + *str++; - } - return x; -} diff --git a/src/lib/lua-cjson/lua_cjson.h b/src/lib/lua-cjson/lua_cjson.h index e1cf06f7..9be5d223 100644 --- a/src/lib/lua-cjson/lua_cjson.h +++ b/src/lib/lua-cjson/lua_cjson.h @@ -1,4 +1,4 @@ -#include "luax.h" +#include "api/api.h" #pragma once diff --git a/src/lib/lua-enet/enet.h b/src/lib/lua-enet/enet.h index 9185a658..570f4a1c 100644 --- a/src/lib/lua-enet/enet.h +++ b/src/lib/lua-enet/enet.h @@ -1,4 +1,4 @@ -#include "luax.h" +#include "api/api.h" #pragma once diff --git a/src/main.c b/src/main.c index 4f907b72..693fccae 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,5 @@ #include "resources/boot.lua.h" #include "api/api.h" -#include "luax.h" #include "platform.h" #include "util.h" #include diff --git a/src/modules/headset/oculus_mobile.c b/src/modules/headset/oculus_mobile.c index 8f892e6f..6f67a7e5 100644 --- a/src/modules/headset/oculus_mobile.c +++ b/src/modules/headset/oculus_mobile.c @@ -297,7 +297,6 @@ bool lovrPlatformHasWindow() { #include #include "oculus_mobile_bridge.h" -#include "luax.h" #include "lib/sds/sds.h" #include "api/api.h"