Skip to content

Commit

Permalink
Binary js serializes correctly using c++
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Dec 26, 2011
1 parent c4fcb93 commit 3cd51b0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
70 changes: 42 additions & 28 deletions external-libs/bson/bson.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ Handle<Value> BSON::New(const Arguments &args) {
bson->_longLowString = Persistent<String>::New(String::New("low_"));
bson->_longHighString = Persistent<String>::New(String::New("high_"));
bson->_objectIDidString = Persistent<String>::New(String::New("id"));
bson->_binaryPositionString = Persistent<String>::New(String::New("position"));
bson->_binarySubTypeString = Persistent<String>::New(String::New("sub_type"));
bson->_binaryBufferString = Persistent<String>::New(String::New("buffer"));

// total number of found classes
uint32_t numberOfClasses = 0;
Expand Down Expand Up @@ -3989,35 +3992,46 @@ uint32_t BSON::serializeJS(BSON *bson, char *serialized_object, uint32_t index,
written = DecodeWrite((serialized_object + index), len, idString, BINARY);
// Adjust the index
index = index + 12;
} else if(bson->binaryString->StrictEquals(constructorString)) {
// Save the string at the offset provided
*(serialized_object + originalIndex) = BSON_DATA_BINARY;

// Let's get the binary object
Local<Object> binaryObject = value->ToObject();

// Grab the size(position of the binary)
uint32_t position = value->ToObject()->Get(bson->_binaryPositionString)->ToUint32()->Value();
// Grab the subtype
uint32_t subType = value->ToObject()->Get(bson->_binarySubTypeString)->ToUint32()->Value();
// Grab the buffer object
Local<Object> bufferObj = value->ToObject()->Get(bson->_binaryBufferString)->ToObject();

// Buffer data pointers
char *data;
uint32_t length;

// Unpack the buffer variable
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(bufferObj);
data = buffer->data();
length = buffer->length();
#else
data = Buffer::Data(bufferObj);
length = Buffer::Length(bufferObj);
#endif

// } else if(Binary::HasInstance(value)) { // || (value->IsObject() && value->ToObject()->GetConstructorName()->Equals(String::New("Binary")))) {
// // Save the string at the offset provided
// *(serialized_object + index) = BSON_DATA_BINARY;
// // Adjust writing position for the first byte
// index = index + 1;
// // Convert name to char*
// ssize_t len = DecodeBytes(name, UTF8);
// ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8);
// // Add null termiation for the string
// *(serialized_object + index + len) = '\0';
// // Adjust the index
// index = index + len + 1;
//
// // Unpack the object and encode
// Local<Object> obj = value->ToObject();
// Binary *binary_obj = Binary::Unwrap<Binary>(obj);
// // Let's write the content to the char* array
// BSON::write_int32((serialized_object + index), binary_obj->index);
// // Adjust index
// index = index + 4;
// // Write subtype
// *(serialized_object + index) = (char)binary_obj->sub_type;
// // Adjust index
// index = index + 1;
// // Write binary content
// memcpy((serialized_object + index), binary_obj->data, binary_obj->index);
// // Adjust index.rar">_</a>
// index = index + binary_obj->index;
// Write the size of the buffer out
BSON::write_int32((serialized_object + index), position);
// Adjust index
index = index + 4;
// Write subtype
*(serialized_object + index) = (char)subType;
// Adjust index
index = index + 1;
// Write binary content
memcpy((serialized_object + index), data, position);
// Adjust index.rar">_</a>
index = index + position;
// } else if(DBRef::HasInstance(value)) { // || (value->IsObject() && value->ToObject()->GetConstructorName()->Equals(String::New("exports.DBRef")))) {
// // Unpack the dbref
// Local<Object> dbref = value->ToObject();
Expand Down
3 changes: 3 additions & 0 deletions external-libs/bson/bson.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class BSON : public ObjectWrap {
Persistent<String> _longLowString;
Persistent<String> _longHighString;
Persistent<String> _objectIDidString;
Persistent<String> _binaryPositionString;
Persistent<String> _binarySubTypeString;
Persistent<String> _binaryBufferString;

// Decode function
static Handle<Value> decodeLong(char *data, uint32_t index);
Expand Down
18 changes: 10 additions & 8 deletions external-libs/bson/test/test_shared_objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var doc = {
minKey:new MinKey(),
maxKey:new MaxKey(),
// symbol:new Symbol("hello"),
// binary:new Binary(new Buffer(256)),
binary:new Binary(new Buffer('Hello world')),
objectId: ObjectID.createFromHexString("4ef48c19a9af58a399000001"),
// code: new Code("function() {}", {}),
// code_w_scope: new Code("function() {}", {c:1}),
Expand All @@ -61,20 +61,20 @@ var doc2 = {
doc:'hello',
long:Long2.fromNumber(100),
timestamp:Timestamp2.fromNumber(10000),
// minKey:new MinKey2(),
// maxKey:new MaxKey2(),
minKey:new MinKey2(),
maxKey:new MaxKey2(),
// symbol:new Symbol2("hello"),
// binary:new Binary2(new Buffer(256)),
binary:new Binary2(new Buffer('Hello world')),
objectId: ObjectID2.createFromHexString("4ef48c19a9af58a399000001"),
// code: new Code2("function() {}", {}),
// code_w_scope: new Code2("function() {}", {c:1}),
// dbref: new DBRef2('collection', new ObjectID2(), 'db'),
object: {
a: 1,
b: 'hello',
// c: {
// long:Long2.fromNumber(100)
// }
c: {
long:Long2.fromNumber(100)
}
}
};

Expand All @@ -84,7 +84,9 @@ var doc2 = {
// Serialize using the c++ driver
var doc2Bin = bsonC.serialize(doc, false, true);

console.dir(bsonC.deserialize(doc2Bin));
var r = bsonC.deserialize(doc2Bin);
console.dir(r);
// console.log(r.binary.value())

// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
Expand Down

0 comments on commit 3cd51b0

Please sign in to comment.