void reset()
	{
		for (int32 Index = 0; Index < argc; ++Index)
		{
			argv[Index].Clear();
		}
		argc = 0;
		retval.Clear();
	}
Exemplo n.º 2
0
char* server_execute_script(const char* code, list_t* args, char** content_type) {
        
        char * replycontent = NULL;
        *content_type = NULL;

        if (!code) {
                fprintf(stderr, "Empty source code\n");
                return GetError(ScriptError::EMPTY);
        }

        v8::Isolate* isolate = v8::Isolate::New();
        
        {
                v8::Isolate::Scope iscope(isolate);
                //v8::V8::Initialize();
                //v8::Locker l(isolate);

                v8::HandleScope handle_scope(isolate);
                //v8::Local<v8::Context> context = v8::Context::New(isolate);
                
                // Create a template for the global object.
                Handle<v8::ObjectTemplate> global = ObjectTemplate::New(isolate);
                InitiliazeGlobalObject(isolate, global, args);
                
                Handle<Context> context = Context::New(isolate, NULL, global);
                
                if (!context.IsEmpty()) {

                        v8::Context::Scope context_scope(context);
                        v8::TryCatch try_catch;
                        
                        Handle<String> source = String::NewFromUtf8(isolate, code);
                        Handle<Script> script = Script::Compile(source);
                        
                        // Compilation error
                        if (try_catch.HasCaught()) {
                                String::Utf8Value exception(try_catch.Exception());
                                replycontent = GetError(ScriptError::COMPILESCRIPT, *exception);
                        } else {
                                Handle<Value> result = script->Run();

                                // Execution error
                                if (try_catch.HasCaught()) {

                                        String::Utf8Value exception(try_catch.Exception());
                                        if (*exception) {

                                                // Script ended with "exit()" function
                                                if (strcmp(*exception, "Exit") == 0) {
                                                        
                                                        const std::ostringstream& stream = Reply::GetStream();
                                                        const std::string& str = stream.str();
                                                        
                                                        replycontent = strdup(str.c_str());
                                                        
                                                } else {
                                                        replycontent = GetError(ScriptError::RUNSCRIPT, *exception);
                                                }
                                                
                                        } else {
                                                replycontent = GetError(ScriptError::RUNSCRIPT);
                                        }
                                } else {
                                        const std::ostringstream& stream = Reply::GetStream();
                                        const std::string& str = stream.str();

                                        replycontent = strdup(str.c_str());
                                }
                                
                        }
                        
                } else {
                        replycontent =  GetError(ScriptError::CONTEXT);
                }

                //context->Dispose();
                
                //Reply::Print();
                Reply::Clear();

                // Delete all "C++ wrapped objects"
                
                DeleteAllCppObjects();

                // Set content_type

                const std::string& mime = Reply::GetMimeType();

                if (!mime.empty()) {
                        *content_type = strdup(mime.c_str());
                } else {
                        *content_type = 0;
                }

                
                context.Clear();
        }

        isolate->Dispose();

        return replycontent;
}