Updated lbs to use new uniproc library

Fixed bug in fs.list_alldirs where it used directory_iterator rather that recursive_directory_iterator
Fixed memory/type bugs in lua_platform.cpp
Updated lbs.lua to reflect this
This commit is contained in:
2025-03-06 19:27:19 +11:00
parent 40ea3caf2a
commit 68e2c49c8f
6 changed files with 80 additions and 47 deletions

View File

@@ -4,7 +4,7 @@
#include "mutex"
#include <chrono>
#include "simple_string.h"
#include "uniproc/include/uniproc.h"
#include "uniproc.h"
int luaopen_platform(lua_State* L)
{
@@ -26,12 +26,12 @@ int lua_platform_exec(lua_State* L)
luaL_argcheck(L, lua_istable(L, 2), 2, "Expected second argument to be a table of strings");
size_t nargs = lua_objlen(L, 2);
char** argv = malloc(nargs * sizeof(char*));
const char** argv = (const char**)malloc(nargs * sizeof(const char*));
for (size_t i = 0; i < nargs; ++i)
{
lua_rawgeti(L, -1, i + 1);
if (!lua_isstring(L, -1))
return luaL_error(L, "Expected all values in argument table to be a string. Index %d was a %s", i, lua_typename(L, lua_type(L, -1)));
lua_rawgeti(L, 2, i+1);
if (lua_type(L, -1) != LUA_TSTRING)
return luaL_error(L, "Expected all values in argument table to be a string. Index %d was a %s", i+1, lua_typename(L, lua_type(L, -1)));
argv[i] = lua_tostring(L, -1);
}
@@ -72,7 +72,8 @@ int lua_platform_exec_parallel(lua_State* L)
const size_t max_parallel = lua_tointeger(L, 2);
const size_t num_cmds = lua_objlen(L, 3);
const size_t* num_args = (size_t*)malloc(num_cmds * sizeof(size_t));
size_t* num_args = (size_t*)malloc(num_cmds * sizeof(size_t));
memset(num_args, 0, sizeof(size_t) * num_cmds);
const char*** args = (const char***)malloc(num_cmds*sizeof(const char**));
// Generate the command stack from lua arguments
@@ -81,9 +82,10 @@ int lua_platform_exec_parallel(lua_State* L)
lua_rawgeti(L, 3, i + 1);
if (lua_istable(L, -1))
{
const size_t num_args = lua_objlen(L, -1);
args[i] = (const char**)malloc(num_args*sizeof(const char*));
for (size_t j = 0; j < num_args; ++j)
const size_t tbl_num_args = lua_objlen(L, -1);
num_args[i] = tbl_num_args;
args[i] = (const char**)malloc(tbl_num_args*sizeof(const char*));
for (size_t j = 0; j < tbl_num_args; ++j)
{
lua_rawgeti(L, -1, j + 1);
if (!lua_isstring(L, -1))
@@ -94,6 +96,7 @@ int lua_platform_exec_parallel(lua_State* L)
}
else if (lua_type(L, -1) == LUA_TSTRING)
{
num_args[i] = 1;
args[i] = (const char**)malloc(1*sizeof(const char*));
args[i][0] = lua_tostring(L, -1);
}
@@ -123,12 +126,12 @@ int lua_platform_exec_parallel(lua_State* L)
{
char buf[64];
// Read the std. streams into the output vectors
while (fgets(buf, sizeof(buf), p.out) != NULL)
std_outs[p.userdata].append(buf);
while (fgets(buf, sizeof(buf), p.err) != NULL)
std_errs[p.userdata].append(buf);
while (fgets(buf, sizeof(buf), p->out) != NULL)
std_outs[p->userdata].append(buf);
while (fgets(buf, sizeof(buf), p->err) != NULL)
std_errs[p->userdata].append(buf);
// Add the process return code to retcode array
uniproc_await_processes(p, retcodes + p.userdata, 1);
uniproc_await_processes(p, retcodes + p->userdata, 1);
// Close the process and zero it
uniproc_close_process(p);
}
@@ -139,7 +142,7 @@ int lua_platform_exec_parallel(lua_State* L)
{
// Create process
*p = uniproc_create_process(cmd, num_args[cmd_idx], args[cmd_idx]);
uniproc_set_userdata(p, num_cmds);
p->userdata = cmd_idx;
cmd_idx++;
// If we failed to create the process, set the output for it to an error value