26
26
#include <lualib.h>
27
27
#include "types.h"
28
28
29
+ #define STREQUAL (a , b ) (!strcmp(a, b))
30
+ #define LM_PROCESS_META "lm_process_t"
31
+
29
32
int lua_lm_process_index (lua_State * L )
30
33
{
31
- lua_pushinteger (L , 1337 );
34
+ lm_process_t * udata = (lm_process_t * )luaL_checkudata (L , 1 , LM_PROCESS_META );
35
+ const char * entry = luaL_checkstring (L , 2 );
36
+
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
+ }
52
+
32
53
return 1 ;
33
54
}
34
55
35
56
int lua_lm_process_tostring (lua_State * L )
36
57
{
37
- lm_process_t * udata = (lm_process_t * )luaL_checkudata (L , 1 , "lm_process_t" );
58
+ lm_process_t * udata = (lm_process_t * )luaL_checkudata (L , 1 , LM_PROCESS_META );
38
59
39
60
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 );
40
61
@@ -44,13 +65,13 @@ int lua_lm_process_tostring(lua_State *L)
44
65
void lua_create_lm_process (lua_State * L )
45
66
{
46
67
lm_process_t * udata = lua_newuserdata (L , sizeof (lm_process_t ));
47
- luaL_getmetatable (L , "lm_process_t" );
68
+ luaL_getmetatable (L , LM_PROCESS_META );
48
69
lua_setmetatable (L , lua_gettop (L ) - 1 );
49
70
}
50
71
51
72
void lua_define_lm_process (lua_State * L )
52
73
{
53
- luaL_newmetatable (L , "lm_process_t" );
74
+ luaL_newmetatable (L , LM_PROCESS_META );
54
75
lua_pushcfunction (L , lua_lm_process_index );
55
76
lua_setfield (L , lua_gettop (L ) - 1 , "__index" );
56
77
lua_pushcfunction (L , lua_lm_process_tostring );
0 commit comments