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 | |
29 // This controls whether this sample is compiled with debugger support. | |
30 // You may trace its usages in source text to see what parts of program | |
31 // are responsible for debugging support. | |
32 // Note that V8 itself should be compiled with enabled debugger support | |
33 // to have it all working. | |
34 #define SUPPORT_DEBUGGING | |
35 | |
36 #include <v8.h> | 28 #include <v8.h> |
37 | 29 |
38 #ifdef SUPPORT_DEBUGGING | 30 #ifdef ENABLE_DEBUGGER_SUPPORT |
39 #include <v8-debug.h> | 31 #include <v8-debug.h> |
40 #endif | 32 #endif // ENABLE_DEBUGGER_SUPPORT |
41 | 33 |
42 #include <fcntl.h> | 34 #include <fcntl.h> |
43 #include <string.h> | 35 #include <string.h> |
44 #include <stdio.h> | 36 #include <stdio.h> |
45 #include <stdlib.h> | 37 #include <stdlib.h> |
46 | 38 |
47 /** | 39 /** |
48 * This sample program should demonstrate certain aspects of debugging | 40 * This sample program should demonstrate certain aspects of debugging |
49 * standalone V8-based application. | 41 * standalone V8-based application. |
50 * | 42 * |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 void ReportException(v8::TryCatch* handler); | 101 void ReportException(v8::TryCatch* handler); |
110 v8::Handle<v8::String> ReadFile(const char* name); | 102 v8::Handle<v8::String> ReadFile(const char* name); |
111 v8::Handle<v8::String> ReadLine(); | 103 v8::Handle<v8::String> ReadLine(); |
112 | 104 |
113 v8::Handle<v8::Value> Print(const v8::Arguments& args); | 105 v8::Handle<v8::Value> Print(const v8::Arguments& args); |
114 v8::Handle<v8::Value> ReadLine(const v8::Arguments& args); | 106 v8::Handle<v8::Value> ReadLine(const v8::Arguments& args); |
115 bool RunCppCycle(v8::Handle<v8::Script> script, v8::Local<v8::Context> context, | 107 bool RunCppCycle(v8::Handle<v8::Script> script, v8::Local<v8::Context> context, |
116 bool report_exceptions); | 108 bool report_exceptions); |
117 | 109 |
118 | 110 |
119 #ifdef SUPPORT_DEBUGGING | 111 #ifdef ENABLE_DEBUGGER_SUPPORT |
120 v8::Persistent<v8::Context> debug_message_context; | 112 v8::Persistent<v8::Context> debug_message_context; |
121 | 113 |
122 void DispatchDebugMessages() { | 114 void DispatchDebugMessages() { |
123 // We are in some random thread. We should already have v8::Locker acquired | 115 // We are in some random thread. We should already have v8::Locker acquired |
124 // (we requested this when registered this callback). We was called | 116 // (we requested this when registered this callback). We was called |
125 // because new debug messages arrived; they may have already been processed, | 117 // because new debug messages arrived; they may have already been processed, |
126 // but we shouldn't worry about this. | 118 // but we shouldn't worry about this. |
127 // | 119 // |
128 // All we have to do is to set context and call ProcessDebugMessages. | 120 // All we have to do is to set context and call ProcessDebugMessages. |
129 // | 121 // |
130 // We should decide which V8 context to use here. This is important for | 122 // We should decide which V8 context to use here. This is important for |
131 // "evaluate" command, because it must be executed some context. | 123 // "evaluate" command, because it must be executed some context. |
132 // In our sample we have only one context, so there is nothing really to | 124 // In our sample we have only one context, so there is nothing really to |
133 // think about. | 125 // think about. |
134 v8::Context::Scope scope(debug_message_context); | 126 v8::Context::Scope scope(debug_message_context); |
135 | 127 |
136 v8::Debug::ProcessDebugMessages(); | 128 v8::Debug::ProcessDebugMessages(); |
137 } | 129 } |
138 #endif | 130 #endif // ENABLE_DEBUGGER_SUPPORT |
139 | 131 |
140 | 132 |
141 int RunMain(int argc, char* argv[]) { | 133 int RunMain(int argc, char* argv[]) { |
142 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 134 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
143 v8::HandleScope handle_scope; | 135 v8::HandleScope handle_scope; |
144 | 136 |
145 v8::Handle<v8::String> script_source(NULL); | 137 v8::Handle<v8::String> script_source(NULL); |
146 v8::Handle<v8::Value> script_name(NULL); | 138 v8::Handle<v8::Value> script_name(NULL); |
147 int script_param_counter = 0; | 139 int script_param_counter = 0; |
148 | 140 |
149 #ifdef SUPPORT_DEBUGGING | 141 #ifdef ENABLE_DEBUGGER_SUPPORT |
150 int port_number = -1; | 142 int port_number = -1; |
151 bool wait_for_connection = false; | 143 bool wait_for_connection = false; |
152 bool support_callback = false; | 144 bool support_callback = false; |
153 #endif | 145 #endif // ENABLE_DEBUGGER_SUPPORT |
154 | 146 |
155 MainCycleType cycle_type = CycleInCpp; | 147 MainCycleType cycle_type = CycleInCpp; |
156 | 148 |
157 for (int i = 1; i < argc; i++) { | 149 for (int i = 1; i < argc; i++) { |
158 const char* str = argv[i]; | 150 const char* str = argv[i]; |
159 if (strcmp(str, "-f") == 0) { | 151 if (strcmp(str, "-f") == 0) { |
160 // Ignore any -f flags for compatibility with the other stand- | 152 // Ignore any -f flags for compatibility with the other stand- |
161 // alone JavaScript engines. | 153 // alone JavaScript engines. |
162 continue; | 154 continue; |
163 } else if (strcmp(str, "--main-cycle-in-cpp") == 0) { | 155 } else if (strcmp(str, "--main-cycle-in-cpp") == 0) { |
164 cycle_type = CycleInCpp; | 156 cycle_type = CycleInCpp; |
165 } else if (strcmp(str, "--main-cycle-in-js") == 0) { | 157 } else if (strcmp(str, "--main-cycle-in-js") == 0) { |
166 cycle_type = CycleInJs; | 158 cycle_type = CycleInJs; |
167 #ifdef SUPPORT_DEBUGGING | 159 #ifdef ENABLE_DEBUGGER_SUPPORT |
168 } else if (strcmp(str, "--callback") == 0) { | 160 } else if (strcmp(str, "--callback") == 0) { |
169 support_callback = true; | 161 support_callback = true; |
170 } else if (strcmp(str, "--wait-for-connection") == 0) { | 162 } else if (strcmp(str, "--wait-for-connection") == 0) { |
171 wait_for_connection = true; | 163 wait_for_connection = true; |
172 } else if (strcmp(str, "-p") == 0 && i + 1 < argc) { | 164 } else if (strcmp(str, "-p") == 0 && i + 1 < argc) { |
173 port_number = atoi(argv[i + 1]); // NOLINT | 165 port_number = atoi(argv[i + 1]); // NOLINT |
174 i++; | 166 i++; |
175 #endif | 167 #endif // ENABLE_DEBUGGER_SUPPORT |
176 } else if (strncmp(str, "--", 2) == 0) { | 168 } else if (strncmp(str, "--", 2) == 0) { |
177 printf("Warning: unknown flag %s.\nTry --help for options\n", str); | 169 printf("Warning: unknown flag %s.\nTry --help for options\n", str); |
178 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { | 170 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { |
179 script_source = v8::String::New(argv[i + 1]); | 171 script_source = v8::String::New(argv[i + 1]); |
180 script_name = v8::String::New("unnamed"); | 172 script_name = v8::String::New("unnamed"); |
181 i++; | 173 i++; |
182 script_param_counter++; | 174 script_param_counter++; |
183 } else { | 175 } else { |
184 // Use argument as a name of file to load. | 176 // Use argument as a name of file to load. |
185 script_source = ReadFile(str); | 177 script_source = ReadFile(str); |
(...skipping 26 matching lines...) Expand all Loading... |
212 global->Set(v8::String::New("read_line"), | 204 global->Set(v8::String::New("read_line"), |
213 v8::FunctionTemplate::New(ReadLine)); | 205 v8::FunctionTemplate::New(ReadLine)); |
214 } | 206 } |
215 | 207 |
216 // Create a new execution environment containing the built-in | 208 // Create a new execution environment containing the built-in |
217 // functions | 209 // functions |
218 v8::Handle<v8::Context> context = v8::Context::New(NULL, global); | 210 v8::Handle<v8::Context> context = v8::Context::New(NULL, global); |
219 // Enter the newly created execution environment. | 211 // Enter the newly created execution environment. |
220 v8::Context::Scope context_scope(context); | 212 v8::Context::Scope context_scope(context); |
221 | 213 |
222 #ifdef SUPPORT_DEBUGGING | 214 #ifdef ENABLE_DEBUGGER_SUPPORT |
223 debug_message_context = v8::Persistent<v8::Context>::New(context); | 215 debug_message_context = v8::Persistent<v8::Context>::New(context); |
224 | 216 |
225 v8::Locker locker; | 217 v8::Locker locker; |
226 | 218 |
227 if (support_callback) { | 219 if (support_callback) { |
228 v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true); | 220 v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true); |
229 } | 221 } |
230 | 222 |
231 if (port_number != -1) { | 223 if (port_number != -1) { |
232 v8::Debug::EnableAgent("lineprocessor", port_number, wait_for_connection); | 224 v8::Debug::EnableAgent("lineprocessor", port_number, wait_for_connection); |
233 } | 225 } |
234 #endif | 226 #endif // ENABLE_DEBUGGER_SUPPORT |
235 | 227 |
236 bool report_exceptions = true; | 228 bool report_exceptions = true; |
237 | 229 |
238 v8::Handle<v8::Script> script; | 230 v8::Handle<v8::Script> script; |
239 { | 231 { |
240 // Compile script in try/catch context. | 232 // Compile script in try/catch context. |
241 v8::TryCatch try_catch; | 233 v8::TryCatch try_catch; |
242 script = v8::Script::Compile(script_source, script_name); | 234 script = v8::Script::Compile(script_source, script_name); |
243 if (script.IsEmpty()) { | 235 if (script.IsEmpty()) { |
244 // Print errors that happened during compilation. | 236 // Print errors that happened during compilation. |
(...skipping 20 matching lines...) Expand all Loading... |
265 return !res; | 257 return !res; |
266 } else { | 258 } else { |
267 // All is already done. | 259 // All is already done. |
268 } | 260 } |
269 return 0; | 261 return 0; |
270 } | 262 } |
271 | 263 |
272 | 264 |
273 bool RunCppCycle(v8::Handle<v8::Script> script, v8::Local<v8::Context> context, | 265 bool RunCppCycle(v8::Handle<v8::Script> script, v8::Local<v8::Context> context, |
274 bool report_exceptions) { | 266 bool report_exceptions) { |
275 #ifdef SUPPORT_DEBUGGING | 267 #ifdef ENABLE_DEBUGGER_SUPPORT |
276 v8::Locker lock; | 268 v8::Locker lock; |
277 #endif | 269 #endif // ENABLE_DEBUGGER_SUPPORT |
278 | 270 |
279 v8::Handle<v8::String> fun_name = v8::String::New("ProcessLine"); | 271 v8::Handle<v8::String> fun_name = v8::String::New("ProcessLine"); |
280 v8::Handle<v8::Value> process_val = | 272 v8::Handle<v8::Value> process_val = |
281 v8::Context::GetCurrent()->Global()->Get(fun_name); | 273 v8::Context::GetCurrent()->Global()->Get(fun_name); |
282 | 274 |
283 // If there is no Process function, or if it is not a function, | 275 // If there is no Process function, or if it is not a function, |
284 // bail out | 276 // bail out |
285 if (!process_val->IsFunction()) { | 277 if (!process_val->IsFunction()) { |
286 printf("Error: Script does not declare 'ProcessLine' global function.\n"); | 278 printf("Error: Script does not declare 'ProcessLine' global function.\n"); |
287 return 1; | 279 return 1; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 } | 412 } |
421 return ReadLine(); | 413 return ReadLine(); |
422 } | 414 } |
423 | 415 |
424 v8::Handle<v8::String> ReadLine() { | 416 v8::Handle<v8::String> ReadLine() { |
425 const int kBufferSize = 1024 + 1; | 417 const int kBufferSize = 1024 + 1; |
426 char buffer[kBufferSize]; | 418 char buffer[kBufferSize]; |
427 | 419 |
428 char* res; | 420 char* res; |
429 { | 421 { |
430 #ifdef SUPPORT_DEBUGGING | 422 #ifdef ENABLE_DEBUGGER_SUPPORT |
431 v8::Unlocker unlocker; | 423 v8::Unlocker unlocker; |
432 #endif | 424 #endif // ENABLE_DEBUGGER_SUPPORT |
433 res = fgets(buffer, kBufferSize, stdin); | 425 res = fgets(buffer, kBufferSize, stdin); |
434 } | 426 } |
435 if (res == NULL) { | 427 if (res == NULL) { |
436 v8::Handle<v8::Primitive> t = v8::Undefined(); | 428 v8::Handle<v8::Primitive> t = v8::Undefined(); |
437 return v8::Handle<v8::String>(v8::String::Cast(*t)); | 429 return v8::Handle<v8::String>(v8::String::Cast(*t)); |
438 } | 430 } |
439 // Remove newline char | 431 // Remove newline char |
440 for (char* pos = buffer; *pos != '\0'; pos++) { | 432 for (char* pos = buffer; *pos != '\0'; pos++) { |
441 if (*pos == '\n') { | 433 if (*pos == '\n') { |
442 *pos = '\0'; | 434 *pos = '\0'; |
443 break; | 435 break; |
444 } | 436 } |
445 } | 437 } |
446 return v8::String::New(buffer); | 438 return v8::String::New(buffer); |
447 } | 439 } |
OLD | NEW |