Skip to content

Commit d2f274c

Browse files
committed
improved lua types code
1 parent 532be12 commit d2f274c

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

bindings/lua/src/types.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,31 @@
2727
#include "types.h"
2828

2929
#define STREQUAL(a, b) (!strcmp(a, b))
30+
#define LUA_ENTRY_INT(name) \
31+
if (STREQUAL(entry, #name)) { \
32+
lua_pushinteger(L, (lua_Integer)udata->name); \
33+
return 1; \
34+
}
35+
#define LUA_ENTRY_STR(name) \
36+
if (STREQUAL(entry, #name)) { \
37+
lua_pushstring(L, udata->name); \
38+
return 1; \
39+
}
3040
#define LM_PROCESS_META "lm_process_t"
3141

3242
int lua_lm_process_index(lua_State *L)
3343
{
3444
lm_process_t *udata = (lm_process_t *)luaL_checkudata(L, 1, LM_PROCESS_META);
3545
const char *entry = luaL_checkstring(L, 2);
3646

37-
if (STREQUAL(entry, "pid")) {
38-
lua_pushinteger(L, (lua_Integer)udata->pid);
39-
} else if (STREQUAL(entry, "ppid")) {
40-
lua_pushinteger(L, (lua_Integer)udata->ppid);
41-
} else if (STREQUAL(entry, "bits")) {
42-
lua_pushinteger(L, (lua_Integer)udata->bits);
43-
} else if (STREQUAL(entry, "start_time")) {
44-
lua_pushinteger(L, (lua_Integer)udata->start_time);
45-
} else if (STREQUAL(entry, "path")) {
46-
lua_pushstring(L, udata->path);
47-
} else if (STREQUAL(entry, "name")) {
48-
lua_pushstring(L, udata->name);
49-
} else {
50-
lua_pushnil(L);
51-
}
47+
LUA_ENTRY_INT(pid)
48+
LUA_ENTRY_INT(ppid)
49+
LUA_ENTRY_INT(bits)
50+
LUA_ENTRY_INT(start_time)
51+
LUA_ENTRY_STR(path)
52+
LUA_ENTRY_STR(name)
5253

54+
lua_pushnil(L);
5355
return 1;
5456
}
5557

0 commit comments

Comments
 (0)