aboutsummaryrefslogtreecommitdiff
path: root/src/lua_state.cc
diff options
context:
space:
mode:
authorfrosty <gabriel@bwaaa.monster>2026-04-22 22:30:21 -0400
committerdacctal <dacctalyt@gmail.com>2026-04-24 03:39:52 +0000
commitf1d7a1903e49c75f9965693247e8d102a96b482c (patch)
tree1edd5ebe0a62c00353f824581a24e0afa305873f /src/lua_state.cc
parent0b9cc83f71399a41cf4e93ef82af7c8ba5c2ab96 (diff)
misc optimisations and QoL improvements
Diffstat (limited to 'src/lua_state.cc')
-rw-r--r--src/lua_state.cc109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/lua_state.cc b/src/lua_state.cc
new file mode 100644
index 0000000..81f8d13
--- /dev/null
+++ b/src/lua_state.cc
@@ -0,0 +1,109 @@
+#include <iostream>
+#include <unordered_map>
+#include "lua_state.hh"
+#include "vars.hh"
+
+static lua_State *L = nullptr;
+static bool config_loaded = false;
+
+std::unordered_map<std::string, std::string> cached_install_directories;
+std::unordered_map<std::string, std::string> cached_repos;
+std::unordered_map<std::string, int> cached_build_systems;
+
+void init_lua_state() {
+ if (L != nullptr) return;
+ L = luaL_newstate();
+ luaL_openlibs(L);
+
+ if (luaL_loadfile(L, config_file.c_str()) || lua_pcall(L, 0, 0, 0)) {
+ std::cout << print_error << "cannot run configuration script: " << lua_tostring(L, -1) << "\n";
+ return;
+ }
+
+ if (luaL_loadfile(L, repo_file.c_str()) || lua_pcall(L, 0, 0, 0)) {
+ std::cout << print_error << "cannot run repository script: " << lua_tostring(L, -1) << "\n";
+ return;
+ }
+
+ config_loaded = true;
+}
+
+void free_lua_state() {
+ if (L != nullptr) {
+ lua_close(L);
+ L = nullptr;
+ }
+ config_loaded = false;
+}
+
+lua_State* get_lua_state() {
+ return L;
+}
+
+void cache_install_directories() {
+ if (!config_loaded || !lua_istable(L, -1)) {
+ lua_getglobal(L, "install_directories");
+ }
+
+ if (!lua_istable(L, -1)) {
+ std::cout << print_error << "lua variable 'install_directories' is not a table.\n";
+ return;
+ }
+
+ lua_pushnil(L);
+ while (lua_next(L, -2) != 0) {
+ const char *key = lua_tostring(L, -2);
+ const char *value = lua_tostring(L, -1);
+ cached_install_directories[key] = value;
+ lua_pop(L, 1);
+ }
+ lua_pop(L, 1);
+}
+
+void cache_repos() {
+ if (!config_loaded) {
+ lua_getglobal(L, "repos");
+ } else if (!lua_istable(L, -1)) {
+ lua_getglobal(L, "repos");
+ }
+
+ if (!lua_istable(L, -1)) {
+ std::cout << print_error << "lua variable 'repos' is not a table.\n";
+ return;
+ }
+
+ lua_pushnil(L);
+ while (lua_next(L, -2) != 0) {
+ const char *key = lua_tostring(L, -2);
+ const char *value = lua_tostring(L, -1);
+ cached_repos[key] = value;
+ lua_pop(L, 1);
+ }
+ lua_pop(L, 1);
+}
+
+void cache_build_systems() {
+ if (!config_loaded) {
+ lua_getglobal(L, "build_systems");
+ } else if (!lua_istable(L, -1)) {
+ lua_getglobal(L, "build_systems");
+ }
+
+ if (!lua_istable(L, -1)) {
+ std::cout << print_error << "lua variable 'build_systems' is not a table.\n";
+ return;
+ }
+
+ lua_pushnil(L);
+ while (lua_next(L, -2) != 0) {
+ const char *key = lua_tostring(L, -2);
+ if (lua_isfunction(L, -1) == 0) {
+ std::cout << print_error << "build value is not a function\n";
+ lua_pop(L, 1);
+ continue;
+ }
+ cached_build_systems[key] = lua_gettop(L);
+ lua_pop(L, 1);
+ }
+ lua_pop(L, 1);
+}