diff options
-rw-r--r-- | Editor/EditorMain.cpp | 7 | ||||
-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 | ||||
-rw-r--r-- | Runtime/Scripting/Debug/Debug.bind.cpp | 2 |
5 files changed, 69 insertions, 10 deletions
diff --git a/Editor/EditorMain.cpp b/Editor/EditorMain.cpp index 67ddc38..f8ee4ef 100644 --- a/Editor/EditorMain.cpp +++ b/Editor/EditorMain.cpp @@ -35,6 +35,11 @@ static int MainLoop() return (INT)msg.wParam; }
+void ErrorHandle(cc8* msg)
+{
+ log_error(std::string("[Lua] ") + msg);
+}
+
void InitLuaState()
{
LuaBind::VM vm;
@@ -45,7 +50,7 @@ void InitLuaState() log_error("Can't setup scripting.");
}
LuaBind::State state = vm.GetMainState();
- state.DoFile("./Scripts/EditorApplication.lua");
+ state.DoFile("./Scripts/EditorApplication.lua", ErrorHandle);
}
#ifdef GAMELAB_DEBUG
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); diff --git a/Runtime/Scripting/Debug/Debug.bind.cpp b/Runtime/Scripting/Debug/Debug.bind.cpp index 1785f01..6d37746 100644 --- a/Runtime/Scripting/Debug/Debug.bind.cpp +++ b/Runtime/Scripting/Debug/Debug.bind.cpp @@ -1,4 +1,4 @@ -#include "Runtime/LuaBind/LuaBind.h" +#include "Runtime/Lua/LuaBind/LuaBind.h" #include "Runtime/Debug/Log.h" int log(lua_State* L) |