summaryrefslogtreecommitdiff
path: root/scripting
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2009-10-10 22:24:08 -0400
committerEliot Horowitz <eliot@10gen.com>2009-10-10 22:24:08 -0400
commit6f30be62d4205d5d2dc4f9bf16327eee26c8ca0c (patch)
tree9572274aac49ba00b79d7ecdd89a704994b8f9bf /scripting
parentd667638de041581406c5b8824e21a4f04b9895e7 (diff)
downloadmongo-6f30be62d4205d5d2dc4f9bf16327eee26c8ca0c.tar.gz
exec checkpoint
Diffstat (limited to 'scripting')
-rw-r--r--scripting/engine_v8.cpp56
-rw-r--r--scripting/engine_v8.h9
-rw-r--r--scripting/v8_utils.cpp48
-rw-r--r--scripting/v8_utils.h11
4 files changed, 64 insertions, 60 deletions
diff --git a/scripting/engine_v8.cpp b/scripting/engine_v8.cpp
index 261fe8a9dfb..cbaba35b6c6 100644
--- a/scripting/engine_v8.cpp
+++ b/scripting/engine_v8.cpp
@@ -5,9 +5,14 @@
namespace mongo {
+ // --- engine ---
+
V8ScriptEngine::V8ScriptEngine()
: _handleScope() , _globalTemplate( ObjectTemplate::New() ) {
+ _globalTemplate->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
+ _globalTemplate->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version));
+
}
V8ScriptEngine::~V8ScriptEngine(){
@@ -19,6 +24,8 @@ namespace mongo {
}
}
+ // --- scope ---
+
V8Scope::V8Scope( V8ScriptEngine * engine )
: _handleScope(),
_context( Context::New( 0 , engine->_globalTemplate ) ) ,
@@ -74,5 +81,54 @@ namespace mongo {
bool V8Scope::getBoolean( const char *field ){
return _global->Get( v8::String::New( field ) )->ToBoolean()->Value();
}
+
+ bool V8Scope::exec( const string& code , const string& name , bool printResult , bool reportError , bool assertOnError, int timeoutMs ){
+
+ if ( timeoutMs ){
+ static bool t = 1;
+ if ( t ){
+ log() << "timeoutMs not support for v8 yet" << endl;
+ t = 0;
+ }
+ }
+
+ HandleScope handle_scope;
+ TryCatch try_catch;
+
+ Handle<Script> script = v8::Script::Compile( v8::String::New( code.c_str() ) ,
+ v8::String::New( name.c_str() ) );
+ if (script.IsEmpty()) {
+ stringstream ss;
+ ss << "compile error: " << &try_catch;
+ _error = ss.str();
+ if (reportError)
+ ReportException(&try_catch);
+ if ( assertOnError )
+ uassert( _error , 0 );
+ return false;
+ }
+
+ Handle<v8::Value> result = script->Run();
+ if ( result.IsEmpty() ){
+ stringstream ss;
+ ss << "exec error: " << &try_catch;
+ _error = ss.str();
+ if ( reportError )
+ ReportException(&try_catch);
+ if ( assertOnError )
+ uassert( _error , 0 );
+ return false;
+ }
+
+ if ( printResult ){
+ cout << toSTLString( result ) << endl;
+ }
+
+ return true;
+ }
+
+ void V8Scope::_startCall(){
+ _error = "";
+ }
} // namespace mongo
diff --git a/scripting/engine_v8.h b/scripting/engine_v8.h
index 61b026b4719..a60d5b9c137 100644
--- a/scripting/engine_v8.h
+++ b/scripting/engine_v8.h
@@ -37,8 +37,8 @@ namespace mongo {
virtual ScriptingFunction _createFunction( const char * code ){ assert( false ); return 0; }
virtual int invoke( ScriptingFunction func , const BSONObj& args, int timeoutMs = 0 , bool ignoreReturn = false ){ assert(0); return 0;}
- virtual bool exec( const string& code , const string& name , bool printResult , bool reportError , bool assertOnError, int timeoutMs ){ assert(0); return 0; }
- virtual string getError(){ assert( false ); return ""; }
+ virtual bool exec( const string& code , const string& name , bool printResult , bool reportError , bool assertOnError, int timeoutMs );
+ virtual string getError(){ return _error; }
virtual void injectNative( const char *field, NativeFunction func ){
Handle< FunctionTemplate > f( v8::FunctionTemplate::New( nativeCallback ) );
@@ -49,13 +49,16 @@ namespace mongo {
void gc(){ assert(0); }
private:
-
+ void _startCall();
+
static Handle< Value > nativeCallback( const Arguments &args );
HandleScope _handleScope;
Handle<Context> _context;
Context::Scope _scope;
Handle<v8::Object> _global;
+
+ string _error;
};
class V8ScriptEngine : public ScriptEngine {
diff --git a/scripting/v8_utils.cpp b/scripting/v8_utils.cpp
index 0777b3c3220..9b3dc4eda4f 100644
--- a/scripting/v8_utils.cpp
+++ b/scripting/v8_utils.cpp
@@ -81,56 +81,8 @@ namespace mongo {
return v8::String::New(v8::V8::GetVersion());
}
- bool ExecuteString(Handle<v8::String> source, Handle<v8::Value> name,
- bool print_result, bool report_exceptions ){
-
- HandleScope handle_scope;
- v8::TryCatch try_catch;
-
- Handle<v8::Script> script = v8::Script::Compile(source, name);
- if (script.IsEmpty()) {
- if (report_exceptions)
- ReportException(&try_catch);
- return false;
- }
-
- Handle<v8::Value> result = script->Run();
- if ( result.IsEmpty() ){
- if (report_exceptions)
- ReportException(&try_catch);
- return false;
- }
-
- if ( print_result ){
-
- Local<Context> current = Context::GetCurrent();
- Local<Object> global = current->Global();
-
- Local<Value> shellPrint = global->Get( String::New( "shellPrint" ) );
-
- if ( shellPrint->IsFunction() ){
- v8::Function * f = (v8::Function*)(*shellPrint);
- Handle<v8::Value> argv[1];
- argv[0] = result;
- f->Call( global , 1 , argv );
- }
- else if ( ! result->IsUndefined() ){
- cout << result << endl;
- }
- }
-
- return true;
- }
-
void ReportException(v8::TryCatch* try_catch) {
cout << try_catch << endl;
}
- extern v8::Handle< v8::Context > baseContext_;
-
- void installShellUtils( Handle<v8::ObjectTemplate>& global ){
- global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
- global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version));
- }
-
}
diff --git a/scripting/v8_utils.h b/scripting/v8_utils.h
index bcbf54f6f2b..56932bccffc 100644
--- a/scripting/v8_utils.h
+++ b/scripting/v8_utils.h
@@ -12,18 +12,11 @@
namespace mongo {
- // Executes a string within the current v8 context.
- bool ExecuteString(v8::Handle<v8::String> source,
- v8::Handle<v8::Value> name,
- bool print_result,
- bool report_exceptions);
-
v8::Handle<v8::Value> Print(const v8::Arguments& args);
+ v8::Handle<v8::Value> Version(const v8::Arguments& args);
+
void ReportException(v8::TryCatch* handler);
-
- void installShellUtils( v8::Handle<v8::ObjectTemplate>& global );
-
#define jsassert(x,msg) assert(x)
std::ostream& operator<<( std::ostream &s, const v8::Handle<v8::Value> & o );