Revert "Change the way modules are destroyed;"

This reverts commit 019814e2c1.
This commit is contained in:
bjorn 2022-08-08 23:16:34 -07:00
parent 0436a5deae
commit 3c71828afa
13 changed files with 33 additions and 59 deletions

View File

@ -28,17 +28,6 @@ LOVR_EXPORT int luaopen_lovr_system(lua_State* L);
LOVR_EXPORT int luaopen_lovr_thread(lua_State* L);
LOVR_EXPORT int luaopen_lovr_timer(lua_State* L);
void lovrAudioDestroy(void);
void lovrEventDestroy(void);
void lovrFilesystemDestroy(void);
void lovrGraphicsDestroy(void);
void lovrHeadsetDestroy(void);
void lovrMathDestroy(void);
void lovrPhysicsDestroy(void);
void lovrSystemDestroy(void);
void lovrThreadModuleDestroy(void);
void lovrTimerDestroy(void);
// Object names are lightuserdata because Variants need a non-Lua string due to threads.
static int luax_meta__tostring(lua_State* L) {
lua_getfield(L, -1, "__info");
@ -127,40 +116,6 @@ void luax_preload(lua_State* L) {
lua_pop(L, 2);
}
// Destroys modules in a specific order
void luax_unload(lua_State* L) {
#ifndef LOVR_DISABLE_TIMER
lovrTimerDestroy();
#endif
#ifndef LOVR_DISABLE_MATH
lovrMathDestroy();
#endif
#ifndef LOVR_DISABLE_EVENT
lovrEventDestroy();
#endif
#ifndef LOVR_DISABLE_THREAD
lovrThreadModuleDestroy();
#endif
#ifndef LOVR_DISABLE_PHYSICS
lovrPhysicsDestroy();
#endif
#ifndef LOVR_DISABLE_AUDIO
lovrAudioDestroy();
#endif
#ifndef LOVR_DISABLE_HEADSET
lovrHeadsetDestroy();
#endif
#ifndef LOVR_DISABLE_GRAPHICS
lovrGraphicsDestroy();
#endif
#ifndef LOVR_DISABLE_SYSTEM
lovrSystemDestroy();
#endif
#ifndef LOVR_DISABLE_FILESYSTEM
lovrFilesystemDestroy();
#endif
}
void _luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions, destructorFn* destructor) {
// Push metatable

View File

@ -111,7 +111,6 @@ typedef struct {
#define luax_clearerror(L) lua_pushnil(L), luax_seterror(L)
void luax_preload(struct lua_State* L);
void luax_unload(struct lua_State* L);
void _luax_registertype(struct lua_State* L, const char* name, const struct luaL_Reg* functions, void (*destructor)(void*));
void* _luax_totype(struct lua_State* L, int index, uint64_t hash);
void* _luax_checktype(struct lua_State* L, int index, uint64_t hash, const char* debug);

View File

@ -331,6 +331,7 @@ int luaopen_lovr_audio(lua_State* L) {
lua_pop(L, 1);
if (lovrAudioInit(spatializer, sampleRate)) {
luax_atexit(L, lovrAudioDestroy);
if (start) {
lovrAudioSetDevice(AUDIO_PLAYBACK, NULL, 0, NULL, AUDIO_SHARED);
lovrAudioStart(AUDIO_PLAYBACK);

View File

@ -214,6 +214,9 @@ int luaopen_lovr_event(lua_State* L) {
lua_pushcfunction(L, nextEvent);
pollRef = luaL_ref(L, LUA_REGISTRYINDEX);
lovrEventInit();
if (lovrEventInit()) {
luax_atexit(L, lovrEventDestroy);
}
return 1;
}

View File

@ -516,7 +516,9 @@ int luaopen_lovr_filesystem(lua_State* L) {
}
lua_pop(L, 1);
lovrFilesystemInit(archive);
if (lovrFilesystemInit(archive)) {
luax_atexit(L, lovrFilesystemDestroy);
}
lua_newtable(L);
luax_register(L, lovrFilesystem);

View File

@ -658,10 +658,17 @@ static int l_lovrGraphicsInit(lua_State* L) {
if (shaderCache) {
config.cacheData = luax_readfile(".lovrshadercache", &config.cacheSize);
luax_atexit(L, luax_writeshadercache);
}
lovrGraphicsInit(&config);
if (lovrGraphicsInit(&config)) {
luax_atexit(L, lovrGraphicsDestroy);
// Finalizers run in the opposite order they were added, so this has to go last
if (shaderCache) {
luax_atexit(L, luax_writeshadercache);
}
}
return 0;
}

View File

@ -703,6 +703,7 @@ int luaopen_lovr_headset(lua_State* L) {
}
lua_pop(L, 1);
luax_atexit(L, lovrHeadsetDestroy);
lovrHeadsetInit(&config);
return 1;
}

View File

@ -389,7 +389,9 @@ int luaopen_lovr_math(lua_State* L) {
lua_pop(L, 1);
// Module
lovrMathInit();
if (lovrMathInit()) {
luax_atexit(L, lovrMathDestroy);
}
// Each Lua state gets its own thread-local Pool
pool = lovrPoolCreate();

View File

@ -169,6 +169,8 @@ int luaopen_lovr_physics(lua_State* L) {
luax_registertype(L, CapsuleShape);
luax_registertype(L, CylinderShape);
luax_registertype(L, MeshShape);
lovrPhysicsInit();
if (lovrPhysicsInit()) {
luax_atexit(L, lovrPhysicsDestroy);
}
return 1;
}

View File

@ -212,6 +212,8 @@ static const luaL_Reg lovrSystem[] = {
int luaopen_lovr_system(lua_State* L) {
lua_newtable(L);
luax_register(L, lovrSystem);
lovrSystemInit();
if (lovrSystemInit()) {
luax_atexit(L, lovrSystemDestroy);
}
return 1;
}

View File

@ -110,6 +110,8 @@ int luaopen_lovr_thread(lua_State* L) {
luax_register(L, lovrThreadModule);
luax_registertype(L, Thread);
luax_registertype(L, Channel);
lovrThreadModuleInit();
if (lovrThreadModuleInit()) {
luax_atexit(L, lovrThreadModuleDestroy);
}
return 1;
}

View File

@ -47,7 +47,8 @@ static const luaL_Reg lovrTimer[] = {
int luaopen_lovr_timer(lua_State* L) {
lua_newtable(L);
luax_register(L, lovrTimer);
lovrTimerInit();
luax_atexit(L, lovrTimerDestroy);
if (lovrTimerInit()) {
luax_atexit(L, lovrTimerDestroy);
}
return 1;
}

View File

@ -132,7 +132,6 @@ int main(int argc, char** argv) {
memset(&cookie.value, 0, sizeof(cookie.value));
}
lua_close(L);
luax_unload(L);
} while (restart);
os_destroy();
@ -148,7 +147,6 @@ void lovrDestroy(void* arg) {
lua_State* L = context->L;
emscripten_cancel_main_loop();
lua_close(L);
luax_unload(L);
os_destroy();
}
}
@ -167,7 +165,6 @@ static void emscriptenLoop(void* arg) {
}
lua_close(context->L);
luax_unload(L);
emscripten_cancel_main_loop();
if (restart) {