| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 * and print converted lines to output itself. This a sample script: | 62 * and print converted lines to output itself. This a sample script: |
| 63 | 63 |
| 64 while (true) { | 64 while (true) { |
| 65 var line = read_line(); | 65 var line = read_line(); |
| 66 if (!line) { | 66 if (!line) { |
| 67 break; | 67 break; |
| 68 } | 68 } |
| 69 var res = line + " | " + line; | 69 var res = line + " | " + line; |
| 70 print(res); | 70 print(res); |
| 71 } | 71 } |
| 72 | |
| 73 * | |
| 74 * When run with "-p" argument, the program starts V8 Debugger Agent and | |
| 75 * allows remote debugger to attach and debug JavaScript code. | |
| 76 * | |
| 77 * Interesting aspects: | |
| 78 * 1. Wait for remote debugger to attach | |
| 79 * Normally the program compiles custom script and immediately runs it. | |
| 80 * If programmer needs to debug script from the very beginning, he should | |
| 81 * run this sample program with "--wait-for-connection" command line parameter. | |
| 82 * This way V8 will suspend on the first statement and wait for | |
| 83 * debugger to attach. | |
| 84 * | |
| 85 * 2. Unresponsive V8 | |
| 86 * V8 Debugger Agent holds a connection with remote debugger, but it does | |
| 87 * respond only when V8 is running some script. In particular, when this program | |
| 88 * is waiting for input, all requests from debugger get deferred until V8 | |
| 89 * is called again. See how "--callback" command-line parameter in this sample | |
| 90 * fixes this issue. | |
| 91 */ | 72 */ |
| 92 | 73 |
| 93 enum MainCycleType { | 74 enum MainCycleType { |
| 94 CycleInCpp, | 75 CycleInCpp, |
| 95 CycleInJs | 76 CycleInJs |
| 96 }; | 77 }; |
| 97 | 78 |
| 98 const char* ToCString(const v8::String::Utf8Value& value); | 79 const char* ToCString(const v8::String::Utf8Value& value); |
| 99 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); | 80 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); |
| 100 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name); | 81 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name); |
| 101 v8::Handle<v8::String> ReadLine(); | 82 v8::Handle<v8::String> ReadLine(); |
| 102 | 83 |
| 103 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); | 84 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 104 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args); | 85 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 105 bool RunCppCycle(v8::Handle<v8::Script> script, | 86 bool RunCppCycle(v8::Handle<v8::Script> script, |
| 106 v8::Local<v8::Context> context, | 87 v8::Local<v8::Context> context, |
| 107 bool report_exceptions); | 88 bool report_exceptions); |
| 108 | 89 |
| 109 | 90 |
| 110 v8::Persistent<v8::Context> debug_message_context; | 91 v8::Persistent<v8::Context> debug_message_context; |
| 111 | 92 |
| 112 int RunMain(int argc, char* argv[]) { | 93 int RunMain(int argc, char* argv[]) { |
| 113 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 94 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 114 v8::Isolate* isolate = v8::Isolate::New(); | 95 v8::Isolate* isolate = v8::Isolate::New(); |
| 115 v8::Isolate::Scope isolate_scope(isolate); | 96 v8::Isolate::Scope isolate_scope(isolate); |
| 116 v8::Locker locker(isolate); | |
| 117 v8::HandleScope handle_scope(isolate); | 97 v8::HandleScope handle_scope(isolate); |
| 118 | 98 |
| 119 v8::Handle<v8::String> script_source; | 99 v8::Handle<v8::String> script_source; |
| 120 v8::Handle<v8::Value> script_name; | 100 v8::Handle<v8::Value> script_name; |
| 121 int script_param_counter = 0; | 101 int script_param_counter = 0; |
| 122 | 102 |
| 123 MainCycleType cycle_type = CycleInCpp; | 103 MainCycleType cycle_type = CycleInCpp; |
| 124 | 104 |
| 125 for (int i = 1; i < argc; i++) { | 105 for (int i = 1; i < argc; i++) { |
| 126 const char* str = argv[i]; | 106 const char* str = argv[i]; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 // All is already done. | 197 // All is already done. |
| 218 } | 198 } |
| 219 return 0; | 199 return 0; |
| 220 } | 200 } |
| 221 | 201 |
| 222 | 202 |
| 223 bool RunCppCycle(v8::Handle<v8::Script> script, | 203 bool RunCppCycle(v8::Handle<v8::Script> script, |
| 224 v8::Local<v8::Context> context, | 204 v8::Local<v8::Context> context, |
| 225 bool report_exceptions) { | 205 bool report_exceptions) { |
| 226 v8::Isolate* isolate = context->GetIsolate(); | 206 v8::Isolate* isolate = context->GetIsolate(); |
| 227 v8::Locker lock(isolate); | |
| 228 | 207 |
| 229 v8::Handle<v8::String> fun_name = | 208 v8::Handle<v8::String> fun_name = |
| 230 v8::String::NewFromUtf8(isolate, "ProcessLine"); | 209 v8::String::NewFromUtf8(isolate, "ProcessLine"); |
| 231 v8::Handle<v8::Value> process_val = context->Global()->Get(fun_name); | 210 v8::Handle<v8::Value> process_val = context->Global()->Get(fun_name); |
| 232 | 211 |
| 233 // If there is no Process function, or if it is not a function, | 212 // If there is no Process function, or if it is not a function, |
| 234 // bail out | 213 // bail out |
| 235 if (!process_val->IsFunction()) { | 214 if (!process_val->IsFunction()) { |
| 236 printf("Error: Script does not declare 'ProcessLine' global function.\n"); | 215 printf("Error: Script does not declare 'ProcessLine' global function.\n"); |
| 237 return 1; | 216 return 1; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 args.GetReturnValue().Set(ReadLine()); | 354 args.GetReturnValue().Set(ReadLine()); |
| 376 } | 355 } |
| 377 | 356 |
| 378 | 357 |
| 379 v8::Handle<v8::String> ReadLine() { | 358 v8::Handle<v8::String> ReadLine() { |
| 380 const int kBufferSize = 1024 + 1; | 359 const int kBufferSize = 1024 + 1; |
| 381 char buffer[kBufferSize]; | 360 char buffer[kBufferSize]; |
| 382 | 361 |
| 383 char* res; | 362 char* res; |
| 384 { | 363 { |
| 385 v8::Unlocker unlocker(v8::Isolate::GetCurrent()); | |
| 386 res = fgets(buffer, kBufferSize, stdin); | 364 res = fgets(buffer, kBufferSize, stdin); |
| 387 } | 365 } |
| 388 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 366 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 389 if (res == NULL) { | 367 if (res == NULL) { |
| 390 v8::Handle<v8::Primitive> t = v8::Undefined(isolate); | 368 v8::Handle<v8::Primitive> t = v8::Undefined(isolate); |
| 391 return v8::Handle<v8::String>::Cast(t); | 369 return v8::Handle<v8::String>::Cast(t); |
| 392 } | 370 } |
| 393 // Remove newline char | 371 // Remove newline char |
| 394 for (char* pos = buffer; *pos != '\0'; pos++) { | 372 for (char* pos = buffer; *pos != '\0'; pos++) { |
| 395 if (*pos == '\n') { | 373 if (*pos == '\n') { |
| 396 *pos = '\0'; | 374 *pos = '\0'; |
| 397 break; | 375 break; |
| 398 } | 376 } |
| 399 } | 377 } |
| 400 return v8::String::NewFromUtf8(isolate, buffer); | 378 return v8::String::NewFromUtf8(isolate, buffer); |
| 401 } | 379 } |
| OLD | NEW |