-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluabinding.c
230 lines (205 loc) · 5.5 KB
/
luabinding.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#ifndef _LUA_BITPACK_H_
#define _LUA_BITPACK_H_
#include <lua.h>
#include <lauxlib.h>
#include "bitpack.h"
#define MT_NAME ("_bitpack_metatable")
static int
size(lua_State *L) {
lua_getiuservalue(L, 1, 1);
bitpack_t bp = lua_touserdata(L, -1);
unsigned long bits_size = bitpack_size(bp);
lua_pushinteger(L, bits_size);
return 1;
}
static int
alloc_bytes(lua_State *L) {
lua_getiuservalue(L, 1, 1);
bitpack_t bp = lua_touserdata(L, -1);
unsigned long mem_size = bitpack_data_size(bp);
lua_pushinteger(L, mem_size);
return 1;
}
static int
on(lua_State *L) {
int pos = luaL_checkinteger(L, 2);
if (pos < 0) {
luaL_error(L, "on pos (%d) must >= 0", pos);
}
lua_getiuservalue(L, 1, 1);
bitpack_t bp = lua_touserdata(L, -1);
lua_pushboolean(L, bitpack_on(bp, pos) == BITPACK_RV_SUCCESS);
return 1;
}
static int
off(lua_State *L) {
int pos = luaL_checkinteger(L, 2);
if (pos < 0) {
luaL_error(L, "on pos (%d) must >= 0", pos);
}
lua_getiuservalue(L, 1, 1);
bitpack_t bp = lua_touserdata(L, -1);
lua_pushboolean(L, bitpack_off(bp, pos) == BITPACK_RV_SUCCESS);
return 1;
}
static int
get(lua_State *L) {
int pos = luaL_checkinteger(L, 2);
if (pos < 0) {
luaL_error(L, "on pos (%d) must >= 0", pos);
}
lua_getiuservalue(L, 1, 1);
bitpack_t bp = lua_touserdata(L, -1);
unsigned char bit;
int rv = bitpack_get(bp, pos, &bit);
lua_pushboolean(L, rv == BITPACK_RV_SUCCESS);
if (rv == BITPACK_RV_SUCCESS) {
lua_pushboolean(L, 1);
lua_pushinteger(L, bit);
} else {
lua_pushboolean(L, 0);
lua_pushinteger(L, rv);
}
return 2;
}
static int
set_bytes(lua_State *L) {
int pos = luaL_checkinteger(L, 2);
if (pos < 0) {
luaL_error(L, "set_bytes pos (%d) must >= 0", pos);
}
size_t len;
const char *bytes = luaL_checklstring(L, 3, &len);
if (!bytes) {
luaL_error(L, "set_bytes bytes string is nil");
}
lua_getiuservalue(L, 1, 1);
bitpack_t bp = lua_touserdata(L, -1);
int rv = bitpack_set_bytes(bp, bytes, len, pos);
if (rv == BITPACK_RV_SUCCESS) {
lua_pushboolean(L, 1);
return 1;
}
lua_pushboolean(L, 0);
lua_pushinteger(L, rv);
return 2;
}
static int
get_bytes(lua_State *L) {
int pos = luaL_checkinteger(L, 2);
if (pos < 0) {
luaL_error(L, "get_bytes pos (%d) must >= 0", pos);
}
int num_bytes = luaL_checkinteger(L, 3);
if (num_bytes <= 0) {
luaL_error(L, "get_bytes number (%d) must > 0", num_bytes);
}
luaL_Buffer b;
luaL_buffinit(L, &b);
lua_getiuservalue(L, 1, 1);
bitpack_t bp = lua_touserdata(L, -1);
int rv = bitpack_get_bytes(bp, num_bytes, pos, &b);
if (rv == BITPACK_RV_SUCCESS) {
luaL_pushresult(&b);
return 1;
} else {
lua_pushnil(L);
lua_pushinteger(L, rv);
return 2;
}
}
static int
append_bytes(lua_State *L) {
size_t len;
const char *bytes = luaL_checklstring(L, 2, &len);
if (!bytes) {
luaL_error(L, "set_bytes bytes string is nil");
}
lua_getiuservalue(L, 1, 1);
bitpack_t bp = lua_touserdata(L, -1);
int rv = bitpack_append_bytes(bp, bytes, len);
if (rv == BITPACK_RV_SUCCESS) {
lua_pushboolean(L, 1);
return 1;
}
lua_pushboolean(L, 0);
lua_pushinteger(L, rv);
return 2;
}
static int
to_bytes(lua_State *L) {
luaL_Buffer b;
luaL_buffinit(L, &b);
lua_getiuservalue(L, 1, 1);
bitpack_t bp = lua_touserdata(L, -1);
bitpack_to_bytes(bp, &b);
luaL_pushresult(&b);
return 1;
}
static int
gc(lua_State *L) {
lua_getiuservalue(L, 1, 1);
if (lua_islightuserdata(L, -1)) {
bitpack_destroy(lua_touserdata(L, -1));
}
return 0;
}
static int
lmetatable(lua_State *L) {
if (luaL_newmetatable(L, MT_NAME)) {
luaL_Reg l[] = {
{"size", size}, // size of the max set bit
{"alloc_bytes", alloc_bytes}, // bytes of allocated
{"on", on}, // set 1
{"off", off}, // set 0
{"get", get}, // get 0/1
{"set_bytes", set_bytes}, // set bytes data to bp
{"get_bytes", get_bytes}, // get bytes array
{"append_bytes", append_bytes}, // append bytes data to bp
{"to_bytes", to_bytes}, // dump bp to bytes array
{ NULL, NULL }
};
luaL_newlib(L, l);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, gc);
lua_setfield(L, -2, "__gc");
}
return 1;
}
static int
lnew(lua_State *L) {
bitpack_t bp = NULL;
if (lua_type(L, 1) == LUA_TNUMBER) {
int num_bytes = luaL_checkinteger(L, 1);
if (num_bytes <= 0) {
luaL_error(L, "num_bytes (%d) must > 0", num_bytes);
}
bp = bitpack_init(num_bytes);
} else if (lua_type(L, 1) == LUA_TSTRING) {
size_t len;
const char *bytes = luaL_checklstring(L, 1, &len);
bp = bitpack_init_from_bytes(bytes, len);
} else {
bp = bitpack_init_default();
}
if (!bp) {
luaL_error(L, "init bitpack failed");
}
lua_newuserdatauv(L, 0, 1);
lua_pushlightuserdata(L, bp);
lua_setiuservalue(L, -2, 1);
lmetatable(L);
lua_setmetatable(L, -2);
return 1;
}
LUAMOD_API int
luaopen_bitpack(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{ "new", lnew },
{ NULL, NULL },
};
luaL_newlib(L, l);
return 1;
}
#endif