From 498135054a168bd839f2ee8ebb1ba245d72a8f3b Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 21 Mar 2025 23:00:08 +0530 Subject: Lua runtime simple value conversion + spec --- src/LuaRuntime.cpp | 35 ++++++++++++++++++++++++++++++++--- src/LuaRuntime.hpp | 6 ++++++ 2 files changed, 38 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp index e61fbf5..e9a30b1 100644 --- a/src/LuaRuntime.cpp +++ b/src/LuaRuntime.cpp @@ -52,13 +52,42 @@ void LuaRuntime::stopEventLoop() { void LuaRuntime::evaluate(QString code) { eventLoop->queueTask([this, code]() { if (luaL_dostring(state, code.toStdString().c_str())) { - qDebug() << "Lua Error: " << lua_tostring(state, -1); + auto value = lua_tostring(state, -1); lua_pop(state, 1); - } else - qDebug() << "done"; + + qDebug() << "Lua Error: " << value; + emit evaluationFailed(value); + } else { + auto value = getValue(-1); + lua_pop(state, 1); + + qDebug() << "result: " << value; + emit evaluationCompleted(value); + } }); } +QVariant LuaRuntime::evaluateSync(QString code) { + auto result = luaL_dostring(state, code.toStdString().c_str()); + return getValue(-1); // TODO: error handling +} + +QVariant LuaRuntime::getValue(int idx) { + if (lua_isstring(state, idx)) + return lua_tostring(state, idx); + + if (lua_isnumber(state, idx)) + return lua_tonumber(state, idx); + + if (lua_isboolean(state, idx)) + return lua_toboolean(state, idx); + + if (lua_isnil(state, idx)) + return 0; // TODO: nil representation + + return lua_tostring(state, idx); +} + int LuaRuntime::lua_onUrlOpen(lua_State *state) { const char *url = luaL_optstring(state, 1, ""); auto runtime = LuaRuntime::instance(); diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp index 443dec5..dd5c0ec 100644 --- a/src/LuaRuntime.hpp +++ b/src/LuaRuntime.hpp @@ -16,14 +16,20 @@ public: } void evaluate(QString code); + QVariant evaluateSync(QString code); void stopEventLoop(); void startEventLoop(); + QVariant getValue(int idx); + DELEGATE(eventLoop, queueTask, queueTask) signals: void urlOpened(QString url, OpenType openType); + void evaluationCompleted(QVariant value); + void evaluationFailed(QString value); + // void outputProduced(QVariant value); protected: LuaRuntime(); -- cgit v1.3.1