Add a print function in the global JS scope for debugging.

BUG=
R=robertphillips@google.com

Author: jcgregorio@google.com

Review URL: https://codereview.chromium.org/100583005

git-svn-id: http://skia.googlecode.com/svn/trunk@12695 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/experimental/SkV8Example/SkV8Example.cpp b/experimental/SkV8Example/SkV8Example.cpp
index 106bef9..a62b919 100644
--- a/experimental/SkV8Example/SkV8Example.cpp
+++ b/experimental/SkV8Example/SkV8Example.cpp
@@ -292,6 +292,27 @@
 }
 #endif
 
+
+// The callback that is invoked by v8 whenever the JavaScript 'print'
+// function is called.  Prints its arguments on stdout separated by
+// spaces and ending with a newline.
+void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
+    bool first = true;
+    HandleScope handle_scope(args.GetIsolate());
+    for (int i = 0; i < args.Length(); i++) {
+        if (first) {
+            first = false;
+        } else {
+            printf(" ");
+        }
+        v8::String::Utf8Value str(args[i]);
+        printf("%s", ToCString(str));
+    }
+    printf("\n");
+    fflush(stdout);
+}
+
+
 // Creates a new execution environment containing the built-in
 // function draw().
 Handle<Context> createRootContext(Isolate* isolate) {
@@ -301,6 +322,9 @@
   // This is where we would inject any globals into the root Context
   // using global->Set(...)
 
+  global->Set(v8::String::NewFromUtf8(isolate, "print"),
+              v8::FunctionTemplate::New(Print));
+
   return Context::New(isolate, NULL, global);
 }