From 903974e7b3ceecb977449ac5ea34808de9501997 Mon Sep 17 00:00:00 2001 From: Nicolas Noble Date: Fri, 2 Aug 2013 15:53:08 -0700 Subject: Heavily revamped the C-to-Lua yielding mechanism. Now more generic. --- includes/BLua.h | 54 ++++++++++----------------------------------------- includes/Exceptions.h | 2 +- includes/Task.h | 8 ++++++-- 3 files changed, 17 insertions(+), 47 deletions(-) (limited to 'includes') diff --git a/includes/BLua.h b/includes/BLua.h index 8f0993f..40e89bd 100644 --- a/includes/BLua.h +++ b/includes/BLua.h @@ -79,6 +79,7 @@ class Lua { Lua(); Lua(lua_State * __L) : L(__L) { } Lua(Lua && oL) : L(oL.L) { oL.L = NULL; } + Lua(const Lua & oL) : L(oL.L) { } Lua & operator=(Lua && oL); @@ -96,7 +97,7 @@ class Lua { void open_bit(); void open_jit(); void open_ffi(); - int wrap_open(openlualib_t open) { int n = gettop(); int r = open(L); while (n < gettop()) remove(n); return r; } + int wrap_open(openlualib_t open) { int n = gettop(); int r = open(L); while (n < gettop()) pop(); return r; } void openlib(const String & libname, const struct luaL_reg *l, int nup) { luaL_openlib(L, libname.to_charp(), l, nup); } void setCallWrap(lua_CallWrapper wrapper); @@ -118,10 +119,10 @@ class Lua { int checkstack(int extra = 1) { return lua_checkstack(L, extra); } int next(int t = -2) { return lua_next(L, t); } - void copy(int n = -1) { checkstack(); lua_pushvalue(L, n); } - void remove(int n = 1) { lua_remove(L, n); } - void insert(int n = 1) { checkstack(); lua_insert(L, n); } - void replace(int n = 1) { lua_replace(L, n); } + void copy(int i = -1) { checkstack(); lua_pushvalue(L, i); } + void remove(int i = 1) { lua_remove(L, i); } + void insert(int i = 1) { checkstack(); lua_insert(L, i); } + void replace(int i = 1) { lua_replace(L, i); } void newtable() { checkstack(); lua_newtable(L); } void * newuser(size_t s) { checkstack(); return lua_newuserdata(L, s); } void settable(int tableIdx = -3, bool raw = false); @@ -161,6 +162,7 @@ class Lua { void dumpvars(IO out, const String & prefix, int idx = -1); Lua thread(bool saveit = true); int yield(int nresults = 0) { return lua_yield(L, nresults); } + int yield(Future) throw (GeneralException) __attribute__((noreturn)); bool yielded() { return lua_status(L) == LUA_YIELD; } bool resume(int nargs = 0) throw (GeneralException); void showstack(int level = M_INFO); @@ -191,14 +193,14 @@ class Lua { protected: private: + void dumpvars_i(IO out, const String & prefix, int idx); void dumpvars_r(IO out, int idx, int depth = 0) throw (GeneralException); + bool resumeC(); + bool yieldC() throw (GeneralException); lua_State * L; friend class LuaStatics; - - Lua & operator=(const Lua &) = delete; - Lua(const Lua &) = delete; }; class LuaException : public GeneralException { @@ -339,16 +341,8 @@ template T * LuaObjectFactory::getMe(Lua & L, int idx) { return L.recast(idx); } class LuaHelpersBase { - public: - static bool resume(Lua & L); - static Events::BaseEvent * getEvent(Lua & L); protected: static void validate(const lua_functypes_t & entry, bool method, int n, Lua & L, const char * className); - static void pushContext(Lua & L, std::function context, Events::BaseEvent * evt); - private: - LuaHelpersBase(std::function context, Events::BaseEvent * evt) : m_context(context), m_evt(evt) { } - std::function m_context; - Events::BaseEvent * m_evt; }; template @@ -375,34 +369,6 @@ class LuaHelpers : public LuaHelpersBase { private: static int method_multiplex(int caller, Lua & L, proceed_t proceed, proceed_static_t proceed_static, lua_functypes_t * tab, bool method) { - int r; - - try { - r = method_multiplex_internal(caller, L, proceed, proceed_static, tab, method); - } - catch (EAgain & e) { - pushContext(L, [caller, proceed, proceed_static, tab, method](Lua & L) -> int { return method_multiplex_resume(caller, L, proceed, proceed_static, tab, method); }, e.getEvent()); - r = L.yield(L.gettop()); - } - - return r; - } - - static int method_multiplex_resume(int caller, Lua & L, proceed_t proceed, proceed_static_t proceed_static, lua_functypes_t * tab, bool method) { - int r; - - try { - r = method_multiplex_internal(caller, L, proceed, proceed_static, tab, method); - } - catch (EAgain & e) { - pushContext(L, [caller, proceed, proceed_static, tab, method](Lua & L) -> int { return method_multiplex_resume(caller, L, proceed, proceed_static, tab, method); }, e.getEvent()); - r = -1; - } - - return r; - } - - static int method_multiplex_internal(int caller, Lua & L, proceed_t proceed, proceed_static_t proceed_static, lua_functypes_t * tab, bool method) { int add = method ? 1 : 0; int n = L.gettop() - add; T * obj = 0; diff --git a/includes/Exceptions.h b/includes/Exceptions.h index 2d452cc..729f523 100644 --- a/includes/Exceptions.h +++ b/includes/Exceptions.h @@ -34,7 +34,7 @@ class GeneralException { const std::vector getTrace() const { return m_trace; } protected: - GeneralException() { } + explicit GeneralException() { } void setMsg(char * msg) { if (m_msg) free(m_msg); m_msg = msg; } void genTrace(); diff --git a/includes/Task.h b/includes/Task.h index 4334ad2..3219397 100644 --- a/includes/Task.h +++ b/includes/Task.h @@ -269,8 +269,10 @@ class Future { R get(); void run(); private: + friend class Lua; func_t m_func; Events::BaseEvent * m_evt = NULL; + bool m_ranOnce = false; }; template @@ -278,9 +280,10 @@ R Future::get() { R r; for (;;) { if (m_evt && !m_evt->gotSignal()) - continue; + Task::operationYield(m_evt, Task::INTERRUPTIBLE); m_evt = NULL; try { + m_ranOnce = true; r = m_func(); return r; } @@ -295,9 +298,10 @@ template void Future::run() { for (;;) { if (m_evt && !m_evt->gotSignal()) - continue; + Task::operationYield(m_evt, Task::INTERRUPTIBLE); m_evt = NULL; try { + m_ranOnce = true; m_func(); return; } -- cgit v1.2.3