-
Notifications
You must be signed in to change notification settings - Fork 14
/
bson.cc
224 lines (185 loc) · 6.37 KB
/
bson.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
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
#include <v8.h>
#include <node.h>
#include <node_object_wrap.h>
extern "C" {
#define MONGO_HAVE_STDINT
#include <bson.h>
}
#include "bson.h"
using namespace v8;
Persistent<FunctionTemplate> ObjectID::constructor_template;
void ObjectID::Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(ObjectID::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("ObjectID"));
NODE_SET_PROTOTYPE_METHOD(ObjectID::constructor_template, "toString", ObjectID::ToString);
target->Set(String::NewSymbol("ObjectID"), constructor_template->GetFunction());
}
void
ObjectID::str(char *str) {
bson_oid_to_string(&oid, str);
}
Handle<Value>
ObjectID::ToString(const Arguments &args) {
ObjectID *o = ObjectWrap::Unwrap<ObjectID>(args.This());
HandleScope scope;
char hex[25];
o->str(hex);
return String::New(hex);
}
const char *
ToCString(const String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
inline void
encodeString(bson_buffer *bb, const char *name, const Local<Value> element) {
String::Utf8Value v(element);
const char *value(ToCString(v));
bson_append_string(bb, name, value);
}
inline void
encodeNumber(bson_buffer *bb, const char *name, const Local<Value> element) {
double value(element->NumberValue());
bson_append_double(bb, name, value);
}
inline void
encodeInteger(bson_buffer *bb, const char *name, const Local<Value> element) {
int value(element->NumberValue());
bson_append_int(bb, name, value);
}
inline void
encodeBoolean(bson_buffer *bb, const char *name, const Local<Value> element) {
bool value(element->IsTrue());
bson_append_bool(bb, name, value);
}
bson encodeObject(const Local<Value> element) {
HandleScope scope;
bson_buffer bb;
bson_buffer_init(&bb);
Local<Object> object = element->ToObject();
Local<Array> properties = object->GetPropertyNames();
for (int i = 0; i < properties->Length(); i++) {
// get the property name and value
Local<Value> prop_name = properties->Get(Integer::New(i));
Local<Value> prop_val = object->Get(prop_name->ToString());
// convert the property name to a c string
String::Utf8Value n(prop_name);
const char *pname = ToCString(n);
// append property using appropriate appender
if (prop_val->IsString()) {
encodeString(&bb, pname, prop_val);
}
else if (prop_val->IsInt32()) {
encodeInteger(&bb, pname, prop_val);
}
else if (prop_val->IsNumber()) {
encodeNumber(&bb, pname, prop_val);
}
else if (prop_val->IsBoolean()) {
encodeBoolean(&bb, pname, prop_val);
}
else if (prop_val->IsObject()
&& ObjectID::constructor_template->HasInstance(prop_val)) {
// get at the delicious wrapped object centre
Local<Object> obj = prop_val->ToObject();
assert(!obj.IsEmpty());
assert(obj->InternalFieldCount() > 0);
ObjectID *o = static_cast<ObjectID*>(Handle<External>::Cast(
obj->GetInternalField(0))->Value());
}
else if (prop_val->IsObject()) {
bson bson(encodeObject(prop_val));
bson_append_bson(&bb, pname, &bson);
bson_destroy(&bson);
}
}
bson bson;
bson_from_buffer(&bson, &bb);
return bson;
}
Handle<Value>
encode(const Arguments &args) {
// TODO assert args.length > 0
// TODO assert args.type == Object
HandleScope scope;
bson bson(encodeObject(args[0]));
return node::Encode(bson.data, bson_size(&bson), node::BINARY);
}
Local<Value>
decodeObjectStr(const char *buf) {
HandleScope scope;
bson_iterator it;
bson_iterator_init(&it, buf);
Local<Object> obj = Object::New();
while (bson_iterator_next(&it)) {
bson_type type = bson_iterator_type(&it);
const char *key = bson_iterator_key(&it);
switch (type) {
case bson_string: {
const char *val = bson_iterator_string(&it);
obj->Set(String::New(key), String::New(val));
}
break;
case bson_object: {
bson bson;
bson_iterator_subobject(&it, &bson);
Handle<Value> sub = decodeObjectStr(bson.data);
obj->Set(String::New(key), sub);
}
break;
case bson_oid: {
char hex_oid[25];
bson_oid_t *oid = bson_iterator_oid(&it);
bson_oid_to_string(oid, hex_oid);
Handle<Value> argv[1];
argv[0] = String::New(hex_oid);
obj->Set(String::New(key),
ObjectID::constructor_template
->GetFunction()->NewInstance(1, argv));
}
break;
case bson_double: {
double val = bson_iterator_double_raw(&it);
obj->Set(String::New(key), Number::New(val));
}
break;
case bson_int: {
int val = bson_iterator_int(&it);
obj->Set(String::New(key), Number::New(val));
}
break;
case bson_bool:
{
bson_bool_t val = bson_iterator_bool(&it);
obj->Set(String::New(key), Boolean::New(val));
}
break;
}
}
return scope.Close(obj);
}
Handle<Value>
decodeObject(const Local<Value> str) {
HandleScope scope;
size_t buflen = str->ToString()->Length();
char buf[buflen];
node::DecodeWrite(buf, buflen, str, node::BINARY);
return decodeObjectStr(buf);
}
Handle<Value>
decode(const Arguments &args) {
HandleScope scope;
return decodeObject(args[0]);
}
// extern "C" void
// init (Handle<Object> target) {
// HandleScope scope;
// target->Set(
// String::New("encode"),
// FunctionTemplate::New(encode)->GetFunction());
// target->Set(
// String::New("decode"),
// FunctionTemplate::New(decode)->GetFunction());
// }