Lua basically running

This commit is contained in:
fm committed 2012-03-14 22:11:20 +01:00
1 parent 5c817c20ed
commit c5878c6302
4 files changed
+46 -21

No files matched your search

+1 -1
View File
@@ -29,7 +29,7 @@ libuevent.so.$(MAJOR): libuevent.o
$(CC) $(LDFLAGS) -shared -o $@ $^ -Wl,-soname=libuevent $(CC) $(LDFLAGS) -shared -o $@ $^ -Wl,-soname=libuevent
luevent.so: luevent.o all luevent.so: luevent.o all
$(CC) $(LDFLAGS) -shared -o $@ $^ -L . -l uevent -Wl,-soname=luevent $(CC) $(LDFLAGS) -shared -o $@ luevent.o -L . -l uevent -Wl,-soname=luevent
test: all test: all
$(CC) -g -o test -D NO_DEBUG test.c libuevent.c && ./test $(CC) -g -o test -D NO_DEBUG test.c libuevent.c && ./test
+2 -1
View File
@@ -442,13 +442,14 @@ void lueRun(struct LUECtxt *ctxt)
do { do {
ret = _lueProcessPending(ctxt, 0); ret = _lueProcessPending(ctxt, 0);
} while (!ctxt->exiting && ret != -2); } while (!ctxt->exiting && ret != -2);
ctxt->exiting = false;
Debug("LUE exiting - bye"); Debug("LUE exiting - bye");
} }
/** /**
* Terminate the application * Terminate the application
*/ */
void lueExit(struct LUECtxt *ctxt) void lueTerminate(struct LUECtxt *ctxt)
{ {
ctxt->exiting = true; ctxt->exiting = true;
} }
+1 -1
View File
@@ -46,7 +46,7 @@ void lueCtxtDestroy(struct LUECtxt *lue);
void lueTimeAdvance(struct LUECtxt *ctxt, int mSecs); void lueTimeAdvance(struct LUECtxt *ctxt, int mSecs);
void lueRun(struct LUECtxt *ctxt); void lueRun(struct LUECtxt *ctxt);
int lueProcessPending(struct LUECtxt *ctxt); int lueProcessPending(struct LUECtxt *ctxt);
void lueExit(struct LUECtxt *ctxt); void lueTerminate(struct LUECtxt *ctxt);
#endif #endif
+42 -18
View File
@@ -1,16 +1,19 @@
/mple** /**
* @file * @file
* Lua binding to libuevent * Lua binding to libuevent
* $Id:$ * $Id:$
* *
* (C) Copyright 2012 flemming.madsen at madsensoft.dk. See libuevent.h * (C) Copyright 2012 flemming.madsen at madsensoft.dk. See libuevent.h
*/ */
#include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <stdbool.h>
#include <assert.h> #include <assert.h>
#include <lua.h> #include <lua.h>
#include <lauxlib.h>
#include "libuevent.h" #include "libuevent.h"
static char * LUA_LTIMER_MT = "libuevent-timer"; static char * LUA_LTIMER_MT = "libuevent-timer";
@@ -35,22 +38,29 @@ static struct LUECtxt *getCtxt(lua_State *L)
return ret; return ret;
} }
static void l_newType(lua_State* L, const char *name)
{
luaL_newmetatable(L, name); // create metatable
lua_pushvalue(L, -1); // dup
lua_setfield(L, -2, "__index"); // metatable.__index = metatable
}
static int l_traceback (lua_State *L) static int l_traceback (lua_State *L)
{ {
lua_getfield(L, LUA_GLOBALSINDEX, "debug"); lua_getfield(L, LUA_GLOBALSINDEX, "debug");
if (!lua_istable(L, -1)) { if (!lua_istable(L, -1)) {
lua_pop(L, 1); lua_pop(L, 1);
return 1; return 1;
} }
lua_getfield(L, -1, "traceback"); lua_getfield(L, -1, "traceback");
if (!lua_isfunction(L, -1)) { if (!lua_isfunction(L, -1)) {
lua_pop(L, 2); lua_pop(L, 2);
return 1; return 1;
} }
lua_pushvalue(L, 1); /* pass error message */ lua_pushvalue(L, 1); /* pass error message */
lua_pushinteger(L, 2); /* skip this function and traceback */ lua_pushinteger(L, 2); /* skip this function and traceback */
lua_call(L, 2, 1); /* call debug.traceback */ lua_call(L, 2, 1); /* call debug.traceback */
return 1; return 1;
} }
// Top half // Top half
@@ -81,7 +91,7 @@ static int _lueHandler(lua_State *L, void *data, bool remObj)
{ {
fprintf(stderr, "luevent: Error: %d:\n%s\n", fprintf(stderr, "luevent: Error: %d:\n%s\n",
ret, lua_isnil(L, -1) ? "(nil)" : lua_tostring(L, -1)); ret, lua_isnil(L, -1) ? "(nil)" : lua_tostring(L, -1));
exit(1); //exit(1);
} }
lua_pop(L, 1); // Error handler, metatable lua_pop(L, 1); // Error handler, metatable
@@ -320,11 +330,23 @@ static int l_systemTime (lua_State *L)
struct timeval v; struct timeval v;
gettimeofday(&v, (struct timezone *) NULL); gettimeofday(&v, (struct timezone *) NULL);
lua_pushinteger(L, t.tv_sec); lua_pushinteger(L, v.tv_sec);
lua_pushinteger(L, v.tv_usec / 1000); lua_pushinteger(L, v.tv_usec / 1000);
return 2; // num of results return 2; // num of results
} }
static int l_run(lua_State * L)
{
lueRun(getCtxt(L));
return 0;
}
static int l_terminate(lua_State * L)
{
lueTerminate(getCtxt(L));
return 0;
}
static int l_cleanup(lua_State * L) static int l_cleanup(lua_State * L)
{ {
struct LUECtxt *ctxt = getCtxt(L); struct LUECtxt *ctxt = getCtxt(L);
@@ -343,6 +365,8 @@ static const luaL_Reg lueventlib[] = {
{"addTimer", ltimer_add}, {"addTimer", ltimer_add},
{"clearTimer", ltimer_clear}, {"clearTimer", ltimer_clear},
{"systemTime", l_systemTime}, {"systemTime", l_systemTime},
{"run", l_run},
{"terminate", l_terminate},
{"__gc", l_cleanup}, {"__gc", l_cleanup},
{ NULL, NULL} { NULL, NULL}
}; };
@@ -366,7 +390,7 @@ static const luaL_Reg lfilehmeta[] = {
int luaopen_luevent(lua_State * L) int luaopen_luevent(lua_State * L)
{ {
struct LUECtxt *lueContext = lueCtxtCreate(); struct LUECtxt *lueContext = lueCtxtCreate();
assert(ctxt); assert(lueContext);
luaL_register(L, lua_tostring(L, 1), lueventlib); // calls luaL_register(L, lua_tostring(L, 1), lueventlib); // calls
lua_setmetatable(L, -1); // Call GC at module cleanup. Pops it too lua_setmetatable(L, -1); // Call GC at module cleanup. Pops it too