OSDN Git Service

Add testV8.
authorWink Saville <wink@google.com>
Thu, 10 Jun 2010 21:28:15 +0000 (14:28 -0700)
committerWink Saville <wink@google.com>
Thu, 10 Jun 2010 21:28:15 +0000 (14:28 -0700)
Change-Id: I26ec3bc964c523fe6f855e8fe3d436064acfb27a

mock-ril/Android.mk
mock-ril/mock-ril.cpp

index 680560d..d2c2357 100644 (file)
@@ -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)
 
index 24fb3ae..bc8e431 100644 (file)
@@ -38,6 +38,7 @@
 #include <utils/Log.h>
 
 #include "t.pb.h"
+#include <v8.h>
 
 #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 : "<string conversion failed>";
+}
+
+// Report an exception
+void LogErrorMessage(v8::Handle<v8::Message> 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<v8::Message> message, v8::Handle<v8::Value> 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<v8::Message> 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<v8::Value> mydata(v8::String::New("hello"));
+    v8::V8::AddMessageListener(ErrorCallback, mydata);
+
+    // Create context and make it the current scope
+    v8::Handle<v8::Context> context = v8::Context::New();
+    v8::Context::Scope context_scope(context);
+
+    // Compile the source
+    v8::Handle<v8::String> source = v8::String::New("\n\n    'Hi\n\n");
+    v8::Handle<v8::Script> script = v8::Script::Compile(source);
+    if (script.IsEmpty()) {
+        ReportException(&try_catch);
+    } else {
+        // Run the resulting script
+        v8::Handle<v8::Value> 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);