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);
// Lua state
lua_State* L = lovrErrorContext = luaL_newstate();
lua_State* L = luaL_newstate();
luaL_openlibs(L);
lovrSetErrorCallback((lovrErrorHandler) luax_vthrow, L);
l_lovrInit(L);
lua_setglobal(L, "lovr");

View File

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

View File

@ -1,6 +1,7 @@
#include "luax.h"
#include "util.h"
#include <stdlib.h>
#include <stdarg.h>
static int luax_meta__tostring(lua_State* L) {
lua_getfield(L, -1, "name");
@ -163,6 +164,11 @@ void luax_pushobject(lua_State* L, void* object) {
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) {
const char* message = luaL_checkstring(L, -1);
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_checktype(lua_State* L, int index, const char* type);
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);
void luax_pushconf(lua_State* L);
int luax_setconf(lua_State* L);

View File

@ -1,9 +1,6 @@
#include "util.h"
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef _WIN32
#include <Windows.h>
#else
@ -11,15 +8,29 @@
#include <dlfcn.h>
#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, ...) {
lua_State* L = (lua_State*) lovrErrorContext;
va_list args;
va_start(args, format);
lua_pushvfstring(L, format, args);
lua_error(L);
va_end(args);
if (lovrErrorCallback) {
va_list args;
va_start(args, format);
lovrErrorCallback(lovrErrorUserdata, format, 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) {

View File

@ -2,6 +2,7 @@
#include "lib/tinycthread/tinycthread.h"
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
#pragma once
@ -20,8 +21,11 @@ typedef struct {
float r, g, b, a;
} 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 lovrSleep(double seconds);
void* _lovrAlloc(const char* type, size_t size, void (*destructor)(void*));