void luaL_checkIsTable(LuaThread* L, int narg) { THREAD_CHECK(L); LuaValue v = *index2addr(L, narg); if(v.isTable()) return; const char* actualType = v.typeName(); const char *msg = lua_pushfstring(L, "Expected a table, got a %s", actualType); luaL_argerror(L, narg, msg); }
void luaL_checkIsFunction(LuaThread *L, int narg) { THREAD_CHECK(L); LuaValue* pv = index2addr(L, narg); assert(pv); LuaValue v = *pv; if(v.isFunction()) return; const char* actualType = v.typeName(); const char *msg = lua_pushfstring(L, "Expected a function, got a %s", actualType); luaL_argerror(L, narg, msg); }
// - LuaValue::operator== --------------------------------------------------- bool LuaValue::operator== (const LuaValue& rhs) const { std::string lhsTypeName = typeName(); std::string rhsTypeName = rhs.typeName(); if (typeName() != rhs.typeName()) return false; else switch (type()) { case LUA_TNIL: return true; case LUA_TBOOLEAN: return asBoolean() == rhs.asBoolean(); case LUA_TNUMBER: return asNumber() == rhs.asNumber(); case LUA_TSTRING: return asString() == rhs.asString(); case LUA_TTABLE: return asTable() == rhs.asTable(); case LUA_TFUNCTION: return asFunction() == rhs.asFunction(); case LUA_TUSERDATA: return asUserData() == rhs.asUserData(); default: { assert( false && "Invalid type found in a call to 'LuaValue::operator==()'."); return 0; // make compilers happy } } }
// - LuaValue::operator> ---------------------------------------------------- bool LuaValue::operator> (const LuaValue& rhs) const { std::string lhsTypeName = typeName(); std::string rhsTypeName = rhs.typeName(); if (lhsTypeName > rhsTypeName) return true; else if (lhsTypeName < rhsTypeName) return false; else // lhsTypeName == rhsTypeName { if (lhsTypeName == "nil") return false; else if (lhsTypeName == "boolean") return asBoolean() > rhs.asBoolean(); else if (lhsTypeName == "number") return asNumber() > rhs.asNumber(); else if (lhsTypeName == "string") return asString() > rhs.asString(); else if (lhsTypeName == "function") return asFunction() > rhs.asFunction(); else if (lhsTypeName == "userdata") return asUserData() > rhs.asUserData(); else if (lhsTypeName == "table") { const LuaValueMap lhsMap = asTable(); const LuaValueMap rhsMap = rhs.asTable(); if (lhsMap.size() > rhsMap.size()) return true; else if (lhsMap.size() < rhsMap.size()) return false; else // lhsMap.size() == rhsMap.size() { typedef LuaValueMap::const_iterator iter_t; iter_t pLHS = lhsMap.begin(); iter_t pRHS = rhsMap.begin(); const iter_t end = lhsMap.end(); while (pLHS != end) { // check the key first if (pLHS->first > pRHS->first) return true; else if (pLHS->first < pRHS->first) return false; // then check the value if (pLHS->second > pRHS->second) return true; else if (pLHS->second < pRHS->second) return false; // make the iterators iterate ++pRHS; ++pLHS; } return false; } } else { assert (false && "Unsupported type found at a call " "to 'LuaValue::operator>()'"); return false; // make the compiler happy. } } }
const char* luaL_typename(LuaThread* L, int index) { THREAD_CHECK(L); LuaValue v = L->stack_.at(index); return v.typeName(); }