Skip to content

Commit

Permalink
added proper index function for lm_process_t in lua
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbo committed Dec 5, 2023
1 parent e48e701 commit 532be12
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
29 changes: 25 additions & 4 deletions bindings/lua/src/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,36 @@
#include <lualib.h>
#include "types.h"

#define STREQUAL(a, b) (!strcmp(a, b))
#define LM_PROCESS_META "lm_process_t"

int lua_lm_process_index(lua_State *L)
{
lua_pushinteger(L, 1337);
lm_process_t *udata = (lm_process_t *)luaL_checkudata(L, 1, LM_PROCESS_META);
const char *entry = luaL_checkstring(L, 2);

if (STREQUAL(entry, "pid")) {
lua_pushinteger(L, (lua_Integer)udata->pid);
} else if (STREQUAL(entry, "ppid")) {
lua_pushinteger(L, (lua_Integer)udata->ppid);
} else if (STREQUAL(entry, "bits")) {
lua_pushinteger(L, (lua_Integer)udata->bits);
} else if (STREQUAL(entry, "start_time")) {
lua_pushinteger(L, (lua_Integer)udata->start_time);
} else if (STREQUAL(entry, "path")) {
lua_pushstring(L, udata->path);
} else if (STREQUAL(entry, "name")) {
lua_pushstring(L, udata->name);
} else {
lua_pushnil(L);
}

return 1;
}

int lua_lm_process_tostring(lua_State *L)
{
lm_process_t *udata = (lm_process_t *)luaL_checkudata(L, 1, "lm_process_t");
lm_process_t *udata = (lm_process_t *)luaL_checkudata(L, 1, LM_PROCESS_META);

lua_pushfstring(L, "lm_process_t(pid: %d, ppid: %d, bits: %d, name: \"%s\", path: \"%s\", start_time: %p)", udata->pid, udata->ppid, udata->bits, udata->name, udata->path, udata->start_time);

Expand All @@ -44,13 +65,13 @@ int lua_lm_process_tostring(lua_State *L)
void lua_create_lm_process(lua_State *L)
{
lm_process_t *udata = lua_newuserdata(L, sizeof(lm_process_t));
luaL_getmetatable(L, "lm_process_t");
luaL_getmetatable(L, LM_PROCESS_META);
lua_setmetatable(L, lua_gettop(L) - 1);
}

void lua_define_lm_process(lua_State *L)
{
luaL_newmetatable(L, "lm_process_t");
luaL_newmetatable(L, LM_PROCESS_META);
lua_pushcfunction(L, lua_lm_process_index);
lua_setfield(L, lua_gettop(L) - 1, "__index");
lua_pushcfunction(L, lua_lm_process_tostring);
Expand Down
11 changes: 10 additions & 1 deletion bindings/lua/tests/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@ print("Lua Tests")
local libmem = require("libmem_lua")
print("libmem_lua loaded")

print(libmem.LM_FindProcess("target"))
target_proc = libmem.LM_FindProcess("target")
print("[*] Target Process")
print("PID: " .. target_proc.pid)
print("PPID: " .. target_proc.ppid)
print("Bits: " .. target_proc.bits)
print("Start Time: " .. target_proc.start_time)
print("Path: " .. target_proc.path)
print("Name: " .. target_proc.name)
print("(invalid): " .. (target_proc.invalid or "nil"))
print(target_proc)

0 comments on commit 532be12

Please sign in to comment.