diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-03-21 23:00:08 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-03-21 23:00:08 +0530 |
| commit | 498135054a168bd839f2ee8ebb1ba245d72a8f3b (patch) | |
| tree | 0da58d35276d1f27fd548fff9dbab2cccebbc4b6 /src/LuaRuntime.cpp | |
| parent | 7ec8336431787156826185628ad3ee05dc327d2a (diff) | |
| download | null-browser-498135054a168bd839f2ee8ebb1ba245d72a8f3b.tar.gz null-browser-498135054a168bd839f2ee8ebb1ba245d72a8f3b.zip | |
Lua runtime simple value conversion + spec
Diffstat (limited to 'src/LuaRuntime.cpp')
| -rw-r--r-- | src/LuaRuntime.cpp | 35 |
1 files changed, 32 insertions, 3 deletions
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(); |
