| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 } | 239 } |
| 240 if (!ExecuteString(source, file_name, false, true)) return 1; | 240 if (!ExecuteString(source, file_name, false, true)) return 1; |
| 241 } | 241 } |
| 242 } | 242 } |
| 243 return 0; | 243 return 0; |
| 244 } | 244 } |
| 245 | 245 |
| 246 | 246 |
| 247 // The read-eval-execute loop of the shell. | 247 // The read-eval-execute loop of the shell. |
| 248 void RunShell(v8::Handle<v8::Context> context) { | 248 void RunShell(v8::Handle<v8::Context> context) { |
| 249 // Enter the execution environment before evaluating any code. |
| 250 v8::HandleScope handle_scope; |
| 251 v8::Context::Scope context_scope(context); |
| 252 v8::Local<v8::String> name(v8::String::New("(shell)")); |
| 253 |
| 249 printf("V8 version %s [sample shell]\n", v8::V8::GetVersion()); | 254 printf("V8 version %s [sample shell]\n", v8::V8::GetVersion()); |
| 250 static const int kBufferSize = 256; | 255 static const int kBufferSize = 256; |
| 251 // Enter the execution environment before evaluating any code. | |
| 252 v8::Context::Scope context_scope(context); | |
| 253 v8::Local<v8::String> name(v8::String::New("(shell)")); | |
| 254 while (true) { | 256 while (true) { |
| 255 char buffer[kBufferSize]; | 257 char buffer[kBufferSize]; |
| 256 printf("> "); | 258 printf("> "); |
| 257 char* str = fgets(buffer, kBufferSize, stdin); | 259 char* str = fgets(buffer, kBufferSize, stdin); |
| 258 if (str == NULL) break; | 260 if (str == NULL) break; |
| 259 v8::HandleScope handle_scope; | |
| 260 ExecuteString(v8::String::New(str), name, true, true); | 261 ExecuteString(v8::String::New(str), name, true, true); |
| 261 } | 262 } |
| 262 printf("\n"); | 263 printf("\n"); |
| 263 } | 264 } |
| 264 | 265 |
| 265 | 266 |
| 266 // Executes a string within the current v8 context. | 267 // Executes a string within the current v8 context. |
| 267 bool ExecuteString(v8::Handle<v8::String> source, | 268 bool ExecuteString(v8::Handle<v8::String> source, |
| 268 v8::Handle<v8::Value> name, | 269 v8::Handle<v8::Value> name, |
| 269 bool print_result, | 270 bool print_result, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 printf("^"); | 329 printf("^"); |
| 329 } | 330 } |
| 330 printf("\n"); | 331 printf("\n"); |
| 331 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 332 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
| 332 if (stack_trace.length() > 0) { | 333 if (stack_trace.length() > 0) { |
| 333 const char* stack_trace_string = ToCString(stack_trace); | 334 const char* stack_trace_string = ToCString(stack_trace); |
| 334 printf("%s\n", stack_trace_string); | 335 printf("%s\n", stack_trace_string); |
| 335 } | 336 } |
| 336 } | 337 } |
| 337 } | 338 } |
| OLD | NEW |