-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplumbattr.c
81 lines (67 loc) · 1.38 KB
/
plumbattr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "a.h"
#define PLUMBATTR "Plumbattr"
typedef struct LPlumbattr LPlumbattr;
struct LPlumbattr
{
Plumbattr *a;
};
void
pushplumbattr(lua_State *L, Plumbattr *a)
{
LPlumbattr *l;
l = (LPlumbattr*)lua_newuserdata(L, sizeof(LPlumbattr));
luaL_getmetatable(L, PLUMBATTR);
lua_setmetatable(L, -2);
l->a = a;
}
Plumbattr*
checkplumbattr(lua_State *L, int index)
{
LPlumbattr *l;
l = (LPlumbattr*)luaL_checkudata(L, index, PLUMBATTR);
luaL_argcheck(L, l != NULL, index, "Plumbattr expected");
return l->a;
}
static int
plumbattr__gc(lua_State *L)
{
/* already freed by plumbmsg gc */
lua_pushboolean(L, 0);
return 1;
}
static int
plumbattr__tostring(lua_State *L)
{
void *p;
p = lua_touserdata(L, 1);
lua_pushfstring(L, "plumbattr: %p", p);
return 1;
}
static int
plumbattr__index(lua_State *L)
{
Plumbattr *a;
const char *s;
a = checkplumbattr(L, 1);
s = luaL_checkstring(L, 2);
if(strncmp(s, "name", 4) == 0)
lua_pushstring(L, a->name);
else if(strncmp(s, "value", 5) == 0)
lua_pushstring(L, a->value);
else if(strncmp(s, "next", 4) == 0)
pushplumbattr(L, a->next);
else
return 0;
return 1;
}
static const struct luaL_Reg plumbattr_funcs[] = {
{ "__gc", plumbattr__gc },
{ "__tostring", plumbattr__tostring },
{ "__index", plumbattr__index },
{ NULL, NULL },
};
void
registerplumbattrmeta(lua_State *L)
{
createmetatable(L, PLUMBATTR, plumbattr_funcs);
}