0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

SERVER-677 number long floatApprox v8

This commit is contained in:
Aaron 2010-03-01 10:36:40 -08:00
parent 89f3e6d65b
commit ee47bb3c12
4 changed files with 62 additions and 26 deletions

View File

@ -551,7 +551,7 @@ namespace JSTests {
ASSERT( s->exec( "f = {f:a.a.top}", "foo", false, true, false ) );
out = s->getObject( "f" );
ASSERT_EQUALS( NumberDouble, out.firstElement().type() );
ASSERT( NumberDouble == out.firstElement().type() || NumberInt == out.firstElement().type() );
s->setObject( "z", BSON( "z" << (long long)( 4 ) ) );
ASSERT( s->exec( "y = {y:z.z.top}", "foo", false, true, false ) );
@ -560,8 +560,13 @@ namespace JSTests {
ASSERT( s->exec( "x = {x:z.z.floatApprox}", "foo", false, true, false ) );
out = s->getObject( "x" );
ASSERT_EQUALS( NumberDouble, out.firstElement().type() );
ASSERT( NumberDouble == out.firstElement().type() || NumberInt == out.firstElement().type() );
ASSERT_EQUALS( double( 4 ), out.firstElement().number() );
ASSERT( s->exec( "w = {w:z.z}", "foo", false, true, false ) );
out = s->getObject( "w" );
ASSERT_EQUALS( mongo::NumberLong, out.firstElement().type() );
ASSERT_EQUALS( 4, out.firstElement().numberLong() );
}
};

View File

@ -496,6 +496,7 @@
93BCE5A610F3FB5200FA139B /* basicPlus.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = basicPlus.js; sourceTree = "<group>"; };
93BFA0E311330A8C0045D084 /* not2.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = not2.js; sourceTree = "<group>"; };
93C38E940FA66622007D6E4A /* basictests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = basictests.cpp; sourceTree = "<group>"; };
93CC40C2113C407A00734218 /* insert1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = insert1.js; sourceTree = "<group>"; };
93D0C1520EF1D377005253B7 /* jsobjtests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsobjtests.cpp; sourceTree = "<group>"; };
93D0C1FB0EF1E267005253B7 /* namespacetests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = namespacetests.cpp; sourceTree = "<group>"; };
93D5A8921117A1380052C931 /* regex6.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = regex6.js; sourceTree = "<group>"; };
@ -744,6 +745,7 @@
934BEB9A10DFFA9600178102 /* jstests */ = {
isa = PBXGroup;
children = (
93CC40C2113C407A00734218 /* insert1.js */,
93BFA0E311330A8C0045D084 /* not2.js */,
93B9FA36112CAC3C0066ECD2 /* shellkillop.js */,
93B9F91A112C7F200066ECD2 /* set4.js */,

View File

@ -603,8 +603,8 @@ namespace mongo {
v8::Handle<v8::Value> numberLongInit( const v8::Arguments& args ) {
if (args.Length() != 2) {
return v8::ThrowException( v8::String::New( "NumberLong needs 2 arguments" ) );
if (args.Length() != 1 && args.Length() != 3) {
return v8::ThrowException( v8::String::New( "NumberLong needs 1 or 3 arguments" ) );
}
v8::Handle<v8::Object> it = args.This();
@ -614,13 +614,25 @@ namespace mongo {
it = f->NewInstance();
}
it->Set( v8::String::New( "top" ) , args[0] );
it->Set( v8::String::New( "bottom" ) , args[1] );
it->Set( v8::String::New( "floatApprox" ) , args[0] );
if ( args.Length() == 3 ) {
it->Set( v8::String::New( "top" ) , args[1] );
it->Set( v8::String::New( "bottom" ) , args[2] );
}
it->SetHiddenValue( v8::String::New( "__NumberLong" ), v8::Number::New( 1 ) );
return it;
}
long long numberLongVal( const v8::Handle< v8::Object > &it ) {
if ( !it->Has( v8::String::New( "top" ) ) )
return (long long)( it->Get( v8::String::New( "floatApprox" ) )->NumberValue() );
return
(long long)
( (unsigned long long)( it->Get( v8::String::New( "top" ) )->ToInt32()->Value() ) << 32 ) +
(unsigned)( it->Get( v8::String::New( "bottom" ) )->ToInt32()->Value() );
}
v8::Handle<v8::Value> numberLongValueOf( const v8::Arguments& args ) {
if (args.Length() != 0) {
@ -629,11 +641,9 @@ namespace mongo {
v8::Handle<v8::Object> it = args.This();
unsigned long long val =
( (unsigned long long)( it->Get( v8::String::New( "top" ) )->ToInt32()->Value() ) << 32 ) +
(unsigned)( it->Get( v8::String::New( "bottom" ) )->ToInt32()->Value() );
long long val = numberLongVal( it );
return v8::Number::New( double( (long long)val ) );
return v8::Number::New( double( val ) );
}
v8::Handle<v8::Value> numberLongToNumber( const v8::Arguments& args ) {
@ -648,12 +658,10 @@ namespace mongo {
v8::Handle<v8::Object> it = args.This();
unsigned long long val =
( (unsigned long long)( it->Get( v8::String::New( "top" ) )->ToInt32()->Value() ) << 32 ) +
(unsigned)( it->Get( v8::String::New( "bottom" ) )->ToInt32()->Value() );
long long val = numberLongVal( it );
stringstream ss;
ss << (long long)val;
ss << val;
string ret = ss.str();
return v8::String::New( ret.c_str() );
}

View File

@ -213,10 +213,17 @@ namespace mongo {
Local<v8::Object> sub = readOnly ? readOnlyObjects->NewInstance() : internalFieldObjects->NewInstance();
unsigned long long val = f.numberLong();
v8::Function* numberLong = getNamedCons( "NumberLong" );
v8::Handle<v8::Value> argv[2];
argv[0] = v8::Integer::New( val >> 32 );
argv[1] = v8::Integer::New( (unsigned long)(val & 0x00000000ffffffff) );
o->Set( v8::String::New( f.fieldName() ), numberLong->NewInstance(2, argv) );
if ( (long long)val == (long long)(double)(long long)(val) ) {
v8::Handle<v8::Value> argv[1];
argv[0] = v8::Number::New( (double)(long long)( val ) );
o->Set( v8::String::New( f.fieldName() ), numberLong->NewInstance( 1, argv ) );
} else {
v8::Handle<v8::Value> argv[3];
argv[0] = v8::Number::New( (double)(long long)(val) );
argv[1] = v8::Integer::New( val >> 32 );
argv[2] = v8::Integer::New( (unsigned long)(val & 0x00000000ffffffff) );
o->Set( v8::String::New( f.fieldName() ), numberLong->NewInstance(3, argv) );
}
break;
}
@ -339,10 +346,17 @@ namespace mongo {
Local<v8::Object> sub = internalFieldObjects->NewInstance();
unsigned long long val = f.numberLong();
v8::Function* numberLong = getNamedCons( "NumberLong" );
v8::Handle<v8::Value> argv[2];
argv[0] = v8::Integer::New( val >> 32 );
argv[1] = v8::Integer::New( (unsigned long)(val & 0x00000000ffffffff) );
return numberLong->NewInstance( 2, argv );
if ( (long long)val == (long long)(double)(long long)(val) ) {
v8::Handle<v8::Value> argv[1];
argv[0] = v8::Number::New( (double)(long long)( val ) );
return numberLong->NewInstance( 1, argv );
} else {
v8::Handle<v8::Value> argv[3];
argv[0] = v8::Number::New( (double)(long long)( val ) );
argv[1] = v8::Integer::New( val >> 32 );
argv[2] = v8::Integer::New( (unsigned long)(val & 0x00000000ffffffff) );
return numberLong->NewInstance( 3, argv );
}
}
case mongo::MinKey: {
@ -451,10 +465,17 @@ namespace mongo {
// TODO might be nice to potentially speed this up with an indexed internal
// field, but I don't yet know how to use an ObjectTemplate with a
// constructor.
unsigned long long val =
( (unsigned long long)( value->ToObject()->Get( v8::String::New( "top" ) )->ToInt32()->Value() ) << 32 ) +
(unsigned)( value->ToObject()->Get( v8::String::New( "bottom" ) )->ToInt32()->Value() );
b.append( sname.c_str(), (long long)val );
v8::Handle< v8::Object > it = value->ToObject();
long long val;
if ( !it->Has( v8::String::New( "top" ) ) ) {
val = (long long)( it->Get( v8::String::New( "floatApprox" ) )->NumberValue() );
} else {
val = (long long)
( (unsigned long long)( it->Get( v8::String::New( "top" ) )->ToInt32()->Value() ) << 32 ) +
(unsigned)( it->Get( v8::String::New( "bottom" ) )->ToInt32()->Value() );
}
b.append( sname.c_str(), val );
}
else if ( !value->ToObject()->GetHiddenValue( v8::String::New( "__DBPointer" ) ).IsEmpty() ) {
OID oid;