OLD | NEW |
---|---|
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 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 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
482 | 482 |
483 | 483 |
484 void Shell::AddHistogramSample(void* histogram, int sample) { | 484 void Shell::AddHistogramSample(void* histogram, int sample) { |
485 Counter* counter = reinterpret_cast<Counter*>(histogram); | 485 Counter* counter = reinterpret_cast<Counter*>(histogram); |
486 counter->AddSample(sample); | 486 counter->AddSample(sample); |
487 } | 487 } |
488 | 488 |
489 void Shell::InstallUtilityScript() { | 489 void Shell::InstallUtilityScript() { |
490 Locker lock; | 490 Locker lock; |
491 HandleScope scope; | 491 HandleScope scope; |
492 utility_context_->SetSecurityToken(Undefined()); | |
493 evaluation_context_->SetSecurityToken(Undefined()); | |
492 Context::Scope utility_scope(utility_context_); | 494 Context::Scope utility_scope(utility_context_); |
Yang
2011/06/22 00:44:08
I just went with your suggestion to not set the se
| |
495 | |
496 #ifdef ENABLE_DEBUGGER_SUPPORT | |
497 // Install the debugger object in the utility scope | |
498 i::Debug* debug = i::Isolate::Current()->debug(); | |
499 debug->Load(); | |
500 i::Handle<i::JSObject> js_debug | |
501 = i::Handle<i::JSObject>(debug->debug_context()->global()); | |
502 utility_context_->Global()->Set(String::New("$debug"), | |
503 Utils::ToLocal(js_debug)); | |
504 debug->debug_context()->set_security_token(HEAP->undefined_value()); | |
505 #endif | |
506 | |
493 // Run the d8 shell utility script in the utility context | 507 // Run the d8 shell utility script in the utility context |
494 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); | 508 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); |
495 i::Vector<const char> shell_source = | 509 i::Vector<const char> shell_source = |
496 i::NativesCollection<i::D8>::GetRawScriptSource(source_index); | 510 i::NativesCollection<i::D8>::GetRawScriptSource(source_index); |
497 i::Vector<const char> shell_source_name = | 511 i::Vector<const char> shell_source_name = |
498 i::NativesCollection<i::D8>::GetScriptName(source_index); | 512 i::NativesCollection<i::D8>::GetScriptName(source_index); |
499 Handle<String> source = String::New(shell_source.start(), | 513 Handle<String> source = String::New(shell_source.start(), |
500 shell_source.length()); | 514 shell_source.length()); |
501 Handle<String> name = String::New(shell_source_name.start(), | 515 Handle<String> name = String::New(shell_source_name.start(), |
502 shell_source_name.length()); | 516 shell_source_name.length()); |
503 Handle<Script> script = Script::Compile(source, name); | 517 Handle<Script> script = Script::Compile(source, name); |
504 script->Run(); | 518 script->Run(); |
505 | 519 |
506 // Mark the d8 shell script as native to avoid it showing up as normal source | 520 // Mark the d8 shell script as native to avoid it showing up as normal source |
507 // in the debugger. | 521 // in the debugger. |
508 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); | 522 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); |
509 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() | 523 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() |
510 ? i::Handle<i::Script>(i::Script::cast( | 524 ? i::Handle<i::Script>(i::Script::cast( |
511 i::JSFunction::cast(*compiled_script)->shared()->script())) | 525 i::JSFunction::cast(*compiled_script)->shared()->script())) |
512 : i::Handle<i::Script>(i::Script::cast( | 526 : i::Handle<i::Script>(i::Script::cast( |
513 i::SharedFunctionInfo::cast(*compiled_script)->script())); | 527 i::SharedFunctionInfo::cast(*compiled_script)->script())); |
514 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE)); | 528 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE)); |
515 } | 529 } |
516 | 530 |
531 | |
517 #ifdef COMPRESS_STARTUP_DATA_BZ2 | 532 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
518 class BZip2Decompressor : public v8::StartupDataDecompressor { | 533 class BZip2Decompressor : public v8::StartupDataDecompressor { |
519 public: | 534 public: |
520 virtual ~BZip2Decompressor() { } | 535 virtual ~BZip2Decompressor() { } |
521 | 536 |
522 protected: | 537 protected: |
523 virtual int DecompressData(char* raw_data, | 538 virtual int DecompressData(char* raw_data, |
524 int* raw_data_size, | 539 int* raw_data_size, |
525 const char* compressed_data, | 540 const char* compressed_data, |
526 int compressed_data_size) { | 541 int compressed_data_size) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
578 global_template->Set(String::New("lol_is_enabled"), Boolean::New(false)); | 593 global_template->Set(String::New("lol_is_enabled"), Boolean::New(false)); |
579 #endif | 594 #endif |
580 | 595 |
581 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); | 596 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); |
582 AddOSMethods(os_templ); | 597 AddOSMethods(os_templ); |
583 global_template->Set(String::New("os"), os_templ); | 598 global_template->Set(String::New("os"), os_templ); |
584 | 599 |
585 return global_template; | 600 return global_template; |
586 } | 601 } |
587 | 602 |
588 void Shell::Initialize() { | 603 |
604 void Shell::Initialize(bool test_shell) { | |
Yang
2011/06/22 00:44:08
Instead of renaming the argument to "interactive",
| |
589 #ifdef COMPRESS_STARTUP_DATA_BZ2 | 605 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
590 BZip2Decompressor startup_data_decompressor; | 606 BZip2Decompressor startup_data_decompressor; |
591 int bz2_result = startup_data_decompressor.Decompress(); | 607 int bz2_result = startup_data_decompressor.Decompress(); |
592 if (bz2_result != BZ_OK) { | 608 if (bz2_result != BZ_OK) { |
593 fprintf(stderr, "bzip error code: %d\n", bz2_result); | 609 fprintf(stderr, "bzip error code: %d\n", bz2_result); |
594 exit(1); | 610 exit(1); |
595 } | 611 } |
596 #endif | 612 #endif |
597 | 613 |
598 Shell::counter_map_ = new CounterMap(); | 614 Shell::counter_map_ = new CounterMap(); |
599 // Set up counters | 615 // Set up counters |
600 if (i::StrLength(i::FLAG_map_counters) != 0) | 616 if (i::StrLength(i::FLAG_map_counters) != 0) |
601 MapCounters(i::FLAG_map_counters); | 617 MapCounters(i::FLAG_map_counters); |
602 if (i::FLAG_dump_counters) { | 618 if (i::FLAG_dump_counters) { |
603 V8::SetCounterFunction(LookupCounter); | 619 V8::SetCounterFunction(LookupCounter); |
604 V8::SetCreateHistogramFunction(CreateHistogram); | 620 V8::SetCreateHistogramFunction(CreateHistogram); |
605 V8::SetAddHistogramSampleFunction(AddHistogramSample); | 621 V8::SetAddHistogramSampleFunction(AddHistogramSample); |
606 } | 622 } |
607 | 623 |
608 // Initialize the global objects | 624 if (test_shell) return; |
625 | |
626 Locker lock; | |
609 HandleScope scope; | 627 HandleScope scope; |
610 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(); | 628 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(); |
611 | |
612 utility_context_ = Context::New(NULL, global_template); | 629 utility_context_ = Context::New(NULL, global_template); |
613 utility_context_->SetSecurityToken(Undefined()); | |
614 Context::Scope utility_scope(utility_context_); | |
615 | 630 |
616 #ifdef ENABLE_DEBUGGER_SUPPORT | 631 #ifdef ENABLE_DEBUGGER_SUPPORT |
617 // Install the debugger object in the utility scope | 632 // Start the debugger agent if requested. |
618 i::Debug* debug = i::Isolate::Current()->debug(); | 633 if (i::FLAG_debugger_agent) { |
619 debug->Load(); | 634 v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port, true); |
620 i::Handle<i::JSObject> js_debug | 635 } |
621 = i::Handle<i::JSObject>(debug->debug_context()->global()); | 636 |
622 utility_context_->Global()->Set(String::New("$debug"), | 637 // Start the in-process debugger if requested. |
623 Utils::ToLocal(js_debug)); | 638 if (i::FLAG_debugger && !i::FLAG_debugger_agent) { |
639 v8::Debug::SetDebugEventListener(HandleDebugEvent); | |
640 } | |
624 #endif | 641 #endif |
625 } | 642 } |
626 | 643 |
627 | 644 |
628 void Shell::RenewEvaluationContext() { | 645 void Shell::RenewEvaluationContext() { |
629 // Initialize the global objects | 646 // Initialize the global objects |
630 HandleScope scope; | 647 HandleScope scope; |
631 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(); | 648 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(); |
632 | 649 |
633 // (Re-)create the evaluation context | 650 // (Re-)create the evaluation context |
634 if (!evaluation_context_.IsEmpty()) { | 651 if (!evaluation_context_.IsEmpty()) { |
635 evaluation_context_.Dispose(); | 652 evaluation_context_.Dispose(); |
636 } | 653 } |
637 evaluation_context_ = Context::New(NULL, global_template); | 654 evaluation_context_ = Context::New(NULL, global_template); |
638 evaluation_context_->SetSecurityToken(Undefined()); | 655 Context::Scope utility_scope(evaluation_context_); |
639 | 656 |
640 Context::Scope utility_scope(utility_context_); | |
641 i::JSArguments js_args = i::FLAG_js_arguments; | 657 i::JSArguments js_args = i::FLAG_js_arguments; |
642 i::Handle<i::FixedArray> arguments_array = | 658 i::Handle<i::FixedArray> arguments_array = |
643 FACTORY->NewFixedArray(js_args.argc()); | 659 FACTORY->NewFixedArray(js_args.argc()); |
644 for (int j = 0; j < js_args.argc(); j++) { | 660 for (int j = 0; j < js_args.argc(); j++) { |
645 i::Handle<i::String> arg = | 661 i::Handle<i::String> arg = |
646 FACTORY->NewStringFromUtf8(i::CStrVector(js_args[j])); | 662 FACTORY->NewStringFromUtf8(i::CStrVector(js_args[j])); |
647 arguments_array->set(j, *arg); | 663 arguments_array->set(j, *arg); |
648 } | 664 } |
649 i::Handle<i::JSArray> arguments_jsarray = | 665 i::Handle<i::JSArray> arguments_jsarray = |
650 FACTORY->NewJSArrayWithElements(arguments_array); | 666 FACTORY->NewJSArrayWithElements(arguments_array); |
651 evaluation_context_->Global()->Set(String::New("arguments"), | 667 evaluation_context_->Global()->Set(String::New("arguments"), |
652 Utils::ToLocal(arguments_jsarray)); | 668 Utils::ToLocal(arguments_jsarray)); |
653 | |
654 #ifdef ENABLE_DEBUGGER_SUPPORT | |
655 i::Debug* debug = i::Isolate::Current()->debug(); | |
656 debug->Load(); | |
657 | |
658 // Set the security token of the debug context to allow access. | |
659 debug->debug_context()->set_security_token(HEAP->undefined_value()); | |
660 | |
661 // Start the debugger agent if requested. | |
662 if (i::FLAG_debugger_agent) { | |
663 v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port, true); | |
664 } | |
665 | |
666 // Start the in-process debugger if requested. | |
667 if (i::FLAG_debugger && !i::FLAG_debugger_agent) { | |
668 v8::Debug::SetDebugEventListener(HandleDebugEvent); | |
669 } | |
670 #endif | |
671 } | 669 } |
672 | 670 |
673 | 671 |
674 void Shell::OnExit() { | 672 void Shell::OnExit() { |
675 if (i::FLAG_dump_counters) { | 673 if (i::FLAG_dump_counters) { |
676 ::printf("+----------------------------------------+-------------+\n"); | 674 ::printf("+----------------------------------------+-------------+\n"); |
677 ::printf("| Name | Value |\n"); | 675 ::printf("| Name | Value |\n"); |
678 ::printf("+----------------------------------------+-------------+\n"); | 676 ::printf("+----------------------------------------+-------------+\n"); |
679 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) { | 677 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) { |
680 Counter* counter = i.CurrentValue(); | 678 Counter* counter = i.CurrentValue(); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
746 return result; | 744 return result; |
747 } | 745 } |
748 | 746 |
749 | 747 |
750 void Shell::RunShell() { | 748 void Shell::RunShell() { |
751 LineEditor* editor = LineEditor::Get(); | 749 LineEditor* editor = LineEditor::Get(); |
752 printf("V8 version %s [console: %s]\n", V8::GetVersion(), editor->name()); | 750 printf("V8 version %s [console: %s]\n", V8::GetVersion(), editor->name()); |
753 if (i::FLAG_debugger) { | 751 if (i::FLAG_debugger) { |
754 printf("JavaScript debugger enabled\n"); | 752 printf("JavaScript debugger enabled\n"); |
755 } | 753 } |
754 | |
756 editor->Open(); | 755 editor->Open(); |
757 while (true) { | 756 while (true) { |
758 Locker locker; | 757 Locker locker; |
759 HandleScope handle_scope; | 758 HandleScope handle_scope; |
760 Context::Scope context_scope(evaluation_context_); | 759 Context::Scope context_scope(evaluation_context_); |
761 i::SmartPointer<char> input = editor->Prompt(Shell::kPrompt); | 760 i::SmartPointer<char> input = editor->Prompt(Shell::kPrompt); |
762 if (input.is_empty()) | 761 if (input.is_empty()) |
763 break; | 762 break; |
764 editor->AddHistory(*input); | 763 editor->AddHistory(*input); |
765 Handle<String> name = String::New("(d8)"); | 764 Handle<String> name = String::New("(d8)"); |
(...skipping 27 matching lines...) Expand all Loading... | |
793 // For each newline-separated line. | 792 // For each newline-separated line. |
794 char* next_line = ReadLine(ptr); | 793 char* next_line = ReadLine(ptr); |
795 | 794 |
796 if (*ptr == '#') { | 795 if (*ptr == '#') { |
797 // Skip comment lines. | 796 // Skip comment lines. |
798 ptr = next_line; | 797 ptr = next_line; |
799 continue; | 798 continue; |
800 } | 799 } |
801 | 800 |
802 Persistent<Context> thread_context = Context::New(NULL, global_template); | 801 Persistent<Context> thread_context = Context::New(NULL, global_template); |
803 thread_context->SetSecurityToken(Undefined()); | |
804 Context::Scope context_scope(thread_context); | 802 Context::Scope context_scope(thread_context); |
805 | 803 |
806 while ((ptr != NULL) && (*ptr != '\0')) { | 804 while ((ptr != NULL) && (*ptr != '\0')) { |
807 char* filename = ptr; | 805 char* filename = ptr; |
808 ptr = ReadWord(ptr); | 806 ptr = ReadWord(ptr); |
809 | 807 |
810 // Skip empty strings. | 808 // Skip empty strings. |
811 if (strlen(filename) == 0) { | 809 if (strlen(filename) == 0) { |
812 break; | 810 break; |
813 } | 811 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
915 return 0; | 913 return 0; |
916 } | 914 } |
917 | 915 |
918 | 916 |
919 int Shell::Main(int argc, char* argv[]) { | 917 int Shell::Main(int argc, char* argv[]) { |
920 // Figure out if we're requested to stress the optimization | 918 // Figure out if we're requested to stress the optimization |
921 // infrastructure by running tests multiple times and forcing | 919 // infrastructure by running tests multiple times and forcing |
922 // optimization in the last run. | 920 // optimization in the last run. |
923 bool FLAG_stress_opt = false; | 921 bool FLAG_stress_opt = false; |
924 bool FLAG_stress_deopt = false; | 922 bool FLAG_stress_deopt = false; |
925 bool run_shell = (argc == 1); | 923 bool FLAG_interactive_shell = false; |
924 bool FLAG_test_shell = false; | |
926 | 925 |
927 for (int i = 0; i < argc; i++) { | 926 for (int i = 0; i < argc; i++) { |
928 if (strcmp(argv[i], "--stress-opt") == 0) { | 927 if (strcmp(argv[i], "--stress-opt") == 0) { |
929 FLAG_stress_opt = true; | 928 FLAG_stress_opt = true; |
930 argv[i] = NULL; | 929 argv[i] = NULL; |
931 } else if (strcmp(argv[i], "--stress-deopt") == 0) { | 930 } else if (strcmp(argv[i], "--stress-deopt") == 0) { |
932 FLAG_stress_deopt = true; | 931 FLAG_stress_deopt = true; |
933 argv[i] = NULL; | 932 argv[i] = NULL; |
934 } else if (strcmp(argv[i], "--noalways-opt") == 0) { | 933 } else if (strcmp(argv[i], "--noalways-opt") == 0) { |
935 // No support for stressing if we can't use --always-opt. | 934 // No support for stressing if we can't use --always-opt. |
936 FLAG_stress_opt = false; | 935 FLAG_stress_opt = false; |
937 FLAG_stress_deopt = false; | 936 FLAG_stress_deopt = false; |
938 } else if (strcmp(argv[i], "--shell") == 0) { | 937 } else if (strcmp(argv[i], "--shell") == 0) { |
939 run_shell = true; | 938 FLAG_interactive_shell = true; |
939 argv[i] = NULL; | |
940 } else if (strcmp(argv[i], "--test") == 0) { | |
941 FLAG_test_shell = true; | |
940 argv[i] = NULL; | 942 argv[i] = NULL; |
941 } | 943 } |
942 } | 944 } |
943 | 945 |
944 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 946 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
945 | 947 |
946 Initialize(); | 948 FLAG_interactive_shell = !FLAG_test_shell && |
Yang
2011/06/22 00:44:08
Basically states: if using shell for test suites,
| |
949 (FLAG_interactive_shell || (argc == 1)); | |
950 | |
951 Initialize(FLAG_test_shell); | |
947 | 952 |
948 int result = 0; | 953 int result = 0; |
949 if (FLAG_stress_opt || FLAG_stress_deopt) { | 954 if (FLAG_stress_opt || FLAG_stress_deopt) { |
950 v8::Testing::SetStressRunType( | 955 v8::Testing::SetStressRunType( |
951 FLAG_stress_opt ? v8::Testing::kStressTypeOpt | 956 FLAG_stress_opt ? v8::Testing::kStressTypeOpt |
952 : v8::Testing::kStressTypeDeopt); | 957 : v8::Testing::kStressTypeDeopt); |
953 int stress_runs = v8::Testing::GetStressRuns(); | 958 int stress_runs = v8::Testing::GetStressRuns(); |
954 for (int i = 0; i < stress_runs && result == 0; i++) { | 959 for (int i = 0; i < stress_runs && result == 0; i++) { |
955 printf("============ Stress %d/%d ============\n", i + 1, stress_runs); | 960 printf("============ Stress %d/%d ============\n", i + 1, stress_runs); |
956 v8::Testing::PrepareStressRun(i); | 961 v8::Testing::PrepareStressRun(i); |
957 result = RunMain(argc, argv); | 962 result = RunMain(argc, argv); |
958 } | 963 } |
959 printf("======== Full Deoptimization =======\n"); | 964 printf("======== Full Deoptimization =======\n"); |
960 v8::Testing::DeoptimizeAll(); | 965 v8::Testing::DeoptimizeAll(); |
961 } else { | 966 } else { |
962 result = RunMain(argc, argv); | 967 result = RunMain(argc, argv); |
963 } | 968 } |
964 | 969 |
965 #ifdef ENABLE_DEBUGGER_SUPPORT | 970 #ifdef ENABLE_DEBUGGER_SUPPORT |
966 if (i::FLAG_remote_debugger) { | 971 if (i::FLAG_remote_debugger) { |
972 InstallUtilityScript(); | |
Yang
2011/06/22 00:44:08
The remote debugger requires stuff from d8.js, the
| |
967 RunRemoteDebugger(i::FLAG_debugger_port); | 973 RunRemoteDebugger(i::FLAG_debugger_port); |
968 return 0; | 974 return 0; |
969 } | 975 } |
970 #endif | 976 #endif |
971 | 977 |
972 if (run_shell) { | 978 if (FLAG_interactive_shell) { |
973 InstallUtilityScript(); | 979 InstallUtilityScript(); |
974 RunShell(); | 980 RunShell(); |
975 } | 981 } |
976 | 982 |
977 v8::V8::Dispose(); | 983 v8::V8::Dispose(); |
978 | 984 |
979 return result; | 985 return result; |
980 } | 986 } |
981 | 987 |
982 } // namespace v8 | 988 } // namespace v8 |
983 | 989 |
984 | 990 |
985 #ifndef GOOGLE3 | 991 #ifndef GOOGLE3 |
986 int main(int argc, char* argv[]) { | 992 int main(int argc, char* argv[]) { |
987 return v8::Shell::Main(argc, argv); | 993 return v8::Shell::Main(argc, argv); |
988 } | 994 } |
989 #endif | 995 #endif |
OLD | NEW |