diff options
author | agirbal <antoine@10gen.com> | 2011-07-15 23:57:08 -0700 |
---|---|---|
committer | agirbal <antoine@10gen.com> | 2011-07-15 23:58:00 -0700 |
commit | c08570d8034469fb9479aaa403ce5f2fd1bca39d (patch) | |
tree | 537dc6ad52727149f8318c8ba5bd2a56cd033e63 /scripting | |
parent | 07e069fdc4e8c92574dc032e4948d650d5672f3c (diff) | |
download | mongo-c08570d8034469fb9479aaa403ce5f2fd1bca39d.tar.gz |
SERVER-854: now NumberInt properly kept for v8
Diffstat (limited to 'scripting')
-rw-r--r-- | scripting/engine_v8.cpp | 35 | ||||
-rw-r--r-- | scripting/engine_v8.h | 2 |
2 files changed, 23 insertions, 14 deletions
diff --git a/scripting/engine_v8.cpp b/scripting/engine_v8.cpp index 0cabec8beeb..7752dc79397 100644 --- a/scripting/engine_v8.cpp +++ b/scripting/engine_v8.cpp @@ -1311,7 +1311,7 @@ namespace mongo { v8ToMongoElement(builder, v8name, fieldName, value); } - void V8Scope::v8ToMongoElement( BSONObjBuilder & b , v8::Handle<v8::String> name , const string sname , v8::Handle<v8::Value> value , int depth ) { + void V8Scope::v8ToMongoElement( BSONObjBuilder & b , v8::Handle<v8::String> name , const string sname , v8::Handle<v8::Value> value , int depth, BSONObj* originalParent ) { if ( value->IsString() ) { // Handle<v8::String> str = Handle<v8::String>::Cast(value); @@ -1327,11 +1327,18 @@ namespace mongo { } if ( value->IsNumber() ) { - // needs to be explicit NumberInt to use integer -// if ( value->IsInt32() ) -// b.append( sname, int( value->ToInt32()->Value() ) ); -// else - b.append( sname , value->ToNumber()->Value() ); + double val = value->ToNumber()->Value(); + // if previous type was integer, keep it + int intval = (int)val; + if (val == intval && originalParent) { + BSONElement elmt = originalParent->getField(sname); + if (elmt.type() == mongo::NumberInt) { + b.append( sname , intval ); + return; + } + } + + b.append( sname , val ); return; } @@ -1446,21 +1453,23 @@ namespace mongo { } BSONObj V8Scope::v8ToMongo( v8::Handle<v8::Object> o , int depth ) { + BSONObj* originalBSON = 0; + if (o->HasNamedLookupInterceptor()) { + originalBSON = unwrapBSONObj(o); + } + if ( !o->GetHiddenValue( V8STR_RO ).IsEmpty() || (o->HasNamedLookupInterceptor() && o->GetHiddenValue( V8STR_MODIFIED ).IsEmpty()) ) { // object was readonly, use bson as is - log(1) << "Using bson as is for v8ToMongo" << endl; - BSONObj* ro = unwrapBSONObj(o); - if (ro) - return *ro; - return BSONObj(); + if (originalBSON) + return *originalBSON; } BSONObjBuilder b; if ( depth == 0 ) { if ( o->HasRealNamedProperty( V8STR_ID ) ) { - v8ToMongoElement( b , V8STR_ID , "_id" , o->Get( V8STR_ID ) ); + v8ToMongoElement( b , V8STR_ID , "_id" , o->Get( V8STR_ID ), 0, originalBSON ); } } @@ -1478,7 +1487,7 @@ namespace mongo { if ( depth == 0 && sname == "_id" ) continue; - v8ToMongoElement( b , name , sname , value , depth + 1 ); + v8ToMongoElement( b , name , sname , value , depth + 1, originalBSON ); } return b.obj(); } diff --git a/scripting/engine_v8.h b/scripting/engine_v8.h index 1920a10a301..3f116c9c2c2 100644 --- a/scripting/engine_v8.h +++ b/scripting/engine_v8.h @@ -112,7 +112,7 @@ namespace mongo { mongo::BSONObj v8ToMongo( v8::Handle<v8::Object> o , int depth = 0 ); void v8ToMongoElement( BSONObjBuilder & b , v8::Handle<v8::String> name , - const string sname , v8::Handle<v8::Value> value , int depth = 0 ); + const string sname , v8::Handle<v8::Value> value , int depth = 0, BSONObj* originalParent=0 ); v8::Handle<v8::Value> mongoToV8Element( const BSONElement &f, bool readOnly = false ); virtual void append( BSONObjBuilder & builder , const char * fieldName , const char * scopeName ); |