First steps towards integrating libv8

This commit is contained in:
DennisOSRM 2012-08-28 11:50:23 +02:00
parent 1fdfac4aaf
commit 4c58674393
3 changed files with 107 additions and 35 deletions

View File

@ -31,6 +31,8 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include <stxxl.h>
#include "ExtractorStructs.h"
#include "V8Helper.h"
typedef stxxl::vector<NodeID> 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<v8::ObjectTemplate> global_templ;
v8::Persistent<v8::Context> 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<v8::Object> 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<v8::String> source = v8::String::New(text.c_str());
v8::TryCatch try_catch;
v8::Handle<v8::Script> script = v8::Script::Compile(source);
if (script.IsEmpty()) {
// Print errors that happened during compilation.
V8Helper::ReportException(&try_catch);
} else {
v8::Handle<v8::Value> 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! */

View File

@ -21,6 +21,22 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef V8HELPER_H_
#define V8HELPER_H_
#include <iostream>
#include <v8.h>
namespace V8Helper {
//Provides an output mechanism to the V8 Engine
static v8::Handle<v8::Value> 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<v8::Value> 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<class T>
@ -101,5 +117,45 @@ v8::Handle<v8::Value> GetInt(v8::Local<v8::String> 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 : "<string conversion failed>";
}
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<v8::Message> 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_ */

View File

@ -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 <omp.h>
#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