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 27 matching lines...) Expand all Loading... |
38 /** | 38 /** |
39 * This sample program shows how to implement a simple javascript shell | 39 * This sample program shows how to implement a simple javascript shell |
40 * based on V8. This includes initializing V8 with command line options, | 40 * based on V8. This includes initializing V8 with command line options, |
41 * creating global functions, compiling and executing strings. | 41 * creating global functions, compiling and executing strings. |
42 * | 42 * |
43 * For a more sophisticated shell, consider using the debug shell D8. | 43 * For a more sophisticated shell, consider using the debug shell D8. |
44 */ | 44 */ |
45 | 45 |
46 | 46 |
47 v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate); | 47 v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate); |
48 void RunShell(v8::Local<v8::Context> context); | 48 void RunShell(v8::Local<v8::Context> context, v8::Platform* platform); |
49 int RunMain(v8::Isolate* isolate, int argc, char* argv[]); | 49 int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc, |
| 50 char* argv[]); |
50 bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source, | 51 bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source, |
51 v8::Local<v8::Value> name, bool print_result, | 52 v8::Local<v8::Value> name, bool print_result, |
52 bool report_exceptions); | 53 bool report_exceptions); |
53 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); | 54 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); |
54 void Read(const v8::FunctionCallbackInfo<v8::Value>& args); | 55 void Read(const v8::FunctionCallbackInfo<v8::Value>& args); |
55 void Load(const v8::FunctionCallbackInfo<v8::Value>& args); | 56 void Load(const v8::FunctionCallbackInfo<v8::Value>& args); |
56 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args); | 57 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args); |
57 void Version(const v8::FunctionCallbackInfo<v8::Value>& args); | 58 void Version(const v8::FunctionCallbackInfo<v8::Value>& args); |
58 v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name); | 59 v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name); |
59 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); | 60 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); |
(...skipping 27 matching lines...) Expand all Loading... |
87 int result; | 88 int result; |
88 { | 89 { |
89 v8::Isolate::Scope isolate_scope(isolate); | 90 v8::Isolate::Scope isolate_scope(isolate); |
90 v8::HandleScope handle_scope(isolate); | 91 v8::HandleScope handle_scope(isolate); |
91 v8::Local<v8::Context> context = CreateShellContext(isolate); | 92 v8::Local<v8::Context> context = CreateShellContext(isolate); |
92 if (context.IsEmpty()) { | 93 if (context.IsEmpty()) { |
93 fprintf(stderr, "Error creating context\n"); | 94 fprintf(stderr, "Error creating context\n"); |
94 return 1; | 95 return 1; |
95 } | 96 } |
96 v8::Context::Scope context_scope(context); | 97 v8::Context::Scope context_scope(context); |
97 result = RunMain(isolate, argc, argv); | 98 result = RunMain(isolate, platform, argc, argv); |
98 if (run_shell) RunShell(context); | 99 if (run_shell) RunShell(context, platform); |
99 } | 100 } |
100 isolate->Dispose(); | 101 isolate->Dispose(); |
101 v8::V8::Dispose(); | 102 v8::V8::Dispose(); |
102 v8::V8::ShutdownPlatform(); | 103 v8::V8::ShutdownPlatform(); |
103 delete platform; | 104 delete platform; |
104 return result; | 105 return result; |
105 } | 106 } |
106 | 107 |
107 | 108 |
108 // Extracts a C string from a V8 Utf8Value. | 109 // Extracts a C string from a V8 Utf8Value. |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 } | 263 } |
263 fclose(file); | 264 fclose(file); |
264 v8::MaybeLocal<v8::String> result = v8::String::NewFromUtf8( | 265 v8::MaybeLocal<v8::String> result = v8::String::NewFromUtf8( |
265 isolate, chars, v8::NewStringType::kNormal, static_cast<int>(size)); | 266 isolate, chars, v8::NewStringType::kNormal, static_cast<int>(size)); |
266 delete[] chars; | 267 delete[] chars; |
267 return result; | 268 return result; |
268 } | 269 } |
269 | 270 |
270 | 271 |
271 // Process remaining command line arguments and execute files | 272 // Process remaining command line arguments and execute files |
272 int RunMain(v8::Isolate* isolate, int argc, char* argv[]) { | 273 int RunMain(v8::Isolate* isolate, v8::Platform* platform, int argc, |
| 274 char* argv[]) { |
273 for (int i = 1; i < argc; i++) { | 275 for (int i = 1; i < argc; i++) { |
274 const char* str = argv[i]; | 276 const char* str = argv[i]; |
275 if (strcmp(str, "--shell") == 0) { | 277 if (strcmp(str, "--shell") == 0) { |
276 run_shell = true; | 278 run_shell = true; |
277 } else if (strcmp(str, "-f") == 0) { | 279 } else if (strcmp(str, "-f") == 0) { |
278 // Ignore any -f flags for compatibility with the other stand- | 280 // Ignore any -f flags for compatibility with the other stand- |
279 // alone JavaScript engines. | 281 // alone JavaScript engines. |
280 continue; | 282 continue; |
281 } else if (strncmp(str, "--", 2) == 0) { | 283 } else if (strncmp(str, "--", 2) == 0) { |
282 fprintf(stderr, | 284 fprintf(stderr, |
283 "Warning: unknown flag %s.\nTry --help for options\n", str); | 285 "Warning: unknown flag %s.\nTry --help for options\n", str); |
284 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { | 286 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { |
285 // Execute argument given to -e option directly. | 287 // Execute argument given to -e option directly. |
286 v8::Local<v8::String> file_name = | 288 v8::Local<v8::String> file_name = |
287 v8::String::NewFromUtf8(isolate, "unnamed", | 289 v8::String::NewFromUtf8(isolate, "unnamed", |
288 v8::NewStringType::kNormal).ToLocalChecked(); | 290 v8::NewStringType::kNormal).ToLocalChecked(); |
289 v8::Local<v8::String> source; | 291 v8::Local<v8::String> source; |
290 if (!v8::String::NewFromUtf8(isolate, argv[++i], | 292 if (!v8::String::NewFromUtf8(isolate, argv[++i], |
291 v8::NewStringType::kNormal) | 293 v8::NewStringType::kNormal) |
292 .ToLocal(&source)) { | 294 .ToLocal(&source)) { |
293 return 1; | 295 return 1; |
294 } | 296 } |
295 if (!ExecuteString(isolate, source, file_name, false, true)) return 1; | 297 bool success = ExecuteString(isolate, source, file_name, false, true); |
| 298 while (v8::platform::PumpMessageLoop(platform, isolate)) continue; |
| 299 if (!success) return 1; |
296 } else { | 300 } else { |
297 // Use all other arguments as names of files to load and run. | 301 // Use all other arguments as names of files to load and run. |
298 v8::Local<v8::String> file_name = | 302 v8::Local<v8::String> file_name = |
299 v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal) | 303 v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal) |
300 .ToLocalChecked(); | 304 .ToLocalChecked(); |
301 v8::Local<v8::String> source; | 305 v8::Local<v8::String> source; |
302 if (!ReadFile(isolate, str).ToLocal(&source)) { | 306 if (!ReadFile(isolate, str).ToLocal(&source)) { |
303 fprintf(stderr, "Error reading '%s'\n", str); | 307 fprintf(stderr, "Error reading '%s'\n", str); |
304 continue; | 308 continue; |
305 } | 309 } |
306 if (!ExecuteString(isolate, source, file_name, false, true)) return 1; | 310 bool success = ExecuteString(isolate, source, file_name, false, true); |
| 311 while (v8::platform::PumpMessageLoop(platform, isolate)) continue; |
| 312 if (!success) return 1; |
307 } | 313 } |
308 } | 314 } |
309 return 0; | 315 return 0; |
310 } | 316 } |
311 | 317 |
312 | 318 |
313 // The read-eval-execute loop of the shell. | 319 // The read-eval-execute loop of the shell. |
314 void RunShell(v8::Local<v8::Context> context) { | 320 void RunShell(v8::Local<v8::Context> context, v8::Platform* platform) { |
315 fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion()); | 321 fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion()); |
316 static const int kBufferSize = 256; | 322 static const int kBufferSize = 256; |
317 // Enter the execution environment before evaluating any code. | 323 // Enter the execution environment before evaluating any code. |
318 v8::Context::Scope context_scope(context); | 324 v8::Context::Scope context_scope(context); |
319 v8::Local<v8::String> name( | 325 v8::Local<v8::String> name( |
320 v8::String::NewFromUtf8(context->GetIsolate(), "(shell)", | 326 v8::String::NewFromUtf8(context->GetIsolate(), "(shell)", |
321 v8::NewStringType::kNormal).ToLocalChecked()); | 327 v8::NewStringType::kNormal).ToLocalChecked()); |
322 while (true) { | 328 while (true) { |
323 char buffer[kBufferSize]; | 329 char buffer[kBufferSize]; |
324 fprintf(stderr, "> "); | 330 fprintf(stderr, "> "); |
325 char* str = fgets(buffer, kBufferSize, stdin); | 331 char* str = fgets(buffer, kBufferSize, stdin); |
326 if (str == NULL) break; | 332 if (str == NULL) break; |
327 v8::HandleScope handle_scope(context->GetIsolate()); | 333 v8::HandleScope handle_scope(context->GetIsolate()); |
328 ExecuteString( | 334 ExecuteString( |
329 context->GetIsolate(), | 335 context->GetIsolate(), |
330 v8::String::NewFromUtf8(context->GetIsolate(), str, | 336 v8::String::NewFromUtf8(context->GetIsolate(), str, |
331 v8::NewStringType::kNormal).ToLocalChecked(), | 337 v8::NewStringType::kNormal).ToLocalChecked(), |
332 name, true, true); | 338 name, true, true); |
| 339 while (v8::platform::PumpMessageLoop(platform, context->GetIsolate())) |
| 340 continue; |
333 } | 341 } |
334 fprintf(stderr, "\n"); | 342 fprintf(stderr, "\n"); |
335 } | 343 } |
336 | 344 |
337 | 345 |
338 // Executes a string within the current v8 context. | 346 // Executes a string within the current v8 context. |
339 bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source, | 347 bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source, |
340 v8::Local<v8::Value> name, bool print_result, | 348 v8::Local<v8::Value> name, bool print_result, |
341 bool report_exceptions) { | 349 bool report_exceptions) { |
342 v8::HandleScope handle_scope(isolate); | 350 v8::HandleScope handle_scope(isolate); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 } | 412 } |
405 fprintf(stderr, "\n"); | 413 fprintf(stderr, "\n"); |
406 v8::String::Utf8Value stack_trace( | 414 v8::String::Utf8Value stack_trace( |
407 try_catch->StackTrace(context).ToLocalChecked()); | 415 try_catch->StackTrace(context).ToLocalChecked()); |
408 if (stack_trace.length() > 0) { | 416 if (stack_trace.length() > 0) { |
409 const char* stack_trace_string = ToCString(stack_trace); | 417 const char* stack_trace_string = ToCString(stack_trace); |
410 fprintf(stderr, "%s\n", stack_trace_string); | 418 fprintf(stderr, "%s\n", stack_trace_string); |
411 } | 419 } |
412 } | 420 } |
413 } | 421 } |
OLD | NEW |