OLD | NEW |
1 // Copyright 2007-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2008 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 4675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4686 { | 4686 { |
4687 v8::Context::Scope context_scope(context_2); | 4687 v8::Context::Scope context_scope(context_2); |
4688 expected_context = context_2; | 4688 expected_context = context_2; |
4689 expected_context_data = data_2; | 4689 expected_context_data = data_2; |
4690 v8::Local<v8::Function> f = CompileFunction(source, "f"); | 4690 v8::Local<v8::Function> f = CompileFunction(source, "f"); |
4691 f->Call(context_2->Global(), 0, NULL); | 4691 f->Call(context_2->Global(), 0, NULL); |
4692 } | 4692 } |
4693 | 4693 |
4694 // Two times compile event and two times break event. | 4694 // Two times compile event and two times break event. |
4695 CHECK_GT(message_handler_hit_count, 4); | 4695 CHECK_GT(message_handler_hit_count, 4); |
| 4696 |
| 4697 v8::Debug::SetMessageHandler2(NULL); |
| 4698 CheckDebuggerUnloaded(); |
4696 } | 4699 } |
4697 | 4700 |
4698 | 4701 |
4699 // Common part of EvalContextData and NestedBreakEventContextData tests. | 4702 // Common part of EvalContextData and NestedBreakEventContextData tests. |
4700 static void ExecuteScriptForContextCheck() { | 4703 static void ExecuteScriptForContextCheck() { |
4701 // Create a context. | 4704 // Create a context. |
4702 v8::Persistent<v8::Context> context_1; | 4705 v8::Persistent<v8::Context> context_1; |
4703 v8::Handle<v8::ObjectTemplate> global_template = | 4706 v8::Handle<v8::ObjectTemplate> global_template = |
4704 v8::Handle<v8::ObjectTemplate>(); | 4707 v8::Handle<v8::ObjectTemplate>(); |
4705 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>(); | 4708 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>(); |
(...skipping 26 matching lines...) Expand all Loading... |
4732 // break event in an eval statement the expected context is the one returned by | 4735 // break event in an eval statement the expected context is the one returned by |
4733 // Message.GetEventContext. | 4736 // Message.GetEventContext. |
4734 TEST(EvalContextData) { | 4737 TEST(EvalContextData) { |
4735 v8::HandleScope scope; | 4738 v8::HandleScope scope; |
4736 v8::Debug::SetMessageHandler2(ContextCheckMessageHandler); | 4739 v8::Debug::SetMessageHandler2(ContextCheckMessageHandler); |
4737 | 4740 |
4738 ExecuteScriptForContextCheck(); | 4741 ExecuteScriptForContextCheck(); |
4739 | 4742 |
4740 // One time compile event and one time break event. | 4743 // One time compile event and one time break event. |
4741 CHECK_GT(message_handler_hit_count, 2); | 4744 CHECK_GT(message_handler_hit_count, 2); |
| 4745 v8::Debug::SetMessageHandler2(NULL); |
| 4746 CheckDebuggerUnloaded(); |
4742 } | 4747 } |
4743 | 4748 |
4744 | 4749 |
4745 static bool sent_eval = false; | 4750 static bool sent_eval = false; |
4746 static int break_count = 0; | 4751 static int break_count = 0; |
| 4752 static int continue_command_send_count = 0; |
4747 // Check that the expected context is the one generating the debug event | 4753 // Check that the expected context is the one generating the debug event |
4748 // including the case of nested break event. | 4754 // including the case of nested break event. |
4749 static void DebugEvalContextCheckMessageHandler( | 4755 static void DebugEvalContextCheckMessageHandler( |
4750 const v8::Debug::Message& message) { | 4756 const v8::Debug::Message& message) { |
4751 CHECK(message.GetEventContext() == expected_context); | 4757 CHECK(message.GetEventContext() == expected_context); |
4752 CHECK(message.GetEventContext()->GetData()->StrictEquals( | 4758 CHECK(message.GetEventContext()->GetData()->StrictEquals( |
4753 expected_context_data)); | 4759 expected_context_data)); |
4754 message_handler_hit_count++; | 4760 message_handler_hit_count++; |
4755 | 4761 |
4756 if (message.IsEvent() && message.GetEvent() == v8::Break) { | 4762 if (message.IsEvent() && message.GetEvent() == v8::Break) { |
4757 break_count++; | 4763 break_count++; |
4758 if (!sent_eval) { | 4764 if (!sent_eval) { |
4759 sent_eval = true; | 4765 sent_eval = true; |
4760 | 4766 |
4761 const int kBufferSize = 1000; | 4767 const int kBufferSize = 1000; |
4762 uint16_t buffer[kBufferSize]; | 4768 uint16_t buffer[kBufferSize]; |
4763 const char* eval_command = | 4769 const char* eval_command = |
4764 "{\"seq\":0," | 4770 "{\"seq\":0," |
4765 "\"type\":\"request\"," | 4771 "\"type\":\"request\"," |
4766 "\"command\":\"evaluate\"," | 4772 "\"command\":\"evaluate\"," |
4767 "arguments:{\"expression\":\"debugger;\"," | 4773 "arguments:{\"expression\":\"debugger;\"," |
4768 "\"global\":true,\"disable_break\":false}}"; | 4774 "\"global\":true,\"disable_break\":false}}"; |
4769 | 4775 |
4770 // Send evaluate command. | 4776 // Send evaluate command. |
4771 v8::Debug::SendCommand(buffer, AsciiToUtf16(eval_command, buffer)); | 4777 v8::Debug::SendCommand(buffer, AsciiToUtf16(eval_command, buffer)); |
4772 return; | 4778 return; |
4773 } else { | 4779 } else { |
4774 // It's a break event caused by the evaluation request above. | 4780 // It's a break event caused by the evaluation request above. |
4775 SendContinueCommand(); | 4781 SendContinueCommand(); |
| 4782 continue_command_send_count++; |
4776 } | 4783 } |
4777 } else if (message.IsResponse()) { | 4784 } else if (message.IsResponse() && continue_command_send_count < 2) { |
4778 // Response to the evaluation request. We're still on the breakpoint so | 4785 // Response to the evaluation request. We're still on the breakpoint so |
4779 // send continue. | 4786 // send continue. |
4780 SendContinueCommand(); | 4787 SendContinueCommand(); |
| 4788 continue_command_send_count++; |
4781 } | 4789 } |
4782 } | 4790 } |
4783 | 4791 |
4784 | 4792 |
4785 // Tests that context returned for break event is correct when the event occurs | 4793 // Tests that context returned for break event is correct when the event occurs |
4786 // in 'evaluate' debugger request. | 4794 // in 'evaluate' debugger request. |
4787 TEST(NestedBreakEventContextData) { | 4795 TEST(NestedBreakEventContextData) { |
4788 v8::HandleScope scope; | 4796 v8::HandleScope scope; |
| 4797 break_count = 0; |
| 4798 message_handler_hit_count = 0; |
4789 v8::Debug::SetMessageHandler2(DebugEvalContextCheckMessageHandler); | 4799 v8::Debug::SetMessageHandler2(DebugEvalContextCheckMessageHandler); |
4790 | 4800 |
4791 ExecuteScriptForContextCheck(); | 4801 ExecuteScriptForContextCheck(); |
4792 | 4802 |
4793 // One time compile event and two times break event. | 4803 // One time compile event and two times break event. |
4794 CHECK_GT(message_handler_hit_count, 3); | 4804 CHECK_GT(message_handler_hit_count, 3); |
4795 | 4805 |
4796 // One break from the source and another from the evaluate request. | 4806 // One break from the source and another from the evaluate request. |
4797 CHECK_EQ(break_count, 2); | 4807 CHECK_EQ(break_count, 2); |
| 4808 v8::Debug::SetMessageHandler2(NULL); |
| 4809 CheckDebuggerUnloaded(); |
4798 } | 4810 } |
4799 | 4811 |
4800 | 4812 |
4801 // Debug event listener which counts the script collected events. | 4813 // Debug event listener which counts the script collected events. |
4802 int script_collected_count = 0; | 4814 int script_collected_count = 0; |
4803 static void DebugEventScriptCollectedEvent(v8::DebugEvent event, | 4815 static void DebugEventScriptCollectedEvent(v8::DebugEvent event, |
4804 v8::Handle<v8::Object> exec_state, | 4816 v8::Handle<v8::Object> exec_state, |
4805 v8::Handle<v8::Object> event_data, | 4817 v8::Handle<v8::Object> event_data, |
4806 v8::Handle<v8::Value> data) { | 4818 v8::Handle<v8::Value> data) { |
4807 // Count the number of breaks. | 4819 // Count the number of breaks. |
4808 if (event == v8::ScriptCollected) { | 4820 if (event == v8::ScriptCollected) { |
4809 script_collected_count++; | 4821 script_collected_count++; |
4810 } | 4822 } |
4811 } | 4823 } |
4812 | 4824 |
4813 | 4825 |
4814 // Test that scripts collected are reported through the debug event listener. | 4826 // Test that scripts collected are reported through the debug event listener. |
4815 TEST(ScriptCollectedEvent) { | 4827 TEST(ScriptCollectedEvent) { |
4816 break_point_hit_count = 0; | 4828 break_point_hit_count = 0; |
| 4829 script_collected_count = 0; |
4817 v8::HandleScope scope; | 4830 v8::HandleScope scope; |
4818 DebugLocalContext env; | 4831 DebugLocalContext env; |
4819 | 4832 |
4820 // Request the loaded scripts to initialize the debugger script cache. | 4833 // Request the loaded scripts to initialize the debugger script cache. |
4821 Debug::GetLoadedScripts(); | 4834 Debug::GetLoadedScripts(); |
4822 | 4835 |
4823 // Do garbage collection to ensure that only the script in this test will be | 4836 // Do garbage collection to ensure that only the script in this test will be |
4824 // collected afterwards. | 4837 // collected afterwards. |
4825 Heap::CollectAllGarbage(); | 4838 Heap::CollectAllGarbage(); |
4826 | 4839 |
(...skipping 24 matching lines...) Expand all Loading... |
4851 script_collected_message_count++; | 4864 script_collected_message_count++; |
4852 v8::Handle<v8::Context> context = message.GetEventContext(); | 4865 v8::Handle<v8::Context> context = message.GetEventContext(); |
4853 CHECK(context.IsEmpty()); | 4866 CHECK(context.IsEmpty()); |
4854 } | 4867 } |
4855 } | 4868 } |
4856 | 4869 |
4857 | 4870 |
4858 // Test that GetEventContext doesn't fail and return empty handle for | 4871 // Test that GetEventContext doesn't fail and return empty handle for |
4859 // ScriptCollected events. | 4872 // ScriptCollected events. |
4860 TEST(ScriptCollectedEventContext) { | 4873 TEST(ScriptCollectedEventContext) { |
4861 break_point_hit_count = 0; | 4874 script_collected_message_count = 0; |
4862 v8::HandleScope scope; | 4875 v8::HandleScope scope; |
4863 | 4876 |
4864 { // Scope for the DebugLocalContext. | 4877 { // Scope for the DebugLocalContext. |
4865 DebugLocalContext env; | 4878 DebugLocalContext env; |
4866 | 4879 |
4867 // Request the loaded scripts to initialize the debugger script cache. | 4880 // Request the loaded scripts to initialize the debugger script cache. |
4868 Debug::GetLoadedScripts(); | 4881 Debug::GetLoadedScripts(); |
4869 | 4882 |
4870 // Do garbage collection to ensure that only the script in this test will be | 4883 // Do garbage collection to ensure that only the script in this test will be |
4871 // collected afterwards. | 4884 // collected afterwards. |
4872 Heap::CollectAllGarbage(); | 4885 Heap::CollectAllGarbage(); |
4873 | 4886 |
4874 script_collected_count = 0; | |
4875 v8::Debug::SetMessageHandler2(ScriptCollectedMessageHandler); | 4887 v8::Debug::SetMessageHandler2(ScriptCollectedMessageHandler); |
4876 { | 4888 { |
4877 v8::Script::Compile(v8::String::New("eval('a=1')"))->Run(); | 4889 v8::Script::Compile(v8::String::New("eval('a=1')"))->Run(); |
4878 v8::Script::Compile(v8::String::New("eval('a=2')"))->Run(); | 4890 v8::Script::Compile(v8::String::New("eval('a=2')"))->Run(); |
4879 } | 4891 } |
4880 } | 4892 } |
4881 | 4893 |
4882 // Do garbage collection to collect the script above which is no longer | 4894 // Do garbage collection to collect the script above which is no longer |
4883 // referenced. | 4895 // referenced. |
4884 Heap::CollectAllGarbage(); | 4896 Heap::CollectAllGarbage(); |
4885 | 4897 |
4886 CHECK_EQ(2, script_collected_message_count); | 4898 CHECK_EQ(2, script_collected_message_count); |
4887 | 4899 |
4888 v8::Debug::SetMessageHandler2(NULL); | 4900 v8::Debug::SetMessageHandler2(NULL); |
4889 } | 4901 } |
| 4902 |
| 4903 |
| 4904 // Debug event listener which counts the after compile events. |
| 4905 int after_compile_message_count = 0; |
| 4906 static void AfterCompileMessageHandler(const v8::Debug::Message& message) { |
| 4907 // Count the number of scripts collected. |
| 4908 if (message.IsEvent()) { |
| 4909 if (message.GetEvent() == v8::AfterCompile) { |
| 4910 after_compile_message_count++; |
| 4911 } else if (message.GetEvent() == v8::Break) { |
| 4912 SendContinueCommand(); |
| 4913 } |
| 4914 } |
| 4915 } |
| 4916 |
| 4917 |
| 4918 // Tests that after compile event is sent as many times as there are scripts |
| 4919 // compiled. |
| 4920 TEST(AfterCompileMessageWhenMessageHandlerIsReset) { |
| 4921 v8::HandleScope scope; |
| 4922 DebugLocalContext env; |
| 4923 after_compile_message_count = 0; |
| 4924 const char* script = "var a=1"; |
| 4925 |
| 4926 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 4927 v8::Script::Compile(v8::String::New(script))->Run(); |
| 4928 v8::Debug::SetMessageHandler2(NULL); |
| 4929 |
| 4930 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 4931 v8::Debug::DebugBreak(); |
| 4932 v8::Script::Compile(v8::String::New(script))->Run(); |
| 4933 |
| 4934 // Setting listener to NULL should cause debugger unload. |
| 4935 v8::Debug::SetMessageHandler2(NULL); |
| 4936 CheckDebuggerUnloaded(); |
| 4937 |
| 4938 // Compilation cache should be disabled when debugger is active. |
| 4939 CHECK_EQ(2, after_compile_message_count); |
| 4940 } |
| 4941 |
| 4942 |
| 4943 // Tests that break event is sent when message handler is reset. |
| 4944 TEST(BreakMessageWhenMessageHandlerIsReset) { |
| 4945 v8::HandleScope scope; |
| 4946 DebugLocalContext env; |
| 4947 after_compile_message_count = 0; |
| 4948 const char* script = "function f() {};"; |
| 4949 |
| 4950 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 4951 v8::Script::Compile(v8::String::New(script))->Run(); |
| 4952 v8::Debug::SetMessageHandler2(NULL); |
| 4953 |
| 4954 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 4955 v8::Debug::DebugBreak(); |
| 4956 v8::Local<v8::Function> f = |
| 4957 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 4958 f->Call(env->Global(), 0, NULL); |
| 4959 |
| 4960 // Setting message handler to NULL should cause debugger unload. |
| 4961 v8::Debug::SetMessageHandler2(NULL); |
| 4962 CheckDebuggerUnloaded(); |
| 4963 |
| 4964 // Compilation cache should be disabled when debugger is active. |
| 4965 CHECK_EQ(1, after_compile_message_count); |
| 4966 } |
| 4967 |
| 4968 |
| 4969 static int exception_event_count = 0; |
| 4970 static void ExceptionMessageHandler(const v8::Debug::Message& message) { |
| 4971 if (message.IsEvent() && message.GetEvent() == v8::Exception) { |
| 4972 exception_event_count++; |
| 4973 SendContinueCommand(); |
| 4974 } |
| 4975 } |
| 4976 |
| 4977 |
| 4978 // Tests that exception event is sent when message handler is reset. |
| 4979 TEST(ExceptionMessageWhenMessageHandlerIsReset) { |
| 4980 v8::HandleScope scope; |
| 4981 DebugLocalContext env; |
| 4982 exception_event_count = 0; |
| 4983 const char* script = "function f() {throw new Error()};"; |
| 4984 |
| 4985 v8::Debug::SetMessageHandler2(AfterCompileMessageHandler); |
| 4986 v8::Script::Compile(v8::String::New(script))->Run(); |
| 4987 v8::Debug::SetMessageHandler2(NULL); |
| 4988 |
| 4989 v8::Debug::SetMessageHandler2(ExceptionMessageHandler); |
| 4990 v8::Local<v8::Function> f = |
| 4991 v8::Local<v8::Function>::Cast(env->Global()->Get(v8::String::New("f"))); |
| 4992 f->Call(env->Global(), 0, NULL); |
| 4993 |
| 4994 // Setting message handler to NULL should cause debugger unload. |
| 4995 v8::Debug::SetMessageHandler2(NULL); |
| 4996 CheckDebuggerUnloaded(); |
| 4997 |
| 4998 CHECK_EQ(1, exception_event_count); |
| 4999 } |
OLD | NEW |