0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

Spider Monkey: object mapping support

This commit is contained in:
Eliot Horowitz 2009-05-06 11:53:39 -04:00
parent d907eb92a8
commit 511f26f338
3 changed files with 71 additions and 9 deletions

View File

@ -137,10 +137,29 @@ namespace JSTests {
delete s;
}
};
// TODO:
// setThis
// init
class ObjectDecoding {
public:
void run(){
Scope * s = globalScriptEngine->createScope();
s->invoke( "z = { num : 1 };" , BSONObj() );
BSONObj out = s->getObject( "z" );
ASSERT_EQUALS( 1 , out["num"].number() );
ASSERT_EQUALS( 1 , out.nFields() );
s->invoke( "z = { x : 'eliot' };" , BSONObj() );
out = s->getObject( "z" );
ASSERT_EQUALS( (string)"eliot" , out["x"].valuestr() );
ASSERT_EQUALS( 1 , out.nFields() );
BSONObj o = BSON( "x" << 17 );
s->setObject( "blah" , o );
ASSERT_EQUALS( o.toString() , s->getObject( "blah" ).toString() );
delete s;
}
};
class All : public UnitTest::Suite {
public:
@ -150,6 +169,7 @@ namespace JSTests {
add< FalseTests >();
add< SimpleFunctions >();
add< ObjectMapping >();
add< ObjectDecoding >();
}
};

View File

@ -58,6 +58,48 @@ namespace mongo {
assert( JS_ValueToBoolean( _context, v , &b ) );
return b;
}
BSONObj toObject( JSObject * o ){
if ( ! o )
return BSONObj();
BSONObjBuilder b;
JSIdArray * properties = JS_Enumerate( _context , o );
assert( properties );
cout << "num properties: " << properties->length << endl;
for ( jsint i=0; i<properties->length; i++ ){
jsid id = properties->vector[i];
jsval nameval;
assert( JS_IdToValue( _context ,id , &nameval ) );
string name = toString( nameval );
append( b , name , getProperty( o , name.c_str() ) );
}
return b.obj();
}
BSONObj toObject( jsval v ){
if ( JSVAL_IS_NULL( v ) ||
JSVAL_IS_VOID( v ) )
return BSONObj();
uassert( "not an object" , JSVAL_IS_OBJECT( v ) );
return toObject( JSVAL_TO_OBJECT( v ) );
}
void append( BSONObjBuilder& b , string name , jsval val ){
switch ( JS_TypeOfValue( _context , val ) ){
case JSTYPE_VOID: b.appendUndefined( name.c_str() ); break;
case JSTYPE_NULL: b.appendNull( name.c_str() ); break;
case JSTYPE_NUMBER: b.append( name.c_str() , toNumber( val ) ); break;
case JSTYPE_STRING: b.append( name.c_str() , toString( val ) ); break;
default: uassert( (string)"can't append type: " + typeString( val ) , 0 );
}
}
// ---------- to spider monkey ---------
@ -97,7 +139,7 @@ namespace mongo {
case String:
return toval( e.valuestr() );
default:
log() << "resolveBSONField can't handle type: " << (int)(e.type()) << endl;
log() << "toval can't handle type: " << (int)(e.type()) << endl;
}
uassert( "not done: toval" , 0 );
@ -304,7 +346,7 @@ namespace mongo {
}
BSONObj getObject( const char *field ){
massert( "not implemented yet: getObject()" , 0 ); throw -1;
return _convertor->toObject( _convertor->getProperty( _global , field ) );
}
JSObject * getJSObject( const char * field ){

View File

@ -128,9 +128,9 @@ namespace mongo {
string ns = c.toString( argv[0] );
BSONObj q = c.toObject( argv[1] );
//uassert( "field selector not supported yet in mongo_find" , argv[2] == JSVAL_NULL );
uassert( "field selector not supported yet in mongo_find" , argv[2] == JSVAL_NULL );
int nToReturn = c.toNumber( argv[3] );
int nToSkip = c.toNumber( argv[4] );
bool slaveOk = c.getBoolean( obj , "slaveOk" );
@ -138,7 +138,7 @@ namespace mongo {
try {
dbtemprelease r; // TODO: remove
auto_ptr<DBClientCursor> cursor = conn->query( ns , BSONObj() , nToReturn , nToSkip , 0 , slaveOk ? Option_SlaveOk : 0 );
auto_ptr<DBClientCursor> cursor = conn->query( ns , q , nToReturn , nToSkip , 0 , slaveOk ? Option_SlaveOk : 0 );
JSObject * mycursor = JS_NewObject( cx , &internal_cursor_class , 0 , 0 );