void LuaEngine::luaPush(const QVariant& variant) { switch (variant.type()) { case QVariant::Bool: { luaPush(variant.toBool()); break; } case QVariant::String: { luaPush(variant.toString()); break; } case QVariant::Int: { luaPush(variant.toInt()); break; } case QVariant::Map: { luaPush(variant.toMap()); break; } case QVariant::Hash: { luaPush(variant.toHash()); break; } default: break; } }
void LuaEngine::luaPush(const QMap<QString, QVariant>& hash) { lua_newtable(L); for (QMap<QString, QVariant>::const_iterator it = hash.constBegin(); it != hash.constEnd(); ++it) { luaPush(it.key()); luaPush(it.value()); lua_settable(L, -3); } }
int defaultStdFunctionWrapper(lua_State* L, LuaStdFunction& fn) { try { return fn(L); } catch (const std::exception& e) { luaPush(L, folly::exceptionStr(e)); lua_error(L); return 0; // unreached } }
int TestObject::luaIndex(lua_State* L) { auto arg = luaGet<folly::StringPiece>(L, 2); if (arg && *arg == "y") { luaPush(L, y); } else { lua_pushnil(L); } return 1; }
QVariant LuaEngine::invokeFunction(const char* object, const char* method, const QVariantList& arguments /*= QVariantList()*/, QLSCallback callback /*= nullptr*/) { int argc = arguments.count(); #ifdef Q_OS_WIN foreach(const QVariant & argv, arguments) #else for (const QVariant & argv : arguments) #endif { luaPush(argv); } invokeFunction(std::string(object), std::string(method), argc, 1, callback != nullptr); QVariant result; if (lua_isnumber(L, -1)) { result = luaPopNumber(); } else if (lua_isstring(L, -1)) { result = QString(luaPopString().c_str()); } else if (lua_isuserdata(L, -1)) { result = (qulonglong)lua_touserdata(L, -1); } else if (lua_istable(L, -1)) { QHash<QString, QVariant> table; lua_pushnil(L); while (lua_next(L, -2) != 0) { table[lua_tostring(L, -2)] = lua_tostring(L, -1); lua_pop(L, 1); } lua_pop(L, 1); // remove table result = table; } return result; }
void LuaEngine::luaPush(const QString& string) { luaPush(string.toStdString()); }
int TestObject::luaValue(lua_State* L) { luaPush(L, x); return 1; }
int TestObject::luaLen(lua_State* L) { luaPush(L, 10); return 1; }
int TestObjectMethodsOnly::luaValue(lua_State* L) { luaPush(L, y); return 1; }