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

124 lines
4.0 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"
2010-09-23 23:36:04 +02:00
#include "v8_db.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();
2010-12-15 15:50:35 +01:00
virtual void init( const 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
v8::Handle<v8::Value> get( const char * field ); // caller must create context and handle scopes
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 );
virtual void rename( const char * from , const char * to );
2009-05-04 17:49:18 +02:00
2009-10-12 00:15:12 +02:00
virtual ScriptingFunction _createFunction( const char * code );
Local< v8::Function > __createFunction( const char * code );
2009-10-12 00:15:12 +02:00
virtual int invoke( ScriptingFunction func , const BSONObj& args, int timeoutMs = 0 , bool ignoreReturn = false );
2010-08-10 19:29:52 +02:00
virtual bool exec( const StringData& code , const string& name , bool printResult , bool reportError , bool assertOnError, int timeoutMs );
2009-10-11 04:24:08 +02:00
virtual string getError(){ return _error; }
2009-10-10 07:30:00 +02:00
virtual void injectNative( const char *field, NativeFunction func );
2009-12-22 20:39:23 +01:00
void gc();
2009-10-10 07:30:00 +02:00
2009-12-23 02:56:57 +01:00
Handle< Context > context() const { return _context; }
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
Persistent<Context> _context;
Persistent<v8::Object> _global;
2009-10-11 04:24:08 +02:00
string _error;
vector< Persistent<Value> > _funcs;
v8::Persistent<v8::Object> _this;
2009-10-13 16:12:44 +02:00
v8::Persistent<v8::Function> _wrapper;
2009-10-13 16:12:44 +02:00
enum ConnectState { NOT , LOCAL , EXTERNAL };
ConnectState _connectState;
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; }
2010-09-23 23:36:04 +02:00
class V8UnlockForClient : public Unlocker {
V8Unlock u_;
};
2010-09-23 23:36:04 +02:00
virtual auto_ptr<Unlocker> newThreadUnlocker() { return auto_ptr< Unlocker >( new V8UnlockForClient ); }
2010-09-23 23:36:04 +02:00
virtual void interrupt( unsigned opSpec );
virtual void interruptAll();
2009-10-10 07:30:00 +02:00
private:
friend class V8Scope;
2009-05-04 17:49:18 +02:00
};
extern ScriptEngine * globalScriptEngine;
2010-09-23 23:36:04 +02:00
extern map< unsigned, int > __interruptSpecToThreadId;
2009-05-04 17:49:18 +02:00
}