LuaYard/LuaCommon.cpp

58 lines
1.3 KiB
C++
Raw Normal View History

2019-08-18 02:00:57 +08:00
#include "LuaCommon.h"
using namespace std;
bool LuaCompareType(lua_State* L, int index, const char* type, bool leave)
{
int indexType = lua_getfield(L, index, "type");
if (indexType == LUA_TSTRING)
{
if (strcmp(lua_tostring(L, -1), type) == 0)
{
if (!leave) lua_pop(L, 1);
return true;
}
else
{
if (!leave) lua_pop(L, 1);
return false;
}
}
else
{
lua_pop(L, 1);
if (leave) lua_pushstring(L, lua_typename(L, indexType));
return false;
}
}
void check(lua_State* L, const vector<int>& typearr, const vector<const char*>& userdata_type)
{
int sz = typearr.size();
int nextIndex = 0;
int maxIndex = userdata_type.size();
for (int i = 0; i < sz; i++)
{
int thisType = lua_type(L, i + 1);
if (thisType != typearr[i])
{
luaL_error(L, "Bad argument #%d. %s expected, got %s", i + 1, lua_typename(L, typearr[i]), lua_typename(L, lua_type(L, i + 1)));
}
if (maxIndex && thisType == LUA_TUSERDATA)
{
if (nextIndex < maxIndex)
{
if (!LuaCompareType(L, i + 1, userdata_type[nextIndex], true))
{
luaL_error(L, "Bad argument #%d. %s expected, got %s", i + 1, userdata_type[nextIndex], lua_tostring(L, -1));
}
lua_pop(L, 1);
nextIndex++;
}
else
{
luaL_error(L, "Not enough userdata type string.");
}
}
}
}