summaryrefslogtreecommitdiff
path: root/scripting
diff options
context:
space:
mode:
authoragirbal <antoine@10gen.com>2011-05-05 16:30:55 -0700
committeragirbal <antoine@10gen.com>2011-05-10 15:30:01 -0700
commit8d13203c40150d93b5747adf4430a8b6edd21ac9 (patch)
treed67d5f240f99b4e770dcd46e0b59fed5c928231d /scripting
parentff711f0d48ac22e403682925e53d11a980b92fea (diff)
downloadmongo-8d13203c40150d93b5747adf4430a8b6edd21ac9.tar.gz
SERVER-2976: added M/R pure jsMode with collection output
Diffstat (limited to 'scripting')
-rw-r--r--scripting/bench.cpp2
-rw-r--r--scripting/engine.cpp8
-rw-r--r--scripting/engine.h5
-rw-r--r--scripting/engine_spidermonkey.cpp10
-rw-r--r--scripting/engine_v8.cpp20
-rw-r--r--scripting/engine_v8.h6
-rw-r--r--scripting/utils.cpp6
7 files changed, 40 insertions, 17 deletions
diff --git a/scripting/bench.cpp b/scripting/bench.cpp
index bfd52b06416..f625c2434b9 100644
--- a/scripting/bench.cpp
+++ b/scripting/bench.cpp
@@ -89,7 +89,7 @@ namespace mongo {
/**
* benchRun( { ops : [] , host : XXX , db : XXXX , parallel : 5 , seconds : 5 }
*/
- BSONObj benchRun( const BSONObj& argsFake ) {
+ BSONObj benchRun( const BSONObj& argsFake, void* data ) {
assert( argsFake.firstElement().isABSONObj() );
BSONObj args = argsFake.firstElement().Obj();
diff --git a/scripting/engine.cpp b/scripting/engine.cpp
index eac00cadf14..ec1de4b00fc 100644
--- a/scripting/engine.cpp
+++ b/scripting/engine.cpp
@@ -398,6 +398,10 @@ namespace mongo {
// _real->setThis( obj );
// }
+ void setFunction( const char *field , const char * code ) {
+ _real->setFunction(field, code);
+ }
+
ScriptingFunction createFunction( const char * code ) {
return _real->createFunction( code );
}
@@ -428,8 +432,8 @@ namespace mongo {
return _real->execFile( filename , printResult , reportError , assertOnError , timeoutMs );
}
- void injectNative( const char *field, NativeFunction func ) {
- _real->injectNative( field , func );
+ void injectNative( const char *field, NativeFunction func, void* data ) {
+ _real->injectNative( field , func, data );
}
void gc() {
diff --git a/scripting/engine.h b/scripting/engine.h
index 65f49542102..eb2d90e6f55 100644
--- a/scripting/engine.h
+++ b/scripting/engine.h
@@ -28,7 +28,7 @@ namespace mongo {
};
typedef unsigned long long ScriptingFunction;
- typedef BSONObj (*NativeFunction) ( const BSONObj &args );
+ typedef BSONObj (*NativeFunction) ( const BSONObj &args, void* data );
class Scope : boost::noncopyable {
public:
@@ -76,6 +76,7 @@ namespace mongo {
virtual void setString( const char *field , const char * 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;
// virtual void setThis( const BSONObj * obj ) = 0;
virtual ScriptingFunction createFunction( const char * code );
@@ -113,7 +114,7 @@ namespace mongo {
virtual bool execFile( const string& filename , bool printResult , bool reportError , bool assertOnError, int timeoutMs = 0 );
- virtual void injectNative( const char *field, NativeFunction func ) = 0;
+ virtual void injectNative( const char *field, NativeFunction func, void* data = 0 ) = 0;
virtual void gc() = 0;
diff --git a/scripting/engine_spidermonkey.cpp b/scripting/engine_spidermonkey.cpp
index abb47054a7c..e7ada5d42d8 100644
--- a/scripting/engine_spidermonkey.cpp
+++ b/scripting/engine_spidermonkey.cpp
@@ -935,7 +935,7 @@ namespace mongo {
BSONObj out;
try {
- out = func( a );
+ out = func( a, 0 );
}
catch ( std::exception& e ) {
JS_ReportError( cx , e.what() );
@@ -1347,6 +1347,12 @@ namespace mongo {
}
}
+ void setFunction( const char *field , const char * code ) {
+ smlock;
+ jsval v = OBJECT_TO_JSVAL(JS_GetFunctionObject(_convertor->compileFunction(code)));
+ JS_SetProperty( _context , _global , field , &v );
+ }
+
void rename( const char * from , const char * to ) {
smlock;
jsval v;
@@ -1517,7 +1523,7 @@ namespace mongo {
return _error;
}
- void injectNative( const char *field, NativeFunction func ) {
+ void injectNative( const char *field, NativeFunction func, void* data ) {
smlock;
string name = field;
_convertor->setProperty( _global , (name + "_").c_str() , _convertor->toval( (double)(long long)func ) );
diff --git a/scripting/engine_v8.cpp b/scripting/engine_v8.cpp
index 45e9b0ab09f..b0286dd3fd3 100644
--- a/scripting/engine_v8.cpp
+++ b/scripting/engine_v8.cpp
@@ -215,6 +215,7 @@ namespace mongo {
V8STR_DBPTR = getV8Str( "__DBPointer" );
V8STR_BINDATA = getV8Str( "__BinData" );
V8STR_NATIVE_FUNC = getV8Str( "_native_function" );
+ V8STR_NATIVE_DATA = getV8Str( "_native_data" );
V8STR_V8_FUNC = getV8Str( "_v8_function" );
injectV8Function("print", Print);
@@ -256,6 +257,7 @@ namespace mongo {
HandleScope handle_scope;
Local< External > f = External::Cast( *args.Callee()->Get( scope->V8STR_NATIVE_FUNC ) );
NativeFunction function = (NativeFunction)(f->Value());
+ Local< External > data = External::Cast( *args.Callee()->Get( scope->V8STR_NATIVE_DATA ) );
BSONObjBuilder b;
for( int i = 0; i < args.Length(); ++i ) {
stringstream ss;
@@ -265,7 +267,7 @@ namespace mongo {
BSONObj nativeArgs = b.obj();
BSONObj ret;
try {
- ret = function( nativeArgs );
+ ret = function( nativeArgs, data->Value() );
}
catch( const std::exception &e ) {
return v8::ThrowException(v8::String::New(e.what()));
@@ -504,6 +506,11 @@ namespace mongo {
return num;
}
+ void V8Scope::setFunction( const char *field , const char * code ) {
+ V8_SIMPLE_HEADER
+ _global->Set( getV8Str( field ) , __createFunction(code) );
+ }
+
// void V8Scope::setThis( const BSONObj * obj ) {
// V8_SIMPLE_HEADER
// if ( ! obj ) {
@@ -643,15 +650,16 @@ namespace mongo {
return true;
}
- void V8Scope::injectNative( const char *field, NativeFunction func ) {
- injectNative(field, func, _global);
+ void V8Scope::injectNative( const char *field, NativeFunction func, void* data ) {
+ injectNative(field, func, _global, data);
}
- void V8Scope::injectNative( const char *field, NativeFunction func, Handle<v8::Object>& obj ) {
+ void V8Scope::injectNative( const char *field, NativeFunction func, Handle<v8::Object>& obj, void* data ) {
V8_SIMPLE_HEADER
Handle< FunctionTemplate > ft = createV8Function(nativeCallback);
ft->Set( this->V8STR_NATIVE_FUNC, External::New( (void*)func ) );
+ ft->Set( this->V8STR_NATIVE_DATA, External::New( data ) );
obj->Set( getV8Str( field ), ft->GetFunction() );
}
@@ -1012,7 +1020,9 @@ namespace mongo {
}
}
- o->SetInternalField(0, v8::External::New((new BSONObj( m.getOwned() ))));
+ BSONObj* own = new BSONObj(m.getOwned());
+// BSONObj* own = new BSONObj(m);
+ o->SetInternalField(0, v8::External::New(own));
// need to set all keys with dummy values, so that order of keys is correct during enumeration
// otherwise v8 will list any newly set property in JS before the ones of underlying BSON obj.
for (BSONObjIterator it(m); it.more();) {
diff --git a/scripting/engine_v8.h b/scripting/engine_v8.h
index 6a9126814db..4b17e0fc796 100644
--- a/scripting/engine_v8.h
+++ b/scripting/engine_v8.h
@@ -85,6 +85,7 @@ namespace mongo {
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);
+ virtual void setFunction( const char *field , const char * code );
// virtual void setThis( const BSONObj * obj );
virtual void rename( const char * from , const char * to );
@@ -95,8 +96,8 @@ namespace mongo {
virtual bool exec( const StringData& code , const string& name , bool printResult , bool reportError , bool assertOnError, int timeoutMs );
virtual string getError() { return _error; }
- virtual void injectNative( const char *field, NativeFunction func );
- void injectNative( const char *field, NativeFunction func, Handle<v8::Object>& obj );
+ virtual void injectNative( const char *field, NativeFunction func, void* data = 0 );
+ void injectNative( const char *field, NativeFunction func, Handle<v8::Object>& obj, void* data = 0 );
void injectV8Function( const char *field, v8Function func );
void injectV8Function( const char *field, v8Function func, Handle<v8::Object>& obj );
void injectV8Function( const char *field, v8Function func, Handle<v8::Template>& t );
@@ -126,6 +127,7 @@ namespace mongo {
Persistent<v8::String> V8STR_LENGTH;
Persistent<v8::String> V8STR_ISOBJECTID;
Persistent<v8::String> V8STR_NATIVE_FUNC;
+ Persistent<v8::String> V8STR_NATIVE_DATA;
Persistent<v8::String> V8STR_V8_FUNC;
Persistent<v8::String> V8STR_RETURN;
Persistent<v8::String> V8STR_ARGS;
diff --git a/scripting/utils.cpp b/scripting/utils.cpp
index e4924440312..612b173fdf8 100644
--- a/scripting/utils.cpp
+++ b/scripting/utils.cpp
@@ -25,7 +25,7 @@ namespace mongo {
void installBenchmarkSystem( Scope& scope );
- BSONObj jsmd5( const BSONObj &a ) {
+ BSONObj jsmd5( const BSONObj &a, void* data ) {
uassert( 10261 , "js md5 needs a string" , a.firstElement().type() == String );
const char * s = a.firstElement().valuestrsafe();
@@ -38,7 +38,7 @@ namespace mongo {
return BSON( "" << digestToString( d ) );
}
- BSONObj JSVersion( const BSONObj& args ) {
+ BSONObj JSVersion( const BSONObj& args, void* data ) {
cout << "version: " << versionString << endl;
if ( strstr( versionString , "+" ) )
printGitVersion();
@@ -46,7 +46,7 @@ namespace mongo {
}
- BSONObj JSSleep(const mongo::BSONObj &args) {
+ BSONObj JSSleep(const mongo::BSONObj &args, void* data) {
assert( args.nFields() == 1 );
assert( args.firstElement().isNumber() );
int ms = int( args.firstElement().number() );