Skip to content

Instantly share code, notes, and snippets.

@fur-q
Last active August 29, 2015 14:06
Show Gist options
  • Save fur-q/e7f745a5a99ebb72c1ed to your computer and use it in GitHub Desktop.
Save fur-q/e7f745a5a99ebb72c1ed to your computer and use it in GitHub Desktop.
im da bes
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#define DIE(e) do { fprintf(stderr, "FATAL: %s\n", e); return 1; } while (0)
static int l_require(lua_State * L, const char * k) {
lua_getglobal(L, "require");
lua_pushstring(L, k);
if (lua_pcall(L, 1, 1, 1)) {
return 1;
}
return 0;
}
static int l_traceback(lua_State * L) {
const char * msg = lua_tostring(L, 1);
if (!msg)
return 0;
luaL_traceback(L, L, msg, 1);
return 1;
}
int main(int argc, const char ** argv) {
lua_State * L = luaL_newstate();
if (!L)
DIE("Error creating Lua state");
luaL_openlibs(L);
lua_pushcfunction(L, l_traceback);
if (l_require(L, "myscript"))
DIE(lua_tostring(L, -1));
lua_close(L);
return 0;
}
CC = gcc
LUA = luajit
loader: loader.o myscript.l.o
$(CC) $(LDFLAGS) -o $@ $^
%.l.o: %.lua
$(LUA) -bg $^ $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment