Lua/LuaState: Add error handling methods

This commit is contained in:
Lynix 2018-05-21 19:31:19 +02:00
parent efa727dfd4
commit f2c16f916a
5 changed files with 40 additions and 19 deletions

View File

@ -101,6 +101,8 @@ Nazara Engine:
- Mesh class now has a OnMeshInvalidateAABB signal, triggered when a mesh invalidates its AABB, which is also submesh updates its AABB - Mesh class now has a OnMeshInvalidateAABB signal, triggered when a mesh invalidates its AABB, which is also submesh updates its AABB
- Model now invalidate properly their bounding volume when their mesh AABB is updated - Model now invalidate properly their bounding volume when their mesh AABB is updated
- Added operator&/|/^ taking an enumeration value and a Flags object using the same enumeration type. - Added operator&/|/^ taking an enumeration value and a Flags object using the same enumeration type.
- Added LuaState::CallWithHandler methods, allowing to setup a error handler function
- Added LuaState::Traceback method
Nazara Development Kit: Nazara Development Kit:
- Added ImageWidget (#139) - Added ImageWidget (#139)

View File

@ -31,7 +31,7 @@ namespace Nz
private: private:
LuaCoroutine(lua_State* internalState, int refIndex); LuaCoroutine(lua_State* internalState, int refIndex);
bool Run(int argCount, int resultCount) override; bool Run(int argCount, int resultCount, int errHandler) override;
int m_ref; int m_ref;
}; };

View File

@ -43,6 +43,8 @@ namespace Nz
bool Call(unsigned int argCount); bool Call(unsigned int argCount);
bool Call(unsigned int argCount, unsigned int resultCount); bool Call(unsigned int argCount, unsigned int resultCount);
bool CallWithHandler(unsigned int argCount, int errorHandler);
bool CallWithHandler(unsigned int argCount, unsigned int resultCount, int errorHandler);
template<typename T> T Check(int* index) const; template<typename T> T Check(int* index) const;
template<typename T> T Check(int* index, T defValue) const; template<typename T> T Check(int* index, T defValue) const;
@ -84,10 +86,10 @@ namespace Nz
void Error(const char* message) const; void Error(const char* message) const;
void Error(const String& message) const; void Error(const String& message) const;
bool Execute(const String& code); bool Execute(const String& code, int errorHandler = 0);
bool ExecuteFromFile(const String& filePath); bool ExecuteFromFile(const String& filePath, int errorHandler = 0);
bool ExecuteFromMemory(const void* data, std::size_t size); bool ExecuteFromMemory(const void* data, std::size_t size, int errorHandler = 0);
bool ExecuteFromStream(Stream& stream); bool ExecuteFromStream(Stream& stream, int errorHandler = 0);
int GetAbsIndex(int index) const; int GetAbsIndex(int index) const;
LuaType GetField(const char* fieldName, int tableIndex = -1) const; LuaType GetField(const char* fieldName, int tableIndex = -1) const;
@ -178,6 +180,8 @@ namespace Nz
void* ToUserdata(int index, const char* tname) const; void* ToUserdata(int index, const char* tname) const;
void* ToUserdata(int index, const String& tname) const; void* ToUserdata(int index, const String& tname) const;
void Traceback(const char* message = nullptr, int level = 0);
LuaState& operator=(const LuaState&) = default; LuaState& operator=(const LuaState&) = default;
LuaState& operator=(LuaState&& instance) = default; LuaState& operator=(LuaState&& instance) = default;
@ -190,7 +194,7 @@ namespace Nz
template<typename T> std::enable_if_t<std::is_signed<T>::value, T> CheckBounds(int index, long long value) const; template<typename T> std::enable_if_t<std::is_signed<T>::value, T> CheckBounds(int index, long long value) const;
template<typename T> std::enable_if_t<std::is_unsigned<T>::value, T> CheckBounds(int index, long long value) const; template<typename T> std::enable_if_t<std::is_unsigned<T>::value, T> CheckBounds(int index, long long value) const;
virtual bool Run(int argCount, int resultCount); virtual bool Run(int argCount, int resultCount, int errHandler);
static int ProxyFunc(lua_State* internalState); static int ProxyFunc(lua_State* internalState);

View File

@ -47,7 +47,7 @@ namespace Nz
} }
} }
bool LuaCoroutine::Run(int argCount, int /*resultCount*/) bool LuaCoroutine::Run(int argCount, int /*resultCount*/, int /*errHandler*/)
{ {
return Resume(argCount) != Ternary_False; return Resume(argCount) != Ternary_False;
} }

View File

@ -144,12 +144,22 @@ namespace Nz
bool LuaState::Call(unsigned int argCount) bool LuaState::Call(unsigned int argCount)
{ {
return Run(argCount, LUA_MULTRET); return Run(argCount, LUA_MULTRET, 0);
} }
bool LuaState::Call(unsigned int argCount, unsigned int resultCount) bool LuaState::Call(unsigned int argCount, unsigned int resultCount)
{ {
return Run(argCount, resultCount); return Run(argCount, resultCount, 0);
}
bool LuaState::CallWithHandler(int errorHandler, unsigned int argCount)
{
return Run(argCount, LUA_MULTRET, errorHandler);
}
bool LuaState::CallWithHandler(int errorHandler, unsigned int argCount, unsigned int resultCount)
{
return Run(argCount, resultCount, errorHandler);
} }
void LuaState::CheckAny(int index) const void LuaState::CheckAny(int index) const
@ -350,7 +360,7 @@ namespace Nz
luaL_error(m_state, message.GetConstBuffer()); luaL_error(m_state, message.GetConstBuffer());
} }
bool LuaState::Execute(const String& code) bool LuaState::Execute(const String& code, int errorHandler)
{ {
if (code.IsEmpty()) if (code.IsEmpty())
return true; return true;
@ -358,29 +368,29 @@ namespace Nz
if (!Load(code)) if (!Load(code))
return false; return false;
return Call(0); return CallWithHandler(errorHandler, 0);
} }
bool LuaState::ExecuteFromFile(const String& filePath) bool LuaState::ExecuteFromFile(const String& filePath, int errorHandler)
{ {
if (!LoadFromFile(filePath)) if (!LoadFromFile(filePath))
return false; return false;
return Call(0); return CallWithHandler(errorHandler, 0);
} }
bool LuaState::ExecuteFromMemory(const void* data, std::size_t size) bool LuaState::ExecuteFromMemory(const void* data, std::size_t size, int errorHandler)
{ {
MemoryView stream(data, size); MemoryView stream(data, size);
return ExecuteFromStream(stream); return ExecuteFromStream(stream, errorHandler);
} }
bool LuaState::ExecuteFromStream(Stream& stream) bool LuaState::ExecuteFromStream(Stream& stream, int errorHandler)
{ {
if (!LoadFromStream(stream)) if (!LoadFromStream(stream))
return false; return false;
return Call(0); return CallWithHandler(errorHandler, 0);
} }
int LuaState::GetAbsIndex(int index) const int LuaState::GetAbsIndex(int index) const
@ -809,14 +819,19 @@ namespace Nz
return luaL_testudata(m_state, index, tname.GetConstBuffer()); return luaL_testudata(m_state, index, tname.GetConstBuffer());
} }
bool LuaState::Run(int argCount, int resultCount) void LuaState::Traceback(const char* message, int level)
{
luaL_traceback(m_state, m_state, message, level);
}
bool LuaState::Run(int argCount, int resultCount, int errHandler)
{ {
LuaInstance& instance = GetInstance(m_state); LuaInstance& instance = GetInstance(m_state);
if (instance.m_level++ == 0) if (instance.m_level++ == 0)
instance.m_clock.Restart(); instance.m_clock.Restart();
int status = lua_pcall(m_state, argCount, resultCount, 0); int status = lua_pcall(m_state, argCount, resultCount, errHandler);
instance.m_level--; instance.m_level--;