summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2010-09-01 01:32:04 -0400
committerEliot Horowitz <eliot@10gen.com>2010-09-01 01:32:35 -0400
commitb416e09061258dcb0af8ab30147b0eb0dcb0cbf1 (patch)
tree48aef2ffc076ef1863c95c1984b4c9ec9f9e2bd6
parenta6e3b04263d32885d4462d4b34c7f77badef820e (diff)
downloadmongo-b416e09061258dcb0af8ab30147b0eb0dcb0cbf1.tar.gz
Scope::rename and ability to block db access prep for SERVER-1715
-rw-r--r--dbtests/jstests.cpp24
-rw-r--r--scripting/engine.cpp4
-rw-r--r--scripting/engine.h18
-rw-r--r--scripting/engine_spidermonkey.cpp9
-rw-r--r--scripting/engine_v8.cpp8
-rw-r--r--scripting/engine_v8.h2
6 files changed, 63 insertions, 2 deletions
diff --git a/dbtests/jstests.cpp b/dbtests/jstests.cpp
index 3881cc10113..878639d74b1 100644
--- a/dbtests/jstests.cpp
+++ b/dbtests/jstests.cpp
@@ -938,6 +938,27 @@ namespace JSTests {
}
};
+ class RenameTest {
+ public:
+ void run(){
+ auto_ptr<Scope> s;
+ s.reset( globalScriptEngine->newScope() );
+
+ s->setNumber( "x" , 5 );
+ ASSERT_EQUALS( 5 , s->getNumber( "x" ) );
+ ASSERT_EQUALS( Undefined , s->type( "y" ) );
+
+ s->rename( "x" , "y" );
+ ASSERT_EQUALS( 5 , s->getNumber( "y" ) );
+ ASSERT_EQUALS( Undefined , s->type( "x" ) );
+
+ s->rename( "y" , "x" );
+ ASSERT_EQUALS( 5 , s->getNumber( "x" ) );
+ ASSERT_EQUALS( Undefined , s->type( "y" ) );
+ }
+ };
+
+
class All : public Suite {
public:
All() : Suite( "js" ) {
@@ -960,7 +981,8 @@ namespace JSTests {
add< TypeConservation >();
add< NumberLong >();
add< NumberLong2 >();
-
+ add< RenameTest >();
+
add< WeirdObjects >();
add< CodeTests >();
add< DBRefTest >();
diff --git a/scripting/engine.cpp b/scripting/engine.cpp
index 91f386f0ca4..4367b758d98 100644
--- a/scripting/engine.cpp
+++ b/scripting/engine.cpp
@@ -385,6 +385,10 @@ namespace mongo {
return _real->createFunction( code );
}
+ void rename( const char * from , const char * to ){
+ _real->rename( from , to );
+ }
+
/**
* @return 0 on success
*/
diff --git a/scripting/engine.h b/scripting/engine.h
index 046f1778148..995b527e0a8 100644
--- a/scripting/engine.h
+++ b/scripting/engine.h
@@ -54,7 +54,22 @@ namespace mongo {
virtual void localConnect( const char * dbName ) = 0;
virtual void externalSetup() = 0;
-
+
+ class NoDBAccess {
+ Scope * _s;
+ public:
+ NoDBAccess( Scope * s ){
+ _s = s;
+ }
+ ~NoDBAccess(){
+ _s->rename( "____db____" , "db" );
+ }
+ };
+ NoDBAccess disableDBAccess( const char * why ){
+ rename( "db" , "____db____" );
+ return NoDBAccess( this );
+ }
+
virtual double getNumber( const char *field ) = 0;
virtual int getNumberInt( const char *field ){ return (int)getNumber( field ); }
virtual long long getNumberLongLong( const char *field ){ return (long long)getNumber( field ); }
@@ -75,6 +90,7 @@ namespace mongo {
virtual ScriptingFunction createFunction( const char * code );
+ virtual void rename( const char * from , const char * to ) = 0;
/**
* @return 0 on success
*/
diff --git a/scripting/engine_spidermonkey.cpp b/scripting/engine_spidermonkey.cpp
index cb3ad2092a3..8952b339702 100644
--- a/scripting/engine_spidermonkey.cpp
+++ b/scripting/engine_spidermonkey.cpp
@@ -1323,6 +1323,15 @@ namespace mongo {
}
}
+ void rename( const char * from , const char * to ){
+ smlock;
+ jsval v;
+ assert( JS_GetProperty( _context , _global , from , &v ) );
+ assert( JS_SetProperty( _context , _global , to , &v ) );
+ v = JSVAL_VOID;
+ assert( JS_SetProperty( _context , _global , from , &v ) );
+ }
+
// ---- functions -----
ScriptingFunction _createFunction( const char * code ){
diff --git a/scripting/engine_v8.cpp b/scripting/engine_v8.cpp
index 99da4a3309e..38b78aa748d 100644
--- a/scripting/engine_v8.cpp
+++ b/scripting/engine_v8.cpp
@@ -296,6 +296,14 @@ namespace mongo {
argv[0] = v8::External::New( createWrapperHolder( obj , true , false ) );
_this = Persistent< v8::Object >::New( _wrapper->NewInstance( 1, argv ) );
}
+
+ void V8Scope::rename( const char * from , const char * to ){
+ V8_SIMPLE_HEADER;
+ v8::Local<v8::String> f = v8::String::New( from );
+ v8::Local<v8::String> t = v8::String::New( to );
+ _global->Set( t , _global->Get( f ) );
+ _global->Set( f , v8::Undefined() );
+ }
int V8Scope::invoke( ScriptingFunction func , const BSONObj& argsObject, int timeoutMs , bool ignoreReturn ){
V8_SIMPLE_HEADER
diff --git a/scripting/engine_v8.h b/scripting/engine_v8.h
index f9689b68926..ca44f4843b8 100644
--- a/scripting/engine_v8.h
+++ b/scripting/engine_v8.h
@@ -55,6 +55,8 @@ namespace mongo {
virtual void setElement( const char *field , const BSONElement& e );
virtual void setObject( const char *field , const BSONObj& obj , bool readOnly);
virtual void setThis( const BSONObj * obj );
+
+ virtual void rename( const char * from , const char * to );
virtual ScriptingFunction _createFunction( const char * code );
Local< v8::Function > __createFunction( const char * code );