Files
mongo/scripting/engine_v8.h

117 lines
3.7 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 11:49:18 -04:00
#pragma once
2009-10-11 18:15:12 -04:00
#include <vector>
2009-05-04 11:49:18 -04:00
#include "engine.h"
#include <v8.h>
using namespace v8;
2009-05-04 11:49:18 -04:00
namespace mongo {
2009-10-10 01:30:00 -04:00
class V8ScriptEngine;
2009-05-04 11:49:18 -04:00
class V8Scope : public Scope {
public:
2009-10-10 01:30:00 -04:00
V8Scope( V8ScriptEngine * engine );
~V8Scope();
2009-05-04 11:49:18 -04:00
virtual void reset();
2009-10-13 10:12:44 -04:00
virtual void init( BSONObj * data );
2009-10-10 01:30:00 -04:00
2009-10-13 10:12:44 -04:00
virtual void localConnect( const char * dbName );
virtual void externalSetup();
2009-05-04 11:49:18 -04:00
v8::Handle<v8::Value> get( const char * field ); // caller must create context and handle scopes
2009-10-10 01:30:00 -04:00
virtual double getNumber( const char *field );
2009-10-27 16:23:07 -04:00
virtual int getNumberInt( const char *field );
virtual long long getNumberLongLong( const char *field );
2009-10-10 01:30:00 -04:00
virtual string getString( const char *field );
virtual bool getBoolean( const char *field );
2009-10-12 11:30:43 -04:00
virtual BSONObj getObject( const char *field );
2009-05-04 11:49:18 -04:00
2009-10-13 10:12:44 -04:00
virtual int type( const char *field );
2009-10-10 01:30:00 -04: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 11:22:30 -04: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 11:49:18 -04:00
2009-10-11 18:15:12 -04:00
virtual ScriptingFunction _createFunction( const char * code );
Local< v8::Function > __createFunction( const char * code );
2009-10-11 18:15:12 -04:00
virtual int invoke( ScriptingFunction func , const BSONObj& args, int timeoutMs = 0 , bool ignoreReturn = false );
2009-10-10 22:24:08 -04: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 01:30:00 -04:00
virtual void injectNative( const char *field, NativeFunction func );
2009-12-22 11:39:23 -08:00
void gc();
2009-10-10 01:30:00 -04:00
2009-12-22 17:56:57 -08:00
Handle< Context > context() const { return _context; }
private:
2009-10-10 22:24:08 -04:00
void _startCall();
static Handle< Value > nativeCallback( const Arguments &args );
2009-11-23 08:33:43 +08:00
static Handle< Value > loadCallback( const Arguments &args );
V8ScriptEngine * _engine;
2009-10-10 01:30:00 -04:00
Persistent<Context> _context;
Persistent<v8::Object> _global;
2009-10-10 22:24:08 -04:00
string _error;
vector< Persistent<Value> > _funcs;
v8::Persistent<v8::Object> _this;
2009-10-13 10:12:44 -04:00
v8::Persistent<v8::Function> _wrapper;
2009-10-13 10:12:44 -04:00
enum ConnectState { NOT , LOCAL , EXTERNAL };
ConnectState _connectState;
2009-05-04 11:49:18 -04:00
};
class V8ScriptEngine : public ScriptEngine {
public:
2009-10-10 01:30:00 -04:00
V8ScriptEngine();
virtual ~V8ScriptEngine();
2009-05-04 11:49:18 -04:00
2009-10-10 01:30:00 -04:00
virtual Scope * createScope(){ return new V8Scope( this ); }
2009-05-04 11:49:18 -04:00
2009-10-10 01:30:00 -04:00
virtual void runTest(){}
bool utf8Ok() const { return true; }
class V8Unlocker : public Unlocker {
v8::Unlocker u_;
};
virtual auto_ptr<Unlocker> newThreadUnlocker() { return auto_ptr< Unlocker >( new V8Unlocker ); }
2009-10-10 01:30:00 -04:00
private:
friend class V8Scope;
2009-05-04 11:49:18 -04:00
};
extern ScriptEngine * globalScriptEngine;
}