summaryrefslogtreecommitdiff
path: root/src/mongo/scripting
diff options
context:
space:
mode:
authorBen Becker <ben.becker@10gen.com>2013-03-13 13:02:27 -0700
committerBen Becker <ben.becker@10gen.com>2013-03-13 13:08:49 -0700
commit7e33eb759679ca0693caaec8c1b12cd394a7285b (patch)
tree206443116a6c1d7824bb427dd72a490a87510518 /src/mongo/scripting
parentc5e0cd08b4f1c90bd80920bc5ab298d7a22eceee (diff)
downloadmongo-7e33eb759679ca0693caaec8c1b12cd394a7285b.tar.gz
SERVER-6646: fix round-trip of null values in v8
Diffstat (limited to 'src/mongo/scripting')
-rw-r--r--src/mongo/scripting/engine.cpp2
-rw-r--r--src/mongo/scripting/engine.h2
-rw-r--r--src/mongo/scripting/engine_spidermonkey.cpp2
-rw-r--r--src/mongo/scripting/engine_spidermonkey_internal.h2
-rw-r--r--src/mongo/scripting/engine_v8.cpp34
-rw-r--r--src/mongo/scripting/engine_v8.h5
6 files changed, 26 insertions, 21 deletions
diff --git a/src/mongo/scripting/engine.cpp b/src/mongo/scripting/engine.cpp
index fc4d042c2af..1886296f6a0 100644
--- a/src/mongo/scripting/engine.cpp
+++ b/src/mongo/scripting/engine.cpp
@@ -363,7 +363,7 @@ namespace mongo {
bool getBoolean(const char* field) { return _real->getBoolean(field); }
BSONObj getObject(const char* field) { return _real->getObject(field); }
void setNumber(const char* field, double val) { _real->setNumber(field, val); }
- void setString(const char* field, const char* val) { _real->setString(field, val); }
+ void setString(const char* field, const StringData& val) { _real->setString(field, val); }
void setElement(const char* field, const BSONElement& val) {
_real->setElement(field, val);
}
diff --git a/src/mongo/scripting/engine.h b/src/mongo/scripting/engine.h
index 8233128e736..0f269e93498 100644
--- a/src/mongo/scripting/engine.h
+++ b/src/mongo/scripting/engine.h
@@ -57,7 +57,7 @@ namespace mongo {
virtual void setElement(const char* field, const BSONElement& e) = 0;
virtual void setNumber(const char* field, double val) = 0;
- virtual void setString(const char* field, const char* val) = 0;
+ virtual void setString(const char* field, const StringData& val) = 0;
virtual void setObject(const char* field, const BSONObj& obj, bool readOnly=true) = 0;
virtual void setBoolean(const char* field, bool val) = 0;
virtual void setFunction(const char* field, const char* code) = 0;
diff --git a/src/mongo/scripting/engine_spidermonkey.cpp b/src/mongo/scripting/engine_spidermonkey.cpp
index 1a0cd117155..0547c90fb77 100644
--- a/src/mongo/scripting/engine_spidermonkey.cpp
+++ b/src/mongo/scripting/engine_spidermonkey.cpp
@@ -1494,7 +1494,7 @@ namespace spidermonkey {
verify( JS_SetProperty( _context , _global , field , &v ) );
}
- void SMScope::setString( const char *field , const char * val ) {
+ void SMScope::setString( const char *field , const StringData& val ) {
smlock;
jsval v = _convertor->toval( val );
verify( JS_SetProperty( _context , _global , field , &v ) );
diff --git a/src/mongo/scripting/engine_spidermonkey_internal.h b/src/mongo/scripting/engine_spidermonkey_internal.h
index 26d772c84f7..70226e054dc 100644
--- a/src/mongo/scripting/engine_spidermonkey_internal.h
+++ b/src/mongo/scripting/engine_spidermonkey_internal.h
@@ -216,7 +216,7 @@ namespace spidermonkey {
void setElement( const char *field , const BSONElement& val );
void setNumber( const char *field , double val );
- void setString( const char *field , const char * val );
+ void setString( const char *field , const StringData& val );
void setObject( const char *field , const BSONObj& obj , bool readOnly );
void setBoolean( const char *field , bool val );
void setThis( const BSONObj * obj );
diff --git a/src/mongo/scripting/engine_v8.cpp b/src/mongo/scripting/engine_v8.cpp
index 52ca1944d41..96eb70a0fb9 100644
--- a/src/mongo/scripting/engine_v8.cpp
+++ b/src/mongo/scripting/engine_v8.cpp
@@ -659,9 +659,9 @@ namespace mongo {
_global->ForceSet(v8StringData(field), v8::Number::New(val));
}
- void V8Scope::setString(const char * field, const char * val) {
+ void V8Scope::setString(const char * field, const StringData& val) {
V8_SIMPLE_HEADER
- _global->ForceSet(v8StringData(field), v8::String::New(val));
+ _global->ForceSet(v8StringData(field), v8::String::New(val.rawData(), val.size()));
}
void V8Scope::setBoolean(const char * field, bool val) {
@@ -897,8 +897,9 @@ namespace mongo {
string fn = str::stream() << "_funcs" << functionNumber;
code = str::stream() << fn << " = " << code;
- v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New(code.c_str()),
- v8::String::New(fn.c_str()));
+ v8::Handle<v8::Script> script = v8::Script::Compile(
+ v8::String::New(code.c_str(), code.length()),
+ v8::String::New(fn.c_str()));
// throw on error
checkV8ErrorState(script, try_catch);
@@ -1012,7 +1013,7 @@ namespace mongo {
v8::Handle<v8::Script> script =
v8::Script::Compile(v8::String::New(code.rawData(), code.size()),
- v8::String::New(name.c_str()));
+ v8::String::New(name.c_str(), name.length()));
if (checkV8ErrorState(script, try_catch, reportError, assertOnError))
return false;
@@ -1224,12 +1225,13 @@ namespace mongo {
registerOpId();
}
- v8::Local<v8::Value> V8Scope::newFunction(const char *code) {
+ v8::Local<v8::Value> V8Scope::newFunction(const StringData& code) {
v8::HandleScope handle_scope;
v8::TryCatch try_catch;
string codeStr = str::stream() << "____MongoToV8_newFunction_temp = " << code;
- v8::Local<v8::Script> compiled = v8::Script::New(v8::String::New(codeStr.c_str()));
+ v8::Local<v8::Script> compiled = v8::Script::New(v8::String::New(codeStr.c_str(),
+ codeStr.length()));
// throw on compile error
checkV8ErrorState(compiled, try_catch);
@@ -1246,7 +1248,8 @@ namespace mongo {
v8::HandleScope handle_scope;
v8::Function* idCons = this->getObjectIdCons();
v8::Handle<v8::Value> argv[1];
- argv[0] = v8::String::New(id.str().c_str());
+ const string& idString = id.str();
+ argv[0] = v8::String::New(idString.c_str(), idString.length());
return handle_scope.Close(idCons->NewInstance(1, argv));
}
@@ -1311,20 +1314,21 @@ namespace mongo {
switch (f.type()) {
case mongo::Code:
- o->ForceSet(name, newFunction(f.valuestr()));
+ o->ForceSet(name, newFunction(StringData(f.valuestr(), f.valuestrsize() - 1)));
break;
case CodeWScope:
if (!f.codeWScopeObject().isEmpty())
log() << "warning: CodeWScope doesn't transfer to db.eval" << endl;
- o->ForceSet(name, newFunction(f.codeWScopeCode()));
+ o->ForceSet(name, newFunction(StringData(f.codeWScopeCode(), f.codeWScopeCodeLen() - 1)));
break;
case mongo::Symbol:
case mongo::String:
- o->ForceSet(name, v8::String::New(f.valuestr()));
+ o->ForceSet(name, v8::String::New(f.valuestr(), f.valuestrsize() - 1));
break;
case mongo::jstOID: {
v8::Function * idCons = getObjectIdCons();
- argv[0] = v8::String::New(f.__oid().str().c_str());
+ const string& oidString = f.__oid().str();
+ argv[0] = v8::String::New(oidString.c_str(), oidString.length());
o->ForceSet(name, idCons->NewInstance(1, argv));
break;
}
@@ -1515,14 +1519,14 @@ namespace mongo {
switch (elem.type()) {
case mongo::Code:
- return newFunction(elem.valuestr());
+ return newFunction(StringData(elem.valuestr(), elem.valuestrsize() - 1));
case CodeWScope:
if (!elem.codeWScopeObject().isEmpty())
log() << "warning: CodeWScope doesn't transfer to db.eval" << endl;
- return newFunction(elem.codeWScopeCode());
+ return newFunction(StringData(elem.codeWScopeCode(), elem.codeWScopeCodeLen() - 1));
case mongo::Symbol:
case mongo::String:
- return v8::String::New(elem.valuestr());
+ return v8::String::New(elem.valuestr(), elem.valuestrsize() - 1);
case mongo::jstOID:
return newId(elem.__oid());
case mongo::NumberDouble:
diff --git a/src/mongo/scripting/engine_v8.h b/src/mongo/scripting/engine_v8.h
index 1406da51cac..e8edc8bbba6 100644
--- a/src/mongo/scripting/engine_v8.h
+++ b/src/mongo/scripting/engine_v8.h
@@ -21,6 +21,7 @@
#include <vector>
#include "mongo/base/disallow_copying.h"
+#include "mongo/base/string_data.h"
#include "mongo/client/dbclientinterface.h"
#include "mongo/client/dbclientcursor.h"
#include "mongo/scripting/engine.h"
@@ -180,7 +181,7 @@ namespace mongo {
virtual BSONObj getObject(const char* field);
virtual void setNumber(const char* field, double val);
- virtual void setString(const char* field, const char* val);
+ virtual void setString(const char* field, const StringData& val);
virtual void setBoolean(const char* field, bool val);
virtual void setElement(const char* field, const BSONElement& e);
virtual void setObject(const char* field, const BSONObj& obj, bool readOnly);
@@ -356,7 +357,7 @@ namespace mongo {
/**
* Create a new function; primarily used for BSON/V8 conversion.
*/
- v8::Local<v8::Value> newFunction(const char *code);
+ v8::Local<v8::Value> newFunction(const StringData& code);
template <typename _HandleType>
bool checkV8ErrorState(const _HandleType& resultHandle,