summaryrefslogtreecommitdiff
path: root/src/scripting/luautil.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-04 15:44:47 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-05 19:47:54 +0100
commit9f67ba0e68d0a85944268c55045c28d6d12983b5 (patch)
treeb3c9be621f83829acabd766a67862d861e53218d /src/scripting/luautil.cpp
parentaed3f8be2d96dce957de6884944ea666aadc5bd2 (diff)
downloadmanaserv-9f67ba0e68d0a85944268c55045c28d6d12983b5.tar.gz
manaserv-9f67ba0e68d0a85944268c55045c28d6d12983b5.tar.bz2
manaserv-9f67ba0e68d0a85944268c55045c28d6d12983b5.tar.xz
manaserv-9f67ba0e68d0a85944268c55045c28d6d12983b5.zip
Improved Lua API argument checking
It's a bit embarrassing the way this has gotten out of hand. The error checking was inconsistent, and in some cases wrong. A host of new helper functions, starting with 'check' rather than 'get', perform error handling on function arguments (they will not return when encountering an error). Reviewed-by: Erik Schilling
Diffstat (limited to 'src/scripting/luautil.cpp')
-rw-r--r--src/scripting/luautil.cpp39
1 files changed, 31 insertions, 8 deletions
diff --git a/src/scripting/luautil.cpp b/src/scripting/luautil.cpp
index e4260a5c..217fc60c 100644
--- a/src/scripting/luautil.cpp
+++ b/src/scripting/luautil.cpp
@@ -124,8 +124,7 @@ Being *getBeing(lua_State *s, int p)
{
if (!lua_islightuserdata(s, p))
return 0;
- Thing *t = static_cast<Thing *>(lua_touserdata(s, p));
- return static_cast<Being *>(t);
+ return static_cast<Being *>(lua_touserdata(s, p));
}
Character *getCharacter(lua_State *s, int p)
@@ -199,24 +198,48 @@ NPC *getNPC(lua_State *s, int p)
}
+Being *checkBeing(lua_State *s, int p)
+{
+ Being *being = getBeing(s, p);
+ luaL_argcheck(s, being, p, "being expected");
+ return being;
+}
+
+Character *checkCharacter(lua_State *s, int p)
+{
+ Character *character = getCharacter(s, p);
+ luaL_argcheck(s, character, p, "character expected");
+ return character;
+}
+
ItemClass *checkItemClass(lua_State *s, int p)
{
ItemClass *itemClass = getItemClass(s, p);
- if (!itemClass)
- luaL_argerror(s, p, "invalid item type parameter");
-
+ luaL_argcheck(s, itemClass, p, "item type expected");
return itemClass;
}
+Monster *checkMonster(lua_State *s, int p)
+{
+ Monster *monster = getMonster(s, p);
+ luaL_argcheck(s, monster, p, "monster expected");
+ return monster;
+}
+
MonsterClass *checkMonsterClass(lua_State *s, int p)
{
MonsterClass *monsterClass = getMonsterClass(s, p);
- if (!monsterClass)
- luaL_argerror(s, p, "invalid monster type parameter");
-
+ luaL_argcheck(s, monsterClass, p, "monster type expected");
return monsterClass;
}
+NPC *checkNPC(lua_State *s, int p)
+{
+ NPC *npc = getNPC(s, p);
+ luaL_argcheck(s, npc, p, "npc expected");
+ return npc;
+}
+
void push(lua_State *s, int val)
{