Skip to content

Commit feb9b4b

Browse files
committed
Generalized CodeWScope for use with JRuby
1 parent 5d72a01 commit feb9b4b

4 files changed

Lines changed: 21 additions & 8 deletions

File tree

src/main/org/bson/BSONCallback.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ public interface BSONCallback {
88

99
void objectStart();
1010
void objectStart(String name);
11+
void objectStart(boolean array);
1112
Object objectDone();
1213

14+
void reset();
15+
Object get();
16+
BSONCallback createBSONCallback();
17+
1318
void arrayStart();
1419
void arrayStart(String name);
1520
Object arrayDone();
16-
1721

1822
void gotNull( String name );
1923
void gotUndefined( String name );

src/main/org/bson/BSONDecoder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ boolean decodeElement()
154154
case CODE_W_SCOPE:
155155
_in.readInt();
156156
_callback.gotCodeWScope( name , _in.readUTF8String() , _readBasicObject() );
157+
157158
break;
158159

159160
case ARRAY:
@@ -239,10 +240,11 @@ BSONObject _readBasicObject()
239240
_in.readInt();
240241

241242
BSONCallback save = _callback;
243+
BSONCallback _basic = _callback.createBSONCallback();
242244
_callback = _basic;
243245
_basic.reset();
244246
_basic.objectStart(false);
245-
247+
246248
while( decodeElement() );
247249
_callback = save;
248250
return (BSONObject)(_basic.get());
@@ -343,6 +345,4 @@ String readUTF8String()
343345
private byte[] _random = new byte[1024];
344346

345347
private PoolOutputBuffer _stringBuffer = new PoolOutputBuffer();
346-
347-
private final BasicBSONCallback _basic = new BasicBSONCallback();
348348
}

src/main/org/bson/BasicBSONCallback.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class BasicBSONCallback implements BSONCallback {
1212
public BasicBSONCallback(){
1313
reset();
1414
}
15-
16-
public BSONObject create(){
17-
return new BasicBSONObject();
15+
16+
public BSONCallback createBSONCallback(){
17+
return new BasicBSONCallback();
1818
}
1919

2020
public BSONObject create( boolean array , List<String> path ){

src/test/org/bson/BSONTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.testng.annotations.Test;
3030

31+
import org.bson.types.CodeWScope;
3132
import org.bson.io.*;
3233

3334
public class BSONTest extends TestCase {
@@ -70,7 +71,6 @@ void _test( BSONObject o , int size , String hash )
7071
public void testBasic1()
7172
throws IOException {
7273
BSONObject o = new BasicBSONObject();
73-
7474
_test( new BasicBSONObject( "x" , true ) , 9 , "6fe24623e4efc5cf07f027f9c66b5456" );
7575

7676
_test( new BasicBSONObject( "x" , null ) , 8 , "12d43430ff6729af501faf0638e68888" );
@@ -109,6 +109,15 @@ public void testOB1(){
109109

110110
}
111111

112+
@Test
113+
public void testCode()
114+
throws IOException{
115+
BSONObject scope = new BasicBSONObject( "x", 1 );
116+
CodeWScope c = new CodeWScope( "function() { x += 1; }" , scope );
117+
BSONObject code_object = new BasicBSONObject( "map" , c);
118+
_test( code_object , 53 , "52918d2367533165bfc617df50335cbb" );
119+
}
120+
112121
@Test
113122
public void testOBBig1(){
114123
BasicOutputBuffer a = new BasicOutputBuffer();

0 commit comments

Comments
 (0)