diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-04-12 11:56:10 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-04-12 11:56:35 +0530 |
| commit | f3f109cd054888f857d51ca2b37b4123f3134069 (patch) | |
| tree | 81250db8a30f85d34d3c101830a37e3535e77054 /src/LuaRuntime.cpp | |
| parent | c0ee5203201d4ddf4ed560f856cb0da958935fc6 (diff) | |
| download | null-browser-f3f109cd054888f857d51ca2b37b4123f3134069.tar.gz null-browser-f3f109cd054888f857d51ca2b37b4123f3134069.zip | |
Add configuration api
Diffstat (limited to 'src/LuaRuntime.cpp')
| -rw-r--r-- | src/LuaRuntime.cpp | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp index 036f47f..07d8193 100644 --- a/src/LuaRuntime.cpp +++ b/src/LuaRuntime.cpp @@ -2,6 +2,7 @@ #include <cstdlib> #include <cstring> #include <lua.hpp> +#include <qvariant.h> extern "C" { #include <luv/luv.h> } @@ -73,13 +74,11 @@ void LuaRuntime::evaluate(const QString &code) { }); } -void LuaRuntime::load_file(const QString &path) { - queue_task([this, path]() { - preserve_top(state, { - if (luaL_dofile(state, path.toStdString().c_str()) != LUA_OK) { - qDebug() << "Load file error:" << lua_tostring(state, -1); - } - }) +void LuaRuntime::load_file_sync(const QString &path) { + preserve_top(state, { + if (luaL_dofile(state, path.toStdString().c_str()) != LUA_OK) { + qDebug() << "Load file error:" << lua_tostring(state, -1); + } }); } @@ -131,6 +130,8 @@ void LuaRuntime::init_web_lib() { luaL_Reg internals[] = { {"register_event", &LuaRuntime::lua_event_register}, + {"set_config", &LuaRuntime::lua_config_set}, + {"get_config", &LuaRuntime::lua_config_get}, {nullptr, nullptr}, }; luaL_newlib(state, internals); @@ -210,6 +211,31 @@ int LuaRuntime::lua_view_create(lua_State *state) { return 1; } +int LuaRuntime::lua_config_set(lua_State *state) { + const char *key = lua_tostring(state, 1); + auto &runtime = LuaRuntime::instance(); + QVariant value = runtime.get_lua_value(2, ""); + emit runtime.config_updated(key, value); + return 1; +} + +int LuaRuntime::lua_config_get(lua_State *state) { + const char *key = lua_tostring(state, 1); + auto &router = WindowActionRouter::instance(); + auto value = router.fetch_config_value(key); + + if (value.typeId() == QMetaType::QString) + lua_pushstring(state, value.toString().toStdString().c_str()); + else if (value.typeId() == QMetaType::Int) + lua_pushinteger(state, value.toInt()); + else if (value.typeId() == QMetaType::Double) + lua_pushnumber(state, value.toDouble()); + else + lua_pushnil(state); + + return 1; +} + int LuaRuntime::lua_keymap_set(lua_State *state) { const char *mode = lua_tostring(state, 1); const char *keyseq = lua_tostring(state, 2); |
