0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00

fix issue when deleting fields from JS SERVER-139

This commit is contained in:
Eliot Horowitz 2009-07-09 12:09:30 -04:00
parent 1d461d26b3
commit ad841f9c4a
2 changed files with 44 additions and 8 deletions

19
jstests/eval8.js Normal file
View File

@ -0,0 +1,19 @@
t = db.eval8;
t.drop();
x = { a : 1 , b : 2 };
t.save( x );
x = t.findOne();
assert( x.a && x.b , "A" );
delete x.b;
assert( x.a && ! x.b , "B" )
x.b = 3;
assert( x.a && x.b , "C" );
assert.eq( 3 , x.b , "D" );
t.save( x );
y = t.findOne();
assert.eq( tojson( x ) , tojson( y ) , "E" );

View File

@ -33,9 +33,10 @@ namespace mongo {
bool _inResolve;
char _magic;
list<string> _extra;
set<string> _removed;
bool _modified;
};
class BSONFieldIterator {
public:
@ -44,9 +45,11 @@ namespace mongo {
BSONObjIterator it( holder->_obj );
while ( it.more() ){
BSONElement e = it.next();
if ( holder->_removed.count( e.fieldName() ) )
continue;
_names.push_back( e.fieldName() );
}
_names.merge( holder->_extra );
_it = _names.begin();
@ -569,29 +572,43 @@ namespace mongo {
(JSEnumerateOp)bson_enumerate, (JSResolveOp)(&resolveBSONField) , JS_ConvertStub, bson_finalize ,
JSCLASS_NO_OPTIONAL_MEMBERS
};
JSBool bson_add_prop( JSContext *cx, JSObject *obj, jsval idval, jsval *vp){
BSONHolder * holder = GETHOLDER( cx , obj );
if ( ! holder->_inResolve ){
Convertor c(cx);
holder->_extra.push_back( c.toString( idval ) );
string name = c.toString( idval );
if ( holder->_obj[name].eoo() )
holder->_extra.push_back( name );
holder->_modified = true;
}
return JS_TRUE;
}
JSBool mark_modified( JSContext *cx, JSObject *obj, jsval idval, jsval *vp){
Convertor c(cx);
BSONHolder * holder = GETHOLDER( cx , obj );
if ( holder->_inResolve )
return JS_TRUE;
holder->_modified = true;
holder->_removed.erase( c.toString( idval ) );
return JS_TRUE;
}
JSBool mark_modified_remove( JSContext *cx, JSObject *obj, jsval idval, jsval *vp){
Convertor c(cx);
BSONHolder * holder = GETHOLDER( cx , obj );
if ( holder->_inResolve )
return JS_TRUE;
holder->_modified = true;
holder->_removed.insert( c.toString( idval ) );
return JS_TRUE;
}
JSClass bson_class = {
"bson_object" , JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE | JSCLASS_NEW_ENUMERATE ,
bson_add_prop, mark_modified, JS_PropertyStub, mark_modified,
bson_add_prop, mark_modified_remove, JS_PropertyStub, mark_modified,
(JSEnumerateOp)bson_enumerate, (JSResolveOp)(&resolveBSONField) , JS_ConvertStub, bson_finalize ,
JSCLASS_NO_OPTIONAL_MEMBERS
};
@ -664,12 +681,12 @@ namespace mongo {
BSONHolder * holder = GETHOLDER( cx , obj );
holder->check();
string s = c.toString( id );
BSONElement e = holder->_obj[ s.c_str() ];
if ( e.type() == EOO ){
if ( e.type() == EOO || holder->_removed.count( s ) ){
*objp = 0;
JS_LeaveLocalRootScope( cx );
return JS_TRUE;