Lua: moved all error's to error.hpp

for easy reuse and maintenance
Dieser Commit ist enthalten in:
Reinder Feenstra 2021-04-20 22:59:51 +02:00
Ursprung 5b4ab8f30d
Commit ef6d5ff9fd
4 geänderte Dateien mit 33 neuen und 19 gelöschten Zeilen

Datei anzeigen

@ -24,11 +24,30 @@
#define TRAINTASTIC_SERVER_LUA_ERROR_HPP
#include <lua.hpp>
#include <stdexcept>
namespace Lua {
// Note:
// All these functions call abort(), it's just a trick to let the compiler understand it is a noretrun function,
// Lua's error funtions aren't marked as noretrun functions, but they are.
[[noreturn]] inline void errorArgumentOutOfRange(lua_State* L, int arg) { luaL_argerror(L, arg, "out of range"); abort(); }
[[noreturn]] inline void errorCantSetNonExistingProperty(lua_State* L) { luaL_error(L, "can't set non existing property"); abort(); }
[[noreturn]] inline void errorCantSetReadOnlyProperty(lua_State* L) { luaL_error(L, "can't set read only property"); abort(); }
[[noreturn]] inline void errorDeadMethod(lua_State* L) { luaL_error(L, "dead method"); abort(); }
[[noreturn]] inline void errorDeadObject(lua_State* L) { luaL_error(L, "dead object"); abort(); }
[[noreturn]] inline void errorExpectedNArgumentsGotN(lua_State* L, int expected, int got) { luaL_error(L, "expected %d arguments, got %d", expected, got); abort(); }
[[noreturn]] inline void errorException(lua_State* L, const std::exception& e) { luaL_error(L, "exceptiop: %s", e.what()); abort(); }
[[noreturn]] inline void errorInternal(lua_State* L) { luaL_error(L, "internal error"); abort(); }
[[noreturn]] inline void errorTableIsReadOnly(lua_State* L) { luaL_error(L, "table is readonly"); abort(); }
}
#endif

Datei anzeigen

@ -22,6 +22,7 @@
#include "method.hpp"
#include "push.hpp"
#include "error.hpp"
#include "../core/abstractmethod.hpp"
#include "../core/object.hpp"
@ -45,8 +46,7 @@ AbstractMethod& Method::check(lua_State* L, int index)
if(!data.object.expired())
return data.method;
luaL_error(L, "dead method");
abort(); // never happens, luaL_error doesn't return
errorDeadMethod(L);
}
AbstractMethod* Method::test(lua_State* L, int index)
@ -57,8 +57,7 @@ AbstractMethod* Method::test(lua_State* L, int index)
else if(!(**data).object.expired())
return &(**data).method;
luaL_error(L, "dead method");
abort(); // never happens, luaL_error doesn't return
errorDeadMethod(L);
}
void Method::push(lua_State* L, AbstractMethod& value)
@ -90,7 +89,7 @@ int Method::__call(lua_State* L)
const int argc = static_cast<int>(method.argumentCount());
if(lua_gettop(L) - 1 != argc)
return luaL_error(L, "expected %d arguments, got %d", argc, lua_gettop(L) - 1);
errorExpectedNArgumentsGotN(L, argc, lua_gettop(L) - 1);
std::vector<AbstractMethod::Argument> args;
args.reserve(argc);
@ -133,7 +132,7 @@ int Method::__call(lua_State* L)
}
catch(const std::exception& e)
{
return luaL_error(L, "exceptiop: %s", e.what());
errorException(L, e);
}
}

Datei anzeigen

@ -25,6 +25,7 @@
#include "check.hpp"
#include "to.hpp"
#include "method.hpp"
#include "error.hpp"
#include "../core/object.hpp"
#include "../core/abstractproperty.hpp"
#include "../core/abstractmethod.hpp"
@ -37,10 +38,7 @@ ObjectPtr Object::check(lua_State* L, int index)
if(ObjectPtr object = data.lock())
return object;
else
{
luaL_error(L, "dead object");
abort(); // never happens, luaL_error doesn't return
}
errorDeadObject(L);
}
ObjectPtr Object::test(lua_State* L, int index)
@ -51,10 +49,7 @@ ObjectPtr Object::test(lua_State* L, int index)
else if(ObjectPtr object = (**data).lock())
return object;
else
{
luaL_error(L, "dead object");
abort(); // never happens, luaL_error doesn't return
}
errorDeadObject(L);
}
void Object::push(lua_State* L, const ObjectPtr& value)
@ -173,7 +168,7 @@ int Object::__newindex(lua_State* L)
// TODO: test scriptable
if(!property->isWriteable())
return luaL_error(L, "can't set read only property");
errorCantSetReadOnlyProperty(L);
try
{
@ -201,17 +196,17 @@ int Object::__newindex(lua_State* L)
default:
assert(false);
return luaL_error(L, "internal error");
errorInternal(L);
}
return 0;
}
catch(const std::exception& e)
{
return luaL_error(L, "%s", e.what());
errorException(L, e);
}
}
return luaL_error(L, "can't set non existing property");
errorCantSetNonExistingProperty(L);
}
}

Datei anzeigen

@ -24,6 +24,7 @@
#define TRAINTASTIC_SERVER_LUA_READONLYTABLE_HPP
#include <lua.hpp>
#include "error.hpp"
namespace Lua {
@ -39,7 +40,7 @@ struct ReadOnlyTable
static int __newindex(lua_State* L)
{
return luaL_error(L, "table is readonly");
errorTableIsReadOnly(L);
}
static void registerType(lua_State* L)