diff --git a/SConstruct b/SConstruct index 3e1061a2c72..6947399e5a7 100644 --- a/SConstruct +++ b/SConstruct @@ -315,7 +315,7 @@ commonFiles += [ "util/background.cpp" , "util/mmap.cpp" , "util/sock.cpp" , " "util/thread_pool.cpp" ] commonFiles += Glob( "util/*.c" ) commonFiles += Split( "client/connpool.cpp client/dbclient.cpp client/model.cpp client/parallel.cpp client/syncclusterconnection.cpp" ) -commonFiles += [ "scripting/engine.cpp" ] +commonFiles += [ "scripting/engine.cpp" , "scripting/utils.cpp" ] #mmap stuff diff --git a/scripting/engine.h b/scripting/engine.h index 99c88cff167..137bc46f5e2 100644 --- a/scripting/engine.h +++ b/scripting/engine.h @@ -26,7 +26,7 @@ namespace mongo { typedef unsigned long long ScriptingFunction; typedef BSONObj (*NativeFunction) ( const BSONObj &args ); - + class Scope : boost::noncopyable { public: Scope(); @@ -117,6 +117,8 @@ namespace mongo { static int _numScopes; }; + void installGlobalUtils( Scope& scope ); + class ScriptEngine : boost::noncopyable { public: ScriptEngine(); @@ -126,6 +128,7 @@ namespace mongo { Scope *s = createScope(); if ( s && _scopeInitCallback ) _scopeInitCallback( *s ); + installGlobalUtils( *s ); return s; } diff --git a/scripting/utils.cpp b/scripting/utils.cpp new file mode 100644 index 00000000000..11ae7f3fcaa --- /dev/null +++ b/scripting/utils.cpp @@ -0,0 +1,36 @@ +// utils.cpp + +#include "../stdafx.h" +#include "engine.h" +#include "../util/md5.hpp" + +namespace mongo { + + BSONObj jsmd5( const BSONObj &a ){ + uassert( 10261 , "js md5 needs a string" , a.firstElement().type() == String ); + const char * s = a.firstElement().valuestrsafe(); + + md5digest d; + md5_state_t st; + md5_init(&st); + md5_append( &st , (const md5_byte_t*)s , strlen( s ) ); + md5_finish(&st, d); + + return BSON( "" << digestToString( d ) ); + } + + BSONObj JSVersion( const BSONObj& args ){ + cout << "version: " << versionString << endl; + if ( strstr( versionString , "+" ) ) + printGitVersion(); + return BSONObj(); + } + + void installGlobalUtils( Scope& scope ){ + scope.injectNative( "hex_md5" , jsmd5 ); + scope.injectNative( "version" , JSVersion ); + } + +} + + diff --git a/shell/utils.cpp b/shell/utils.cpp index 2bbb417cbe0..5adbee31de6 100644 --- a/shell/utils.cpp +++ b/shell/utils.cpp @@ -18,7 +18,6 @@ #include "../client/dbclient.h" #include "../util/processinfo.h" -#include "../util/md5.hpp" #include "utils.h" extern const char * jsconcatcode_server; @@ -139,13 +138,6 @@ namespace mongo { return b.obj(); } - BSONObj JSVersion( const BSONObj& args ){ - cout << "version: " << versionString << endl; - if ( strstr( versionString , "+" ) ) - printGitVersion(); - return BSONObj(); - } - #ifndef _WIN32 #include #include @@ -474,19 +466,6 @@ namespace mongo { void KillMongoProgramInstances() {} #endif - BSONObj jsmd5( const BSONObj &a ){ - uassert( 10261 , "js md5 needs a string" , a.firstElement().type() == String ); - const char * s = a.firstElement().valuestrsafe(); - - md5digest d; - md5_state_t st; - md5_init(&st); - md5_append( &st , (const md5_byte_t*)s , strlen( s ) ); - md5_finish(&st, d); - - return BSON( "" << digestToString( d ) ); - } - unsigned _randomSeed; BSONObj JSSrand( const BSONObj &a ) { @@ -512,8 +491,6 @@ namespace mongo { scope.injectNative( "sleep" , JSSleep ); scope.injectNative( "quit", Quit ); scope.injectNative( "getMemInfo" , JSGetMemInfo ); - scope.injectNative( "version" , JSVersion ); - scope.injectNative( "hex_md5" , jsmd5 ); scope.injectNative( "_srand" , JSSrand ); scope.injectNative( "_rand" , JSRand ); #if !defined(_WIN32) diff --git a/shell/utils.h b/shell/utils.h index 7c98e2c5d38..01393a2e5d6 100644 --- a/shell/utils.h +++ b/shell/utils.h @@ -5,7 +5,7 @@ #include "../scripting/engine.h" namespace mongo { - + namespace shellUtils { extern std::string _dbConnect; @@ -13,7 +13,7 @@ namespace mongo { void RecordMyLocation( const char *_argv0 ); void installShellUtils( Scope& scope ); - + // Scoped management of mongo program instances. Simple implementation: // destructor kills all mongod instances created by the shell. struct MongoProgramScope {