forked from moai/luamongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_bsontypes.cpp
383 lines (319 loc) · 9.27 KB
/
mongo_bsontypes.cpp
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <iostream>
#include <client/dbclient.h>
#include "utils.h"
#include "common.h"
using namespace mongo;
void push_bsontype_table(lua_State* L, mongo::BSONType bsontype);
extern const char *bson_name(int type);
extern void lua_to_bson(lua_State *L, int stackpos, BSONObj &obj);
extern void bson_to_lua(lua_State *L, const BSONObj &obj);
static int bson_type_Date(lua_State *L) {
push_bsontype_table(L, mongo::Date);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1); // t[1] = function arg #1
return 1;
}
static int bson_type_Timestamp(lua_State*L) {
push_bsontype_table(L, mongo::Timestamp);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1); // t[1] = function arg #1
return 1;
}
static int bson_type_RegEx(lua_State *L) {
push_bsontype_table(L, mongo::RegEx);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1); // t[1] = function arg #1
lua_pushvalue(L, 2);
lua_rawseti(L, -2, 2); // set t[2] = function arg #2
return 1;
}
static int bson_type_NumberInt(lua_State *L) {
push_bsontype_table(L, mongo::NumberInt);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1); // t[1] = function arg #1
return 1;
}
static int bson_type_NumberLong(lua_State *L) {
push_bsontype_table(L, mongo::NumberLong);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1); // t[1] = function arg #1
return 1;
}
static int bson_type_Symbol(lua_State *L) {
push_bsontype_table(L, mongo::Symbol);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1); // t[1] = function arg #1
return 1;
}
static int bson_type_BinData(lua_State *L) {
push_bsontype_table(L, mongo::BinData);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1); // t[1] = function arg #1
return 1;
}
static int bson_type_ObjectID(lua_State *L) {
if(lua_gettop(L) == 0)
lua_pushstring(L, mongo::OID::gen().toString().data());
push_bsontype_table(L, mongo::jstOID);
lua_pushvalue(L, 1);
lua_rawseti(L, -2, 1); // t[1] = function arg #1
return 1;
}
static int bson_type_NULL(lua_State *L) {
push_bsontype_table(L, mongo::jstNULL);
// no arg
return 1;
}
static int integer_value(lua_State *L) {
if (lua_gettop(L) > 1) {
luaL_checkint(L, 2);
lua_pushvalue(L, 2);
lua_rawseti(L, 1, 1);
return 0;
}
lua_rawgeti(L, 1, 1);
return 1;
}
static int number_value(lua_State *L) {
if (lua_gettop(L) > 1) {
luaL_checknumber(L, 2);
lua_pushvalue(L, 2);
lua_rawseti(L, 1, 1);
return 0;
}
lua_rawgeti(L, 1, 1);
return 1;
}
static int string_value(lua_State *L) {
if (lua_gettop(L) > 1) {
luaL_checkstring(L, 2);
lua_pushvalue(L, 2);
lua_rawseti(L, 1, 1);
return 0;
}
lua_rawgeti(L, 1, 1);
return 1;
}
static int null_value(lua_State *L) {
lua_pushnil(L);
return 1;
}
static int stringpair_value(lua_State *L) {
if (lua_gettop(L) > 1) {
luaL_checkstring(L, 2);
luaL_checkstring(L, 3);
lua_pushvalue(L, 2);
lua_rawseti(L, 1, 1);
lua_pushvalue(L, 3);
lua_rawseti(L, 1, 2);
return 0;
}
lua_rawgeti(L, 1, 1);
lua_rawgeti(L, 1, 2);
return 2;
}
static int generic_tostring(lua_State *L) {
lua_rawgeti(L, 1, 1);
if (!lua_isstring(L, -1)) lua_pushstring(L, "nil");
return 1;
}
static int longlong_tostring(lua_State *L) {
lua_rawgeti(L, 1, 1);
lua_Number num = lua_tonumber(L, -1);
char numstr[64];
int len = snprintf(numstr, 64, "%.f", num);
lua_pushlstring(L, numstr, len);
return 1;
}
static int date_tostring(lua_State *L) {
char datestr[64];
lua_rawgeti(L, 1, 1);
time_t t = (time_t)(lua_tonumber(L, -1)/1000);
#if defined(_WIN32)
ctime_s(datestr, 64, &t);
#else
ctime_r(&t,datestr);
#endif
datestr[24] = 0; // don't want the \n
lua_pushstring(L, datestr);
return 1;
}
static int regex_tostring(lua_State *L) {
lua_rawgeti(L, 1, 1);
lua_rawgeti(L, 1, 2);
lua_pushfstring(L, "/%s/%s", lua_tostring(L, -2), lua_tostring(L, -1));
return 1;
}
static int null_tostring(lua_State *L) {
lua_pushstring(L, "NULL");
return 1;
}
// TODO:
// all of this should be in Lua so it can get JIT wins
// bind the bson typeids
// each type could have its cached metatable (from registry)
// all these types are represented as tables
// the metatable entry __bsontype dictates the type
// the t[1] represents the object itself, with some types using other fields
void push_bsontype_table(lua_State* L, mongo::BSONType bsontype) {
lua_newtable(L);
lua_newtable(L);
lua_pushstring(L, "__bsontype");
lua_pushinteger(L, bsontype);
lua_settable(L, -3);
lua_pushstring(L, "__call");
switch(bsontype) {
case mongo::NumberInt:
lua_pushcfunction(L, integer_value);
break;
case mongo::NumberLong:
case mongo::Date:
case mongo::Timestamp:
lua_pushcfunction(L, number_value);
break;
case mongo::Symbol:
case mongo::BinData:
case mongo::jstOID:
lua_pushcfunction(L, string_value);
break;
case mongo::RegEx:
lua_pushcfunction(L, stringpair_value);
break;
case mongo::jstNULL:
lua_pushcfunction(L, null_value);
break;
default:
;
}
lua_settable(L, -3);
lua_pushstring(L, "__tostring");
switch(bsontype) {
case mongo::NumberInt:
case mongo::Symbol:
case mongo::BinData:
case mongo::jstOID:
case mongo::Timestamp:
lua_pushcfunction(L, generic_tostring);
break;
case mongo::NumberLong:
lua_pushcfunction(L, longlong_tostring);
break;
case mongo::Date:
lua_pushcfunction(L, date_tostring);
break;
case mongo::RegEx:
lua_pushcfunction(L, regex_tostring);
break;
case mongo::jstNULL:
lua_pushcfunction(L, null_tostring);
break;
default:
;
}
lua_settable(L, -3);
lua_setmetatable(L, -2);
}
/*
* typename = mongo.type(obj)
*/
static int bson_type_name(lua_State *L) {
if (lua_istable(L, 1)) {
int bsontype_found = luaL_getmetafield(L, 1, "__bsontype");
if (bsontype_found) {
int bson_type = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_pushfstring(L, "%s.%s", LUAMONGO_ROOT, bson_name(bson_type));
} else {
lua_pushstring(L, luaL_typename(L, 1));
}
} else {
lua_pushstring(L, luaL_typename(L, 1));
}
return 1;
}
/*
* num = mongo.tonumber(obj)
*/
static int bson_tonumber(lua_State *L) {
int base = luaL_optint(L, 2, 10);
if (base == 10) { /* standard conversion */
luaL_checkany(L, 1);
if (lua_isnumber(L, 1)) {
lua_pushnumber(L, lua_tonumber(L, 1));
return 1;
} else if (lua_istable(L, 1)) {
int bsontype_found = luaL_getmetafield(L, 1, "__bsontype");
if (bsontype_found) {
lua_rawgeti(L, 1, 1);
if (lua_isnumber(L, -1)) {
lua_pushnumber(L, lua_tonumber(L, -1));
return 1;
}
lua_pop(L, 1);
}
}
} else {
const char *s1 = luaL_checkstring(L, 1);
char *s2;
unsigned long n;
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
n = strtoul(s1, &s2, base);
if (s1 != s2) { /* at least one valid digit? */
while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
if (*s2 == '\0') { /* no invalid trailing characters? */
lua_pushnumber(L, (lua_Number)n);
return 1;
}
}
}
lua_pushnil(L); /* else not a number */
return 1;
}
static int bson_tojson(lua_State *L) {
int resultcount = 1;
BSONObj obj;
if (lua_istable(L, 1)) {
lua_to_bson(L, 1, obj);
lua_pushstring(L, obj.toString().c_str());
} else {
lua_pushnil(L);
lua_pushfstring(L, "Argument is not a table");
resultcount = 2;
}
return resultcount;
}
static int bson_fromjson(lua_State *L) {
const char *json = luaL_checkstring(L, 1);
int resultcount = 1;
BSONObj obj;
try {
bson_to_lua(L, fromjson(json));
} catch (std::exception &e) {
lua_pushnil(L);
lua_pushfstring(L, "Error parsing JSON: %s", e.what());
resultcount = 2;
}
return resultcount;
}
int mongo_bsontypes_register(lua_State *L) {
static const luaL_Reg bsontype_methods[] = {
{"Date", bson_type_Date},
{"Timestamp", bson_type_Timestamp},
{"RegEx", bson_type_RegEx},
{"NumberInt", bson_type_NumberInt},
{"NumberLong", bson_type_NumberLong},
{"Symbol", bson_type_Symbol},
{"BinData", bson_type_BinData},
{"ObjectId", bson_type_ObjectID},
{"NULL", bson_type_NULL},
// Utils
{"type", bson_type_name},
{"tonumber", bson_tonumber},
{"tojson", bson_tojson},
{"fromjson", bson_fromjson},
{NULL, NULL}
};
//luaL_register(L, LUAMONGO_ROOT, bsontype_methods);
luaL_newlib(L, bsontype_methods);
return 1;
}