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

SERVER-470 MINOR abstract shell init functionality so v8_utils need not link with shell

This commit is contained in:
Aaron 2009-12-29 18:27:49 -08:00
parent d1e3f2afe5
commit 8573d53da7
5 changed files with 34 additions and 26 deletions

View File

@ -47,7 +47,7 @@ namespace JSTests {
public: public:
void run(){ void run(){
auto_ptr<Scope> s; auto_ptr<Scope> s;
s.reset( globalScriptEngine->createScope() ); s.reset( globalScriptEngine->newScope() );
s->setNumber( "x" , 5 ); s->setNumber( "x" , 5 );
ASSERT( 5 == s->getNumber( "x" ) ); ASSERT( 5 == s->getNumber( "x" ) );
@ -74,7 +74,7 @@ namespace JSTests {
// Not worrying about this for now SERVER-446. // Not worrying about this for now SERVER-446.
/* /*
auto_ptr<Scope> s; auto_ptr<Scope> s;
s.reset( globalScriptEngine->createScope() ); s.reset( globalScriptEngine->newScope() );
s->setBoolean( "x" , true ); s->setBoolean( "x" , true );
ASSERT( s->getBoolean( "x" ) ); ASSERT( s->getBoolean( "x" ) );
@ -88,7 +88,7 @@ namespace JSTests {
class FalseTests { class FalseTests {
public: public:
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
ASSERT( ! s->getBoolean( "x" ) ); ASSERT( ! s->getBoolean( "x" ) );
@ -103,7 +103,7 @@ namespace JSTests {
class SimpleFunctions { class SimpleFunctions {
public: public:
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
s->invoke( "x=5;" , BSONObj() ); s->invoke( "x=5;" , BSONObj() );
ASSERT( 5 == s->getNumber( "x" ) ); ASSERT( 5 == s->getNumber( "x" ) );
@ -132,7 +132,7 @@ namespace JSTests {
class ObjectMapping { class ObjectMapping {
public: public:
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
BSONObj o = BSON( "x" << 17 << "y" << "eliot" << "z" << "sara" ); BSONObj o = BSON( "x" << 17 << "y" << "eliot" << "z" << "sara" );
s->setObject( "blah" , o ); s->setObject( "blah" , o );
@ -189,7 +189,7 @@ namespace JSTests {
class ObjectDecoding { class ObjectDecoding {
public: public:
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
s->invoke( "z = { num : 1 };" , BSONObj() ); s->invoke( "z = { num : 1 };" , BSONObj() );
BSONObj out = s->getObject( "z" ); BSONObj out = s->getObject( "z" );
@ -214,7 +214,7 @@ namespace JSTests {
public: public:
void run(){ void run(){
#ifdef MOZJS #ifdef MOZJS
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
s->localConnect( "blah" ); s->localConnect( "blah" );
@ -247,7 +247,7 @@ namespace JSTests {
class SetImplicit { class SetImplicit {
public: public:
void run() { void run() {
Scope *s = globalScriptEngine->createScope(); Scope *s = globalScriptEngine->newScope();
BSONObj o = BSON( "foo" << "bar" ); BSONObj o = BSON( "foo" << "bar" );
s->setObject( "a.b", o ); s->setObject( "a.b", o );
@ -268,7 +268,7 @@ namespace JSTests {
class ObjectModReadonlyTests { class ObjectModReadonlyTests {
public: public:
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
BSONObj o = BSON( "x" << 17 << "y" << "eliot" << "z" << "sara" << "zz" << BSONObj() ); BSONObj o = BSON( "x" << 17 << "y" << "eliot" << "z" << "sara" << "zz" << BSONObj() );
s->setObject( "blah" , o , true ); s->setObject( "blah" , o , true );
@ -315,7 +315,7 @@ namespace JSTests {
class OtherJSTypes { class OtherJSTypes {
public: public:
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
{ // date { // date
BSONObj o; BSONObj o;
@ -382,7 +382,7 @@ namespace JSTests {
class SpecialDBTypes { class SpecialDBTypes {
public: public:
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
BSONObjBuilder b; BSONObjBuilder b;
b.appendTimestamp( "a" , 123456789 ); b.appendTimestamp( "a" , 123456789 );
@ -418,7 +418,7 @@ namespace JSTests {
class TypeConservation { class TypeConservation {
public: public:
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
// -- A -- // -- A --
@ -527,7 +527,7 @@ namespace JSTests {
} }
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
s->localConnect( "blah" ); s->localConnect( "blah" );
@ -600,7 +600,7 @@ namespace JSTests {
class CodeTests { class CodeTests {
public: public:
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
{ {
BSONObjBuilder b; BSONObjBuilder b;
@ -687,7 +687,7 @@ namespace JSTests {
} }
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
s->localConnect( "asd" ); s->localConnect( "asd" );
const char * foo = "asdas\0asdasd"; const char * foo = "asdas\0asdasd";
@ -721,7 +721,7 @@ namespace JSTests {
class VarTests { class VarTests {
public: public:
void run(){ void run(){
Scope * s = globalScriptEngine->createScope(); Scope * s = globalScriptEngine->newScope();
ASSERT( s->exec( "a = 5;" , "a" , false , true , false ) ); ASSERT( s->exec( "a = 5;" , "a" , false , true , false ) );
ASSERT_EQUALS( 5 , s->getNumber("a" ) ); ASSERT_EQUALS( 5 , s->getNumber("a" ) );

View File

@ -380,7 +380,7 @@ namespace mongo {
Scope * s = scopeCache->get( pool ); Scope * s = scopeCache->get( pool );
if ( ! s ){ if ( ! s ){
s = createScope(); s = newScope();
} }
auto_ptr<Scope> p; auto_ptr<Scope> p;

View File

@ -122,7 +122,12 @@ namespace mongo {
ScriptEngine(); ScriptEngine();
virtual ~ScriptEngine(); virtual ~ScriptEngine();
virtual Scope * createScope() = 0; virtual Scope * newScope() {
Scope *s = createScope();
if ( s && _scopeInitCallback )
_scopeInitCallback( *s );
return s;
}
virtual void runTest() = 0; virtual void runTest() = 0;
@ -134,8 +139,15 @@ namespace mongo {
void threadDone(); void threadDone();
struct Unlocker { virtual ~Unlocker() {} }; struct Unlocker { virtual ~Unlocker() {} };
virtual auto_ptr<Unlocker> newThreadUnlocker() { return auto_ptr< Unlocker >( new Unlocker ); } virtual auto_ptr<Unlocker> newThreadUnlocker() { return auto_ptr< Unlocker >( new Unlocker ); }
void setScopeInitCallback( void ( *func )( Scope & ) ) { _scopeInitCallback = func; }
protected:
virtual Scope * createScope() = 0;
private:
void ( *_scopeInitCallback )( Scope & );
}; };
extern ScriptEngine * globalScriptEngine; extern ScriptEngine * globalScriptEngine;

View File

@ -26,7 +26,6 @@
#include <boost/thread/thread.hpp> #include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp> #include <boost/thread/xtime.hpp>
#include "engine_v8.h" #include "engine_v8.h"
#include <shell/utils.h>
using namespace std; using namespace std;
using namespace v8; using namespace v8;
@ -191,9 +190,7 @@ namespace mongo {
Handle< v8::Function > fun; Handle< v8::Function > fun;
auto_ptr< V8Scope > scope; auto_ptr< V8Scope > scope;
if ( config_.newScope_ ) { if ( config_.newScope_ ) {
scope.reset( dynamic_cast< V8Scope * >( globalScriptEngine->createScope() ) ); scope.reset( dynamic_cast< V8Scope * >( globalScriptEngine->newScope() ) );
// these lines duplicated from dbshell, extract to common location.
mongo::shellUtils::initScope( *scope );
context = scope->context(); context = scope->context();
// A v8::Function tracks the context in which it was created, so we have to // A v8::Function tracks the context in which it was created, so we have to
// create a new function in the new context. // create a new function in the new context.

View File

@ -255,7 +255,8 @@ int _main(int argc, char* argv[]) {
mongo::shellUtils::RecordMyLocation( argv[ 0 ] ); mongo::shellUtils::RecordMyLocation( argv[ 0 ] );
mongo::ScriptEngine::setup(); mongo::ScriptEngine::setup();
auto_ptr< mongo::Scope > scope( mongo::globalScriptEngine->createScope() ); mongo::globalScriptEngine->setScopeInitCallback( mongo::shellUtils::initScope );
auto_ptr< mongo::Scope > scope( mongo::globalScriptEngine->newScope() );
string url = "test"; string url = "test";
string dbhost; string dbhost;
@ -356,8 +357,6 @@ int _main(int argc, char* argv[]) {
} }
} }
} }
mongo::shellUtils::initScope( *scope );
cout << "MongoDB shell version: " << mongo::versionString << endl; cout << "MongoDB shell version: " << mongo::versionString << endl;