From 8781a6d4f9d5840c7c9954706c972564bd40e76e Mon Sep 17 00:00:00 2001 From: forever2k38 Date: Tue, 13 Oct 2026 22:10:24 +0200 Subject: feat: copy build config instead of just makeopts --- src/config.c | 35 ++++++++++++++--------------------- src/config.h | 7 ++++--- src/main.c | 5 ++++- src/package.c | 6 ++++-- src/package.h | 2 +- src/pow_lua.c | 15 +++++++++++++-- src/repo.c | 2 +- 7 files changed, 41 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/config.c b/src/config.c index 7cfde0c..cbbbbe2 100644 --- a/src/config.c +++ b/src/config.c @@ -6,18 +6,16 @@ #include "stdlib.h" #include "string.h" -int pow_open_config(pow_config *cfg, char *path) { +int pow_open_config(lua_State *L_global, pow_config *cfg, char *path) { int err; - cfg->L = luaL_newstate(); + cfg->L_global = L_global; - if (!cfg->L) { - return 1; - } + luaL_openlibs(L_global); - lua_State *L = cfg->L; // just easier to access + lua_State *L = lua_newthread(L_global); - luaL_openlibs(L); + cfg->L_cfg = L; err = luaL_loadfile(L, path); @@ -101,7 +99,7 @@ int pow_open_config(pow_config *cfg, char *path) { lua_pop(L, 1); lua_pop(L, 1); - free(repo->path); + free((void*)repo->path); free(repo); return 1; @@ -119,19 +117,15 @@ int pow_open_config(pow_config *cfg, char *path) { lua_pop(L, 1); - lua_getfield(L, -1, "makeopts"); + lua_getfield(L, -1, "build"); - if (!lua_isstring(L, -1)) { - pow_log_info("no makeopts specified"); + if (!lua_istable(L, -1)) { + pow_log_info("no build options specified"); } else { - cfg->makeopts = strdup(lua_tostring(L, -1)); - - if (!cfg->makeopts) { - pow_log_error("failed to strdup makeopts"); - - return 1; - } + pow_log_info("build opts defined"); } + + cfg->build_cfg_ref = luaL_ref(L, LUA_REGISTRYINDEX); } return 0; @@ -139,14 +133,13 @@ int pow_open_config(pow_config *cfg, char *path) { int pow_close_config(pow_config *cfg) { for (size_t i = 0; i < cfg->repos_count; i++) { - free(cfg->repos[i]->path); + free((void*)cfg->repos[i]->path); free(cfg->repos[i]); } free(cfg->repos); - free(cfg->makeopts); - lua_close(cfg->L); + lua_close(cfg->L_cfg); return 0; } diff --git a/src/config.h b/src/config.h index a5371f7..8bbee8b 100644 --- a/src/config.h +++ b/src/config.h @@ -10,12 +10,13 @@ typedef struct pow_config { pow_repo **repos; size_t repos_count; - char *makeopts; + lua_State *L_global; + lua_State *L_cfg; - lua_State *L; + int build_cfg_ref; } pow_config; -int pow_open_config(pow_config *cfg, char *path); +int pow_open_config(lua_State *L, pow_config *cfg, char *path); int pow_close_config(pow_config *cfg); #endif diff --git a/src/main.c b/src/main.c index 9f9d676..331bd03 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include "repo.h" #include "log.h" +#include "lauxlib.h" #include "curl/curl.h" #include "stdio.h" #include "stdlib.h" @@ -25,7 +26,9 @@ int main(int argc, char *argv[]) { pow_config config = {0}; - pow_open_config(&config, args.cfg_path); + lua_State* L = luaL_newstate(); + + pow_open_config(L, &config, args.cfg_path); if (args.flags & POW_FLAG_QUERY) { pow_repo_check_package(&config, args.package); diff --git a/src/package.c b/src/package.c index 76216c7..2a947c0 100644 --- a/src/package.c +++ b/src/package.c @@ -167,7 +167,7 @@ int pow_extract_package_info(lua_State *L, pow_package *pkg) { return 0; } -int pow_package_open(pow_package *pkg, pow_config *cfg, const char *path) { +int pow_package_open(lua_State *L_global, pow_package *pkg, pow_config *cfg, const char *path) { FILE *fp; int c; int err; @@ -212,10 +212,12 @@ int pow_package_open(pow_package *pkg, pow_config *cfg, const char *path) { package_buffer[total] = '\0'; - lua_State *L = luaL_newstate(); + lua_State *L = lua_newthread(L_global); pkg->L = L; + // push the config object from cfg thread to pkg thread + luaL_openlibs(L); pow_lua_load(L, cfg); diff --git a/src/package.h b/src/package.h index 008a9fc..eccbb08 100644 --- a/src/package.h +++ b/src/package.h @@ -27,7 +27,7 @@ typedef struct { int pkg_ref; } pow_package; -int pow_package_open(pow_package *pkg, pow_config *cfg, const char *path); +int pow_package_open(lua_State *L_global, pow_package *pkg, pow_config *cfg, const char *path); int pow_package_close(pow_package *pkg); #endif diff --git a/src/pow_lua.c b/src/pow_lua.c index 642077f..b3fa69b 100644 --- a/src/pow_lua.c +++ b/src/pow_lua.c @@ -305,8 +305,18 @@ int pow_lua_load(lua_State *L, pow_config *cfg) { lua_createtable(L, 0, 1); - lua_pushstring(L, cfg->makeopts); - lua_setfield(L, -2, "makeopts"); + lua_setfield(L, -2, "config"); + + lua_rawgeti(cfg->L_cfg, LUA_REGISTRYINDEX, cfg->build_cfg_ref); + + if (!lua_istable(cfg->L_cfg, -1)) { + pow_log_error("failed to initialize lua: config.build is not a table"); + lua_pop(cfg->L_cfg, 1); + + return 1; + } + + lua_xmove(cfg->L_cfg, L, 1); lua_setfield(L, -2, "config"); @@ -314,3 +324,4 @@ int pow_lua_load(lua_State *L, pow_config *cfg) { return 0; } + diff --git a/src/repo.c b/src/repo.c index 51aa121..13ada2e 100644 --- a/src/repo.c +++ b/src/repo.c @@ -78,7 +78,7 @@ int pow_repo_get_package(pow_config *cfg, pow_package *pkg, free(package_path); - pow_package_open(pkg, cfg, full_path); + pow_package_open(cfg->L_global, pkg, cfg, full_path); free(full_path); -- cgit v1.3