//engine_v8.cpp /* Copyright 2009 10gen Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "engine_v8.h" #include "v8_wrapper.h" #include "v8_utils.h" #include "v8_db.h" #define V8_SIMPLE_HEADER V8Lock l; HandleScope handle_scope; Context::Scope context_scope( _context ); namespace mongo { // guarded by v8 mutex map< unsigned, int > __interruptSpecToThreadId; // --- engine --- V8ScriptEngine::V8ScriptEngine() { } V8ScriptEngine::~V8ScriptEngine() { } void ScriptEngine::setup() { if ( !globalScriptEngine ) { globalScriptEngine = new V8ScriptEngine(); } } void V8ScriptEngine::interrupt( unsigned opSpec ) { v8::Locker l; if ( __interruptSpecToThreadId.count( opSpec ) ) { V8::TerminateExecution( __interruptSpecToThreadId[ opSpec ] ); } } void V8ScriptEngine::interruptAll() { v8::Locker l; vector< int > toKill; // v8 mutex could potentially be yielded during the termination call for( map< unsigned, int >::const_iterator i = __interruptSpecToThreadId.begin(); i != __interruptSpecToThreadId.end(); ++i ) { toKill.push_back( i->second ); } for( vector< int >::const_iterator i = toKill.begin(); i != toKill.end(); ++i ) { V8::TerminateExecution( *i ); } } // --- scope --- V8Scope::V8Scope( V8ScriptEngine * engine ) : _engine( engine ) , _connectState( NOT ) { V8Lock l; HandleScope handleScope; _context = Context::New(); Context::Scope context_scope( _context ); _global = Persistent< v8::Object >::New( _context->Global() ); _this = Persistent< v8::Object >::New( v8::Object::New() ); V8STR_CONN = Persistent::New(v8::String::New( "_conn" )); V8STR_ID = Persistent::New(v8::String::New( "_id" )); V8STR_LENGTH = Persistent::New(v8::String::New( "length" )); V8STR_ISOBJECTID = Persistent::New(v8::String::New( "isObjectId" )); V8STR_RETURN = Persistent::New(v8::String::New( "return" )); V8STR_ARGS = Persistent::New(v8::String::New( "args" )); V8STR_T = Persistent::New(v8::String::New( "t" )); V8STR_I = Persistent::New(v8::String::New( "i" )); V8STR_EMPTY = Persistent::New(v8::String::New( "" )); V8STR_MINKEY = Persistent::New(v8::String::New( "$MinKey" )); V8STR_MAXKEY = Persistent::New(v8::String::New( "$MaxKey" )); V8STR_NUMBERLONG = Persistent::New(v8::String::New( "__NumberLong" )); V8STR_DBPTR = Persistent::New(v8::String::New( "__DBPointer" )); V8STR_BINDATA = Persistent::New(v8::String::New( "__BinData" )); V8STR_NATIVE_FUNC = Persistent::New(v8::String::New( "_native_function" )); V8STR_V8_FUNC = Persistent::New(v8::String::New( "_v8_function" )); injectV8Function("print", Print); injectV8Function("version", Version); injectV8Function("load", load); _wrapper = Persistent< v8::Function >::New( getObjectWrapperTemplate(this)->GetFunction() ); injectV8Function("gc", GCV8); installDBTypes( this, _global ); } V8Scope::~V8Scope() { V8Lock l; Context::Scope context_scope( _context ); _wrapper.Dispose(); _this.Dispose(); for( unsigned i = 0; i < _funcs.size(); ++i ) _funcs[ i ].Dispose(); _funcs.clear(); _global.Dispose(); _context.Dispose(); } /** * JS Callback that will call a c++ function with BSON arguments. */ Handle< Value > V8Scope::nativeCallback( V8Scope* scope, const Arguments &args ) { V8Lock l; HandleScope handle_scope; Local< External > f = External::Cast( *args.Callee()->Get( scope->V8STR_NATIVE_FUNC ) ); NativeFunction function = (NativeFunction)(f->Value()); BSONObjBuilder b; for( int i = 0; i < args.Length(); ++i ) { stringstream ss; ss << i; scope->v8ToMongoElement( b, scope->V8STR_EMPTY, ss.str(), args[ i ] ); } BSONObj nativeArgs = b.obj(); BSONObj ret; try { ret = function( nativeArgs ); } catch( const std::exception &e ) { return v8::ThrowException(v8::String::New(e.what())); } catch( ... ) { return v8::ThrowException(v8::String::New("unknown exception")); } return handle_scope.Close( scope->mongoToV8Element( ret.firstElement() ) ); } Handle< Value > V8Scope::load( V8Scope* scope, const Arguments &args ) { Context::Scope context_scope(scope->_context); for (int i = 0; i < args.Length(); ++i) { std::string filename(toSTLString(args[i])); if (!scope->execFile(filename, false , true , false)) { return v8::ThrowException(v8::String::New((std::string("error loading file: ") + filename).c_str())); } } return v8::True(); } /** * JS Callback that will call a c++ function with the v8 scope and v8 arguments. * Handles interrupts, exception handling, etc * * The implementation below assumes that SERVER-1816 has been fixed - in * particular, interrupted() must return true if an interrupt was ever * sent; currently that is not the case if a new killop overwrites the data * for an old one */ v8::Handle< v8::Value > V8Scope::v8Callback( const v8::Arguments &args ) { disableV8Interrupt(); // we don't want to have to audit all v8 calls for termination exceptions, so we don't allow these exceptions during the callback if ( globalScriptEngine->interrupted() ) { v8::V8::TerminateExecution(); // experimentally it seems that TerminateExecution() will override the return value return v8::Undefined(); } Local< External > f = External::Cast( *args.Callee()->Get( v8::String::New( "_v8_function" ) ) ); v8Function function = (v8Function)(f->Value()); Local< External > scp = External::Cast( *args.Data() ); V8Scope* scope = (V8Scope*)(scp->Value()); v8::Handle< v8::Value > ret; string exception; try { ret = function( scope, args ); } catch( const std::exception &e ) { exception = e.what(); } catch( ... ) { exception = "unknown exception"; } enableV8Interrupt(); if ( globalScriptEngine->interrupted() ) { v8::V8::TerminateExecution(); return v8::Undefined(); } if ( !exception.empty() ) { // technically, ThrowException is supposed to be the last v8 call before returning ret = v8::ThrowException( v8::String::New( exception.c_str() ) ); } return ret; } // ---- global stuff ---- void V8Scope::init( const BSONObj * data ) { V8Lock l; if ( ! data ) return; BSONObjIterator i( *data ); while ( i.more() ) { BSONElement e = i.next(); setElement( e.fieldName() , e ); } } void V8Scope::setNumber( const char * field , double val ) { V8_SIMPLE_HEADER _global->Set( v8::String::New( field ) , v8::Number::New( val ) ); } void V8Scope::setString( const char * field , const char * val ) { V8_SIMPLE_HEADER _global->Set( v8::String::New( field ) , v8::String::New( val ) ); } void V8Scope::setBoolean( const char * field , bool val ) { V8_SIMPLE_HEADER _global->Set( v8::String::New( field ) , v8::Boolean::New( val ) ); } void V8Scope::setElement( const char *field , const BSONElement& e ) { V8_SIMPLE_HEADER _global->Set( v8::String::New( field ) , mongoToV8Element( e ) ); } void V8Scope::setObject( const char *field , const BSONObj& obj , bool readOnly) { 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 ) { V8_SIMPLE_HEADER Handle v = get( field ); if ( v->IsNull() ) return jstNULL; if ( v->IsUndefined() ) return Undefined; if ( v->IsString() ) return String; if ( v->IsFunction() ) return Code; if ( v->IsArray() ) return Array; if ( v->IsBoolean() ) return Bool; if ( v->IsInt32() ) return NumberInt; if ( v->IsNumber() ) return NumberDouble; if ( v->IsExternal() ) { uassert( 10230 , "can't handle external yet" , 0 ); return -1; } if ( v->IsDate() ) return Date; if ( v->IsObject() ) return Object; throw UserException( 12509, (string)"don't know what this is: " + field ); } v8::Handle V8Scope::get( const char * field ) { return _global->Get( v8::String::New( field ) ); } double V8Scope::getNumber( const char *field ) { V8_SIMPLE_HEADER return get( field )->ToNumber()->Value(); } int V8Scope::getNumberInt( const char *field ) { V8_SIMPLE_HEADER return get( field )->ToInt32()->Value(); } long long V8Scope::getNumberLongLong( const char *field ) { V8_SIMPLE_HEADER return get( field )->ToInteger()->Value(); } string V8Scope::getString( const char *field ) { V8_SIMPLE_HEADER return toSTLString( get( field ) ); } bool V8Scope::getBoolean( const char *field ) { V8_SIMPLE_HEADER return get( field )->ToBoolean()->Value(); } BSONObj V8Scope::getObject( const char * field ) { V8_SIMPLE_HEADER Handle v = get( field ); if ( v->IsNull() || v->IsUndefined() ) return BSONObj(); uassert( 10231 , "not an object" , v->IsObject() ); return v8ToMongo( v->ToObject() ); } // --- functions ----- bool hasFunctionIdentifier( const string& code ) { if ( code.size() < 9 || code.find( "function" ) != 0 ) return false; return code[8] == ' ' || code[8] == '('; } Local< v8::Function > V8Scope::__createFunction( const char * raw ) { raw = jsSkipWhiteSpace( raw ); string code = raw; if ( !hasFunctionIdentifier( code ) ) { if ( code.find( "\n" ) == string::npos && ! hasJSReturn( code ) && ( code.find( ";" ) == string::npos || code.find( ";" ) == code.size() - 1 ) ) { code = "return " + code; } code = "function(){ " + code + "}"; } int num = _funcs.size() + 1; string fn; { stringstream ss; ss << "_funcs" << num; fn = ss.str(); } code = fn + " = " + code; TryCatch try_catch; // this might be time consuming, consider allowing an interrupt Handle