add table management (untested)

This commit is contained in:
Syping 2019-10-21 21:28:37 +02:00
parent 6085acc7bf
commit 3e74aaa2dd

View file

@ -264,6 +264,29 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant)
currentId++;
}
}
else if ((QMetaType::Type)variant.type() == QMetaType::QVariantList) {
QVariantList variantList = variant.toList();
lua_createtable(L_p, 0, variantList.count());
int currentId = 1;
for (const QVariant &variant : variantList) {
lua_pushnumber(L_p, currentId);
pushVariant(L_p, variant);
lua_settable(L_p, -3);
currentId++;
}
}
else if ((QMetaType::Type)variant.type() == QMetaType::QVariantMap) {
QVariantMap variantMap = variant.toMap();
lua_createtable(L_p, 0, variantMap.count());
QVariantMap::const_iterator it = variantMap.constBegin();
QVariantMap::const_iterator end = variantMap.constEnd();
while (it != end) {
lua_pushstring(L_p, it.key().toUtf8().data());
pushVariant(L_p, it.value());
lua_settable(L_p, -3);
it++;
}
}
else if ((QMetaType::Type)variant.type() == QMetaType::Void || (QMetaType::Type)variant.type() == QMetaType::VoidStar) {
lua_pushlightuserdata(L_p, variant.value<void*>());
}
@ -303,6 +326,20 @@ QVariant LuaEngine::getVariant(lua_State *L_p, int index)
else if (lua_isstring(L_p, index)) {
return QVariant::fromValue(QString(lua_tostring(L_p, index)));
}
else if (lua_istable(L_p, index)) {
QVariantMap variantMap;
lua_pushvalue(L_p, index);
lua_pushnil(L_p);
while (lua_next(L_p, -2) != 0) {
lua_pushvalue(L_p, -2);
QString key = QString(lua_tostring(L_p, -1));
QVariant value = getVariant(L_p, -2);
variantMap.insert(key, value);
lua_pop(L_p, 2);
}
lua_pop(L_p, 1);
return QVariant::fromValue(variantMap);
}
else if (lua_isuserdata(L_p, index)) {
return QVariant::fromValue(lua_touserdata(L_p, index));
}