diff --git a/Extractor/ExtractorCallBacks.h b/Extractor/ExtractorCallBacks.h index 778e8a2c4..86fb56348 100644 --- a/Extractor/ExtractorCallBacks.h +++ b/Extractor/ExtractorCallBacks.h @@ -31,6 +31,8 @@ or see http://www.gnu.org/licenses/agpl.txt. #include #include "ExtractorStructs.h" +#include "V8Helper.h" + typedef stxxl::vector STXXLNodeIDVector; typedef stxxl::vector<_Node> STXXLNodeVector; typedef stxxl::vector<_Edge> STXXLEdgeVector; @@ -97,14 +99,62 @@ private: return n; } + /** V8 JavaScript Engine **/ + v8::HandleScope handle_scope; + v8::Handle global_templ; + v8::Persistent context; + + //forbid default c'tor + ExtractorCallbacks() {externalMemory = NULL; stringMap = NULL; } public: - ExtractorCallbacks(STXXLContainers * ext, Settings set, StringMap * strMap) { + explicit ExtractorCallbacks(STXXLContainers * ext, Settings set, StringMap * strMap) { externalMemory = ext; settings = set; stringMap = strMap; + + global_templ = v8::ObjectTemplate::New(); + + + //register global print function + global_templ->Set(v8::String::New("print"), v8::FunctionTemplate::New(V8Helper::PrintToConsole)); + + //register global version function + global_templ->Set(v8::String::New("version"), v8::FunctionTemplate::New(V8Helper::Version)); + +// v8::Persistent script_core; + + context = v8::Context::New(0, global_templ); + //Enter the created context for compiling + v8::Context::Scope context_scope = v8::Context::Scope(context); + + //todo: open speedprofile.js + std::string text = "print('Starting V8 Scripting Engine '+version());"; + v8::Handle source = v8::String::New(text.c_str()); + v8::TryCatch try_catch; + v8::Handle script = v8::Script::Compile(source); + if (script.IsEmpty()) { + // Print errors that happened during compilation. + V8Helper::ReportException(&try_catch); + } else { + v8::Handle result = script->Run(); + if (result.IsEmpty()) { + assert(try_catch.HasCaught()); + // Print errors that happened during execution. + V8Helper::ReportException(&try_catch); + } else { + assert(!try_catch.HasCaught()); + if (!result->IsUndefined()) { + // If all went well and the result wasn't undefined then print + // the returned value. + v8::String::Utf8Value str(result); + std::cout << V8Helper::ToCString(str) << std::endl; + } + } + } } ~ExtractorCallbacks() { + context.Dispose(); } /** warning: caller needs to take care of synchronization! */ diff --git a/Extractor/V8Helper.h b/Extractor/V8Helper.h index e6a67cd08..46fc4ee3e 100644 --- a/Extractor/V8Helper.h +++ b/Extractor/V8Helper.h @@ -21,6 +21,22 @@ or see http://www.gnu.org/licenses/agpl.txt. #ifndef V8HELPER_H_ #define V8HELPER_H_ +#include + +#include + +namespace V8Helper { +//Provides an output mechanism to the V8 Engine +static v8::Handle PrintToConsole (const v8::Arguments& args){ + std::cout << "[JS] : " << *v8::String::AsciiValue(args[0]) << std::endl; + return v8::Undefined(); +} + +//Returns the version of the V8 Engine +v8::Handle Version(const v8::Arguments& args) { + return v8::String::New(v8::V8::GetVersion()); +} + //Most of the stuff below is from http://blog.owned.co.za provided with an MIT License template @@ -101,5 +117,45 @@ v8::Handle GetInt(v8::Local property, const v8::AccessorI return JSPropertyType::New(value); } +// Extracts a C string from a V8 Utf8Value. +const char* ToCString(const v8::String::Utf8Value& value) { + return *value ? *value : ""; +} + +void ReportException(v8::TryCatch* try_catch) { + v8::HandleScope handle_scope; + v8::String::Utf8Value exception(try_catch->Exception()); + const char* exception_string = ToCString(exception); + v8::Handle message = try_catch->Message(); + if (message.IsEmpty()) { + // V8 didn't provide any extra information about this error; just + // print the exception. + std::cout << exception_string << std::endl; + } else { + // Print (filename):(line number): (message). + v8::String::Utf8Value filename(message->GetScriptResourceName()); + int linenum = message->GetLineNumber(); + std::cout << ToCString(filename) << ":" << linenum << ": " << exception_string << std::endl; + // Print line of source code. + v8::String::Utf8Value sourceline(message->GetSourceLine()); + std::cout << ToCString(sourceline) << std::endl; + + // Print wavy underline (GetUnderline is deprecated). + int start = message->GetStartColumn(); + for (int i = 0; i < start; ++i) { + std::cout << " "; + } + + for (int i = start, end = message->GetEndColumn(); i < end; ++i) { + std::cout << "^"; + } + std::cout << std::endl; + v8::String::Utf8Value stack_trace(try_catch->StackTrace()); + if (stack_trace.length()) { + std::cout << ToCString(stack_trace) << std::endl; + } + } +} +} #endif /* V8HELPER_H_ */ diff --git a/Util/OpenMPReplacement.h b/Util/OpenMPReplacement.h deleted file mode 100644 index 232dcbd62..000000000 --- a/Util/OpenMPReplacement.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - open source routing machine - Copyright (C) Dennis Luxen, others 2010 - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU AFFERO General Public License as published by -the Free Software Foundation; either version 3 of the License, or -any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -or see http://www.gnu.org/licenses/agpl.txt. - -*/ - -#ifndef _OPENMPREPLACEMENTY_H -#define _OPENMPREPLACEMENTY_H - -#ifdef _OPENMP -#include -#else -inline const int omp_get_num_procs() { return 1; } -inline const int omp_get_max_threads() { return 1; } -inline const int omp_get_thread_num() { return 0; } -inline const void omp_set_num_threads(int i) {} -#endif - -#endif