diff options
author | Aaron <aaron@10gen.com> | 2009-12-29 11:13:08 -0800 |
---|---|---|
committer | Aaron <aaron@10gen.com> | 2009-12-29 11:13:08 -0800 |
commit | 453aa1977ebb8a897ae803bc093fa8d09a4afe26 (patch) | |
tree | 0690c3e1740ea372a22af9667b175831b049a7e6 /scripting/engine_v8.cpp | |
parent | 91362b9e354970f54a2acab9cf7089bf603713e1 (diff) | |
download | mongo-453aa1977ebb8a897ae803bc093fa8d09a4afe26.tar.gz |
SERVER-446 MINOR refactor usage of v8 raii objects
Diffstat (limited to 'scripting/engine_v8.cpp')
-rw-r--r-- | scripting/engine_v8.cpp | 84 |
1 files changed, 28 insertions, 56 deletions
diff --git a/scripting/engine_v8.cpp b/scripting/engine_v8.cpp index 320a29f9256..b95cfc00b01 100644 --- a/scripting/engine_v8.cpp +++ b/scripting/engine_v8.cpp @@ -21,6 +21,8 @@ #include "v8_utils.h" #include "v8_db.h" +#define V8_SIMPLE_HEADER Locker l; HandleScope handle_scope; Context::Scope context_scope( _context ); + namespace mongo { // --- engine --- @@ -129,46 +131,34 @@ namespace mongo { } void V8Scope::setNumber( const char * field , double val ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER _global->Set( v8::String::New( field ) , v8::Number::New( val ) ); } void V8Scope::setString( const char * field , const char * val ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER _global->Set( v8::String::New( field ) , v8::String::New( val ) ); } void V8Scope::setBoolean( const char * field , bool val ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER _global->Set( v8::String::New( field ) , v8::Boolean::New( val ) ); } void V8Scope::setElement( const char *field , const BSONElement& e ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER _global->Set( v8::String::New( field ) , mongoToV8Element( e ) ); } void V8Scope::setObject( const char *field , const BSONObj& obj , bool readOnly){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER // Set() accepts a ReadOnly parameter, but this just prevents the field itself // from being overwritten and doesn't protect the object stored in 'field'. _global->Set( v8::String::New( field ) , mongoToV8( obj, false, readOnly) ); } int V8Scope::type( const char *field ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER Handle<Value> v = get( field ); if ( v->IsNull() ) return jstNULL; @@ -203,44 +193,32 @@ namespace mongo { } double V8Scope::getNumber( const char *field ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER return get( field )->ToNumber()->Value(); } int V8Scope::getNumberInt( const char *field ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER return get( field )->ToInt32()->Value(); } long long V8Scope::getNumberLongLong( const char *field ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER return get( field )->ToInteger()->Value(); } string V8Scope::getString( const char *field ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER return toSTLString( get( field ) ); } bool V8Scope::getBoolean( const char *field ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER return get( field )->ToBoolean()->Value(); } BSONObj V8Scope::getObject( const char * field ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER Handle<Value> v = get( field ); if ( v->IsNull() || v->IsUndefined() ) return BSONObj(); @@ -251,10 +229,7 @@ namespace mongo { // --- functions ----- ScriptingFunction V8Scope::_createFunction( const char * raw ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); - + V8_SIMPLE_HEADER for(; isspace( *raw ); ++raw ); // skip whitespace string code = raw; if ( code.find( "function" ) == string::npos ){ @@ -300,10 +275,7 @@ namespace mongo { } void V8Scope::setThis( const BSONObj * obj ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); - + V8_SIMPLE_HEADER if ( ! obj ){ _this = Persistent< v8::Object >::New( v8::Object::New() ); return; @@ -316,9 +288,7 @@ namespace mongo { } int V8Scope::invoke( ScriptingFunction func , const BSONObj& argsObject, int timeoutMs , bool ignoreReturn ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER Handle<Value> funcValue = _funcs[func-1]; TryCatch try_catch; @@ -361,9 +331,7 @@ namespace mongo { } } - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER TryCatch try_catch; @@ -399,6 +367,14 @@ namespace mongo { return true; } + void V8Scope::injectNative( const char *field, NativeFunction func ){ + V8_SIMPLE_HEADER + + Handle< FunctionTemplate > f( v8::FunctionTemplate::New( nativeCallback ) ); + f->Set( v8::String::New( "_native_function" ), External::New( (void*)func ) ); + _global->Set( v8::String::New( field ), f->GetFunction() ); + } + void V8Scope::gc() { Locker l; while( V8::IdleNotification() ); @@ -407,9 +383,7 @@ namespace mongo { // ----- db access ----- void V8Scope::localConnect( const char * dbName ){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER if ( _connectState == EXTERNAL ) throw UserException( "externalSetup already called, can't call externalSetup" ); @@ -430,9 +404,7 @@ namespace mongo { } void V8Scope::externalSetup(){ - Locker l; - HandleScope handle_scope; - Context::Scope context_scope( _context ); + V8_SIMPLE_HEADER if ( _connectState == EXTERNAL ) return; |