Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fix issue when deleting fields from JS SERVER-139

  • Loading branch information...
commit ad841f9c4a02473be11cef7f3c31e516c9f7239e 1 parent 1d461d2
Eliot erh authored

Showing 2 changed files with 44 additions and 8 deletions. Show diff stats Hide diff stats

  1. +19 0 jstests/eval8.js
  2. +25 8 scripting/engine_spidermonkey.cpp
19 jstests/eval8.js
... ... @@ -0,0 +1,19 @@
  1 +
  2 +t = db.eval8;
  3 +t.drop();
  4 +
  5 +x = { a : 1 , b : 2 };
  6 +t.save( x );
  7 +x = t.findOne();
  8 +
  9 +assert( x.a && x.b , "A" );
  10 +delete x.b;
  11 +
  12 +assert( x.a && ! x.b , "B" )
  13 +x.b = 3;
  14 +assert( x.a && x.b , "C" );
  15 +assert.eq( 3 , x.b , "D" );
  16 +
  17 +t.save( x );
  18 +y = t.findOne();
  19 +assert.eq( tojson( x ) , tojson( y ) , "E" );
33 scripting/engine_spidermonkey.cpp
@@ -33,9 +33,10 @@ namespace mongo {
33 33 bool _inResolve;
34 34 char _magic;
35 35 list<string> _extra;
  36 + set<string> _removed;
36 37 bool _modified;
37 38 };
38   -
  39 +
39 40 class BSONFieldIterator {
40 41 public:
41 42
@@ -44,9 +45,11 @@ namespace mongo {
44 45 BSONObjIterator it( holder->_obj );
45 46 while ( it.more() ){
46 47 BSONElement e = it.next();
  48 + if ( holder->_removed.count( e.fieldName() ) )
  49 + continue;
47 50 _names.push_back( e.fieldName() );
48 51 }
49   -
  52 +
50 53 _names.merge( holder->_extra );
51 54
52 55 _it = _names.begin();
@@ -569,29 +572,43 @@ namespace mongo {
569 572 (JSEnumerateOp)bson_enumerate, (JSResolveOp)(&resolveBSONField) , JS_ConvertStub, bson_finalize ,
570 573 JSCLASS_NO_OPTIONAL_MEMBERS
571 574 };
572   -
  575 +
573 576 JSBool bson_add_prop( JSContext *cx, JSObject *obj, jsval idval, jsval *vp){
574 577 BSONHolder * holder = GETHOLDER( cx , obj );
575 578 if ( ! holder->_inResolve ){
576 579 Convertor c(cx);
577   - holder->_extra.push_back( c.toString( idval ) );
  580 + string name = c.toString( idval );
  581 + if ( holder->_obj[name].eoo() )
  582 + holder->_extra.push_back( name );
578 583 holder->_modified = true;
579 584 }
580 585 return JS_TRUE;
581 586 }
582   -
  587 +
583 588
584 589 JSBool mark_modified( JSContext *cx, JSObject *obj, jsval idval, jsval *vp){
  590 + Convertor c(cx);
  591 + BSONHolder * holder = GETHOLDER( cx , obj );
  592 + if ( holder->_inResolve )
  593 + return JS_TRUE;
  594 + holder->_modified = true;
  595 + holder->_removed.erase( c.toString( idval ) );
  596 + return JS_TRUE;
  597 + }
  598 +
  599 + JSBool mark_modified_remove( JSContext *cx, JSObject *obj, jsval idval, jsval *vp){
  600 + Convertor c(cx);
585 601 BSONHolder * holder = GETHOLDER( cx , obj );
586 602 if ( holder->_inResolve )
587 603 return JS_TRUE;
588 604 holder->_modified = true;
  605 + holder->_removed.insert( c.toString( idval ) );
589 606 return JS_TRUE;
590 607 }
591 608
592 609 JSClass bson_class = {
593 610 "bson_object" , JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE | JSCLASS_NEW_ENUMERATE ,
594   - bson_add_prop, mark_modified, JS_PropertyStub, mark_modified,
  611 + bson_add_prop, mark_modified_remove, JS_PropertyStub, mark_modified,
595 612 (JSEnumerateOp)bson_enumerate, (JSResolveOp)(&resolveBSONField) , JS_ConvertStub, bson_finalize ,
596 613 JSCLASS_NO_OPTIONAL_MEMBERS
597 614 };
@@ -664,12 +681,12 @@ namespace mongo {
664 681
665 682 BSONHolder * holder = GETHOLDER( cx , obj );
666 683 holder->check();
667   -
  684 +
668 685 string s = c.toString( id );
669 686
670 687 BSONElement e = holder->_obj[ s.c_str() ];
671 688
672   - if ( e.type() == EOO ){
  689 + if ( e.type() == EOO || holder->_removed.count( s ) ){
673 690 *objp = 0;
674 691 JS_LeaveLocalRootScope( cx );
675 692 return JS_TRUE;

0 comments on commit ad841f9

Please sign in to comment.
Something went wrong with that request. Please try again.