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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*
* Lua module wrapping librados::bufferlist
*/
#include <errno.h>
#include <string>
#include <sstream>
#include <math.h>
#include <lua.hpp>
#include "include/types.h"
#include "include/buffer.h"
#include "objclass/objclass.h"
#include "cls/lua/cls_lua.h"
#define LUA_BUFFERLIST "ClsLua.Bufferlist"
struct bufferlist_wrap {
bufferlist *bl;
int gc; /* do garbage collect? */
};
static inline struct bufferlist_wrap *to_blwrap(lua_State *L, int pos = 1)
{
return (bufferlist_wrap *)luaL_checkudata(L, pos, LUA_BUFFERLIST);
}
bufferlist *clslua_checkbufferlist(lua_State *L, int pos)
{
struct bufferlist_wrap *blw = to_blwrap(L, pos);
return blw->bl;
}
/*
* Pushes a new bufferlist userdata object onto the stack. If @set is non-null
* it is assumed to be a bufferlist that should not be garbage collected.
*/
bufferlist *clslua_pushbufferlist(lua_State *L, bufferlist *set)
{
bufferlist_wrap *blw = static_cast<bufferlist_wrap *>(lua_newuserdata(L, sizeof(*blw)));
blw->bl = set ? set : new bufferlist();
blw->gc = set ? 0 : 1;
luaL_getmetatable(L, LUA_BUFFERLIST);
lua_setmetatable(L, -2);
return blw->bl;
}
/*
* Create a new bufferlist
*/
static int bl_new(lua_State *L)
{
clslua_pushbufferlist(L, NULL);
return 1;
}
/*
* Convert bufferlist to Lua string
*/
static int bl_str(lua_State *L)
{
bufferlist *bl = clslua_checkbufferlist(L);
lua_pushlstring(L, bl->c_str(), bl->length());
return 1;
}
/*
* Append a Lua string to bufferlist
*/
static int bl_append(lua_State *L)
{
bufferlist *bl = clslua_checkbufferlist(L);
luaL_checktype(L, 2, LUA_TSTRING);
size_t len;
const char *data = lua_tolstring(L, 2, &len);
bl->append(data, len);
return 0;
}
/*
* Return the length in bytes of bufferlist
*/
static int bl_len(lua_State *L)
{
bufferlist *bl = clslua_checkbufferlist(L);
lua_pushinteger(L, bl->length());
return 1;
}
/*
* Perform byte-for-byte bufferlist equality test
*/
static int bl_eq(lua_State *L)
{
bufferlist *bl1 = clslua_checkbufferlist(L, 1);
bufferlist *bl2 = clslua_checkbufferlist(L, 2);
lua_pushboolean(L, *bl1 == *bl2 ? 1 : 0);
return 1;
}
/*
* Bufferlist < operator
*/
static int bl_lt(lua_State *L)
{
bufferlist *bl1 = clslua_checkbufferlist(L, 1);
bufferlist *bl2 = clslua_checkbufferlist(L, 2);
lua_pushboolean(L, *bl1 < *bl2 ? 1 : 0);
return 1;
}
/*
* Bufferlist <= operator
*/
static int bl_le(lua_State *L)
{
bufferlist *bl1 = clslua_checkbufferlist(L, 1);
bufferlist *bl2 = clslua_checkbufferlist(L, 2);
lua_pushboolean(L, *bl1 <= *bl2 ? 1 : 0);
return 1;
}
/*
* Bufferlist concatentation
*/
static int bl_concat(lua_State *L)
{
bufferlist *bl1 = clslua_checkbufferlist(L, 1);
bufferlist *bl2 = clslua_checkbufferlist(L, 2);
bufferlist *ret = clslua_pushbufferlist(L, NULL);
ret->append(bl1->c_str(), bl1->length());
ret->append(bl2->c_str(), bl2->length());
return 1;
}
/*
* Garbage collect bufferlist
*/
static int bl_gc(lua_State *L)
{
struct bufferlist_wrap *blw = to_blwrap(L);
ceph_assert(blw);
ceph_assert(blw->bl);
if (blw->gc)
delete blw->bl;
return 0;
}
static const struct luaL_Reg bufferlist_methods[] = {
{"str", bl_str},
{"append", bl_append},
{"__concat", bl_concat},
{"__len", bl_len},
{"__lt", bl_lt},
{"__le", bl_le},
{"__gc", bl_gc},
{"__eq", bl_eq},
{NULL, NULL}
};
static const struct luaL_Reg bllib_f[] = {
{"new", bl_new},
{NULL, NULL}
};
LUALIB_API int luaopen_bufferlist(lua_State *L)
{
/* Setup bufferlist user-data type */
luaL_newmetatable(L, LUA_BUFFERLIST);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_setfuncs(L, bufferlist_methods, 0);
lua_pop(L, 1);
lua_newtable(L);
luaL_setfuncs(L, bllib_f, 0);
return 1;
}
|