Skip to content

Commit

Permalink
SERVER-776 NumberLong in v8
Browse files Browse the repository at this point in the history
  • Loading branch information
astaple committed Jun 23, 2010
1 parent bf36eeb commit 1e61dd9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
8 changes: 8 additions & 0 deletions jstests/numberlong.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
assert.eq.automsg( "0", "new NumberLong()" );

n = new NumberLong( 4 );
assert.eq.automsg( "4", "n" );
assert.eq.automsg( "4", "n.toNumber()" );
Expand All @@ -9,6 +11,9 @@ a.a = n;
p = tojson( a );
assert.eq.automsg( "'{ \"a\" : NumberLong( 4 ) }'", "p" );

assert.eq.automsg( "NumberLong( 4 )", "eval( tojson( NumberLong( 4 ) ) )" );
assert.eq.automsg( "a", "eval( tojson( a ) )" );

n = new NumberLong( -4 );
assert.eq.automsg( "-4", "n" );
assert.eq.automsg( "-4", "n.toNumber()" );
Expand All @@ -31,6 +36,9 @@ a.a = n;
p = tojson( a );
assert.eq.automsg( "'{ \"a\" : NumberLong( \"11111111111111111\" ) }'", "p" );

assert.eq.automsg( "NumberLong( '11111111111111111' )", "eval( tojson( NumberLong( '11111111111111111' ) ) )" );
assert.eq.automsg( "a", "eval( tojson( a ) )" );

n = new NumberLong( "-11111111111111111" );
assert.eq.automsg( "-11111111111111112", "n.toNumber()" );
assert.eq.automsg( "-11111111111111108", "n + 4" );
Expand Down
2 changes: 1 addition & 1 deletion scripting/sm_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ namespace mongo {
char *endPtr = 0;
errno = 0;
long long n = strtoll( numStr, &endPtr, 10 );
smuassert( cx , "could not convert numeric representation of string to long" , *endPtr == 0 && errno != ERANGE );
smuassert( cx , "could not convert string to long long" , *endPtr == 0 && errno != ERANGE );
c.makeLongObj( n, obj );
}

Expand Down
40 changes: 34 additions & 6 deletions scripting/v8_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,8 @@ namespace mongo {

v8::Handle<v8::Value> numberLongInit( const v8::Arguments& args ) {

if (args.Length() != 1 && args.Length() != 3) {
return v8::ThrowException( v8::String::New( "NumberLong needs 1 or 3 arguments" ) );
if (args.Length() != 0 && args.Length() != 1 && args.Length() != 3) {
return v8::ThrowException( v8::String::New( "NumberLong needs 0, 1 or 3 arguments" ) );
}

v8::Handle<v8::Object> it = args.This();
Expand All @@ -642,9 +642,33 @@ namespace mongo {
v8::Function* f = getNamedCons( "NumberLong" );
it = f->NewInstance();
}

it->Set( v8::String::New( "floatApprox" ) , args[0] );
if ( args.Length() == 3 ) {

if ( args.Length() == 0 ) {
it->Set( v8::String::New( "floatApprox" ), v8::Number::New( 0 ) );
} else if ( args.Length() == 1 ) {
if ( args[ 0 ]->IsNumber() ) {
it->Set( v8::String::New( "floatApprox" ), args[ 0 ] );
} else {
v8::String::Utf8Value data( args[ 0 ] );
string num = *data;
const char *numStr = num.c_str();
char *endPtr = 0;
errno = 0;
long long n = strtoll( numStr, &endPtr, 10 );
if ( !( *endPtr == 0 && errno != ERANGE ) ) {
return v8::ThrowException( v8::String::New( "could not convert string to long long" ) );
}
unsigned long long val = n;
if ( (long long)val == (long long)(double)(long long)(val) ) {
it->Set( v8::String::New( "floatApprox" ), v8::Number::New( (double)(long long)( val ) ) );
} else {
it->Set( v8::String::New( "floatApprox" ), v8::Number::New( (double)(long long)( val ) ) );
it->Set( v8::String::New( "top" ), v8::Integer::New( val >> 32 ) );
it->Set( v8::String::New( "bottom" ), v8::Integer::New( (unsigned long)(val & 0x00000000ffffffff) ) );
}
}
} else {
it->Set( v8::String::New( "floatApprox" ) , args[0] );
it->Set( v8::String::New( "top" ) , args[1] );
it->Set( v8::String::New( "bottom" ) , args[2] );
}
Expand Down Expand Up @@ -690,7 +714,11 @@ namespace mongo {
long long val = numberLongVal( it );

stringstream ss;
ss << val;
if ( val == (long long)(double)( val ) ) {
ss << "NumberLong( " << double( val ) << " )";
} else {
ss << "NumberLong( \"" << val << "\" )";
}
string ret = ss.str();
return v8::String::New( ret.c_str() );
}
Expand Down

0 comments on commit 1e61dd9

Please sign in to comment.