Skip to content

Commit

Permalink
Consistent behaviour for Long in C++ parser and Long in JS parser. fi…
Browse files Browse the repository at this point in the history
…nally
  • Loading branch information
christkv committed Oct 4, 2011
1 parent ebe746f commit e1c472d
Show file tree
Hide file tree
Showing 4 changed files with 881 additions and 878 deletions.
21 changes: 10 additions & 11 deletions external-libs/bson/bson.cc
Expand Up @@ -602,21 +602,21 @@ uint32_t BSON::serialize(char *serialized_object, uint32_t index, Handle<Value>
// Adjust index for double
index = index + 8;
} else if(l_number <= BSON_INT32_MAX && l_number >= BSON_INT32_MIN) {
printf("--------------------------------------------------------------- 2\n");
// printf("--------------------------------------------------------------- 2\n");
// Smaller than 32 bit, write as 32 bit value
BSON::write_int32(serialized_object + index, value->ToInt32()->Value());
// Adjust the size of the index
index = index + 4;
} else if(l_number <= (2^53) && l_number >= (-2^53)) {
printf("--------------------------------------------------------------- 3\n");
// printf("--------------------------------------------------------------- 3\n");
// Write the double to the char array
BSON::write_double((serialized_object + index), d_number);
// Adjust type to be double
*(serialized_object + first_pointer) = BSON_DATA_NUMBER;
// Adjust index for double
index = index + 8;
} else {
printf("--------------------------------------------------------------- 4\n");
// printf("--------------------------------------------------------------- 4\n");
BSON::write_double((serialized_object + index), d_number);
// BSON::write_int64((serialized_object + index), d_number);
// BSON::write_int64((serialized_object + index), l_number);
Expand Down Expand Up @@ -1196,8 +1196,7 @@ Handle<Value> BSON::deserialize(char *data, bool is_array_item) {
// Free up the memory
free(string_name);
} else if(type == BSON_DATA_LONG) {
printf("=================================================== 1\n");

// printf("=================================================== 1\n");
// Read the null terminated index String
char *string_name = BSON::extract_string(data, index);
if(string_name == NULL) return VException("Invalid C String found.");
Expand Down Expand Up @@ -1734,16 +1733,16 @@ Handle<Value> BSON::decodeLong(char *data, uint32_t index) {
int64_t value = 0;
memcpy(&value, (data + index), 8);

printf("==================================== %llu\n", value);
// printf("==================================== %llu\n", value);

if(value >= (-2^53) && value <= (2^53)) {
printf("----------------------------------------------- 2\n");

}
// if(value >= (-2^53) && value <= (2^53)) {
// printf("----------------------------------------------- 2\n");
//
// }

// If value is < 2^53 and >-2^53
if((highBits < 0x200000 || (highBits == 0x200000 && lowBits == 0)) && highBits >= -0x200000) {
printf("----------------------------------------------- 1\n");
// printf("----------------------------------------------- 1\n");
int64_t finalValue = 0;
memcpy(&finalValue, (data + index), 8);
return scope.Close(Number::New(finalValue));
Expand Down
15 changes: 14 additions & 1 deletion lib/mongodb/bson/bson.js
Expand Up @@ -27,6 +27,10 @@ BSON.BSON_INT32_MIN = -0x80000000;
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.

// Internal long versions
var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double.
var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double.

// BSON DATA TYPES
BSON.BSON_DATA_NUMBER = 1;
BSON.BSON_DATA_STRING = 2;
Expand Down Expand Up @@ -289,7 +293,7 @@ BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object,
var highBits = long.getHighBits();

// If the number is the same after long conversion force double
if(value.toString() == long.toNumber().toString()) {
if(value.toString() == long.toNumber().toString() || value >= Long.MAX_VALUE || value <= Long.MIN_VALUE) {
buffer[startIndex] = BSON.BSON_DATA_NUMBER;
// Write float
ieee754.writeIEEE754(buffer, value, index, 'little', 52, 8);
Expand Down Expand Up @@ -1131,6 +1135,15 @@ BSON.deserialize = function(data, options) {
if (type === BSON.BSON_DATA_LONG) {
// Convert to long
value = new Long(low_bits, high_bits);
// Convert to number


if(value.lessThanOrEqual(JS_INT_MAX_LONG) && value.greaterThanOrEqual(JS_INT_MIN_LONG)) {
// debug("--------------------------------------------------------------- 0")
value = value.toNumber();
}


// convert back to number and compare the converted value with the original
// var value2 = value.toNumber();
//
Expand Down
5 changes: 0 additions & 5 deletions lib/mongodb/goog/math/long.js
Expand Up @@ -100,19 +100,14 @@ exports.Long.fromNumber = function(value) {
// var debug = require('sys').debug;

if (isNaN(value) || !isFinite(value)) {
// debug("----------------------------------- 0")
return exports.Long.ZERO;
} else if (value <= -exports.Long.TWO_PWR_63_DBL_) {
// debug("----------------------------------- 1")
return exports.Long.MIN_VALUE;
} else if (value + 1 >= exports.Long.TWO_PWR_63_DBL_) {
// debug("----------------------------------- 2")
return exports.Long.MAX_VALUE;
} else if (value < 0) {
// debug("----------------------------------- 3")
return exports.Long.fromNumber(-value).negate();
} else {
// debug("----------------------------------- 1")
return new exports.Long(
(value % exports.Long.TWO_PWR_32_DBL_) | 0,
(value / exports.Long.TWO_PWR_32_DBL_) | 0);
Expand Down

0 comments on commit e1c472d

Please sign in to comment.