From 6a3b0adf0d4d47c5a69b2a5613f4d0445cfadc36 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Thu, 1 Sep 2016 08:49:13 -0400 Subject: [PATCH] error detection for coroutine and protected function --- .gitignore | 1 + sol/coroutine.hpp | 3 +++ sol/protected_function.hpp | 9 ++++++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 7b539c5c..67ddbed9 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,4 @@ main.exe main.o lua-5.3.3/ *.pdf +main.lua diff --git a/sol/coroutine.hpp b/sol/coroutine.hpp index 3060d657..4abfa78e 100644 --- a/sol/coroutine.hpp +++ b/sol/coroutine.hpp @@ -63,6 +63,9 @@ namespace sol { luacall(n, LUA_MULTRET); int poststacksize = lua_gettop(lua_state()); int returncount = poststacksize - (firstreturn - 1); + if (error()) { + return protected_function_result(lua_state(), lua_absindex(lua_state(), -1), 1, returncount, status()); + } return protected_function_result(lua_state(), firstreturn, returncount, returncount, status()); } diff --git a/sol/protected_function.hpp b/sol/protected_function.hpp index a6b24502..7766fc42 100644 --- a/sol/protected_function.hpp +++ b/sol/protected_function.hpp @@ -64,8 +64,8 @@ namespace sol { } }; - int luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, handler& h) const { - return lua_pcallk(base_t::lua_state(), static_cast(argcount), static_cast(resultcount), h.stackindex, 0, nullptr); + call_status luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, handler& h) const { + return static_cast(lua_pcallk(base_t::lua_state(), static_cast(argcount), static_cast(resultcount), h.stackindex, 0, nullptr)); } template @@ -104,9 +104,12 @@ namespace sol { }; try { #endif // No Exceptions - code = static_cast(luacall(n, LUA_MULTRET, h)); + code = luacall(n, LUA_MULTRET, h); int poststacksize = lua_gettop(base_t::lua_state()); returncount = poststacksize - (stacksize - 1); + if (code != call_status::yielded && != call_status::ok) { + return protected_function_result(base_t::lua_state(), lua_absindex(base_t::lua_state(), -1), returncount, returncount, code); + } #ifndef SOL_NO_EXCEPTIONS } // Handle C++ errors thrown from C++ functions bound inside of lua