1
0
Fork 0
mirror of https://github.com/bjornbytes/lovr.git synced 2024-07-02 20:43:35 +00:00

Error system no longer relies on Lua;

This commit is contained in:
bjorn 2018-09-26 10:39:17 -07:00
parent 737f0b6dc3
commit 148a2bdb45
6 changed files with 39 additions and 14 deletions

View file

@ -12,8 +12,9 @@ static int threadRunner(void* data) {
mtx_unlock(&thread->lock); mtx_unlock(&thread->lock);
// Lua state // Lua state
lua_State* L = lovrErrorContext = luaL_newstate(); lua_State* L = luaL_newstate();
luaL_openlibs(L); luaL_openlibs(L);
lovrSetErrorCallback((lovrErrorHandler) luax_vthrow, L);
l_lovrInit(L); l_lovrInit(L);
lua_setglobal(L, "lovr"); lua_setglobal(L, "lovr");

View file

@ -82,9 +82,11 @@ void lovrDestroy() {
} }
bool lovrRun(int argc, char** argv, int* status) { bool lovrRun(int argc, char** argv, int* status) {
lua_State* L = lovrErrorContext = luaL_newstate(); lua_State* L = luaL_newstate();
luaL_openlibs(L); luaL_openlibs(L);
lovrSetErrorCallback((lovrErrorHandler) luax_vthrow, L);
glfwSetErrorCallback(onGlfwError); glfwSetErrorCallback(onGlfwError);
lovrAssert(glfwInit(), "Error initializing GLFW"); lovrAssert(glfwInit(), "Error initializing GLFW");
glfwSetTime(0); glfwSetTime(0);

View file

@ -1,6 +1,7 @@
#include "luax.h" #include "luax.h"
#include "util.h" #include "util.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
static int luax_meta__tostring(lua_State* L) { static int luax_meta__tostring(lua_State* L) {
lua_getfield(L, -1, "name"); lua_getfield(L, -1, "name");
@ -163,6 +164,11 @@ void luax_pushobject(lua_State* L, void* object) {
lua_remove(L, -2); lua_remove(L, -2);
} }
void luax_vthrow(lua_State* L, const char* format, va_list args) {
lua_pushvfstring(L, format, args);
lua_error(L);
}
int luax_getstack(lua_State* L) { int luax_getstack(lua_State* L) {
const char* message = luaL_checkstring(L, -1); const char* message = luaL_checkstring(L, -1);
lua_getglobal(L, "debug"); lua_getglobal(L, "debug");

View file

@ -16,6 +16,7 @@ void luax_extendtype(lua_State* L, const char* base, const char* name, const lua
void* _luax_totype(lua_State* L, int index, const char* type); void* _luax_totype(lua_State* L, int index, const char* type);
void* _luax_checktype(lua_State* L, int index, const char* type); void* _luax_checktype(lua_State* L, int index, const char* type);
void luax_pushobject(lua_State* L, void* object); void luax_pushobject(lua_State* L, void* object);
void luax_vthrow(lua_State* L, const char* format, va_list args);
int luax_getstack(lua_State* L); int luax_getstack(lua_State* L);
void luax_pushconf(lua_State* L); void luax_pushconf(lua_State* L);
int luax_setconf(lua_State* L); int luax_setconf(lua_State* L);

View file

@ -1,9 +1,6 @@
#include "util.h" #include "util.h"
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#ifdef _WIN32 #ifdef _WIN32
#include <Windows.h> #include <Windows.h>
#else #else
@ -11,15 +8,29 @@
#include <dlfcn.h> #include <dlfcn.h>
#endif #endif
_Thread_local void* lovrErrorContext = NULL; _Thread_local lovrErrorHandler lovrErrorCallback = NULL;
_Thread_local void* lovrErrorUserdata = NULL;
void lovrSetErrorCallback(lovrErrorHandler callback, void* userdata) {
lovrErrorCallback = callback;
lovrErrorUserdata = userdata;
}
void lovrThrow(const char* format, ...) { void lovrThrow(const char* format, ...) {
lua_State* L = (lua_State*) lovrErrorContext; if (lovrErrorCallback) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
lua_pushvfstring(L, format, args); lovrErrorCallback(lovrErrorUserdata, format, args);
lua_error(L); va_end(args);
va_end(args); } else {
va_list args;
va_start(args, format);
fprintf(stderr, "Error: ");
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
va_end(args);
exit(EXIT_FAILURE);
}
} }
void lovrSleep(double seconds) { void lovrSleep(double seconds) {

View file

@ -2,6 +2,7 @@
#include "lib/tinycthread/tinycthread.h" #include "lib/tinycthread/tinycthread.h"
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <stdarg.h>
#pragma once #pragma once
@ -20,8 +21,11 @@ typedef struct {
float r, g, b, a; float r, g, b, a;
} Color; } Color;
extern _Thread_local void* lovrErrorContext; typedef void (*lovrErrorHandler)(void* userdata, const char* format, va_list args);
extern _Thread_local lovrErrorHandler lovrErrorCallback;
extern _Thread_local void* lovrErrorUserdata;
void lovrSetErrorCallback(lovrErrorHandler callback, void* context);
void lovrThrow(const char* format, ...); void lovrThrow(const char* format, ...);
void lovrSleep(double seconds); void lovrSleep(double seconds);
void* _lovrAlloc(const char* type, size_t size, void (*destructor)(void*)); void* _lovrAlloc(const char* type, size_t size, void (*destructor)(void*));