From: Wink Saville Date: Thu, 10 Jun 2010 21:28:15 +0000 (-0700) Subject: Add testV8. X-Git-Tag: android-x86-4.4-r1~142 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=16c00522e27da0cfcbc4d926900e534e1583168f;p=android-x86%2Fhardware-ril.git Add testV8. Change-Id: I26ec3bc964c523fe6f855e8fe3d436064acfb27a --- diff --git a/mock-ril/Android.mk b/mock-ril/Android.mk index 680560d..d2c2357 100644 --- a/mock-ril/Android.mk +++ b/mock-ril/Android.mk @@ -11,13 +11,14 @@ LOCAL_SHARED_LIBRARIES := \ libcutils libutils libril LOCAL_STATIC_LIBRARIES := \ - libprotobuf-cpp-2.3.0-lite + libprotobuf-cpp-2.3.0-lite libv8 # for asprinf LOCAL_CFLAGS := -D_GNU_SOURCE -UNDEBUG -DGOOGLE_PROTOBUF_NO_RTTI LOCAL_C_INCLUDES := \ external/protobuf/src \ + external/v8/include \ bionic \ $(KERNEL_HEADERS) diff --git a/mock-ril/mock-ril.cpp b/mock-ril/mock-ril.cpp index 24fb3ae..bc8e431 100644 --- a/mock-ril/mock-ril.cpp +++ b/mock-ril/mock-ril.cpp @@ -38,6 +38,7 @@ #include #include "t.pb.h" +#include #define MOCK_RIL_VER_STRING "Android Mock-ril 0.1" @@ -158,6 +159,89 @@ static void * mainLoop(void *param) return NULL; } +// Extracts a C string from a V8 Utf8Value. +const char* ToCString(const v8::String::Utf8Value& value) { + return *value ? *value : ""; +} + +// Report an exception +void LogErrorMessage(v8::Handle message, const char *alternate_message) { + v8::HandleScope handle_scope; + if (message.IsEmpty()) { + // V8 didn't provide any extra information about this error; just + // print the exception. + LOGD("%s", alternate_message); + } else { + v8::String::Utf8Value filename(message->GetScriptResourceName()); + const char* filename_string = ToCString(filename); + int linenum = message->GetLineNumber(); + LOGD("file:%s line:%i", filename_string, linenum); + + // Print line of source code. + v8::String::Utf8Value sourceline(message->GetSourceLine()); + const char* sourceline_string = ToCString(sourceline); + LOGD("%s", sourceline_string); + + // Print location information under source line + int start = message->GetStartColumn(); + int end = message->GetEndColumn(); + char *error_string = new char[end]; + memset(error_string, ' ', start); + memset(&error_string[start], '^', end - start); + error_string[end] = 0; + LOGD("%s", error_string); + delete [] error_string; + } +} + +void ErrorCallback(v8::Handle message, v8::Handle data) { + LogErrorMessage(message, ""); +} + +// Report an exception +void ReportException(v8::TryCatch* try_catch) { + v8::HandleScope handle_scope; + + v8::String::Utf8Value exception(try_catch->Exception()); + v8::Handle msg = try_catch->Message(); + if (msg.IsEmpty()) { + // Why is try_catch->Message empty? + // is always empty on compile errors + } + LogErrorMessage(msg, ToCString(exception)); +} + +void testV8() { + LOGD("testV8 E:"); + v8::HandleScope handle_scope; + v8::TryCatch try_catch; + + // Catch errors as they occur as using ReportException doesn't work?? + v8::Handle mydata(v8::String::New("hello")); + v8::V8::AddMessageListener(ErrorCallback, mydata); + + // Create context and make it the current scope + v8::Handle context = v8::Context::New(); + v8::Context::Scope context_scope(context); + + // Compile the source + v8::Handle source = v8::String::New("\n\n 'Hi\n\n"); + v8::Handle script = v8::Script::Compile(source); + if (script.IsEmpty()) { + ReportException(&try_catch); + } else { + // Run the resulting script + v8::Handle result = script->Run(); + if (result.IsEmpty()) { + ReportException(&try_catch); + } else { + v8::String::Utf8Value result_string(result); + LOGD("testV8 result=%s", ToCString(result_string)); + } + } + LOGD("testV8 X:"); +} + pthread_t s_tid_mainloop; const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv) @@ -167,6 +251,8 @@ const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **a LOGD("RIL_Init E"); + testV8(); + s_rilenv = env; ret = pthread_attr_init (&attr);