From 9f67ba0e68d0a85944268c55045c28d6d12983b5 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Sun, 4 Mar 2012 15:44:47 +0100 Subject: 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 --- src/scripting/luautil.cpp | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'src/scripting/luautil.cpp') diff --git a/src/scripting/luautil.cpp b/src/scripting/luautil.cpp index e4260a5..217fc60 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(lua_touserdata(s, p)); - return static_cast(t); + return static_cast(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) { -- cgit