0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 01:21:03 +01:00
mongodb/scripting/engine_v8.h

120 lines
3.9 KiB
C
Raw Normal View History

//engine_v8.h
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2009-05-04 17:49:18 +02:00
#pragma once
2009-10-12 00:15:12 +02:00
#include <vector>
2009-05-04 17:49:18 +02:00
#include "engine.h"
#include <v8.h>
using namespace v8;
2009-05-04 17:49:18 +02:00
namespace mongo {
2009-10-10 07:30:00 +02:00
class V8ScriptEngine;
2009-05-04 17:49:18 +02:00
class V8Scope : public Scope {
public:
2009-10-10 07:30:00 +02:00
V8Scope( V8ScriptEngine * engine );
~V8Scope();
2009-05-04 17:49:18 +02:00
virtual void reset();
2009-10-13 16:12:44 +02:00
virtual void init( BSONObj * data );
2009-10-10 07:30:00 +02:00
2009-10-13 16:12:44 +02:00
virtual void localConnect( const char * dbName );
virtual void externalSetup();
2009-05-04 17:49:18 +02:00
2009-10-12 17:30:43 +02:00
v8::Handle<v8::Value> get( const char * field );
2009-10-10 07:30:00 +02:00
virtual double getNumber( const char *field );
2009-10-27 21:23:07 +01:00
virtual int getNumberInt( const char *field );
virtual long long getNumberLongLong( const char *field );
2009-10-10 07:30:00 +02:00
virtual string getString( const char *field );
virtual bool getBoolean( const char *field );
2009-10-12 17:30:43 +02:00
virtual BSONObj getObject( const char *field );
2009-05-04 17:49:18 +02:00
2009-10-13 16:12:44 +02:00
virtual int type( const char *field );
2009-10-10 07:30:00 +02:00
virtual void setNumber( const char *field , double val );
virtual void setString( const char *field , const char * val );
virtual void setBoolean( const char *field , bool val );
2009-10-12 17:22:30 +02:00
virtual void setElement( const char *field , const BSONElement& e );
virtual void setObject( const char *field , const BSONObj& obj , bool readOnly);
virtual void setThis( const BSONObj * obj );
2009-05-04 17:49:18 +02:00
2009-10-12 00:15:12 +02:00
virtual ScriptingFunction _createFunction( const char * code );
virtual int invoke( ScriptingFunction func , const BSONObj& args, int timeoutMs = 0 , bool ignoreReturn = false );
2009-10-11 04:24:08 +02:00
virtual bool exec( const string& code , const string& name , bool printResult , bool reportError , bool assertOnError, int timeoutMs );
virtual string getError(){ return _error; }
2009-10-10 07:30:00 +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 ) );
2009-10-10 07:30:00 +02:00
_global->Set( v8::String::New( field ), f->GetFunction() );
}
2009-10-13 16:12:44 +02:00
void gc(){} // no-op in v8
2009-10-10 07:30:00 +02:00
private:
2009-10-11 04:24:08 +02:00
void _startCall();
static Handle< Value > nativeCallback( const Arguments &args );
2009-11-23 01:33:43 +01:00
static Handle< Value > loadCallback( const Arguments &args );
V8ScriptEngine * _engine;
2009-10-10 07:30:00 +02:00
HandleScope _handleScope;
Handle<Context> _context;
Context::Scope _scope;
Handle<v8::Object> _global;
2009-10-11 04:24:08 +02:00
string _error;
2009-10-12 00:15:12 +02:00
vector< v8::Handle<Value> > _funcs;
v8::Handle<v8::Object> _this;
2009-10-13 16:12:44 +02:00
v8::Handle<v8::Function> _wrapper;
2009-10-13 16:12:44 +02:00
enum ConnectState { NOT , LOCAL , EXTERNAL };
ConnectState _connectState;
string _localDBName;
2009-05-04 17:49:18 +02:00
};
class V8ScriptEngine : public ScriptEngine {
public:
2009-10-10 07:30:00 +02:00
V8ScriptEngine();
virtual ~V8ScriptEngine();
2009-05-04 17:49:18 +02:00
2009-10-10 07:30:00 +02:00
virtual Scope * createScope(){ return new V8Scope( this ); }
2009-05-04 17:49:18 +02:00
2009-10-10 07:30:00 +02:00
virtual void runTest(){}
bool utf8Ok() const { return true; }
private:
//HandleScope _handleScope;
//Handle<ObjectTemplate> _globalTemplate;
//Handle<FunctionTemplate> _externalTemplate;
//Handle<FunctionTemplate> _localTemplate;
2009-10-10 07:30:00 +02:00
friend class V8Scope;
2009-05-04 17:49:18 +02:00
};
extern ScriptEngine * globalScriptEngine;
}