| 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include <v8.h> | 28 #include <v8.h> |
| 29 | 29 |
| 30 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 31 #include <v8-debug.h> | 30 #include <v8-debug.h> |
| 32 #endif // ENABLE_DEBUGGER_SUPPORT | |
| 33 | 31 |
| 34 #include <fcntl.h> | 32 #include <fcntl.h> |
| 35 #include <string.h> | 33 #include <string.h> |
| 36 #include <stdio.h> | 34 #include <stdio.h> |
| 37 #include <stdlib.h> | 35 #include <stdlib.h> |
| 38 | 36 |
| 39 /** | 37 /** |
| 40 * This sample program should demonstrate certain aspects of debugging | 38 * This sample program should demonstrate certain aspects of debugging |
| 41 * standalone V8-based application. | 39 * standalone V8-based application. |
| 42 * | 40 * |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name); | 100 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name); |
| 103 v8::Handle<v8::String> ReadLine(); | 101 v8::Handle<v8::String> ReadLine(); |
| 104 | 102 |
| 105 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); | 103 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 106 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args); | 104 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 107 bool RunCppCycle(v8::Handle<v8::Script> script, | 105 bool RunCppCycle(v8::Handle<v8::Script> script, |
| 108 v8::Local<v8::Context> context, | 106 v8::Local<v8::Context> context, |
| 109 bool report_exceptions); | 107 bool report_exceptions); |
| 110 | 108 |
| 111 | 109 |
| 112 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 113 v8::Persistent<v8::Context> debug_message_context; | 110 v8::Persistent<v8::Context> debug_message_context; |
| 114 | 111 |
| 115 void DispatchDebugMessages() { | 112 void DispatchDebugMessages() { |
| 116 // We are in some random thread. We should already have v8::Locker acquired | 113 // We are in some random thread. We should already have v8::Locker acquired |
| 117 // (we requested this when registered this callback). We was called | 114 // (we requested this when registered this callback). We was called |
| 118 // because new debug messages arrived; they may have already been processed, | 115 // because new debug messages arrived; they may have already been processed, |
| 119 // but we shouldn't worry about this. | 116 // but we shouldn't worry about this. |
| 120 // | 117 // |
| 121 // All we have to do is to set context and call ProcessDebugMessages. | 118 // All we have to do is to set context and call ProcessDebugMessages. |
| 122 // | 119 // |
| 123 // We should decide which V8 context to use here. This is important for | 120 // We should decide which V8 context to use here. This is important for |
| 124 // "evaluate" command, because it must be executed some context. | 121 // "evaluate" command, because it must be executed some context. |
| 125 // In our sample we have only one context, so there is nothing really to | 122 // In our sample we have only one context, so there is nothing really to |
| 126 // think about. | 123 // think about. |
| 127 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 124 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 128 v8::HandleScope handle_scope(isolate); | 125 v8::HandleScope handle_scope(isolate); |
| 129 v8::Local<v8::Context> context = | 126 v8::Local<v8::Context> context = |
| 130 v8::Local<v8::Context>::New(isolate, debug_message_context); | 127 v8::Local<v8::Context>::New(isolate, debug_message_context); |
| 131 v8::Context::Scope scope(context); | 128 v8::Context::Scope scope(context); |
| 132 | 129 |
| 133 v8::Debug::ProcessDebugMessages(); | 130 v8::Debug::ProcessDebugMessages(); |
| 134 } | 131 } |
| 135 #endif // ENABLE_DEBUGGER_SUPPORT | |
| 136 | 132 |
| 137 | 133 |
| 138 int RunMain(int argc, char* argv[]) { | 134 int RunMain(int argc, char* argv[]) { |
| 139 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 135 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 140 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 136 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 141 v8::HandleScope handle_scope(isolate); | 137 v8::HandleScope handle_scope(isolate); |
| 142 | 138 |
| 143 v8::Handle<v8::String> script_source; | 139 v8::Handle<v8::String> script_source; |
| 144 v8::Handle<v8::Value> script_name; | 140 v8::Handle<v8::Value> script_name; |
| 145 int script_param_counter = 0; | 141 int script_param_counter = 0; |
| 146 | 142 |
| 147 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 148 int port_number = -1; | 143 int port_number = -1; |
| 149 bool wait_for_connection = false; | 144 bool wait_for_connection = false; |
| 150 bool support_callback = false; | 145 bool support_callback = false; |
| 151 #endif // ENABLE_DEBUGGER_SUPPORT | |
| 152 | 146 |
| 153 MainCycleType cycle_type = CycleInCpp; | 147 MainCycleType cycle_type = CycleInCpp; |
| 154 | 148 |
| 155 for (int i = 1; i < argc; i++) { | 149 for (int i = 1; i < argc; i++) { |
| 156 const char* str = argv[i]; | 150 const char* str = argv[i]; |
| 157 if (strcmp(str, "-f") == 0) { | 151 if (strcmp(str, "-f") == 0) { |
| 158 // Ignore any -f flags for compatibility with the other stand- | 152 // Ignore any -f flags for compatibility with the other stand- |
| 159 // alone JavaScript engines. | 153 // alone JavaScript engines. |
| 160 continue; | 154 continue; |
| 161 } else if (strcmp(str, "--main-cycle-in-cpp") == 0) { | 155 } else if (strcmp(str, "--main-cycle-in-cpp") == 0) { |
| 162 cycle_type = CycleInCpp; | 156 cycle_type = CycleInCpp; |
| 163 } else if (strcmp(str, "--main-cycle-in-js") == 0) { | 157 } else if (strcmp(str, "--main-cycle-in-js") == 0) { |
| 164 cycle_type = CycleInJs; | 158 cycle_type = CycleInJs; |
| 165 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 166 } else if (strcmp(str, "--callback") == 0) { | 159 } else if (strcmp(str, "--callback") == 0) { |
| 167 support_callback = true; | 160 support_callback = true; |
| 168 } else if (strcmp(str, "--wait-for-connection") == 0) { | 161 } else if (strcmp(str, "--wait-for-connection") == 0) { |
| 169 wait_for_connection = true; | 162 wait_for_connection = true; |
| 170 } else if (strcmp(str, "-p") == 0 && i + 1 < argc) { | 163 } else if (strcmp(str, "-p") == 0 && i + 1 < argc) { |
| 171 port_number = atoi(argv[i + 1]); // NOLINT | 164 port_number = atoi(argv[i + 1]); // NOLINT |
| 172 i++; | 165 i++; |
| 173 #endif // ENABLE_DEBUGGER_SUPPORT | |
| 174 } else if (strncmp(str, "--", 2) == 0) { | 166 } else if (strncmp(str, "--", 2) == 0) { |
| 175 printf("Warning: unknown flag %s.\nTry --help for options\n", str); | 167 printf("Warning: unknown flag %s.\nTry --help for options\n", str); |
| 176 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { | 168 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { |
| 177 script_source = v8::String::NewFromUtf8(isolate, argv[i + 1]); | 169 script_source = v8::String::NewFromUtf8(isolate, argv[i + 1]); |
| 178 script_name = v8::String::NewFromUtf8(isolate, "unnamed"); | 170 script_name = v8::String::NewFromUtf8(isolate, "unnamed"); |
| 179 i++; | 171 i++; |
| 180 script_param_counter++; | 172 script_param_counter++; |
| 181 } else { | 173 } else { |
| 182 // Use argument as a name of file to load. | 174 // Use argument as a name of file to load. |
| 183 script_source = ReadFile(isolate, str); | 175 script_source = ReadFile(isolate, str); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 211 global->Set(v8::String::NewFromUtf8(isolate, "read_line"), | 203 global->Set(v8::String::NewFromUtf8(isolate, "read_line"), |
| 212 v8::FunctionTemplate::New(isolate, ReadLine)); | 204 v8::FunctionTemplate::New(isolate, ReadLine)); |
| 213 } | 205 } |
| 214 | 206 |
| 215 // Create a new execution environment containing the built-in | 207 // Create a new execution environment containing the built-in |
| 216 // functions | 208 // functions |
| 217 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global); | 209 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global); |
| 218 // Enter the newly created execution environment. | 210 // Enter the newly created execution environment. |
| 219 v8::Context::Scope context_scope(context); | 211 v8::Context::Scope context_scope(context); |
| 220 | 212 |
| 221 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 222 debug_message_context.Reset(isolate, context); | 213 debug_message_context.Reset(isolate, context); |
| 223 | 214 |
| 224 v8::Locker locker(isolate); | 215 v8::Locker locker(isolate); |
| 225 | 216 |
| 226 if (support_callback) { | 217 if (support_callback) { |
| 227 v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true); | 218 v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true); |
| 228 } | 219 } |
| 229 | 220 |
| 230 if (port_number != -1) { | 221 if (port_number != -1) { |
| 231 v8::Debug::EnableAgent("lineprocessor", port_number, wait_for_connection); | 222 v8::Debug::EnableAgent("lineprocessor", port_number, wait_for_connection); |
| 232 } | 223 } |
| 233 #endif // ENABLE_DEBUGGER_SUPPORT | |
| 234 | 224 |
| 235 bool report_exceptions = true; | 225 bool report_exceptions = true; |
| 236 | 226 |
| 237 v8::Handle<v8::Script> script; | 227 v8::Handle<v8::Script> script; |
| 238 { | 228 { |
| 239 // Compile script in try/catch context. | 229 // Compile script in try/catch context. |
| 240 v8::TryCatch try_catch; | 230 v8::TryCatch try_catch; |
| 241 v8::ScriptOrigin origin(script_name); | 231 v8::ScriptOrigin origin(script_name); |
| 242 script = v8::Script::Compile(script_source, &origin); | 232 script = v8::Script::Compile(script_source, &origin); |
| 243 if (script.IsEmpty()) { | 233 if (script.IsEmpty()) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 268 // All is already done. | 258 // All is already done. |
| 269 } | 259 } |
| 270 return 0; | 260 return 0; |
| 271 } | 261 } |
| 272 | 262 |
| 273 | 263 |
| 274 bool RunCppCycle(v8::Handle<v8::Script> script, | 264 bool RunCppCycle(v8::Handle<v8::Script> script, |
| 275 v8::Local<v8::Context> context, | 265 v8::Local<v8::Context> context, |
| 276 bool report_exceptions) { | 266 bool report_exceptions) { |
| 277 v8::Isolate* isolate = context->GetIsolate(); | 267 v8::Isolate* isolate = context->GetIsolate(); |
| 278 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 279 v8::Locker lock(isolate); | 268 v8::Locker lock(isolate); |
| 280 #endif // ENABLE_DEBUGGER_SUPPORT | |
| 281 | 269 |
| 282 v8::Handle<v8::String> fun_name = | 270 v8::Handle<v8::String> fun_name = |
| 283 v8::String::NewFromUtf8(isolate, "ProcessLine"); | 271 v8::String::NewFromUtf8(isolate, "ProcessLine"); |
| 284 v8::Handle<v8::Value> process_val = context->Global()->Get(fun_name); | 272 v8::Handle<v8::Value> process_val = context->Global()->Get(fun_name); |
| 285 | 273 |
| 286 // If there is no Process function, or if it is not a function, | 274 // If there is no Process function, or if it is not a function, |
| 287 // bail out | 275 // bail out |
| 288 if (!process_val->IsFunction()) { | 276 if (!process_val->IsFunction()) { |
| 289 printf("Error: Script does not declare 'ProcessLine' global function.\n"); | 277 printf("Error: Script does not declare 'ProcessLine' global function.\n"); |
| 290 return 1; | 278 return 1; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 args.GetReturnValue().Set(ReadLine()); | 416 args.GetReturnValue().Set(ReadLine()); |
| 429 } | 417 } |
| 430 | 418 |
| 431 | 419 |
| 432 v8::Handle<v8::String> ReadLine() { | 420 v8::Handle<v8::String> ReadLine() { |
| 433 const int kBufferSize = 1024 + 1; | 421 const int kBufferSize = 1024 + 1; |
| 434 char buffer[kBufferSize]; | 422 char buffer[kBufferSize]; |
| 435 | 423 |
| 436 char* res; | 424 char* res; |
| 437 { | 425 { |
| 438 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 439 v8::Unlocker unlocker(v8::Isolate::GetCurrent()); | 426 v8::Unlocker unlocker(v8::Isolate::GetCurrent()); |
| 440 #endif // ENABLE_DEBUGGER_SUPPORT | |
| 441 res = fgets(buffer, kBufferSize, stdin); | 427 res = fgets(buffer, kBufferSize, stdin); |
| 442 } | 428 } |
| 443 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 429 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 444 if (res == NULL) { | 430 if (res == NULL) { |
| 445 v8::Handle<v8::Primitive> t = v8::Undefined(isolate); | 431 v8::Handle<v8::Primitive> t = v8::Undefined(isolate); |
| 446 return v8::Handle<v8::String>::Cast(t); | 432 return v8::Handle<v8::String>::Cast(t); |
| 447 } | 433 } |
| 448 // Remove newline char | 434 // Remove newline char |
| 449 for (char* pos = buffer; *pos != '\0'; pos++) { | 435 for (char* pos = buffer; *pos != '\0'; pos++) { |
| 450 if (*pos == '\n') { | 436 if (*pos == '\n') { |
| 451 *pos = '\0'; | 437 *pos = '\0'; |
| 452 break; | 438 break; |
| 453 } | 439 } |
| 454 } | 440 } |
| 455 return v8::String::NewFromUtf8(isolate, buffer); | 441 return v8::String::NewFromUtf8(isolate, buffer); |
| 456 } | 442 } |
| OLD | NEW |