diff --git a/scripting/engine_v8.cpp b/scripting/engine_v8.cpp index 11a7ca5bb65..bfc2e92b045 100644 --- a/scripting/engine_v8.cpp +++ b/scripting/engine_v8.cpp @@ -72,22 +72,22 @@ namespace mongo { _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" )); + V8STR_CONN = getV8Str( "_conn" ); + V8STR_ID = getV8Str( "_id" ); + V8STR_LENGTH = getV8Str( "length" ); + V8STR_ISOBJECTID = getV8Str( "isObjectId" ); + V8STR_RETURN = getV8Str( "return" ); + V8STR_ARGS = getV8Str( "args" ); + V8STR_T = getV8Str( "t" ); + V8STR_I = getV8Str( "i" ); + V8STR_EMPTY = getV8Str( "" ); + V8STR_MINKEY = getV8Str( "$MinKey" ); + V8STR_MAXKEY = getV8Str( "$MaxKey" ); + V8STR_NUMBERLONG = getV8Str( "__NumberLong" ); + V8STR_DBPTR = getV8Str( "__DBPointer" ); + V8STR_BINDATA = getV8Str( "__BinData" ); + V8STR_NATIVE_FUNC = getV8Str( "_native_function" ); + V8STR_V8_FUNC = getV8Str( "_v8_function" ); injectV8Function("print", Print); injectV8Function("version", Version); @@ -110,6 +110,12 @@ namespace mongo { _funcs.clear(); _global.Dispose(); _context.Dispose(); + std::map >::iterator it = _strCache.begin(); + std::map >::iterator end = _strCache.end(); + while (it != end) { + it->second.Dispose(); + ++it; + } } /** @@ -513,26 +519,24 @@ namespace mongo { obj->Set( v8::String::New( field ), ft->GetFunction() ); } - Handle V8Scope::injectV8Function( const char *field, v8Function func ) { - return injectV8Function(field, func, _global); + void V8Scope::injectV8Function( const char *field, v8Function func ) { + injectV8Function(field, func, _global); } - Handle V8Scope::injectV8Function( const char *field, v8Function func, Handle& obj ) { + void V8Scope::injectV8Function( const char *field, v8Function func, Handle& obj ) { V8_SIMPLE_HEADER Handle< FunctionTemplate > ft = createV8Function(func); Handle f = ft->GetFunction(); obj->Set( v8::String::New( field ), f ); - return f; } - Handle V8Scope::injectV8Function( const char *field, v8Function func, Handle& t ) { + void V8Scope::injectV8Function( const char *field, v8Function func, Handle& t ) { V8_SIMPLE_HEADER Handle< FunctionTemplate > ft = createV8Function(func); Handle f = ft->GetFunction(); t->Set( v8::String::New( field ), f ); - return f; } Handle V8Scope::createV8Function( v8Function func ) { @@ -1161,4 +1165,18 @@ namespace mongo { return v8::Undefined(); } + /** + * Gets a V8 strings from the scope's cache, creating one if needed + */ + v8::Persistent V8Scope::getV8Str(string str) { + Persistent ptr = _strCache[str]; + if (ptr.IsEmpty()) { + ptr = Persistent::New(v8::String::New(str.c_str())); + _strCache[str] = ptr; +// cout << "Adding str " + str << endl; + } +// cout << "Returning str " + str << endl; + return ptr; + } + } // namespace mongo diff --git a/scripting/engine_v8.h b/scripting/engine_v8.h index e42522f029b..e242374f427 100644 --- a/scripting/engine_v8.h +++ b/scripting/engine_v8.h @@ -97,9 +97,9 @@ namespace mongo { virtual void injectNative( const char *field, NativeFunction func ); void injectNative( const char *field, NativeFunction func, Handle& obj ); - Handle injectV8Function( const char *field, v8Function func ); - Handle injectV8Function( const char *field, v8Function func, Handle& obj ); - Handle injectV8Function( const char *field, v8Function func, Handle& t ); + void injectV8Function( const char *field, v8Function func ); + void injectV8Function( const char *field, v8Function func, Handle& obj ); + void injectV8Function( const char *field, v8Function func, Handle& t ); Handle createV8Function( v8Function func ); void gc(); @@ -117,6 +117,8 @@ namespace mongo { v8::Function * getObjectIdCons(); Local< v8::Value > newId( const OID &id ); + v8::Persistent getV8Str(string str); + Persistent V8STR_CONN; Persistent V8STR_ID; Persistent V8STR_LENGTH; @@ -159,6 +161,8 @@ namespace mongo { enum ConnectState { NOT , LOCAL , EXTERNAL }; ConnectState _connectState; + + std::map > _strCache; }; class V8ScriptEngine : public ScriptEngine {