diff options
author | Nicolas Noble <pixel@nobis-crew.org> | 2013-08-02 15:53:08 -0700 |
---|---|---|
committer | Nicolas Noble <pixel@nobis-crew.org> | 2013-08-02 15:53:08 -0700 |
commit | 903974e7b3ceecb977449ac5ea34808de9501997 (patch) | |
tree | 1c61d4574712a95a106c8647084a95b91d529a5b /includes | |
parent | f416d651f3d6551aa0efbcdb8b5838269de9bff3 (diff) |
Heavily revamped the C-to-Lua yielding mechanism. Now more generic.
Diffstat (limited to 'includes')
-rw-r--r-- | includes/BLua.h | 54 | ||||
-rw-r--r-- | includes/Exceptions.h | 2 | ||||
-rw-r--r-- | includes/Task.h | 8 |
3 files changed, 17 insertions, 47 deletions
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<Handle> 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<int>) 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<Handle> out, const String & prefix, int idx); void dumpvars_r(IO<Handle> 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 <class T> T * LuaObjectFactory::getMe(Lua & L, int idx) { return L.recast<T>(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<int(Lua & L)> context, Events::BaseEvent * evt); - private: - LuaHelpersBase(std::function<int(Lua & L)> context, Events::BaseEvent * evt) : m_context(context), m_evt(evt) { } - std::function<int(Lua & L)> m_context; - Events::BaseEvent * m_evt; }; template <class T> @@ -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<String> 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<class R> @@ -278,9 +280,10 @@ R Future<R>::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<class R> void Future<R>::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; } |