summaryrefslogtreecommitdiff
path: root/scripting/engine_v8.cpp
diff options
context:
space:
mode:
authoragirbal <antoine@10gen.com>2011-07-15 23:57:08 -0700
committeragirbal <antoine@10gen.com>2011-07-15 23:58:00 -0700
commitc08570d8034469fb9479aaa403ce5f2fd1bca39d (patch)
tree537dc6ad52727149f8318c8ba5bd2a56cd033e63 /scripting/engine_v8.cpp
parent07e069fdc4e8c92574dc032e4948d650d5672f3c (diff)
downloadmongo-c08570d8034469fb9479aaa403ce5f2fd1bca39d.tar.gz
SERVER-854: now NumberInt properly kept for v8
Diffstat (limited to 'scripting/engine_v8.cpp')
-rw-r--r--scripting/engine_v8.cpp35
1 files changed, 22 insertions, 13 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();
}