I am really stupid. I forgot to return the value of the function serverSendRequest.
Can i do this function void? среда, 20 марта 2013 г., 0:38:37 UTC+4 пользователь Егор Яковлев написал: > > Can you help me, please. I dont know whats wrong with my programm. I can > admit a trivial error with V8 api. > > I have some code, that send request into the server using "curlpp" library > and write the answer in std::string "jsonString" variable. And this part > of the program works. > > #include <v8.h> >> #include <fstream> >> #include <string> >> #include <cerrno> >> #include <string> >> #include <sstream> >> #include <iostream> >> #include <curlpp/cURLpp.hpp> >> #include <curlpp/Easy.hpp> >> #include <curlpp/Options.hpp> >> using namespace v8; >> using namespace cURLpp; >> using namespace Options; >> using namespace types; >> std::string get_file_contents(const char *filename) >> { >> ... >> } >> class Callback { >> public: >> static std::string jsonString; >> private: >> static size_t writer(char* ptr, size_t size, size_t nmemb); >> public: >> static Handle<Value> serverSendRequest(const Arguments&); >> }; >> std::string Callback::jsonString; >> size_t Callback::writer(char* ptr, size_t size, size_t nmemb) >> { >> size_t realsize = size * nmemb; >> Callback::jsonString.append(ptr,realsize); >> return realsize; >> } >> >> Handle<Value> Callback::serverSendRequest(const Arguments& args) >> { >> Callback::jsonString = ""; >> std::string requestBody = ""; >> std::string requestType = "POST"; >> std::string url = "http://localhost"; >> if (!args[0]->IsNull()){ >> ... >> url = ... ; >> } >> if (!args[1]->IsNull()){ >> ... >> requestType = ...; >> } >> if (!args[2]->IsNull()) >> { >> ... >> requestBody = ... ; >> } >> CURL *curl; >> CURLcode res; >> struct curl_slist *headers = NULL; >> std::ostringstream oss; >> curl_slist_append(headers, "Accept: application/json"); >> curl_slist_append(headers, "Content-Type: application/json"); >> curl_slist_append(headers, "charsets: utf-8"); >> curl = curl_easy_init(); >> if (curl) >> { >> if (requestType == "GET"){ >> curl_easy_setopt(curl, CURLOPT_HTTPGET,1); >> url = url + "?" + requestBody; >> } >> else if (requestType == "POST") { >> curl_easy_setopt( curl, CURLOPT_POST,1); >> curl_easy_setopt( curl, CURLOPT_POSTFIELDSIZE, >> requestBody.size()); >> curl_easy_setopt( curl, CURLOPT_POSTFIELDS, >> requestBody.c_str()); >> } >> curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); >> curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); >> curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Callback::writer); >> curl_easy_perform(curl); >> >> //there is my right json string >> printf("%s\n", Callback::jsonString.c_str()); >> } >> } >> int main(int argc, char* argv[]) { >> std::string js_file = get_file_contents(argv[1]); >> HandleScope handle_scope; >> Handle<ObjectTemplate> global = ObjectTemplate::New(); >> global->Set(String::New("serverSendRequest"), >> FunctionTemplate::New(Callback::serverSendRequest)); >> Persistent<Context> context = Context::New(NULL,global); >> >> Context::Scope context_scope(context); >> Handle<String> source = String::New(js_file.c_str()); >> Handle<Script> script = Script::Compile(source); >> >> Handle<Value> result = script->Run(); >> >> context.Dispose(context->GetIsolate()); >> return 0; >> } > > > > But also i have a segmentation fault. > > I compile it on Ubuntu x64 (v8 - 2013-03-19: Version 3.17.13) with > command: > > g++ -I./src programm.cpp -o programm > out/x64.release/obj.target/tools/gyp/libv8_{base,snapshot}.a `curlpp-config > --cflags` `curlpp-config --libs` -lpthread > > > *The output of my programm:* > > {some json text} > > Program received signal SIGSEGV, Segmentation fault. > 0x000000000043740f in > v8::internal::Builtin_HandleApiCall(v8::internal::(anonymous > namespace)::BuiltinArguments<(v8::internal::BuiltinExtraArguments)1>, > v8::internal::Isolate*) () > > > > > > Thanx for your attention. > > -- -- v8-users mailing list v8-users@googlegroups.com http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.