-
Notifications
You must be signed in to change notification settings - Fork 0
/
bson2v8.cc
171 lines (143 loc) · 4.74 KB
/
bson2v8.cc
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
#include "bson2v8.h"
#define V8Symbol v8::String::NewSymbol
using namespace v8;
using namespace std;
// Handle legacy V8 API
namespace bson2v8 {
char*
Utilities::GetCStringFromV8String (Local<Value> value, const char* fallback) {
if (value->IsString()) {
v8::String::AsciiValue string(value);
char *str = (char *) malloc(string.length() + 1);
strcpy(str, *string);
return str;
};
char *str = (char *) malloc(strlen(fallback) + 1);
strcpy(str, fallback);
return str;
}
Local<Object>
Utilities::ParseBSON (const char* buffer) {
Local<Object> object = Object::New();
bson_iterator iterator[1];
bson_iterator_from_buffer(iterator, buffer);
if (array_flag == 1)
object = Array::New();
while (bson_iterator_next(iterator)) {
const int type = bson_iterator_type(iterator);
const char* name = bson_iterator_key(iterator);
char oid_hex[25];
if (type == 0) break;
switch (type) {
case BSON_DOUBLE:
object->Set(String::NewSymbol(name),
Number::New(bson_iterator_double(iterator)));
break;
case BSON_INT:
object->Set(String::NewSymbol(name),
Integer::New(bson_iterator_int(iterator)));
break;
case BSON_LONG:
object->Set(String::NewSymbol(name),
Integer::NewFromUnsigned(bson_iterator_long(iterator)));
break;
case BSON_STRING:
case BSON_SYMBOL:
object->Set(String::NewSymbol(name),
String::New(bson_iterator_string(iterator)));
break;
case BSON_OBJECT:
object->Set(String::NewSymbol(name), ParseBSON(bson_iterator_value(iterator)));
break;
case BSON_ARRAY:
object->Set(String::NewSymbol(name), ParseBSON(bson_iterator_value(iterator), 1));
break;
case BSON_BINDATA:
object->Set(String::NewSymbol(name), String::New("null"));
break;
case BSON_OID:
oid_hex[0] = '\0';
bson_oid_to_string(bson_iterator_oid(iterator), oid_hex);
object->Set(String::NewSymbol(name), String::New(oid_hex));
break;
case BSON_BOOL:
object->Set(String::NewSymbol(name),
Boolean::New(bson_iterator_bool(iterator)));
break;
case BSON_DATE:
object->Set(String::NewSymbol(name),
Date::New(bson_iterator_date(iterator)));
break;
};
};
return object;
}
Local<Object>
Utilities::ParseBSON (const char* buffer) {
return ParseBSON(buffer, 0);
}
Local<Object>
Utilities::ParseBSON (const bson* b, int array_flag) {
return ParseBSON(b->data, array_flag);
}
Local<Object>
Utilities::ParseBSON (const bson* b) {
return ParseBSON(b->data, 0);
}
void
Utilities::ToBSON (Local<Value> in, bson* out) {
Local<Object> global = Context::GetCurrent()->Global();
Local<Object> JSObject = Local<Object>::Cast(global->Get(V8Symbol("Object")));
Local<Function> ObjKeysFunc = Local<Function>::Cast(JSObject->Get(V8Symbol("keys")));
int ownsData = out->ownsData;
uint32_t keys_len = 0;
Local<Value> argv[1] = {in};
Local<Array> keys = Local<Array>::Cast(ObjKeysFunc->Call(global, 1, argv));
if (1 != ownsData) bson_init(out);
if (in->IsArray()) {
keys_len = Local<Array>::Cast(in)->Length();
} else if (in->IsObject()) {
Local<Value> argv[1] = { in };
keys_len = keys->Length();
}
for (uint32_t i=0; i<keys_len; i++) {
char* key; Local<Value> value;
if (in->IsArray()) {
std::ostringstream oss; oss << i;
key = const_cast<char*>(oss.str().c_str());
value = Local<Object>::Cast(in)->Get(i);
}
else if (in->IsObject()) {
key = GetCStringFromV8String(keys->Get(i), "");
value = Local<Object>::Cast(in)->Get(V8Symbol(key));
};
if (value->IsString() || value->IsStringObject()) {
bson_append_string(out, key, GetCStringFromV8String(value, ""));
}
else if (value->IsNumber() || value->IsNumberObject()) {
bson_append_int(out, key, value->Uint32Value());
}
else if (value->IsTrue()) {
bson_append_bool(out, key, true);
}
else if (value->IsFalse()) {
bson_append_bool(out, key, false);
}
else if (value->IsDate()) {
bson_append_date(out, key, Local<Date>::Cast(value)->NumberValue());
}
else if (value->IsArray()) {
bson_append_start_array(out, key);
ToBSON(value, out);
bson_append_finish_array(out);
}
else if (value->IsObject()) {
bson_append_start_object(out, key);
ToBSON(value, out);
bson_append_finish_object(out);
};
};
// finish this bson
if (1 != ownsData) bson_finish(out);
}
}