| OLD | NEW |
| 1 // Copyright 2009 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 |
| 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 | 28 |
| 29 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 30 #include <bzlib.h> |
| 31 #endif |
| 32 #include <errno.h> |
| 29 #include <stdlib.h> | 33 #include <stdlib.h> |
| 30 #include <errno.h> | |
| 31 | 34 |
| 32 #include "v8.h" | 35 #include "v8.h" |
| 33 | 36 |
| 34 #include "d8.h" | 37 #include "d8.h" |
| 35 #include "d8-debug.h" | 38 #include "d8-debug.h" |
| 36 #include "debug.h" | 39 #include "debug.h" |
| 37 #include "api.h" | 40 #include "api.h" |
| 38 #include "natives.h" | 41 #include "natives.h" |
| 39 #include "platform.h" | 42 #include "platform.h" |
| 40 | 43 |
| 44 #if !defined(_WIN32) && !defined(_WIN64) |
| 45 #include <unistd.h> // NOLINT |
| 46 #endif |
| 41 | 47 |
| 42 namespace v8 { | 48 namespace v8 { |
| 43 | 49 |
| 44 | 50 |
| 45 const char* Shell::kHistoryFileName = ".d8_history"; | 51 const char* Shell::kHistoryFileName = ".d8_history"; |
| 46 const char* Shell::kPrompt = "d8> "; | 52 const char* Shell::kPrompt = "d8> "; |
| 47 | 53 |
| 48 | 54 |
| 49 LineEditor *LineEditor::first_ = NULL; | 55 LineEditor *LineEditor::first_ = NULL; |
| 50 | 56 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 return i::SmartPointer<char>(str ? i::StrDup(str) : str); | 93 return i::SmartPointer<char>(str ? i::StrDup(str) : str); |
| 88 } | 94 } |
| 89 | 95 |
| 90 | 96 |
| 91 CounterMap* Shell::counter_map_; | 97 CounterMap* Shell::counter_map_; |
| 92 i::OS::MemoryMappedFile* Shell::counters_file_ = NULL; | 98 i::OS::MemoryMappedFile* Shell::counters_file_ = NULL; |
| 93 CounterCollection Shell::local_counters_; | 99 CounterCollection Shell::local_counters_; |
| 94 CounterCollection* Shell::counters_ = &local_counters_; | 100 CounterCollection* Shell::counters_ = &local_counters_; |
| 95 Persistent<Context> Shell::utility_context_; | 101 Persistent<Context> Shell::utility_context_; |
| 96 Persistent<Context> Shell::evaluation_context_; | 102 Persistent<Context> Shell::evaluation_context_; |
| 103 i::Mutex* Shell::context_mutex_(i::OS::CreateMutex()); |
| 104 ShellOptions Shell::options; |
| 97 | 105 |
| 98 | 106 |
| 99 bool CounterMap::Match(void* key1, void* key2) { | 107 bool CounterMap::Match(void* key1, void* key2) { |
| 100 const char* name1 = reinterpret_cast<const char*>(key1); | 108 const char* name1 = reinterpret_cast<const char*>(key1); |
| 101 const char* name2 = reinterpret_cast<const char*>(key2); | 109 const char* name2 = reinterpret_cast<const char*>(key2); |
| 102 return strcmp(name1, name2) == 0; | 110 return strcmp(name1, name2) == 0; |
| 103 } | 111 } |
| 104 | 112 |
| 105 | 113 |
| 106 // Converts a V8 value to a C string. | 114 // Converts a V8 value to a C string. |
| 107 const char* Shell::ToCString(const v8::String::Utf8Value& value) { | 115 const char* Shell::ToCString(const v8::String::Utf8Value& value) { |
| 108 return *value ? *value : "<string conversion failed>"; | 116 return *value ? *value : "<string conversion failed>"; |
| 109 } | 117 } |
| 110 | 118 |
| 111 | 119 |
| 112 // Executes a string within the current v8 context. | 120 // Executes a string within the current v8 context. |
| 113 bool Shell::ExecuteString(Handle<String> source, | 121 bool Shell::ExecuteString(Handle<String> source, |
| 114 Handle<Value> name, | 122 Handle<Value> name, |
| 115 bool print_result, | 123 bool print_result, |
| 116 bool report_exceptions) { | 124 bool report_exceptions) { |
| 117 HandleScope handle_scope; | 125 HandleScope handle_scope; |
| 118 TryCatch try_catch; | 126 TryCatch try_catch; |
| 127 options.script_executed = true; |
| 119 if (i::FLAG_debugger) { | 128 if (i::FLAG_debugger) { |
| 120 // When debugging make exceptions appear to be uncaught. | 129 // When debugging make exceptions appear to be uncaught. |
| 121 try_catch.SetVerbose(true); | 130 try_catch.SetVerbose(true); |
| 122 } | 131 } |
| 123 Handle<Script> script = Script::Compile(source, name); | 132 Handle<Script> script = Script::Compile(source, name); |
| 124 if (script.IsEmpty()) { | 133 if (script.IsEmpty()) { |
| 125 // Print errors that happened during compilation. | 134 // Print errors that happened during compilation. |
| 126 if (report_exceptions && !i::FLAG_debugger) | 135 if (report_exceptions && !i::FLAG_debugger) |
| 127 ReportException(&try_catch); | 136 ReportException(&try_catch); |
| 128 return false; | 137 return false; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 return ThrowException(String::New("Error loading file")); | 220 return ThrowException(String::New("Error loading file")); |
| 212 } | 221 } |
| 213 if (!ExecuteString(source, String::New(*file), false, false)) { | 222 if (!ExecuteString(source, String::New(*file), false, false)) { |
| 214 return ThrowException(String::New("Error executing file")); | 223 return ThrowException(String::New("Error executing file")); |
| 215 } | 224 } |
| 216 } | 225 } |
| 217 return Undefined(); | 226 return Undefined(); |
| 218 } | 227 } |
| 219 | 228 |
| 220 | 229 |
| 230 Handle<Value> Shell::CreateExternalArray(const Arguments& args, |
| 231 ExternalArrayType type, |
| 232 size_t element_size) { |
| 233 ASSERT(element_size == 1 || element_size == 2 || element_size == 4 || |
| 234 element_size == 8); |
| 235 if (args.Length() != 1) { |
| 236 return ThrowException( |
| 237 String::New("Array constructor needs one parameter.")); |
| 238 } |
| 239 size_t length = 0; |
| 240 if (args[0]->IsUint32()) { |
| 241 length = args[0]->Uint32Value(); |
| 242 } else if (args[0]->IsNumber()) { |
| 243 double raw_length = args[0]->NumberValue(); |
| 244 if (raw_length < 0) { |
| 245 return ThrowException(String::New("Array length must not be negative.")); |
| 246 } |
| 247 if (raw_length > i::ExternalArray::kMaxLength) { |
| 248 return ThrowException( |
| 249 String::New("Array length exceeds maximum length.")); |
| 250 } |
| 251 length = static_cast<size_t>(raw_length); |
| 252 } else { |
| 253 return ThrowException(String::New("Array length must be a number.")); |
| 254 } |
| 255 if (length > static_cast<size_t>(i::ExternalArray::kMaxLength)) { |
| 256 return ThrowException(String::New("Array length exceeds maximum length.")); |
| 257 } |
| 258 void* data = calloc(length, element_size); |
| 259 if (data == NULL) { |
| 260 return ThrowException(String::New("Memory allocation failed.")); |
| 261 } |
| 262 Handle<Object> array = Object::New(); |
| 263 Persistent<Object> persistent_array = Persistent<Object>::New(array); |
| 264 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); |
| 265 persistent_array.MarkIndependent(); |
| 266 array->SetIndexedPropertiesToExternalArrayData(data, type, length); |
| 267 array->Set(String::New("length"), Int32::New(length), ReadOnly); |
| 268 array->Set(String::New("BYTES_PER_ELEMENT"), Int32::New(element_size)); |
| 269 return array; |
| 270 } |
| 271 |
| 272 |
| 273 void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) { |
| 274 free(data); |
| 275 object.Dispose(); |
| 276 } |
| 277 |
| 278 |
| 279 Handle<Value> Shell::Int8Array(const Arguments& args) { |
| 280 return CreateExternalArray(args, v8::kExternalByteArray, sizeof(int8_t)); |
| 281 } |
| 282 |
| 283 |
| 284 Handle<Value> Shell::Uint8Array(const Arguments& args) { |
| 285 return CreateExternalArray(args, kExternalUnsignedByteArray, sizeof(uint8_t)); |
| 286 } |
| 287 |
| 288 |
| 289 Handle<Value> Shell::Int16Array(const Arguments& args) { |
| 290 return CreateExternalArray(args, kExternalShortArray, sizeof(int16_t)); |
| 291 } |
| 292 |
| 293 |
| 294 Handle<Value> Shell::Uint16Array(const Arguments& args) { |
| 295 return CreateExternalArray(args, kExternalUnsignedShortArray, |
| 296 sizeof(uint16_t)); |
| 297 } |
| 298 |
| 299 |
| 300 Handle<Value> Shell::Int32Array(const Arguments& args) { |
| 301 return CreateExternalArray(args, kExternalIntArray, sizeof(int32_t)); |
| 302 } |
| 303 |
| 304 |
| 305 Handle<Value> Shell::Uint32Array(const Arguments& args) { |
| 306 return CreateExternalArray(args, kExternalUnsignedIntArray, sizeof(uint32_t)); |
| 307 } |
| 308 |
| 309 |
| 310 Handle<Value> Shell::Float32Array(const Arguments& args) { |
| 311 return CreateExternalArray(args, kExternalFloatArray, |
| 312 sizeof(float)); // NOLINT |
| 313 } |
| 314 |
| 315 |
| 316 Handle<Value> Shell::Float64Array(const Arguments& args) { |
| 317 return CreateExternalArray(args, kExternalDoubleArray, |
| 318 sizeof(double)); // NOLINT |
| 319 } |
| 320 |
| 321 |
| 322 Handle<Value> Shell::PixelArray(const Arguments& args) { |
| 323 return CreateExternalArray(args, kExternalPixelArray, sizeof(uint8_t)); |
| 324 } |
| 325 |
| 326 |
| 221 Handle<Value> Shell::Yield(const Arguments& args) { | 327 Handle<Value> Shell::Yield(const Arguments& args) { |
| 222 v8::Unlocker unlocker; | 328 v8::Unlocker unlocker; |
| 223 return Undefined(); | 329 return Undefined(); |
| 224 } | 330 } |
| 225 | 331 |
| 226 | 332 |
| 227 Handle<Value> Shell::Quit(const Arguments& args) { | 333 Handle<Value> Shell::Quit(const Arguments& args) { |
| 228 int exit_code = args[0]->Int32Value(); | 334 int exit_code = args[0]->Int32Value(); |
| 229 OnExit(); | 335 OnExit(); |
| 230 exit(exit_code); | 336 exit(exit_code); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 size_t buckets) { | 507 size_t buckets) { |
| 402 return GetCounter(name, true); | 508 return GetCounter(name, true); |
| 403 } | 509 } |
| 404 | 510 |
| 405 | 511 |
| 406 void Shell::AddHistogramSample(void* histogram, int sample) { | 512 void Shell::AddHistogramSample(void* histogram, int sample) { |
| 407 Counter* counter = reinterpret_cast<Counter*>(histogram); | 513 Counter* counter = reinterpret_cast<Counter*>(histogram); |
| 408 counter->AddSample(sample); | 514 counter->AddSample(sample); |
| 409 } | 515 } |
| 410 | 516 |
| 517 void Shell::InstallUtilityScript() { |
| 518 Locker lock; |
| 519 HandleScope scope; |
| 520 // If we use the utility context, we have to set the security tokens so that |
| 521 // utility, evaluation and debug context can all access each other. |
| 522 utility_context_->SetSecurityToken(Undefined()); |
| 523 evaluation_context_->SetSecurityToken(Undefined()); |
| 524 Context::Scope utility_scope(utility_context_); |
| 411 | 525 |
| 412 void Shell::Initialize() { | 526 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 413 Shell::counter_map_ = new CounterMap(); | 527 // Install the debugger object in the utility scope |
| 414 // Set up counters | 528 i::Debug* debug = i::Isolate::Current()->debug(); |
| 415 if (i::StrLength(i::FLAG_map_counters) != 0) | 529 debug->Load(); |
| 416 MapCounters(i::FLAG_map_counters); | 530 i::Handle<i::JSObject> js_debug |
| 417 if (i::FLAG_dump_counters) { | 531 = i::Handle<i::JSObject>(debug->debug_context()->global()); |
| 418 V8::SetCounterFunction(LookupCounter); | 532 utility_context_->Global()->Set(String::New("$debug"), |
| 419 V8::SetCreateHistogramFunction(CreateHistogram); | 533 Utils::ToLocal(js_debug)); |
| 420 V8::SetAddHistogramSampleFunction(AddHistogramSample); | 534 debug->debug_context()->set_security_token(HEAP->undefined_value()); |
| 535 #endif |
| 536 |
| 537 // Run the d8 shell utility script in the utility context |
| 538 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); |
| 539 i::Vector<const char> shell_source = |
| 540 i::NativesCollection<i::D8>::GetRawScriptSource(source_index); |
| 541 i::Vector<const char> shell_source_name = |
| 542 i::NativesCollection<i::D8>::GetScriptName(source_index); |
| 543 Handle<String> source = String::New(shell_source.start(), |
| 544 shell_source.length()); |
| 545 Handle<String> name = String::New(shell_source_name.start(), |
| 546 shell_source_name.length()); |
| 547 Handle<Script> script = Script::Compile(source, name); |
| 548 script->Run(); |
| 549 // Mark the d8 shell script as native to avoid it showing up as normal source |
| 550 // in the debugger. |
| 551 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); |
| 552 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() |
| 553 ? i::Handle<i::Script>(i::Script::cast( |
| 554 i::JSFunction::cast(*compiled_script)->shared()->script())) |
| 555 : i::Handle<i::Script>(i::Script::cast( |
| 556 i::SharedFunctionInfo::cast(*compiled_script)->script())); |
| 557 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE)); |
| 558 |
| 559 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 560 // Start the in-process debugger if requested. |
| 561 if (i::FLAG_debugger && !i::FLAG_debugger_agent) { |
| 562 v8::Debug::SetDebugEventListener(HandleDebugEvent); |
| 421 } | 563 } |
| 564 #endif |
| 565 } |
| 422 | 566 |
| 423 // Initialize the global objects | 567 |
| 424 HandleScope scope; | 568 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 569 class BZip2Decompressor : public v8::StartupDataDecompressor { |
| 570 public: |
| 571 virtual ~BZip2Decompressor() { } |
| 572 |
| 573 protected: |
| 574 virtual int DecompressData(char* raw_data, |
| 575 int* raw_data_size, |
| 576 const char* compressed_data, |
| 577 int compressed_data_size) { |
| 578 ASSERT_EQ(v8::StartupData::kBZip2, |
| 579 v8::V8::GetCompressedStartupDataAlgorithm()); |
| 580 unsigned int decompressed_size = *raw_data_size; |
| 581 int result = |
| 582 BZ2_bzBuffToBuffDecompress(raw_data, |
| 583 &decompressed_size, |
| 584 const_cast<char*>(compressed_data), |
| 585 compressed_data_size, |
| 586 0, 1); |
| 587 if (result == BZ_OK) { |
| 588 *raw_data_size = decompressed_size; |
| 589 } |
| 590 return result; |
| 591 } |
| 592 }; |
| 593 #endif |
| 594 |
| 595 Handle<ObjectTemplate> Shell::CreateGlobalTemplate() { |
| 425 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); | 596 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); |
| 426 global_template->Set(String::New("print"), FunctionTemplate::New(Print)); | 597 global_template->Set(String::New("print"), FunctionTemplate::New(Print)); |
| 427 global_template->Set(String::New("write"), FunctionTemplate::New(Write)); | 598 global_template->Set(String::New("write"), FunctionTemplate::New(Write)); |
| 428 global_template->Set(String::New("read"), FunctionTemplate::New(Read)); | 599 global_template->Set(String::New("read"), FunctionTemplate::New(Read)); |
| 429 global_template->Set(String::New("readline"), | 600 global_template->Set(String::New("readline"), |
| 430 FunctionTemplate::New(ReadLine)); | 601 FunctionTemplate::New(ReadLine)); |
| 431 global_template->Set(String::New("load"), FunctionTemplate::New(Load)); | 602 global_template->Set(String::New("load"), FunctionTemplate::New(Load)); |
| 432 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); | 603 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); |
| 433 global_template->Set(String::New("version"), FunctionTemplate::New(Version)); | 604 global_template->Set(String::New("version"), FunctionTemplate::New(Version)); |
| 434 | 605 |
| 606 // Bind the handlers for external arrays. |
| 607 global_template->Set(String::New("Int8Array"), |
| 608 FunctionTemplate::New(Int8Array)); |
| 609 global_template->Set(String::New("Uint8Array"), |
| 610 FunctionTemplate::New(Uint8Array)); |
| 611 global_template->Set(String::New("Int16Array"), |
| 612 FunctionTemplate::New(Int16Array)); |
| 613 global_template->Set(String::New("Uint16Array"), |
| 614 FunctionTemplate::New(Uint16Array)); |
| 615 global_template->Set(String::New("Int32Array"), |
| 616 FunctionTemplate::New(Int32Array)); |
| 617 global_template->Set(String::New("Uint32Array"), |
| 618 FunctionTemplate::New(Uint32Array)); |
| 619 global_template->Set(String::New("Float32Array"), |
| 620 FunctionTemplate::New(Float32Array)); |
| 621 global_template->Set(String::New("Float64Array"), |
| 622 FunctionTemplate::New(Float64Array)); |
| 623 global_template->Set(String::New("PixelArray"), |
| 624 FunctionTemplate::New(PixelArray)); |
| 625 |
| 435 #ifdef LIVE_OBJECT_LIST | 626 #ifdef LIVE_OBJECT_LIST |
| 436 global_template->Set(String::New("lol_is_enabled"), Boolean::New(true)); | 627 global_template->Set(String::New("lol_is_enabled"), Boolean::New(true)); |
| 437 #else | 628 #else |
| 438 global_template->Set(String::New("lol_is_enabled"), Boolean::New(false)); | 629 global_template->Set(String::New("lol_is_enabled"), Boolean::New(false)); |
| 439 #endif | 630 #endif |
| 440 | 631 |
| 441 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); | 632 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); |
| 442 AddOSMethods(os_templ); | 633 AddOSMethods(os_templ); |
| 443 global_template->Set(String::New("os"), os_templ); | 634 global_template->Set(String::New("os"), os_templ); |
| 444 | 635 |
| 636 return global_template; |
| 637 } |
| 638 |
| 639 |
| 640 void Shell::Initialize() { |
| 641 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 642 BZip2Decompressor startup_data_decompressor; |
| 643 int bz2_result = startup_data_decompressor.Decompress(); |
| 644 if (bz2_result != BZ_OK) { |
| 645 fprintf(stderr, "bzip error code: %d\n", bz2_result); |
| 646 exit(1); |
| 647 } |
| 648 #endif |
| 649 |
| 650 Shell::counter_map_ = new CounterMap(); |
| 651 // Set up counters |
| 652 if (i::StrLength(i::FLAG_map_counters) != 0) |
| 653 MapCounters(i::FLAG_map_counters); |
| 654 if (i::FLAG_dump_counters) { |
| 655 V8::SetCounterFunction(LookupCounter); |
| 656 V8::SetCreateHistogramFunction(CreateHistogram); |
| 657 V8::SetAddHistogramSampleFunction(AddHistogramSample); |
| 658 } |
| 659 |
| 660 if (options.test_shell) return; |
| 661 |
| 662 Locker lock; |
| 663 HandleScope scope; |
| 664 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(); |
| 445 utility_context_ = Context::New(NULL, global_template); | 665 utility_context_ = Context::New(NULL, global_template); |
| 446 utility_context_->SetSecurityToken(Undefined()); | 666 |
| 447 Context::Scope utility_scope(utility_context_); | 667 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 668 // Start the debugger agent if requested. |
| 669 if (i::FLAG_debugger_agent) { |
| 670 v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port, true); |
| 671 } |
| 672 #endif |
| 673 } |
| 674 |
| 675 |
| 676 Persistent<Context> Shell::CreateEvaluationContext() { |
| 677 // This needs to be a critical section since this is not thread-safe |
| 678 i::ScopedLock lock(context_mutex_); |
| 679 // Initialize the global objects |
| 680 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(); |
| 681 Persistent<Context> context = Context::New(NULL, global_template); |
| 682 Context::Scope scope(context); |
| 448 | 683 |
| 449 i::JSArguments js_args = i::FLAG_js_arguments; | 684 i::JSArguments js_args = i::FLAG_js_arguments; |
| 450 i::Handle<i::FixedArray> arguments_array = | 685 i::Handle<i::FixedArray> arguments_array = |
| 451 FACTORY->NewFixedArray(js_args.argc()); | 686 FACTORY->NewFixedArray(js_args.argc()); |
| 452 for (int j = 0; j < js_args.argc(); j++) { | 687 for (int j = 0; j < js_args.argc(); j++) { |
| 453 i::Handle<i::String> arg = | 688 i::Handle<i::String> arg = |
| 454 FACTORY->NewStringFromUtf8(i::CStrVector(js_args[j])); | 689 FACTORY->NewStringFromUtf8(i::CStrVector(js_args[j])); |
| 455 arguments_array->set(j, *arg); | 690 arguments_array->set(j, *arg); |
| 456 } | 691 } |
| 457 i::Handle<i::JSArray> arguments_jsarray = | 692 i::Handle<i::JSArray> arguments_jsarray = |
| 458 FACTORY->NewJSArrayWithElements(arguments_array); | 693 FACTORY->NewJSArrayWithElements(arguments_array); |
| 459 global_template->Set(String::New("arguments"), | 694 context->Global()->Set(String::New("arguments"), |
| 460 Utils::ToLocal(arguments_jsarray)); | 695 Utils::ToLocal(arguments_jsarray)); |
| 461 | 696 return context; |
| 462 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 463 // Install the debugger object in the utility scope | |
| 464 i::Debug* debug = i::Isolate::Current()->debug(); | |
| 465 debug->Load(); | |
| 466 i::Handle<i::JSObject> js_debug | |
| 467 = i::Handle<i::JSObject>(debug->debug_context()->global()); | |
| 468 utility_context_->Global()->Set(String::New("$debug"), | |
| 469 Utils::ToLocal(js_debug)); | |
| 470 #endif | |
| 471 | |
| 472 // Run the d8 shell utility script in the utility context | |
| 473 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); | |
| 474 i::Vector<const char> shell_source | |
| 475 = i::NativesCollection<i::D8>::GetScriptSource(source_index); | |
| 476 i::Vector<const char> shell_source_name | |
| 477 = i::NativesCollection<i::D8>::GetScriptName(source_index); | |
| 478 Handle<String> source = String::New(shell_source.start(), | |
| 479 shell_source.length()); | |
| 480 Handle<String> name = String::New(shell_source_name.start(), | |
| 481 shell_source_name.length()); | |
| 482 Handle<Script> script = Script::Compile(source, name); | |
| 483 script->Run(); | |
| 484 | |
| 485 // Mark the d8 shell script as native to avoid it showing up as normal source | |
| 486 // in the debugger. | |
| 487 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); | |
| 488 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() | |
| 489 ? i::Handle<i::Script>(i::Script::cast( | |
| 490 i::JSFunction::cast(*compiled_script)->shared()->script())) | |
| 491 : i::Handle<i::Script>(i::Script::cast( | |
| 492 i::SharedFunctionInfo::cast(*compiled_script)->script())); | |
| 493 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE)); | |
| 494 | |
| 495 // Create the evaluation context | |
| 496 evaluation_context_ = Context::New(NULL, global_template); | |
| 497 evaluation_context_->SetSecurityToken(Undefined()); | |
| 498 | |
| 499 #ifdef ENABLE_DEBUGGER_SUPPORT | |
| 500 // Set the security token of the debug context to allow access. | |
| 501 debug->debug_context()->set_security_token(HEAP->undefined_value()); | |
| 502 | |
| 503 // Start the debugger agent if requested. | |
| 504 if (i::FLAG_debugger_agent) { | |
| 505 v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port, true); | |
| 506 } | |
| 507 | |
| 508 // Start the in-process debugger if requested. | |
| 509 if (i::FLAG_debugger && !i::FLAG_debugger_agent) { | |
| 510 v8::Debug::SetDebugEventListener(HandleDebugEvent); | |
| 511 } | |
| 512 #endif | |
| 513 } | 697 } |
| 514 | 698 |
| 515 | 699 |
| 516 void Shell::OnExit() { | 700 void Shell::OnExit() { |
| 517 if (i::FLAG_dump_counters) { | 701 if (i::FLAG_dump_counters) { |
| 518 ::printf("+----------------------------------------+-------------+\n"); | 702 printf("+----------------------------------------+-------------+\n"); |
| 519 ::printf("| Name | Value |\n"); | 703 printf("| Name | Value |\n"); |
| 520 ::printf("+----------------------------------------+-------------+\n"); | 704 printf("+----------------------------------------+-------------+\n"); |
| 521 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) { | 705 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) { |
| 522 Counter* counter = i.CurrentValue(); | 706 Counter* counter = i.CurrentValue(); |
| 523 if (counter->is_histogram()) { | 707 if (counter->is_histogram()) { |
| 524 ::printf("| c:%-36s | %11i |\n", i.CurrentKey(), counter->count()); | 708 printf("| c:%-36s | %11i |\n", i.CurrentKey(), counter->count()); |
| 525 ::printf("| t:%-36s | %11i |\n", | 709 printf("| t:%-36s | %11i |\n", i.CurrentKey(), counter->sample_total()); |
| 526 i.CurrentKey(), | |
| 527 counter->sample_total()); | |
| 528 } else { | 710 } else { |
| 529 ::printf("| %-38s | %11i |\n", i.CurrentKey(), counter->count()); | 711 printf("| %-38s | %11i |\n", i.CurrentKey(), counter->count()); |
| 530 } | 712 } |
| 531 } | 713 } |
| 532 ::printf("+----------------------------------------+-------------+\n"); | 714 printf("+----------------------------------------+-------------+\n"); |
| 533 } | 715 } |
| 534 if (counters_file_ != NULL) | 716 if (counters_file_ != NULL) |
| 535 delete counters_file_; | 717 delete counters_file_; |
| 536 } | 718 } |
| 537 | 719 |
| 538 | 720 |
| 539 static char* ReadChars(const char* name, int* size_out) { | 721 static char* ReadChars(const char* name, int* size_out) { |
| 540 v8::Unlocker unlocker; // Release the V8 lock while reading files. | 722 // Release the V8 lock while reading files. |
| 723 v8::Unlocker unlocker(Isolate::GetCurrent()); |
| 541 FILE* file = i::OS::FOpen(name, "rb"); | 724 FILE* file = i::OS::FOpen(name, "rb"); |
| 542 if (file == NULL) return NULL; | 725 if (file == NULL) return NULL; |
| 543 | 726 |
| 544 fseek(file, 0, SEEK_END); | 727 fseek(file, 0, SEEK_END); |
| 545 int size = ftell(file); | 728 int size = ftell(file); |
| 546 rewind(file); | 729 rewind(file); |
| 547 | 730 |
| 548 char* chars = new char[size + 1]; | 731 char* chars = new char[size + 1]; |
| 549 chars[size] = '\0'; | 732 chars[size] = '\0'; |
| 550 for (int i = 0; i < size;) { | 733 for (int i = 0; i < size;) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 return result; | 771 return result; |
| 589 } | 772 } |
| 590 | 773 |
| 591 | 774 |
| 592 void Shell::RunShell() { | 775 void Shell::RunShell() { |
| 593 LineEditor* editor = LineEditor::Get(); | 776 LineEditor* editor = LineEditor::Get(); |
| 594 printf("V8 version %s [console: %s]\n", V8::GetVersion(), editor->name()); | 777 printf("V8 version %s [console: %s]\n", V8::GetVersion(), editor->name()); |
| 595 if (i::FLAG_debugger) { | 778 if (i::FLAG_debugger) { |
| 596 printf("JavaScript debugger enabled\n"); | 779 printf("JavaScript debugger enabled\n"); |
| 597 } | 780 } |
| 781 |
| 598 editor->Open(); | 782 editor->Open(); |
| 599 while (true) { | 783 while (true) { |
| 600 Locker locker; | 784 Locker locker; |
| 601 HandleScope handle_scope; | 785 HandleScope handle_scope; |
| 602 Context::Scope context_scope(evaluation_context_); | 786 Context::Scope context_scope(evaluation_context_); |
| 603 i::SmartPointer<char> input = editor->Prompt(Shell::kPrompt); | 787 i::SmartPointer<char> input = editor->Prompt(Shell::kPrompt); |
| 604 if (input.is_empty()) | 788 if (input.is_empty()) |
| 605 break; | 789 break; |
| 606 editor->AddHistory(*input); | 790 editor->AddHistory(*input); |
| 607 Handle<String> name = String::New("(d8)"); | 791 Handle<String> name = String::New("(d8)"); |
| 608 ExecuteString(String::New(*input), name, true, true); | 792 ExecuteString(String::New(*input), name, true, true); |
| 609 } | 793 } |
| 610 editor->Close(); | 794 editor->Close(); |
| 611 printf("\n"); | 795 printf("\n"); |
| 612 } | 796 } |
| 613 | 797 |
| 614 | 798 |
| 615 class ShellThread : public i::Thread { | 799 class ShellThread : public i::Thread { |
| 616 public: | 800 public: |
| 617 ShellThread(i::Isolate* isolate, int no, i::Vector<const char> files) | 801 ShellThread(int no, i::Vector<const char> files) |
| 618 : Thread(isolate, "d8:ShellThread"), | 802 : Thread("d8:ShellThread"), |
| 619 no_(no), files_(files) { } | 803 no_(no), files_(files) { } |
| 620 virtual void Run(); | 804 virtual void Run(); |
| 621 private: | 805 private: |
| 622 int no_; | 806 int no_; |
| 623 i::Vector<const char> files_; | 807 i::Vector<const char> files_; |
| 624 }; | 808 }; |
| 625 | 809 |
| 626 | 810 |
| 627 void ShellThread::Run() { | 811 void ShellThread::Run() { |
| 628 // Prepare the context for this thread. | |
| 629 Locker locker; | |
| 630 HandleScope scope; | |
| 631 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); | |
| 632 global_template->Set(String::New("print"), | |
| 633 FunctionTemplate::New(Shell::Print)); | |
| 634 global_template->Set(String::New("write"), | |
| 635 FunctionTemplate::New(Shell::Write)); | |
| 636 global_template->Set(String::New("read"), | |
| 637 FunctionTemplate::New(Shell::Read)); | |
| 638 global_template->Set(String::New("readline"), | |
| 639 FunctionTemplate::New(Shell::ReadLine)); | |
| 640 global_template->Set(String::New("load"), | |
| 641 FunctionTemplate::New(Shell::Load)); | |
| 642 global_template->Set(String::New("yield"), | |
| 643 FunctionTemplate::New(Shell::Yield)); | |
| 644 global_template->Set(String::New("version"), | |
| 645 FunctionTemplate::New(Shell::Version)); | |
| 646 | |
| 647 char* ptr = const_cast<char*>(files_.start()); | 812 char* ptr = const_cast<char*>(files_.start()); |
| 648 while ((ptr != NULL) && (*ptr != '\0')) { | 813 while ((ptr != NULL) && (*ptr != '\0')) { |
| 649 // For each newline-separated line. | 814 // For each newline-separated line. |
| 650 char* next_line = ReadLine(ptr); | 815 char* next_line = ReadLine(ptr); |
| 651 | 816 |
| 652 if (*ptr == '#') { | 817 if (*ptr == '#') { |
| 653 // Skip comment lines. | 818 // Skip comment lines. |
| 654 ptr = next_line; | 819 ptr = next_line; |
| 655 continue; | 820 continue; |
| 656 } | 821 } |
| 657 | 822 |
| 658 Persistent<Context> thread_context = Context::New(NULL, global_template); | 823 // Prepare the context for this thread. |
| 659 thread_context->SetSecurityToken(Undefined()); | 824 Locker locker; |
| 825 HandleScope scope; |
| 826 Persistent<Context> thread_context = Shell::CreateEvaluationContext(); |
| 660 Context::Scope context_scope(thread_context); | 827 Context::Scope context_scope(thread_context); |
| 661 | 828 |
| 662 while ((ptr != NULL) && (*ptr != '\0')) { | 829 while ((ptr != NULL) && (*ptr != '\0')) { |
| 663 char* filename = ptr; | 830 char* filename = ptr; |
| 664 ptr = ReadWord(ptr); | 831 ptr = ReadWord(ptr); |
| 665 | 832 |
| 666 // Skip empty strings. | 833 // Skip empty strings. |
| 667 if (strlen(filename) == 0) { | 834 if (strlen(filename) == 0) { |
| 668 break; | 835 break; |
| 669 } | 836 } |
| 670 | 837 |
| 671 Handle<String> str = Shell::ReadFile(filename); | 838 Handle<String> str = Shell::ReadFile(filename); |
| 672 if (str.IsEmpty()) { | 839 if (str.IsEmpty()) { |
| 673 printf("WARNING: %s not found\n", filename); | 840 printf("WARNING: %s not found\n", filename); |
| 674 break; | 841 break; |
| 675 } | 842 } |
| 676 | 843 |
| 677 Shell::ExecuteString(str, String::New(filename), false, false); | 844 Shell::ExecuteString(str, String::New(filename), false, false); |
| 678 } | 845 } |
| 679 | 846 |
| 680 thread_context.Dispose(); | 847 thread_context.Dispose(); |
| 681 ptr = next_line; | 848 ptr = next_line; |
| 682 } | 849 } |
| 683 } | 850 } |
| 684 | 851 |
| 685 | 852 |
| 686 int Shell::Main(int argc, char* argv[]) { | 853 void SourceGroup::ExitShell(int exit_code) { |
| 687 i::FlagList::SetFlagsFromCommandLine(&argc, argv, true); | 854 // Use _exit instead of exit to avoid races between isolate |
| 688 if (i::FLAG_help) { | 855 // threads and static destructors. |
| 689 return 1; | 856 fflush(stdout); |
| 690 } | 857 fflush(stderr); |
| 691 Initialize(); | 858 _exit(exit_code); |
| 692 bool run_shell = (argc == 1); | 859 } |
| 693 | 860 |
| 694 // Default use preemption if threads are created. | 861 |
| 695 bool use_preemption = true; | 862 void SourceGroup::Execute() { |
| 696 | 863 for (int i = begin_offset_; i < end_offset_; ++i) { |
| 697 // Default to use lowest possible thread preemption interval to test as many | 864 const char* arg = argv_[i]; |
| 698 // edgecases as possible. | 865 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { |
| 699 int preemption_interval = 1; | 866 // Execute argument given to -e option directly. |
| 700 | 867 HandleScope handle_scope; |
| 868 Handle<String> file_name = String::New("unnamed"); |
| 869 Handle<String> source = String::New(argv_[i + 1]); |
| 870 if (!Shell::ExecuteString(source, file_name, false, true)) { |
| 871 ExitShell(1); |
| 872 return; |
| 873 } |
| 874 ++i; |
| 875 } else if (arg[0] == '-') { |
| 876 // Ignore other options. They have been parsed already. |
| 877 } else { |
| 878 // Use all other arguments as names of files to load and run. |
| 879 HandleScope handle_scope; |
| 880 Handle<String> file_name = String::New(arg); |
| 881 Handle<String> source = ReadFile(arg); |
| 882 if (source.IsEmpty()) { |
| 883 printf("Error reading '%s'\n", arg); |
| 884 ExitShell(1); |
| 885 return; |
| 886 } |
| 887 if (!Shell::ExecuteString(source, file_name, false, true)) { |
| 888 ExitShell(1); |
| 889 return; |
| 890 } |
| 891 } |
| 892 } |
| 893 } |
| 894 |
| 895 |
| 896 Handle<String> SourceGroup::ReadFile(const char* name) { |
| 897 FILE* file = fopen(name, "rb"); |
| 898 if (file == NULL) return Handle<String>(); |
| 899 |
| 900 fseek(file, 0, SEEK_END); |
| 901 int size = ftell(file); |
| 902 rewind(file); |
| 903 |
| 904 char* chars = new char[size + 1]; |
| 905 chars[size] = '\0'; |
| 906 for (int i = 0; i < size;) { |
| 907 int read = fread(&chars[i], 1, size - i, file); |
| 908 i += read; |
| 909 } |
| 910 fclose(file); |
| 911 Handle<String> result = String::New(chars, size); |
| 912 delete[] chars; |
| 913 return result; |
| 914 } |
| 915 |
| 916 |
| 917 i::Thread::Options SourceGroup::GetThreadOptions() { |
| 918 i::Thread::Options options; |
| 919 options.name = "IsolateThread"; |
| 920 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less |
| 921 // which is not enough to parse the big literal expressions used in tests. |
| 922 // The stack size should be at least StackGuard::kLimitSize + some |
| 923 // OS-specific padding for thread startup code. |
| 924 options.stack_size = 2 << 20; // 2 Mb seems to be enough |
| 925 return options; |
| 926 } |
| 927 |
| 928 |
| 929 void SourceGroup::ExecuteInThread() { |
| 930 Isolate* isolate = Isolate::New(); |
| 931 do { |
| 932 if (next_semaphore_ != NULL) next_semaphore_->Wait(); |
| 933 { |
| 934 Isolate::Scope iscope(isolate); |
| 935 Locker lock(isolate); |
| 936 HandleScope scope; |
| 937 Persistent<Context> context = Shell::CreateEvaluationContext(); |
| 938 { |
| 939 Context::Scope cscope(context); |
| 940 Execute(); |
| 941 } |
| 942 context.Dispose(); |
| 943 } |
| 944 if (done_semaphore_ != NULL) done_semaphore_->Signal(); |
| 945 } while (!Shell::options.last_run); |
| 946 isolate->Dispose(); |
| 947 } |
| 948 |
| 949 |
| 950 void SourceGroup::StartExecuteInThread() { |
| 951 if (thread_ == NULL) { |
| 952 thread_ = new IsolateThread(this); |
| 953 thread_->Start(); |
| 954 } |
| 955 next_semaphore_->Signal(); |
| 956 } |
| 957 |
| 958 |
| 959 void SourceGroup::WaitForThread() { |
| 960 if (thread_ == NULL) return; |
| 961 if (Shell::options.last_run) { |
| 962 thread_->Join(); |
| 963 thread_ = NULL; |
| 964 } else { |
| 965 done_semaphore_->Wait(); |
| 966 } |
| 967 } |
| 968 |
| 969 |
| 970 bool Shell::SetOptions(int argc, char* argv[]) { |
| 971 for (int i = 0; i < argc; i++) { |
| 972 if (strcmp(argv[i], "--stress-opt") == 0) { |
| 973 options.stress_opt = true; |
| 974 argv[i] = NULL; |
| 975 } else if (strcmp(argv[i], "--stress-deopt") == 0) { |
| 976 options.stress_deopt = true; |
| 977 argv[i] = NULL; |
| 978 } else if (strcmp(argv[i], "--noalways-opt") == 0) { |
| 979 // No support for stressing if we can't use --always-opt. |
| 980 options.stress_opt = false; |
| 981 options.stress_deopt = false; |
| 982 } else if (strcmp(argv[i], "--shell") == 0) { |
| 983 options.interactive_shell = true; |
| 984 argv[i] = NULL; |
| 985 } else if (strcmp(argv[i], "--test") == 0) { |
| 986 options.test_shell = true; |
| 987 argv[i] = NULL; |
| 988 } else if (strcmp(argv[i], "--preemption") == 0) { |
| 989 options.use_preemption = true; |
| 990 argv[i] = NULL; |
| 991 } else if (strcmp(argv[i], "--no-preemption") == 0) { |
| 992 options.use_preemption = false; |
| 993 argv[i] = NULL; |
| 994 } else if (strcmp(argv[i], "--preemption-interval") == 0) { |
| 995 if (++i < argc) { |
| 996 argv[i-1] = NULL; |
| 997 char* end = NULL; |
| 998 options.preemption_interval = strtol(argv[i], &end, 10); // NOLINT |
| 999 if (options.preemption_interval <= 0 |
| 1000 || *end != '\0' |
| 1001 || errno == ERANGE) { |
| 1002 printf("Invalid value for --preemption-interval '%s'\n", argv[i]); |
| 1003 return false; |
| 1004 } |
| 1005 argv[i] = NULL; |
| 1006 } else { |
| 1007 printf("Missing value for --preemption-interval\n"); |
| 1008 return false; |
| 1009 } |
| 1010 } else if (strcmp(argv[i], "-f") == 0) { |
| 1011 // Ignore any -f flags for compatibility with other stand-alone |
| 1012 // JavaScript engines. |
| 1013 continue; |
| 1014 } else if (strcmp(argv[i], "--isolate") == 0) { |
| 1015 options.num_isolates++; |
| 1016 } |
| 1017 } |
| 1018 |
| 1019 // Run parallel threads if we are not using --isolate |
| 1020 for (int i = 1; i < argc; i++) { |
| 1021 if (argv[i] == NULL) continue; |
| 1022 if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) { |
| 1023 if (options.num_isolates > 1) { |
| 1024 printf("-p is not compatible with --isolate\n"); |
| 1025 return false; |
| 1026 } |
| 1027 argv[i] = NULL; |
| 1028 if (options.parallel_files == NULL) { |
| 1029 options.parallel_files = new i::List<i::Vector<const char> >(); |
| 1030 } |
| 1031 int size = 0; |
| 1032 const char* files = ReadChars(argv[++i], &size); |
| 1033 if (files == NULL) { |
| 1034 printf("-p option incomplete\n"); |
| 1035 return false; |
| 1036 } |
| 1037 argv[i] = NULL; |
| 1038 options.parallel_files->Add(i::Vector<const char>(files, size)); |
| 1039 } |
| 1040 } |
| 1041 |
| 1042 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 1043 |
| 1044 // set up isolated source groups |
| 1045 options.isolate_sources = new SourceGroup[options.num_isolates]; |
| 1046 SourceGroup* current = options.isolate_sources; |
| 1047 current->Begin(argv, 1); |
| 1048 for (int i = 1; i < argc; i++) { |
| 1049 const char* str = argv[i]; |
| 1050 if (strcmp(str, "--isolate") == 0) { |
| 1051 current->End(i); |
| 1052 current++; |
| 1053 current->Begin(argv, i + 1); |
| 1054 } else if (strncmp(argv[i], "--", 2) == 0) { |
| 1055 printf("Warning: unknown flag %s.\nTry --help for options\n", argv[i]); |
| 1056 } |
| 1057 } |
| 1058 current->End(argc); |
| 1059 |
| 1060 return true; |
| 1061 } |
| 1062 |
| 1063 |
| 1064 int Shell::RunMain(int argc, char* argv[]) { |
| 701 i::List<i::Thread*> threads(1); | 1065 i::List<i::Thread*> threads(1); |
| 702 | 1066 |
| 703 { | 1067 { |
| 704 // Acquire the V8 lock once initialization has finished. Since the thread | 1068 if (options.parallel_files != NULL) |
| 705 // below may spawn new threads accessing V8 holding the V8 lock here is | 1069 for (int i = 0; i < options.parallel_files->length(); i++) { |
| 706 // mandatory. | 1070 i::Vector<const char> files = options.parallel_files->at(i); |
| 707 Locker locker; | 1071 ShellThread* thread = new ShellThread(threads.length(), files); |
| 708 Context::Scope context_scope(evaluation_context_); | |
| 709 for (int i = 1; i < argc; i++) { | |
| 710 char* str = argv[i]; | |
| 711 if (strcmp(str, "--shell") == 0) { | |
| 712 run_shell = true; | |
| 713 } else if (strcmp(str, "--preemption") == 0) { | |
| 714 use_preemption = true; | |
| 715 } else if (strcmp(str, "--no-preemption") == 0) { | |
| 716 use_preemption = false; | |
| 717 } else if (strcmp(str, "--preemption-interval") == 0) { | |
| 718 if (i + 1 < argc) { | |
| 719 char* end = NULL; | |
| 720 preemption_interval = strtol(argv[++i], &end, 10); // NOLINT | |
| 721 if (preemption_interval <= 0 || *end != '\0' || errno == ERANGE) { | |
| 722 printf("Invalid value for --preemption-interval '%s'\n", argv[i]); | |
| 723 return 1; | |
| 724 } | |
| 725 } else { | |
| 726 printf("Missing value for --preemption-interval\n"); | |
| 727 return 1; | |
| 728 } | |
| 729 } else if (strcmp(str, "-f") == 0) { | |
| 730 // Ignore any -f flags for compatibility with other stand-alone | |
| 731 // JavaScript engines. | |
| 732 continue; | |
| 733 } else if (strncmp(str, "--", 2) == 0) { | |
| 734 printf("Warning: unknown flag %s.\nTry --help for options\n", str); | |
| 735 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { | |
| 736 // Execute argument given to -e option directly. | |
| 737 v8::HandleScope handle_scope; | |
| 738 v8::Handle<v8::String> file_name = v8::String::New("unnamed"); | |
| 739 v8::Handle<v8::String> source = v8::String::New(argv[i + 1]); | |
| 740 if (!ExecuteString(source, file_name, false, true)) { | |
| 741 OnExit(); | |
| 742 return 1; | |
| 743 } | |
| 744 i++; | |
| 745 } else if (strcmp(str, "-p") == 0 && i + 1 < argc) { | |
| 746 int size = 0; | |
| 747 const char* files = ReadChars(argv[++i], &size); | |
| 748 if (files == NULL) return 1; | |
| 749 ShellThread* thread = | |
| 750 new ShellThread(i::Isolate::Current(), | |
| 751 threads.length(), | |
| 752 i::Vector<const char>(files, size)); | |
| 753 thread->Start(); | 1072 thread->Start(); |
| 754 threads.Add(thread); | 1073 threads.Add(thread); |
| 755 } else { | 1074 } |
| 756 // Use all other arguments as names of files to load and run. | 1075 |
| 757 HandleScope handle_scope; | 1076 for (int i = 1; i < options.num_isolates; ++i) { |
| 758 Handle<String> file_name = v8::String::New(str); | 1077 options.isolate_sources[i].StartExecuteInThread(); |
| 759 Handle<String> source = ReadFile(str); | 1078 } |
| 760 if (source.IsEmpty()) { | 1079 |
| 761 printf("Error reading '%s'\n", str); | 1080 Locker lock; |
| 762 return 1; | 1081 HandleScope scope; |
| 763 } | 1082 Persistent<Context> context = CreateEvaluationContext(); |
| 764 if (!ExecuteString(source, file_name, false, true)) { | 1083 { |
| 765 OnExit(); | 1084 Context::Scope cscope(context); |
| 766 return 1; | 1085 options.isolate_sources[0].Execute(); |
| 767 } | 1086 } |
| 768 } | 1087 if (options.last_run) { |
| 769 } | 1088 // Keep using the same context in the interactive shell |
| 770 | 1089 evaluation_context_ = context; |
| 1090 } else { |
| 1091 context.Dispose(); |
| 1092 } |
| 771 // Start preemption if threads have been created and preemption is enabled. | 1093 // Start preemption if threads have been created and preemption is enabled. |
| 772 if (threads.length() > 0 && use_preemption) { | 1094 if (options.parallel_files != NULL |
| 773 Locker::StartPreemption(preemption_interval); | 1095 && threads.length() > 0 |
| 774 } | 1096 && options.use_preemption) { |
| 775 | 1097 Locker::StartPreemption(options.preemption_interval); |
| 776 #ifdef ENABLE_DEBUGGER_SUPPORT | 1098 } |
| 777 // Run the remote debugger if requested. | 1099 } |
| 778 if (i::FLAG_remote_debugger) { | 1100 |
| 779 RunRemoteDebugger(i::FLAG_debugger_port); | 1101 for (int i = 1; i < options.num_isolates; ++i) { |
| 780 return 0; | 1102 options.isolate_sources[i].WaitForThread(); |
| 781 } | 1103 } |
| 782 #endif | 1104 |
| 783 } | 1105 if (options.parallel_files != NULL) |
| 784 if (run_shell) | 1106 for (int i = 0; i < threads.length(); i++) { |
| 785 RunShell(); | 1107 i::Thread* thread = threads[i]; |
| 786 for (int i = 0; i < threads.length(); i++) { | 1108 thread->Join(); |
| 787 i::Thread* thread = threads[i]; | 1109 delete thread; |
| 788 thread->Join(); | 1110 } |
| 789 delete thread; | 1111 |
| 790 } | |
| 791 OnExit(); | 1112 OnExit(); |
| 792 return 0; | 1113 return 0; |
| 793 } | 1114 } |
| 794 | 1115 |
| 795 | 1116 |
| 1117 int Shell::Main(int argc, char* argv[]) { |
| 1118 if (!SetOptions(argc, argv)) return 1; |
| 1119 Initialize(); |
| 1120 |
| 1121 int result = 0; |
| 1122 if (options.stress_opt || options.stress_deopt) { |
| 1123 Testing::SetStressRunType( |
| 1124 options.stress_opt ? Testing::kStressTypeOpt |
| 1125 : Testing::kStressTypeDeopt); |
| 1126 int stress_runs = Testing::GetStressRuns(); |
| 1127 for (int i = 0; i < stress_runs && result == 0; i++) { |
| 1128 printf("============ Stress %d/%d ============\n", i + 1, stress_runs); |
| 1129 Testing::PrepareStressRun(i); |
| 1130 options.last_run = (i == stress_runs - 1); |
| 1131 result = RunMain(argc, argv); |
| 1132 } |
| 1133 printf("======== Full Deoptimization =======\n"); |
| 1134 Testing::DeoptimizeAll(); |
| 1135 } else { |
| 1136 result = RunMain(argc, argv); |
| 1137 } |
| 1138 |
| 1139 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1140 // Run remote debugger if requested, but never on --test |
| 1141 if (i::FLAG_remote_debugger && !options.test_shell) { |
| 1142 InstallUtilityScript(); |
| 1143 RunRemoteDebugger(i::FLAG_debugger_port); |
| 1144 return 0; |
| 1145 } |
| 1146 #endif |
| 1147 |
| 1148 // Run interactive shell if explicitly requested or if no script has been |
| 1149 // executed, but never on --test |
| 1150 |
| 1151 if (( options.interactive_shell |
| 1152 || !options.script_executed ) |
| 1153 && !options.test_shell ) { |
| 1154 InstallUtilityScript(); |
| 1155 RunShell(); |
| 1156 } |
| 1157 |
| 1158 V8::Dispose(); |
| 1159 |
| 1160 return result; |
| 1161 } |
| 1162 |
| 796 } // namespace v8 | 1163 } // namespace v8 |
| 797 | 1164 |
| 798 | 1165 |
| 799 #ifndef GOOGLE3 | 1166 #ifndef GOOGLE3 |
| 800 int main(int argc, char* argv[]) { | 1167 int main(int argc, char* argv[]) { |
| 801 return v8::Shell::Main(argc, argv); | 1168 return v8::Shell::Main(argc, argv); |
| 802 } | 1169 } |
| 803 #endif | 1170 #endif |
| OLD | NEW |