Home | History | Annotate | Download | only in samples
      1 // Copyright 2012 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #include <v8.h>
     29 
     30 #ifdef ENABLE_DEBUGGER_SUPPORT
     31 #include <v8-debug.h>
     32 #endif  // ENABLE_DEBUGGER_SUPPORT
     33 
     34 #include <fcntl.h>
     35 #include <string.h>
     36 #include <stdio.h>
     37 #include <stdlib.h>
     38 
     39 /**
     40  * This sample program should demonstrate certain aspects of debugging
     41  * standalone V8-based application.
     42  *
     43  * The program reads input stream, processes it line by line and print
     44  * the result to output. The actual processing is done by custom JavaScript
     45  * script. The script is specified with command line parameters.
     46  *
     47  * The main cycle of the program will sequentially read lines from standard
     48  * input, process them and print to standard output until input closes.
     49  * There are 2 possible configuration in regard to main cycle.
     50  *
     51  * 1. The main cycle is on C++ side. Program should be run with
     52  * --main-cycle-in-cpp option. Script must declare a function named
     53  * "ProcessLine". The main cycle in C++ reads lines and calls this function
     54  * for processing every time. This is a sample script:
     55 
     56 function ProcessLine(input_line) {
     57   return ">>>" + input_line + "<<<";
     58 }
     59 
     60  *
     61  * 2. The main cycle is in JavaScript. Program should be run with
     62  * --main-cycle-in-js option. Script gets run one time at all and gets
     63  * API of 2 global functions: "read_line" and "print". It should read input
     64  * and print converted lines to output itself. This a sample script:
     65 
     66 while (true) {
     67   var line = read_line();
     68   if (!line) {
     69     break;
     70   }
     71   var res = line + " | " + line;
     72   print(res);
     73 }
     74 
     75  *
     76  * When run with "-p" argument, the program starts V8 Debugger Agent and
     77  * allows remote debugger to attach and debug JavaScript code.
     78  *
     79  * Interesting aspects:
     80  * 1. Wait for remote debugger to attach
     81  * Normally the program compiles custom script and immediately runs it.
     82  * If programmer needs to debug script from the very beginning, he should
     83  * run this sample program with "--wait-for-connection" command line parameter.
     84  * This way V8 will suspend on the first statement and wait for
     85  * debugger to attach.
     86  *
     87  * 2. Unresponsive V8
     88  * V8 Debugger Agent holds a connection with remote debugger, but it does
     89  * respond only when V8 is running some script. In particular, when this program
     90  * is waiting for input, all requests from debugger get deferred until V8
     91  * is called again. See how "--callback" command-line parameter in this sample
     92  * fixes this issue.
     93  */
     94 
     95 enum MainCycleType {
     96   CycleInCpp,
     97   CycleInJs
     98 };
     99 
    100 const char* ToCString(const v8::String::Utf8Value& value);
    101 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
    102 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
    103 v8::Handle<v8::String> ReadLine();
    104 
    105 void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
    106 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args);
    107 bool RunCppCycle(v8::Handle<v8::Script> script,
    108                  v8::Local<v8::Context> context,
    109                  bool report_exceptions);
    110 
    111 
    112 #ifdef ENABLE_DEBUGGER_SUPPORT
    113 v8::Persistent<v8::Context> debug_message_context;
    114 
    115 void DispatchDebugMessages() {
    116   // We are in some random thread. We should already have v8::Locker acquired
    117   // (we requested this when registered this callback). We was called
    118   // because new debug messages arrived; they may have already been processed,
    119   // but we shouldn't worry about this.
    120   //
    121   // All we have to do is to set context and call ProcessDebugMessages.
    122   //
    123   // We should decide which V8 context to use here. This is important for
    124   // "evaluate" command, because it must be executed some context.
    125   // In our sample we have only one context, so there is nothing really to
    126   // think about.
    127   v8::Isolate* isolate = v8::Isolate::GetCurrent();
    128   v8::HandleScope handle_scope(isolate);
    129   v8::Local<v8::Context> context =
    130       v8::Local<v8::Context>::New(isolate, debug_message_context);
    131   v8::Context::Scope scope(context);
    132 
    133   v8::Debug::ProcessDebugMessages();
    134 }
    135 #endif  // ENABLE_DEBUGGER_SUPPORT
    136 
    137 
    138 int RunMain(int argc, char* argv[]) {
    139   v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
    140   v8::Isolate* isolate = v8::Isolate::GetCurrent();
    141   v8::HandleScope handle_scope(isolate);
    142 
    143   v8::Handle<v8::String> script_source;
    144   v8::Handle<v8::Value> script_name;
    145   int script_param_counter = 0;
    146 
    147 #ifdef ENABLE_DEBUGGER_SUPPORT
    148   int port_number = -1;
    149   bool wait_for_connection = false;
    150   bool support_callback = false;
    151 #endif  // ENABLE_DEBUGGER_SUPPORT
    152 
    153   MainCycleType cycle_type = CycleInCpp;
    154 
    155   for (int i = 1; i < argc; i++) {
    156     const char* str = argv[i];
    157     if (strcmp(str, "-f") == 0) {
    158       // Ignore any -f flags for compatibility with the other stand-
    159       // alone JavaScript engines.
    160       continue;
    161     } else if (strcmp(str, "--main-cycle-in-cpp") == 0) {
    162       cycle_type = CycleInCpp;
    163     } else if (strcmp(str, "--main-cycle-in-js") == 0) {
    164       cycle_type = CycleInJs;
    165 #ifdef ENABLE_DEBUGGER_SUPPORT
    166     } else if (strcmp(str, "--callback") == 0) {
    167       support_callback = true;
    168     } else if (strcmp(str, "--wait-for-connection") == 0) {
    169       wait_for_connection = true;
    170     } else if (strcmp(str, "-p") == 0 && i + 1 < argc) {
    171       port_number = atoi(argv[i + 1]);  // NOLINT
    172       i++;
    173 #endif  // ENABLE_DEBUGGER_SUPPORT
    174     } else if (strncmp(str, "--", 2) == 0) {
    175       printf("Warning: unknown flag %s.\nTry --help for options\n", str);
    176     } else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
    177       script_source = v8::String::NewFromUtf8(isolate, argv[i + 1]);
    178       script_name = v8::String::NewFromUtf8(isolate, "unnamed");
    179       i++;
    180       script_param_counter++;
    181     } else {
    182       // Use argument as a name of file to load.
    183       script_source = ReadFile(isolate, str);
    184       script_name = v8::String::NewFromUtf8(isolate, str);
    185       if (script_source.IsEmpty()) {
    186         printf("Error reading '%s'\n", str);
    187         return 1;
    188       }
    189       script_param_counter++;
    190     }
    191   }
    192 
    193   if (script_param_counter == 0) {
    194     printf("Script is not specified\n");
    195     return 1;
    196   }
    197   if (script_param_counter != 1) {
    198     printf("Only one script may be specified\n");
    199     return 1;
    200   }
    201 
    202   // Create a template for the global object.
    203   v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
    204 
    205   // Bind the global 'print' function to the C++ Print callback.
    206   global->Set(v8::String::NewFromUtf8(isolate, "print"),
    207               v8::FunctionTemplate::New(Print));
    208 
    209   if (cycle_type == CycleInJs) {
    210     // Bind the global 'read_line' function to the C++ Print callback.
    211     global->Set(v8::String::NewFromUtf8(isolate, "read_line"),
    212                 v8::FunctionTemplate::New(ReadLine));
    213   }
    214 
    215   // Create a new execution environment containing the built-in
    216   // functions
    217   v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global);
    218   // Enter the newly created execution environment.
    219   v8::Context::Scope context_scope(context);
    220 
    221 #ifdef ENABLE_DEBUGGER_SUPPORT
    222   debug_message_context.Reset(isolate, context);
    223 
    224   v8::Locker locker(isolate);
    225 
    226   if (support_callback) {
    227     v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true);
    228   }
    229 
    230   if (port_number != -1) {
    231     v8::Debug::EnableAgent("lineprocessor", port_number, wait_for_connection);
    232   }
    233 #endif  // ENABLE_DEBUGGER_SUPPORT
    234 
    235   bool report_exceptions = true;
    236 
    237   v8::Handle<v8::Script> script;
    238   {
    239     // Compile script in try/catch context.
    240     v8::TryCatch try_catch;
    241     script = v8::Script::Compile(script_source, script_name);
    242     if (script.IsEmpty()) {
    243       // Print errors that happened during compilation.
    244       if (report_exceptions)
    245         ReportException(isolate, &try_catch);
    246       return 1;
    247     }
    248   }
    249 
    250   {
    251     v8::TryCatch try_catch;
    252 
    253     script->Run();
    254     if (try_catch.HasCaught()) {
    255       if (report_exceptions)
    256         ReportException(isolate, &try_catch);
    257       return 1;
    258     }
    259   }
    260 
    261   if (cycle_type == CycleInCpp) {
    262     bool res = RunCppCycle(script,
    263                            isolate->GetCurrentContext(),
    264                            report_exceptions);
    265     return !res;
    266   } else {
    267     // All is already done.
    268   }
    269   return 0;
    270 }
    271 
    272 
    273 bool RunCppCycle(v8::Handle<v8::Script> script,
    274                  v8::Local<v8::Context> context,
    275                  bool report_exceptions) {
    276   v8::Isolate* isolate = context->GetIsolate();
    277 #ifdef ENABLE_DEBUGGER_SUPPORT
    278   v8::Locker lock(isolate);
    279 #endif  // ENABLE_DEBUGGER_SUPPORT
    280 
    281   v8::Handle<v8::String> fun_name =
    282       v8::String::NewFromUtf8(isolate, "ProcessLine");
    283   v8::Handle<v8::Value> process_val = context->Global()->Get(fun_name);
    284 
    285   // If there is no Process function, or if it is not a function,
    286   // bail out
    287   if (!process_val->IsFunction()) {
    288     printf("Error: Script does not declare 'ProcessLine' global function.\n");
    289     return 1;
    290   }
    291 
    292   // It is a function; cast it to a Function
    293   v8::Handle<v8::Function> process_fun =
    294       v8::Handle<v8::Function>::Cast(process_val);
    295 
    296 
    297   while (!feof(stdin)) {
    298     v8::HandleScope handle_scope(isolate);
    299 
    300     v8::Handle<v8::String> input_line = ReadLine();
    301     if (input_line == v8::Undefined(isolate)) {
    302       continue;
    303     }
    304 
    305     const int argc = 1;
    306     v8::Handle<v8::Value> argv[argc] = { input_line };
    307 
    308     v8::Handle<v8::Value> result;
    309     {
    310       v8::TryCatch try_catch;
    311       result = process_fun->Call(isolate->GetCurrentContext()->Global(),
    312                                  argc, argv);
    313       if (try_catch.HasCaught()) {
    314         if (report_exceptions)
    315           ReportException(isolate, &try_catch);
    316         return false;
    317       }
    318     }
    319     v8::String::Utf8Value str(result);
    320     const char* cstr = ToCString(str);
    321     printf("%s\n", cstr);
    322   }
    323 
    324   return true;
    325 }
    326 
    327 
    328 int main(int argc, char* argv[]) {
    329   v8::V8::InitializeICU();
    330   int result = RunMain(argc, argv);
    331   v8::V8::Dispose();
    332   return result;
    333 }
    334 
    335 
    336 // Extracts a C string from a V8 Utf8Value.
    337 const char* ToCString(const v8::String::Utf8Value& value) {
    338   return *value ? *value : "<string conversion failed>";
    339 }
    340 
    341 
    342 // Reads a file into a v8 string.
    343 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
    344   FILE* file = fopen(name, "rb");
    345   if (file == NULL) return v8::Handle<v8::String>();
    346 
    347   fseek(file, 0, SEEK_END);
    348   int size = ftell(file);
    349   rewind(file);
    350 
    351   char* chars = new char[size + 1];
    352   chars[size] = '\0';
    353   for (int i = 0; i < size;) {
    354     int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
    355     i += read;
    356   }
    357   fclose(file);
    358   v8::Handle<v8::String> result =
    359       v8::String::NewFromUtf8(isolate, chars, v8::String::kNormalString, size);
    360   delete[] chars;
    361   return result;
    362 }
    363 
    364 
    365 void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
    366   v8::HandleScope handle_scope(isolate);
    367   v8::String::Utf8Value exception(try_catch->Exception());
    368   const char* exception_string = ToCString(exception);
    369   v8::Handle<v8::Message> message = try_catch->Message();
    370   if (message.IsEmpty()) {
    371     // V8 didn't provide any extra information about this error; just
    372     // print the exception.
    373     printf("%s\n", exception_string);
    374   } else {
    375     // Print (filename):(line number): (message).
    376     v8::String::Utf8Value filename(message->GetScriptResourceName());
    377     const char* filename_string = ToCString(filename);
    378     int linenum = message->GetLineNumber();
    379     printf("%s:%i: %s\n", filename_string, linenum, exception_string);
    380     // Print line of source code.
    381     v8::String::Utf8Value sourceline(message->GetSourceLine());
    382     const char* sourceline_string = ToCString(sourceline);
    383     printf("%s\n", sourceline_string);
    384     // Print wavy underline (GetUnderline is deprecated).
    385     int start = message->GetStartColumn();
    386     for (int i = 0; i < start; i++) {
    387       printf(" ");
    388     }
    389     int end = message->GetEndColumn();
    390     for (int i = start; i < end; i++) {
    391       printf("^");
    392     }
    393     printf("\n");
    394   }
    395 }
    396 
    397 
    398 // The callback that is invoked by v8 whenever the JavaScript 'print'
    399 // function is called.  Prints its arguments on stdout separated by
    400 // spaces and ending with a newline.
    401 void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
    402   bool first = true;
    403   for (int i = 0; i < args.Length(); i++) {
    404     v8::HandleScope handle_scope(args.GetIsolate());
    405     if (first) {
    406       first = false;
    407     } else {
    408       printf(" ");
    409     }
    410     v8::String::Utf8Value str(args[i]);
    411     const char* cstr = ToCString(str);
    412     printf("%s", cstr);
    413   }
    414   printf("\n");
    415   fflush(stdout);
    416 }
    417 
    418 
    419 // The callback that is invoked by v8 whenever the JavaScript 'read_line'
    420 // function is called. Reads a string from standard input and returns.
    421 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args) {
    422   if (args.Length() > 0) {
    423     args.GetIsolate()->ThrowException(
    424         v8::String::NewFromUtf8(args.GetIsolate(), "Unexpected arguments"));
    425     return;
    426   }
    427   args.GetReturnValue().Set(ReadLine());
    428 }
    429 
    430 
    431 v8::Handle<v8::String> ReadLine() {
    432   const int kBufferSize = 1024 + 1;
    433   char buffer[kBufferSize];
    434 
    435   char* res;
    436   {
    437 #ifdef ENABLE_DEBUGGER_SUPPORT
    438     v8::Unlocker unlocker(v8::Isolate::GetCurrent());
    439 #endif  // ENABLE_DEBUGGER_SUPPORT
    440     res = fgets(buffer, kBufferSize, stdin);
    441   }
    442   v8::Isolate* isolate = v8::Isolate::GetCurrent();
    443   if (res == NULL) {
    444     v8::Handle<v8::Primitive> t = v8::Undefined(isolate);
    445     return v8::Handle<v8::String>::Cast(t);
    446   }
    447   // Remove newline char
    448   for (char* pos = buffer; *pos != '\0'; pos++) {
    449     if (*pos == '\n') {
    450       *pos = '\0';
    451       break;
    452     }
    453   }
    454   return v8::String::NewFromUtf8(isolate, buffer);
    455 }
    456