handle js -> bson in 1 place

This commit is contained in:
Eliot Horowitz
2009-09-21 15:54:41 -04:00
parent c2456cf402
commit dcbe8fbd95
3 changed files with 39 additions and 21 deletions

View File

@@ -18,6 +18,41 @@ namespace mongo {
ScriptEngine::~ScriptEngine(){
}
void Scope::append( BSONObjBuilder & builder , const char * fieldName , const char * scopeName ){
int t = type( scopeName );
switch ( t ){
case Object:
builder.append( fieldName , getObject( scopeName ) );
break;
case Array:
builder.appendArray( fieldName , getObject( scopeName ) );
break;
case NumberDouble:
builder.append( fieldName , getNumber( scopeName ) );
break;
case String:
builder.append( fieldName , getString( scopeName ).c_str() );
break;
case Bool:
builder.appendBool( fieldName , getBoolean( scopeName ) );
break;
case jstNULL:
case Undefined:
builder.appendNull( fieldName );
break;
case Date:
builder.appendDate( fieldName , (unsigned long long) getNumber( scopeName ) );
break;
default:
stringstream temp;
temp << "can't append type from:";
temp << t;
uassert( temp.str() , 0 );
}
}
int Scope::invoke( const char* code , const BSONObj& args, int timeoutMs ){
ScriptingFunction func = createFunction( code );
uassert( "compile failed" , func );