diff options
author | chai <chaifix@163.com> | 2021-10-18 23:20:28 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-18 23:20:28 +0800 |
commit | 45c05ac5610416e75a123995af649681d43adf7f (patch) | |
tree | a4373cc0589cd1d7f3c1d6c5ab75d9b41767c9b6 /Runtime/Lua | |
parent | 83474a5ec3a25da9f66192f03f9b0628ad219404 (diff) |
* lua error handle
Diffstat (limited to 'Runtime/Lua')
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindConfig.h | 2 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindState.cpp | 59 | ||||
-rw-r--r-- | Runtime/Lua/LuaBind/LuaBindState.h | 9 |
3 files changed, 62 insertions, 8 deletions
diff --git a/Runtime/Lua/LuaBind/LuaBindConfig.h b/Runtime/Lua/LuaBind/LuaBindConfig.h index ded072e..829c049 100644 --- a/Runtime/Lua/LuaBind/LuaBindConfig.h +++ b/Runtime/Lua/LuaBind/LuaBindConfig.h @@ -54,7 +54,7 @@ namespace LuaBind #define LUA_BIND_ENABLE_PLAIN_CLASS 0 #define LUA_BIND_ENABLE_PLAIN_ENUM 0 -#define LUA_BIND_PROFILER 1 +#define LUA_BIND_PROFILER 0 } diff --git a/Runtime/Lua/LuaBind/LuaBindState.cpp b/Runtime/Lua/LuaBind/LuaBindState.cpp index 4ee87f4..8b6a5aa 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.cpp +++ b/Runtime/Lua/LuaBind/LuaBindState.cpp @@ -103,9 +103,16 @@ namespace LuaBind luaL_dostring(mState, code.c_str()); } - void State::DoFile(const std::string & path) + void State::DoFile(const std::string & path, ErrorHandler handler) { - luaL_dofile(mState, path.c_str()); + //luaL_dofile(mState, path.c_str()); + LoadFile(path); + Call(0, 0, handler); + } + + void State::LoadFile(const std::string& file) + { + luaL_loadfile(mState, file.c_str()); } int State::AbsIndex(int idx) @@ -122,9 +129,53 @@ namespace LuaBind return idx; } - void State::Call(int nArgs, int nResults) + static int TraceBack(lua_State* L) + { + int msgIdx = lua_gettop(L); + if (!lua_isstring(L, -1)) + 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, msgIdx); + lua_pushinteger(L, 2); + lua_call(L, 2, 1); + return 1; + } + + void State::Call(int nArgs, int nResults, ErrorHandler handler) { - lua_pcall(mState, nArgs, nResults, 0); + int oldTop = GetTop() - nArgs - 1; + int func = 0; + if (handler != NULL) + { + func = oldTop + 1; + lua_pushcfunction(mState, TraceBack); + lua_insert(mState, oldTop + 1); + } + if (lua_pcall(mState, nArgs, nResults, func) == 0) + { + lua_remove(mState, func); + } + else + { + if (handler != NULL) + { + lua_remove(mState, func); + cc8* err = CheckValue<cc8*>(-1); + handler(err); + lua_pop(mState, 1); + } + } } void State::PushNil() diff --git a/Runtime/Lua/LuaBind/LuaBindState.h b/Runtime/Lua/LuaBind/LuaBindState.h index 776b23d..1f236df 100644 --- a/Runtime/Lua/LuaBind/LuaBindState.h +++ b/Runtime/Lua/LuaBind/LuaBindState.h @@ -15,6 +15,8 @@ namespace LuaBind class StrongRef; class WeakRef; + typedef void (*ErrorHandler) (cc8 * msg); + // 对lua_State的代理,除了保存一个lua_State的引用不保存其他内容。一个实例的metatable如下: // class table // member table @@ -60,7 +62,7 @@ namespace LuaBind int GetTop(); bool CheckParams(int idx, cc8* format); int AbsIndex(int idx); - void Call(int nArgs, int nResults); + void Call(int nArgs, int nResults, ErrorHandler handler = NULL); //------------------------------------------------------------------------------// @@ -137,7 +139,8 @@ namespace LuaBind //------------------------------------------------------------------------------// void DoString(const std::string& code); - void DoFile(const std::string& file); + void DoFile(const std::string& file, ErrorHandler handler = NULL); + void LoadFile(const std::string& file); //------------------------------------------------------------------------------// // 注册方法 @@ -187,7 +190,7 @@ namespace LuaBind }; //--------------------------------------------------------------------------------// - // GetValue()模板特化 +// GetValue()模板特化 template <> bool State::GetValue < bool >(int idx, const bool value); template <> cc8* State::GetValue < cc8* >(int idx, const cc8* value); |