summaryrefslogtreecommitdiff
path: root/Runtime/Lua/LuaBind/LuaBindState.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime/Lua/LuaBind/LuaBindState.cpp')
-rw-r--r--Runtime/Lua/LuaBind/LuaBindState.cpp59
1 files changed, 55 insertions, 4 deletions
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()