From b42dd5c67e9d9e86b0a62736fb8fda324a1d1806 Mon Sep 17 00:00:00 2001 From: Wojciech Piekutowski Date: Sun, 29 Aug 2010 00:20:19 -0400 Subject: [PATCH] NumberLong shell output changed to NumberLong(111111111111) or NumberLong(11111) (when absolute value < 2^30) SERVER-1659 Signed-off-by: Eliot Horowitz --- dbtests/jstests.cpp | 2 +- jstests/numberlong.js | 28 ++++++++++++++-------------- scripting/sm_db.cpp | 14 ++++++++------ scripting/v8_db.cpp | 13 ++++++++----- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/dbtests/jstests.cpp b/dbtests/jstests.cpp index 81b9673daad..a9d9db85b9b 100644 --- a/dbtests/jstests.cpp +++ b/dbtests/jstests.cpp @@ -541,7 +541,7 @@ namespace JSTests { ASSERT( s->exec( "c = {c:a.a.toString()}", "foo", false, true, false ) ); out = s->getObject( "c" ); stringstream ss; - ss << "NumberLong( \"" << val << "\" )"; + ss << "NumberLong(\"" << val << "\")"; ASSERT_EQUALS( ss.str(), out.firstElement().valuestr() ); ASSERT( s->exec( "d = {d:a.a.toNumber()}", "foo", false, true, false ) ); diff --git a/jstests/numberlong.js b/jstests/numberlong.js index 848ef8732f5..1cbbc7a798a 100644 --- a/jstests/numberlong.js +++ b/jstests/numberlong.js @@ -4,50 +4,50 @@ n = new NumberLong( 4 ); assert.eq.automsg( "4", "n" ); assert.eq.automsg( "4", "n.toNumber()" ); assert.eq.automsg( "8", "n + 4" ); -assert.eq.automsg( "'NumberLong( 4 )'", "n.toString()" ); -assert.eq.automsg( "'NumberLong( 4 )'", "tojson( n )" ); +assert.eq.automsg( "'NumberLong(4)'", "n.toString()" ); +assert.eq.automsg( "'NumberLong(4)'", "tojson( n )" ); a = {} a.a = n; p = tojson( a ); -assert.eq.automsg( "'{ \"a\" : NumberLong( 4 ) }'", "p" ); +assert.eq.automsg( "'{ \"a\" : NumberLong(4) }'", "p" ); -assert.eq.automsg( "NumberLong( 4 )", "eval( tojson( NumberLong( 4 ) ) )" ); +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()" ); assert.eq.automsg( "0", "n + 4" ); -assert.eq.automsg( "'NumberLong( -4 )'", "n.toString()" ); -assert.eq.automsg( "'NumberLong( -4 )'", "tojson( n )" ); +assert.eq.automsg( "'NumberLong(-4)'", "n.toString()" ); +assert.eq.automsg( "'NumberLong(-4)'", "tojson( n )" ); a = {} a.a = n; p = tojson( a ); -assert.eq.automsg( "'{ \"a\" : NumberLong( -4 ) }'", "p" ); +assert.eq.automsg( "'{ \"a\" : NumberLong(-4) }'", "p" ); // too big to fit in double n = new NumberLong( "11111111111111111" ); assert.eq.automsg( "11111111111111112", "n.toNumber()" ); assert.eq.automsg( "11111111111111116", "n + 4" ); -assert.eq.automsg( "'NumberLong( \"11111111111111111\" )'", "n.toString()" ); -assert.eq.automsg( "'NumberLong( \"11111111111111111\" )'", "tojson( n )" ); +assert.eq.automsg( "'NumberLong(\"11111111111111111\")'", "n.toString()" ); +assert.eq.automsg( "'NumberLong(\"11111111111111111\")'", "tojson( n )" ); a = {} a.a = n; p = tojson( a ); -assert.eq.automsg( "'{ \"a\" : NumberLong( \"11111111111111111\" ) }'", "p" ); +assert.eq.automsg( "'{ \"a\" : NumberLong(\"11111111111111111\") }'", "p" ); -assert.eq.automsg( "NumberLong( '11111111111111111' )", "eval( tojson( NumberLong( '11111111111111111' ) ) )" ); +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" ); -assert.eq.automsg( "'NumberLong( \"-11111111111111111\" )'", "n.toString()" ); -assert.eq.automsg( "'NumberLong( \"-11111111111111111\" )'", "tojson( n )" ); +assert.eq.automsg( "'NumberLong(\"-11111111111111111\")'", "n.toString()" ); +assert.eq.automsg( "'NumberLong(\"-11111111111111111\")'", "tojson( n )" ); a = {} a.a = n; p = tojson( a ); -assert.eq.automsg( "'{ \"a\" : NumberLong( \"-11111111111111111\" ) }'", "p" ); +assert.eq.automsg( "'{ \"a\" : NumberLong(\"-11111111111111111\") }'", "p" ); // parsing assert.throws.automsg( function() { new NumberLong( "" ); } ); diff --git a/scripting/sm_db.cpp b/scripting/sm_db.cpp index 0e4cf3e40ee..b8962f96321 100644 --- a/scripting/sm_db.cpp +++ b/scripting/sm_db.cpp @@ -880,12 +880,14 @@ namespace mongo { JSBool numberlong_tostring(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ Convertor c(cx); stringstream ss; - if ( c.hasProperty( obj, "top" ) ) { - long long val = c.toNumberLongUnsafe( obj ); - ss << "NumberLong( \"" << val << "\" )"; - } else { - ss << "NumberLong( " << c.getNumber( obj, "floatApprox" ) << " )"; - } + long long val = c.toNumberLongUnsafe( obj ); + const long long limit = 2LL << 30; + + if ( val <= -limit || limit <= val ) + ss << "NumberLong(\"" << val << "\")"; + else + ss << "NumberLong(" << val << ")"; + string ret = ss.str(); return *rval = c.toval( ret.c_str() ); } diff --git a/scripting/v8_db.cpp b/scripting/v8_db.cpp index 470414ff366..e1788755042 100644 --- a/scripting/v8_db.cpp +++ b/scripting/v8_db.cpp @@ -719,11 +719,14 @@ namespace mongo { v8::Handle it = args.This(); stringstream ss; - if ( !it->Has( v8::String::New( "top" ) ) ) { - ss << "NumberLong( " << it->Get( v8::String::New( "floatApprox" ) )->NumberValue() << " )"; - } else { - ss << "NumberLong( \"" << numberLongVal( it ) << "\" )"; - } + long long val = numberLongVal( it ); + const long long limit = 2LL << 30; + + if ( val <= -limit || limit <= val ) + ss << "NumberLong(\"" << val << "\")"; + else + ss << "NumberLong(" << val << ")"; + string ret = ss.str(); return v8::String::New( ret.c_str() ); }