diff options
author | Eliot Horowitz <eliot@10gen.com> | 2009-10-13 11:11:55 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2009-10-13 11:11:55 -0400 |
commit | feabc146574ecd56e8e3901e79c90d4e97567e36 (patch) | |
tree | 13988df758541a20b4a28a97568a4a8907c77757 /scripting | |
parent | f7553c7835e4d7a388bc82ba57a256a4c7441931 (diff) | |
download | mongo-feabc146574ecd56e8e3901e79c90d4e97567e36.tar.gz |
v8: some cleaning
Diffstat (limited to 'scripting')
-rw-r--r-- | scripting/engine.h | 4 | ||||
-rw-r--r-- | scripting/engine_v8.cpp | 7 | ||||
-rw-r--r-- | scripting/engine_v8.h | 2 | ||||
-rw-r--r-- | scripting/v8_db.cpp | 37 | ||||
-rw-r--r-- | scripting/v8_db.h | 6 |
5 files changed, 39 insertions, 17 deletions
diff --git a/scripting/engine.h b/scripting/engine.h index e5c164be836..502f82949b0 100644 --- a/scripting/engine.h +++ b/scripting/engine.h @@ -56,7 +56,9 @@ namespace mongo { int invoke( const char* code , const BSONObj& args, int timeoutMs = 0 ); void invokeSafe( const char* code , const BSONObj& args, int timeoutMs = 0 ){ - uassert( "invoke failed" , invoke( code , args , timeoutMs ) == 0 ); + if ( invoke( code , args , timeoutMs ) == 0 ) + return; + throw UserException( (string)"invoke failed: " + getError() ); } virtual bool exec( const string& code , const string& name , bool printResult , bool reportError , bool assertOnError, int timeoutMs = 0 ) = 0; diff --git a/scripting/engine_v8.cpp b/scripting/engine_v8.cpp index a8899b7bf9d..220f6c9e340 100644 --- a/scripting/engine_v8.cpp +++ b/scripting/engine_v8.cpp @@ -15,7 +15,7 @@ namespace mongo { _globalTemplate->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); _externalTemplate = getMongoFunctionTemplate( false ); - //_localTemplate = getMongoFunctionTemplate( true ); + _localTemplate = getMongoFunctionTemplate( true ); installDBTypes( _globalTemplate ); } @@ -307,6 +307,11 @@ namespace mongo { // ----- internal ----- + void V8Scope::reset(){ + _error = ""; + _context->Enter(); + } + void V8Scope::_startCall(){ _error = ""; } diff --git a/scripting/engine_v8.h b/scripting/engine_v8.h index 13dc6d2cc0d..92b1d1f181f 100644 --- a/scripting/engine_v8.h +++ b/scripting/engine_v8.h @@ -16,7 +16,7 @@ namespace mongo { V8Scope( V8ScriptEngine * engine ); ~V8Scope(); - virtual void reset(){} + virtual void reset() virtual void init( BSONObj * data ); virtual void localConnect( const char * dbName ); diff --git a/scripting/v8_db.cpp b/scripting/v8_db.cpp index 2ce6b6c0f3f..f3e3072300a 100644 --- a/scripting/v8_db.cpp +++ b/scripting/v8_db.cpp @@ -16,8 +16,7 @@ namespace mongo { #define DDD(x) v8::Handle<v8::FunctionTemplate> getMongoFunctionTemplate( bool local ){ - uassert( "local not supported" , ! local ); - v8::Local<v8::FunctionTemplate> mongo = FunctionTemplate::New( mongoInit ); + v8::Local<v8::FunctionTemplate> mongo = FunctionTemplate::New( local ? mongoConsLocal : mongoConsExternal ); v8::Local<v8::Template> proto = mongo->PrototypeTemplate(); @@ -51,7 +50,7 @@ namespace mongo { } - Handle<Value> mongoInit(const Arguments& args){ + Handle<Value> mongoConsExternal(const Arguments& args){ char host[255]; @@ -63,10 +62,11 @@ namespace mongo { strcpy( host , "127.0.0.1" ); } - DBClientConnection * conn = new DBClientConnection( true ); + DBClientConnection * cc = new DBClientConnection( true ); + DBClientBase * conn = cc; string errmsg; - if ( ! conn->connect( host , errmsg ) ){ + if ( ! cc->connect( host , errmsg ) ){ return v8::ThrowException( v8::String::New( "couldn't connect" ) ); } @@ -77,6 +77,20 @@ namespace mongo { return v8::Undefined(); } + Handle<Value> mongoConsLocal(const Arguments& args){ + + if ( args.Length() > 0 ) + return v8::ThrowException( v8::String::New( "local Mongo constructor takes no args" ) ); + + DBClientBase * conn = createDirectClient(); + + // NOTE I don't believe the conn object will ever be freed. + args.This()->Set( CONN_STRING , External::New( conn ) ); + args.This()->Set( v8::String::New( "slaveOk" ) , Boolean::New( false ) ); + + return v8::Undefined(); + } + // --- @@ -86,9 +100,9 @@ namespace mongo { #define GETNS char ns[args[0]->ToString()->Utf8Length()]; args[0]->ToString()->WriteUtf8( ns ); #endif - DBClientConnection * getConnection( const Arguments& args ){ + DBClientBase * getConnection( const Arguments& args ){ Local<External> c = External::Cast( *(args.This()->Get( CONN_STRING )) ); - DBClientConnection * conn = (DBClientConnection*)(c->Value()); + DBClientBase * conn = (DBClientBase*)(c->Value()); assert( conn ); return conn; } @@ -103,10 +117,9 @@ namespace mongo { 4 - skip */ Handle<Value> mongoFind(const Arguments& args){ - cerr << "in mongoFind" << endl; jsassert( args.Length() == 5 , "find needs 5 args" ); jsassert( args[1]->IsObject() , "needs to be an object" ); - DBClientConnection * conn = getConnection( args ); + DBClientBase * conn = getConnection( args ); GETNS; BSONObj q = v8ToMongo( args[1]->ToObject() ); @@ -148,7 +161,7 @@ namespace mongo { jsassert( args.Length() == 2 , "insert needs 2 args" ); jsassert( args[1]->IsObject() , "have to insert an object" ); - DBClientConnection * conn = getConnection( args ); + DBClientBase * conn = getConnection( args ); GETNS; v8::Handle<v8::Object> in = args[1]->ToObject(); @@ -175,7 +188,7 @@ namespace mongo { jsassert( args.Length() == 2 , "remove needs 2 args" ); jsassert( args[1]->IsObject() , "have to remove an object template" ); - DBClientConnection * conn = getConnection( args ); + DBClientBase * conn = getConnection( args ); GETNS; v8::Handle<v8::Object> in = args[1]->ToObject(); @@ -197,7 +210,7 @@ namespace mongo { jsassert( args[1]->IsObject() , "1st param to update has to be an object" ); jsassert( args[2]->IsObject() , "2nd param to update has to be an object" ); - DBClientConnection * conn = getConnection( args ); + DBClientBase * conn = getConnection( args ); GETNS; v8::Handle<v8::Object> q = args[1]->ToObject(); diff --git a/scripting/v8_db.h b/scripting/v8_db.h index 4042c6b1e98..4761d207dfd 100644 --- a/scripting/v8_db.h +++ b/scripting/v8_db.h @@ -16,10 +16,12 @@ namespace mongo { // the actual globals - mongo::DBClientConnection * getConnection( const v8::Arguments& args ); + mongo::DBClientBase * getConnection( const v8::Arguments& args ); // Mongo members - v8::Handle<v8::Value> mongoInit(const v8::Arguments& args); + v8::Handle<v8::Value> mongoConsLocal(const v8::Arguments& args); + v8::Handle<v8::Value> mongoConsExternal(const v8::Arguments& args); + v8::Handle<v8::Value> mongoFind(const v8::Arguments& args); v8::Handle<v8::Value> mongoInsert(const v8::Arguments& args); v8::Handle<v8::Value> mongoRemove(const v8::Arguments& args); |