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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 | 65 |
66 | 66 |
67 int main(int argc, char* argv[]) { | 67 int main(int argc, char* argv[]) { |
68 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 68 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
69 run_shell = (argc == 1); | 69 run_shell = (argc == 1); |
70 int result; | 70 int result; |
71 { | 71 { |
72 v8::HandleScope handle_scope; | 72 v8::HandleScope handle_scope; |
73 v8::Persistent<v8::Context> context = CreateShellContext(); | 73 v8::Persistent<v8::Context> context = CreateShellContext(); |
74 if (context.IsEmpty()) { | 74 if (context.IsEmpty()) { |
75 printf("Error creating context\n"); | 75 fprintf(stderr, "Error creating context\n"); |
76 return 1; | 76 return 1; |
77 } | 77 } |
78 context->Enter(); | 78 context->Enter(); |
79 result = RunMain(argc, argv); | 79 result = RunMain(argc, argv); |
80 if (run_shell) RunShell(context); | 80 if (run_shell) RunShell(context); |
81 context->Exit(); | 81 context->Exit(); |
82 context.Dispose(); | 82 context.Dispose(); |
83 } | 83 } |
84 v8::V8::Dispose(); | 84 v8::V8::Dispose(); |
85 return result; | 85 return result; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 int RunMain(int argc, char* argv[]) { | 219 int RunMain(int argc, char* argv[]) { |
220 for (int i = 1; i < argc; i++) { | 220 for (int i = 1; i < argc; i++) { |
221 const char* str = argv[i]; | 221 const char* str = argv[i]; |
222 if (strcmp(str, "--shell") == 0) { | 222 if (strcmp(str, "--shell") == 0) { |
223 run_shell = true; | 223 run_shell = true; |
224 } else if (strcmp(str, "-f") == 0) { | 224 } else if (strcmp(str, "-f") == 0) { |
225 // Ignore any -f flags for compatibility with the other stand- | 225 // Ignore any -f flags for compatibility with the other stand- |
226 // alone JavaScript engines. | 226 // alone JavaScript engines. |
227 continue; | 227 continue; |
228 } else if (strncmp(str, "--", 2) == 0) { | 228 } else if (strncmp(str, "--", 2) == 0) { |
229 printf("Warning: unknown flag %s.\nTry --help for options\n", str); | 229 fprintf(stderr, |
230 "Warning: unknown flag %s.\nTry --help for options\n", str); | |
230 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { | 231 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { |
231 // Execute argument given to -e option directly. | 232 // Execute argument given to -e option directly. |
232 v8::Handle<v8::String> file_name = v8::String::New("unnamed"); | 233 v8::Handle<v8::String> file_name = v8::String::New("unnamed"); |
233 v8::Handle<v8::String> source = v8::String::New(argv[++i]); | 234 v8::Handle<v8::String> source = v8::String::New(argv[++i]); |
234 if (!ExecuteString(source, file_name, false, true)) return 1; | 235 if (!ExecuteString(source, file_name, false, true)) return 1; |
235 } else { | 236 } else { |
236 // Use all other arguments as names of files to load and run. | 237 // Use all other arguments as names of files to load and run. |
237 v8::Handle<v8::String> file_name = v8::String::New(str); | 238 v8::Handle<v8::String> file_name = v8::String::New(str); |
238 v8::Handle<v8::String> source = ReadFile(str); | 239 v8::Handle<v8::String> source = ReadFile(str); |
239 if (source.IsEmpty()) { | 240 if (source.IsEmpty()) { |
240 printf("Error reading '%s'\n", str); | 241 fprintf(stderr, "Error reading '%s'\n", str); |
241 continue; | 242 continue; |
242 } | 243 } |
243 if (!ExecuteString(source, file_name, false, true)) return 1; | 244 if (!ExecuteString(source, file_name, false, true)) return 1; |
244 } | 245 } |
245 } | 246 } |
246 return 0; | 247 return 0; |
247 } | 248 } |
248 | 249 |
249 | 250 |
250 // The read-eval-execute loop of the shell. | 251 // The read-eval-execute loop of the shell. |
251 void RunShell(v8::Handle<v8::Context> context) { | 252 void RunShell(v8::Handle<v8::Context> context) { |
252 printf("V8 version %s [sample shell]\n", v8::V8::GetVersion()); | 253 fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion()); |
Yang
2012/11/13 08:48:17
This is the prompt of the interactive shell and mu
Patrick Dubroy
2012/11/13 09:56:28
I thought I mentioned this yesterday, but that is
| |
253 static const int kBufferSize = 256; | 254 static const int kBufferSize = 256; |
254 // Enter the execution environment before evaluating any code. | 255 // Enter the execution environment before evaluating any code. |
255 v8::Context::Scope context_scope(context); | 256 v8::Context::Scope context_scope(context); |
256 v8::Local<v8::String> name(v8::String::New("(shell)")); | 257 v8::Local<v8::String> name(v8::String::New("(shell)")); |
257 while (true) { | 258 while (true) { |
258 char buffer[kBufferSize]; | 259 char buffer[kBufferSize]; |
259 printf("> "); | 260 fprintf(stderr, "> "); |
Yang
2012/11/13 08:48:17
Ditto.
| |
260 char* str = fgets(buffer, kBufferSize, stdin); | 261 char* str = fgets(buffer, kBufferSize, stdin); |
261 if (str == NULL) break; | 262 if (str == NULL) break; |
262 v8::HandleScope handle_scope; | 263 v8::HandleScope handle_scope; |
263 ExecuteString(v8::String::New(str), name, true, true); | 264 ExecuteString(v8::String::New(str), name, true, true); |
264 } | 265 } |
265 printf("\n"); | 266 fprintf(stderr, "\n"); |
Yang
2012/11/13 08:48:17
Ditto.
| |
266 } | 267 } |
267 | 268 |
268 | 269 |
269 // Executes a string within the current v8 context. | 270 // Executes a string within the current v8 context. |
270 bool ExecuteString(v8::Handle<v8::String> source, | 271 bool ExecuteString(v8::Handle<v8::String> source, |
271 v8::Handle<v8::Value> name, | 272 v8::Handle<v8::Value> name, |
272 bool print_result, | 273 bool print_result, |
273 bool report_exceptions) { | 274 bool report_exceptions) { |
274 v8::HandleScope handle_scope; | 275 v8::HandleScope handle_scope; |
275 v8::TryCatch try_catch; | 276 v8::TryCatch try_catch; |
(...skipping 27 matching lines...) Expand all Loading... | |
303 | 304 |
304 | 305 |
305 void ReportException(v8::TryCatch* try_catch) { | 306 void ReportException(v8::TryCatch* try_catch) { |
306 v8::HandleScope handle_scope; | 307 v8::HandleScope handle_scope; |
307 v8::String::Utf8Value exception(try_catch->Exception()); | 308 v8::String::Utf8Value exception(try_catch->Exception()); |
308 const char* exception_string = ToCString(exception); | 309 const char* exception_string = ToCString(exception); |
309 v8::Handle<v8::Message> message = try_catch->Message(); | 310 v8::Handle<v8::Message> message = try_catch->Message(); |
310 if (message.IsEmpty()) { | 311 if (message.IsEmpty()) { |
311 // V8 didn't provide any extra information about this error; just | 312 // V8 didn't provide any extra information about this error; just |
312 // print the exception. | 313 // print the exception. |
313 printf("%s\n", exception_string); | 314 fprintf(stderr, "%s\n", exception_string); |
Yang
2012/11/13 08:48:17
This reports uncaught exceptions caused in javascr
Patrick Dubroy
2012/11/13 09:56:28
I disagree. An uncaught exception needs to be logg
| |
314 } else { | 315 } else { |
315 // Print (filename):(line number): (message). | 316 // Print (filename):(line number): (message). |
316 v8::String::Utf8Value filename(message->GetScriptResourceName()); | 317 v8::String::Utf8Value filename(message->GetScriptResourceName()); |
317 const char* filename_string = ToCString(filename); | 318 const char* filename_string = ToCString(filename); |
318 int linenum = message->GetLineNumber(); | 319 int linenum = message->GetLineNumber(); |
319 printf("%s:%i: %s\n", filename_string, linenum, exception_string); | 320 fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string); |
320 // Print line of source code. | 321 // Print line of source code. |
321 v8::String::Utf8Value sourceline(message->GetSourceLine()); | 322 v8::String::Utf8Value sourceline(message->GetSourceLine()); |
322 const char* sourceline_string = ToCString(sourceline); | 323 const char* sourceline_string = ToCString(sourceline); |
323 printf("%s\n", sourceline_string); | 324 fprintf(stderr, "%s\n", sourceline_string); |
324 // Print wavy underline (GetUnderline is deprecated). | 325 // Print wavy underline (GetUnderline is deprecated). |
325 int start = message->GetStartColumn(); | 326 int start = message->GetStartColumn(); |
326 for (int i = 0; i < start; i++) { | 327 for (int i = 0; i < start; i++) { |
327 printf(" "); | 328 fprintf(stderr, " "); |
328 } | 329 } |
329 int end = message->GetEndColumn(); | 330 int end = message->GetEndColumn(); |
330 for (int i = start; i < end; i++) { | 331 for (int i = start; i < end; i++) { |
331 printf("^"); | 332 fprintf(stderr, "^"); |
332 } | 333 } |
333 printf("\n"); | 334 fprintf(stderr, "\n"); |
334 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 335 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
335 if (stack_trace.length() > 0) { | 336 if (stack_trace.length() > 0) { |
336 const char* stack_trace_string = ToCString(stack_trace); | 337 const char* stack_trace_string = ToCString(stack_trace); |
337 printf("%s\n", stack_trace_string); | 338 fprintf(stderr, "%s\n", stack_trace_string); |
338 } | 339 } |
339 } | 340 } |
340 } | 341 } |
OLD | NEW |