summaryrefslogtreecommitdiff
path: root/scripting/engine_v8.cpp
blob: 261fe8a9dfb8dc71391ed43d5ef42055290270d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "engine_v8.h"

#include "v8_wrapper.h"
#include "v8_utils.h"

namespace mongo {

    V8ScriptEngine::V8ScriptEngine()
        : _handleScope() , _globalTemplate( ObjectTemplate::New() ) {
        
    }

    V8ScriptEngine::~V8ScriptEngine(){
    }

    void ScriptEngine::setup(){
        if ( !globalScriptEngine ){
            globalScriptEngine = new V8ScriptEngine();
        }
    }

    V8Scope::V8Scope( V8ScriptEngine * engine ) 
        : _handleScope(),
          _context( Context::New( 0 , engine->_globalTemplate ) ) ,
          _scope( _context ) ,
          _global( _context->Global() ){
        
    }

    V8Scope::~V8Scope(){
        
    }

    Handle< Value > V8Scope::nativeCallback( const Arguments &args ) {
        Local< External > f = External::Cast( *args.Callee()->Get( v8::String::New( "_native_function" ) ) );
        NativeFunction function = ( NativeFunction )( f->Value() );
        BSONObjBuilder b;
        for( int i = 0; i < args.Length(); ++i ) {
            stringstream ss;
            ss << i;
            v8ToMongoElement( b, v8::String::New( "foo" ), ss.str(), args[ i ] );
        }
        BSONObj ret;
        try {
            ret = function( b.done() );
        } catch( const std::exception &e ) {
            return v8::ThrowException(v8::String::New(e.what()));
        } catch( ... ) {
            return v8::ThrowException(v8::String::New("unknown exception"));            
        }
        return mongoToV8Element( ret.firstElement() );
    }

    void V8Scope::setNumber( const char * field , double val ){
        _global->Set( v8::String::New( field ) , v8::Number::New( val ) );
    }

    void V8Scope::setString( const char * field , const char * val ){
        _global->Set( v8::String::New( field ) , v8::String::New( val ) );
    }

    void V8Scope::setBoolean( const char * field , bool val ){
        _global->Set( v8::String::New( field ) , v8::Boolean::New( val ) );
    }

    double V8Scope::getNumber( const char *field ){ 
        return _global->Get( v8::String::New( field ) )->ToNumber()->Value();
    }

    string V8Scope::getString( const char *field ){ 
        return toSTLString( _global->Get( v8::String::New( field ) ) );
    }

    bool V8Scope::getBoolean( const char *field ){ 
        return _global->Get( v8::String::New( field ) )->ToBoolean()->Value();
    }
    
} // namespace mongo