00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #pragma once
00019
00020 #include <vector>
00021 #include "engine.h"
00022 #include "v8_db.h"
00023 #include <v8.h>
00024
00025 using namespace v8;
00026
00027 namespace mongo {
00028
00029 class V8ScriptEngine;
00030
00031 class V8Scope : public Scope {
00032 public:
00033
00034 V8Scope( V8ScriptEngine * engine );
00035 ~V8Scope();
00036
00037 virtual void reset();
00038 virtual void init( const BSONObj * data );
00039
00040 virtual void localConnect( const char * dbName );
00041 virtual void externalSetup();
00042
00043 v8::Handle<v8::Value> get( const char * field );
00044 virtual double getNumber( const char *field );
00045 virtual int getNumberInt( const char *field );
00046 virtual long long getNumberLongLong( const char *field );
00047 virtual string getString( const char *field );
00048 virtual bool getBoolean( const char *field );
00049 virtual BSONObj getObject( const char *field );
00050
00051 virtual int type( const char *field );
00052
00053 virtual void setNumber( const char *field , double val );
00054 virtual void setString( const char *field , const char * val );
00055 virtual void setBoolean( const char *field , bool val );
00056 virtual void setElement( const char *field , const BSONElement& e );
00057 virtual void setObject( const char *field , const BSONObj& obj , bool readOnly);
00058 virtual void setThis( const BSONObj * obj );
00059
00060 virtual void rename( const char * from , const char * to );
00061
00062 virtual ScriptingFunction _createFunction( const char * code );
00063 Local< v8::Function > __createFunction( const char * code );
00064 virtual int invoke( ScriptingFunction func , const BSONObj& args, int timeoutMs = 0 , bool ignoreReturn = false );
00065 virtual bool exec( const StringData& code , const string& name , bool printResult , bool reportError , bool assertOnError, int timeoutMs );
00066 virtual string getError() { return _error; }
00067
00068 virtual void injectNative( const char *field, NativeFunction func );
00069
00070 void gc();
00071
00072 Handle< Context > context() const { return _context; }
00073
00074 private:
00075 void _startCall();
00076
00077 static Handle< Value > nativeCallback( const Arguments &args );
00078
00079 static Handle< Value > loadCallback( const Arguments &args );
00080
00081 V8ScriptEngine * _engine;
00082
00083 Persistent<Context> _context;
00084 Persistent<v8::Object> _global;
00085
00086 string _error;
00087 vector< Persistent<Value> > _funcs;
00088 v8::Persistent<v8::Object> _this;
00089
00090 v8::Persistent<v8::Function> _wrapper;
00091
00092 enum ConnectState { NOT , LOCAL , EXTERNAL };
00093 ConnectState _connectState;
00094 };
00095
00096 class V8ScriptEngine : public ScriptEngine {
00097 public:
00098 V8ScriptEngine();
00099 virtual ~V8ScriptEngine();
00100
00101 virtual Scope * createScope() { return new V8Scope( this ); }
00102
00103 virtual void runTest() {}
00104
00105 bool utf8Ok() const { return true; }
00106
00107 class V8UnlockForClient : public Unlocker {
00108 V8Unlock u_;
00109 };
00110
00111 virtual auto_ptr<Unlocker> newThreadUnlocker() { return auto_ptr< Unlocker >( new V8UnlockForClient ); }
00112
00113 virtual void interrupt( unsigned opSpec );
00114 virtual void interruptAll();
00115
00116 private:
00117 friend class V8Scope;
00118 };
00119
00120
00121 extern ScriptEngine * globalScriptEngine;
00122 extern map< unsigned, int > __interruptSpecToThreadId;
00123 }