-
Notifications
You must be signed in to change notification settings - Fork 58
/
utils.cpp
383 lines (350 loc) · 11.6 KB
/
utils.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
383
#include <iostream>
#include <client/dbclient.h>
#include "utils.h"
#include "common.h"
#include <limits.h>
#include <sstream>
using namespace mongo;
extern void push_bsontype_table(lua_State* L, mongo::BSONType bsontype);
void lua_push_value(lua_State *L, const BSONElement &elem);
const char *bson_name(int type);
static void bson_to_array(lua_State *L, const BSONObj &obj) {
BSONObjIterator it = BSONObjIterator(obj);
lua_newtable(L);
int n = 1;
while (it.more()) {
BSONElement elem = it.next();
lua_push_value(L, elem);
lua_rawseti(L, -2, n++);
}
}
static void bson_to_table(lua_State *L, const BSONObj &obj) {
BSONObjIterator it = BSONObjIterator(obj);
lua_newtable(L);
while (it.more()) {
BSONElement elem = it.next();
const char *key = elem.fieldName();
lua_pushstring(L, key);
lua_push_value(L, elem);
lua_rawset(L, -3);
}
}
void lua_push_value(lua_State *L, const BSONElement &elem) {
lua_checkstack(L, 2);
int type = elem.type();
switch(type) {
case mongo::Undefined:
lua_pushnil(L);
break;
case mongo::NumberInt:
lua_pushinteger(L, elem.numberInt());
break;
case mongo::NumberLong:
case mongo::NumberDouble:
lua_pushnumber(L, elem.number());
break;
case mongo::Bool:
lua_pushboolean(L, elem.boolean());
break;
case mongo::String:
lua_pushstring(L, elem.valuestr());
break;
case mongo::Array:
bson_to_array(L, elem.embeddedObject());
break;
case mongo::Object:
bson_to_table(L, elem.embeddedObject());
break;
case mongo::Date:
push_bsontype_table(L, mongo::Date);
lua_pushnumber(L, elem.date());
lua_rawseti(L, -2, 1);
break;
case mongo::Timestamp:
{
push_bsontype_table(L, mongo::Date);
Timestamp_t t = elem.Timestamp();
lua_pushnumber(L, t.seconds() + t.increment());
lua_rawseti(L, -2, 1);
}
break;
case mongo::Symbol:
push_bsontype_table(L, mongo::Symbol);
lua_pushstring(L, elem.valuestr());
lua_rawseti(L, -2, 1);
break;
case mongo::BinData: {
push_bsontype_table(L, mongo::BinData);
int l;
const char* c = elem.binData(l);
lua_pushlstring(L, c, l);
lua_rawseti(L, -2, 1);
break;
}
case mongo::RegEx:
push_bsontype_table(L, mongo::RegEx);
lua_pushstring(L, elem.regex());
lua_rawseti(L, -2, 1);
lua_pushstring(L, elem.regexFlags());
lua_rawseti(L, -2, 2);
break;
case mongo::jstOID:
push_bsontype_table(L, mongo::jstOID);
lua_pushstring(L, elem.__oid().toString().c_str());
lua_rawseti(L, -2, 1);
break;
case mongo::jstNULL:
push_bsontype_table(L, mongo::jstNULL);
break;
case mongo::EOO:
break;
/*default:
luaL_error(L, LUAMONGO_UNSUPPORTED_BSON_TYPE, bson_name(type));*/
}
}
static void lua_append_bson(lua_State *L, const char *key, int stackpos, BSONObjBuilder *builder, int ref) {
int type = lua_type(L, stackpos);
if (type == LUA_TTABLE) {
if (stackpos < 0) stackpos = lua_gettop(L) + stackpos + 1;
lua_checkstack(L, 3);
int bsontype_found = luaL_getmetafield(L, stackpos, "__bsontype");
if (!bsontype_found) {
// not a special bsontype
// handle as a regular table, iterating keys
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
lua_pushvalue(L, stackpos);
lua_rawget(L, -2);
if (lua_toboolean(L, -1)) { // do nothing if the same table encountered
lua_pop(L, 2);
} else {
lua_pop(L, 1);
lua_pushvalue(L, stackpos);
lua_pushboolean(L, 1);
lua_rawset(L, -3);
lua_pop(L, 1);
BSONObjBuilder b;
bool dense = true;
int len = 0;
for (lua_pushnil(L); lua_next(L, stackpos); lua_pop(L, 1)) {
++len;
if ((lua_type(L, -2) != LUA_TNUMBER) || (lua_tointeger(L, -2) != len)) {
lua_pop(L, 2);
dense = false;
break;
}
}
if (dense) {
for (int i = 0; i < len; i++) {
lua_rawgeti(L, stackpos, i+1);
std::stringstream ss;
ss << i;
lua_append_bson(L, ss.str().c_str(), -1, &b, ref);
lua_pop(L, 1);
}
builder->appendArray(key, b.obj());
} else {
for (lua_pushnil(L); lua_next(L, stackpos); lua_pop(L, 1)) {
switch (lua_type(L, -2)) { // key type
case LUA_TNUMBER: {
std::stringstream ss;
ss << lua_tonumber(L, -2);
lua_append_bson(L, ss.str().c_str(), -1, &b, ref);
break;
}
case LUA_TSTRING: {
lua_append_bson(L, lua_tostring(L, -2), -1, &b, ref);
break;
}
}
}
builder->append(key, b.obj());
}
}
} else {
int bson_type = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_rawgeti(L, -1, 1);
switch (bson_type) {
case mongo::Date:
builder->appendDate(key, lua_tonumber(L, -1));
break;
case mongo::Timestamp:
builder->appendTimestamp(key);
break;
case mongo::RegEx: {
const char* regex = lua_tostring(L, -1);
lua_rawgeti(L, -2, 2); // options
const char* options = lua_tostring(L, -1);
lua_pop(L, 1);
if (regex && options) builder->appendRegex(key, regex, options);
break;
}
case mongo::NumberInt:
builder->append(key, static_cast<int32_t>(lua_tointeger(L, -1)));
break;
case mongo::NumberLong:
builder->append(key, static_cast<long long int>(lua_tonumber(L, -1)));
break;
case mongo::Symbol: {
const char* c = lua_tostring(L, -1);
if (c) builder->appendSymbol(key, c);
break;
}
case mongo::BinData: {
size_t l;
const char* c = lua_tolstring(L, -1, &l);
if (c) builder->appendBinData(key, l, mongo::BinDataGeneral, c);
break;
}
case mongo::jstOID: {
OID oid;
const char* c = lua_tostring(L, -1);
if (c) {
oid.init(c);
builder->appendOID(key, &oid);
}
break;
}
case mongo::jstNULL:
builder->appendNull(key);
break;
/*default:
luaL_error(L, LUAMONGO_UNSUPPORTED_BSON_TYPE, luaL_typename(L, stackpos));*/
}
lua_pop(L, 1);
}
} else if (type == LUA_TNIL) {
builder->appendNull(key);
} else if (type == LUA_TNUMBER) {
double numval = lua_tonumber(L, stackpos);
if ((numval == floor(numval)) && fabs(numval)< INT_MAX ) {
// The numeric value looks like an integer, treat it as such.
// This is closer to how JSON datatypes behave.
int intval = lua_tointeger(L, stackpos);
builder->append(key, static_cast<int32_t>(intval));
} else {
builder->append(key, numval);
}
} else if (type == LUA_TBOOLEAN) {
builder->appendBool(key, lua_toboolean(L, stackpos));
} else if (type == LUA_TSTRING) {
builder->append(key, lua_tostring(L, stackpos));
}/* else {
luaL_error(L, LUAMONGO_UNSUPPORTED_LUA_TYPE, luaL_typename(L, stackpos));
}*/
}
void bson_to_lua(lua_State *L, const BSONObj &obj) {
if (obj.isEmpty()) {
lua_pushnil(L);
} else {
bson_to_table(L, obj);
}
}
// stackpos must be relative to the bottom, i.e., not negative
void lua_to_bson(lua_State *L, int stackpos, BSONObj &obj) {
BSONObjBuilder builder;
lua_newtable(L);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
for (lua_pushnil(L); lua_next(L, stackpos); lua_pop(L, 1)) {
switch (lua_type(L, -2)) { // key type
case LUA_TNUMBER: {
std::ostringstream ss;
ss << lua_tonumber(L, -2);
lua_append_bson(L, ss.str().c_str(), -1, &builder, ref);
break;
}
case LUA_TSTRING: {
lua_append_bson(L, lua_tostring(L, -2), -1, &builder, ref);
break;
}
}
}
luaL_unref(L, LUA_REGISTRYINDEX, ref);
obj = builder.obj();
}
const char *bson_name(int type) {
const char *name;
switch(type) {
case mongo::EOO:
name = "EndOfObject";
break;
case mongo::NumberDouble:
name = "NumberDouble";
break;
case mongo::String:
name = "String";
break;
case mongo::Object:
name = "Object";
break;
case mongo::Array:
name = "Array";
break;
case mongo::BinData:
name = "BinData";
break;
case mongo::Undefined:
name = "Undefined";
break;
case mongo::jstOID:
name = "ObjectID";
break;
case mongo::Bool:
name = "Bool";
break;
case mongo::Date:
name = "Date";
break;
case mongo::jstNULL:
name = "NULL";
break;
case mongo::RegEx:
name = "RegEx";
break;
case mongo::DBRef:
name = "DBRef";
break;
case mongo::Code:
name = "Code";
break;
case mongo::Symbol:
name = "Symbol";
break;
case mongo::CodeWScope:
name = "CodeWScope";
break;
case mongo::NumberInt:
name = "NumberInt";
break;
case mongo::Timestamp:
name = "Timestamp";
break;
case mongo::NumberLong:
name = "NumberLong";
break;
default:
name = "UnknownType";
}
return name;
}
/* this was removed in Lua 5.2 */
LUALIB_API int luaL_typeerror (lua_State *L, int narg, const char *tname) {
const char *msg;
msg = lua_pushfstring(L, "%s expected, got %s",
tname, lua_typename(L, narg));
return luaL_argerror(L, narg, msg);
}
#if LUA_VERSION_NUM < 502
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup+1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup+1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
#endif