2009-05-04 17:49:18 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "engine.h"
|
2009-05-04 23:37:02 +02:00
|
|
|
#include <v8.h>
|
|
|
|
|
|
|
|
using namespace v8;
|
2009-05-04 17:49:18 +02:00
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
|
|
|
class V8Scope : public Scope {
|
|
|
|
public:
|
|
|
|
virtual void reset() {}
|
|
|
|
virtual void init( BSONObj * data ) {}
|
|
|
|
|
|
|
|
virtual void localConnect( const char * dbName ) {}
|
|
|
|
|
|
|
|
virtual double getNumber( const char *field ) { assert( false ); return 0; }
|
|
|
|
virtual string getString( const char *field ) { assert( false ); return ""; }
|
|
|
|
virtual bool getBoolean( const char *field ) { assert( false ); return false; }
|
|
|
|
virtual BSONObj getObject( const char *field ) { assert( false ); return BSONObj(); }
|
|
|
|
|
|
|
|
virtual int type( const char *field ) { assert( false ); return 0; }
|
|
|
|
|
2009-05-10 04:18:32 +02:00
|
|
|
virtual void setNumber( const char *field , double val ) { assert(0); }
|
|
|
|
virtual void setString( const char *field , const char * val ) { assert(0); }
|
|
|
|
virtual void setObject( const char *field , const BSONObj& obj , bool readOnly) { assert(0); }
|
|
|
|
virtual void setBoolean( const char *field , bool val ) { assert(0); }
|
|
|
|
virtual void setThis( const BSONObj * obj ) { assert(0); }
|
2009-05-04 17:49:18 +02:00
|
|
|
|
|
|
|
virtual ScriptingFunction createFunction( const char * code ) { assert( false ); return 0; }
|
|
|
|
virtual int invoke( ScriptingFunction func , const BSONObj& args ) { assert( false ); return 0; }
|
|
|
|
virtual string getError() { assert( false ); return ""; }
|
|
|
|
|
2009-05-04 23:37:02 +02:00
|
|
|
virtual void injectNative( const char *field, NativeFunction func ) {
|
|
|
|
Handle< FunctionTemplate > f( v8::FunctionTemplate::New( nativeCallback ) );
|
|
|
|
f->Set( v8::String::New( "_native_function" ), External::New( (void*)func ) );
|
|
|
|
global_->Set( v8::String::New( field ), f );
|
|
|
|
}
|
|
|
|
|
|
|
|
void setGlobal( const Handle< v8::ObjectTemplate > &global ) {
|
|
|
|
global_ = global;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static Handle< Value > nativeCallback( const Arguments &args );
|
|
|
|
Handle< v8::ObjectTemplate > global_;
|
2009-05-04 17:49:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class V8ScriptEngine : public ScriptEngine {
|
|
|
|
public:
|
|
|
|
V8ScriptEngine() {}
|
|
|
|
virtual ~V8ScriptEngine() {}
|
|
|
|
|
|
|
|
virtual Scope * createScope() { return new V8Scope(); }
|
|
|
|
|
|
|
|
virtual void runTest() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
extern ScriptEngine * globalScriptEngine;
|
|
|
|
}
|