From c5878c63024ebe5d7ca33186b012365f8dd577e2 Mon Sep 17 00:00:00 2001 From: fm Date: Wed, 14 Mar 2012 22:11:20 +0100 Subject: [PATCH] Lua basically running --- Makefile | 2 +- libuevent.c | 3 ++- libuevent.h | 2 +- luevent.c | 60 +++++++++++++++++++++++++++++++++++++---------------- 4 files changed, 46 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index ea0a87f..1da79fa 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ libuevent.so.$(MAJOR): libuevent.o $(CC) $(LDFLAGS) -shared -o $@ $^ -Wl,-soname=libuevent 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 $(CC) -g -o test -D NO_DEBUG test.c libuevent.c && ./test diff --git a/libuevent.c b/libuevent.c index 8005940..50110db 100644 --- a/libuevent.c +++ b/libuevent.c @@ -442,13 +442,14 @@ void lueRun(struct LUECtxt *ctxt) do { ret = _lueProcessPending(ctxt, 0); } while (!ctxt->exiting && ret != -2); + ctxt->exiting = false; Debug("LUE exiting - bye"); } /** * Terminate the application */ -void lueExit(struct LUECtxt *ctxt) +void lueTerminate(struct LUECtxt *ctxt) { ctxt->exiting = true; } diff --git a/libuevent.h b/libuevent.h index 7c75e8d..2e376f4 100644 --- a/libuevent.h +++ b/libuevent.h @@ -46,7 +46,7 @@ void lueCtxtDestroy(struct LUECtxt *lue); void lueTimeAdvance(struct LUECtxt *ctxt, int mSecs); void lueRun(struct LUECtxt *ctxt); int lueProcessPending(struct LUECtxt *ctxt); -void lueExit(struct LUECtxt *ctxt); +void lueTerminate(struct LUECtxt *ctxt); #endif diff --git a/luevent.c b/luevent.c index a8fb011..dacce00 100644 --- a/luevent.c +++ b/luevent.c @@ -1,16 +1,19 @@ -/mple** +/** * @file * Lua binding to libuevent * $Id:$ * * (C) Copyright 2012 flemming.madsen at madsensoft.dk. See libuevent.h */ +#include #include +#include #include #include #include #include #include +#include #include "libuevent.h" static char * LUA_LTIMER_MT = "libuevent-timer"; @@ -35,22 +38,29 @@ static struct LUECtxt *getCtxt(lua_State *L) 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) { - lua_getfield(L, LUA_GLOBALSINDEX, "debug"); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); - return 1; - } - lua_getfield(L, -1, "traceback"); - if (!lua_isfunction(L, -1)) { - lua_pop(L, 2); - return 1; - } - lua_pushvalue(L, 1); /* pass error message */ - lua_pushinteger(L, 2); /* skip this function and traceback */ - lua_call(L, 2, 1); /* call debug.traceback */ - return 1; + lua_getfield(L, LUA_GLOBALSINDEX, "debug"); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + return 1; + } + lua_getfield(L, -1, "traceback"); + if (!lua_isfunction(L, -1)) { + lua_pop(L, 2); + return 1; + } + lua_pushvalue(L, 1); /* pass error message */ + lua_pushinteger(L, 2); /* skip this function and traceback */ + lua_call(L, 2, 1); /* call debug.traceback */ + return 1; } // Top half @@ -81,7 +91,7 @@ static int _lueHandler(lua_State *L, void *data, bool remObj) { fprintf(stderr, "luevent: Error: %d:\n%s\n", ret, lua_isnil(L, -1) ? "(nil)" : lua_tostring(L, -1)); - exit(1); + //exit(1); } lua_pop(L, 1); // Error handler, metatable @@ -320,11 +330,23 @@ static int l_systemTime (lua_State *L) struct timeval v; gettimeofday(&v, (struct timezone *) NULL); - lua_pushinteger(L, t.tv_sec); + lua_pushinteger(L, v.tv_sec); lua_pushinteger(L, v.tv_usec / 1000); 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) { struct LUECtxt *ctxt = getCtxt(L); @@ -343,6 +365,8 @@ static const luaL_Reg lueventlib[] = { {"addTimer", ltimer_add}, {"clearTimer", ltimer_clear}, {"systemTime", l_systemTime}, + {"run", l_run}, + {"terminate", l_terminate}, {"__gc", l_cleanup}, { NULL, NULL} }; @@ -366,7 +390,7 @@ static const luaL_Reg lfilehmeta[] = { int luaopen_luevent(lua_State * L) { struct LUECtxt *lueContext = lueCtxtCreate(); - assert(ctxt); + assert(lueContext); luaL_register(L, lua_tostring(L, 1), lueventlib); // calls lua_setmetatable(L, -1); // Call GC at module cleanup. Pops it too