| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 "}" | 75 "}" |
| 76 "f()"))->Run(); | 76 "f()"))->Run(); |
| 77 CHECK(try_catch.HasCaught()); | 77 CHECK(try_catch.HasCaught()); |
| 78 CHECK(try_catch.Exception()->IsNull()); | 78 CHECK(try_catch.Exception()->IsNull()); |
| 79 CHECK(try_catch.Message().IsEmpty()); | 79 CHECK(try_catch.Message().IsEmpty()); |
| 80 CHECK(!try_catch.CanContinue()); | 80 CHECK(!try_catch.CanContinue()); |
| 81 return v8::Undefined(); | 81 return v8::Undefined(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 | 84 |
| 85 v8::Handle<v8::Value> DoLoopNoCall(const v8::Arguments& args) { |
| 86 v8::TryCatch try_catch; |
| 87 v8::Script::Compile(v8::String::New("var term = true;" |
| 88 "while(true) {" |
| 89 " if (term) terminate();" |
| 90 " term = false;" |
| 91 "}"))->Run(); |
| 92 CHECK(try_catch.HasCaught()); |
| 93 CHECK(try_catch.Exception()->IsNull()); |
| 94 CHECK(try_catch.Message().IsEmpty()); |
| 95 CHECK(!try_catch.CanContinue()); |
| 96 return v8::Undefined(); |
| 97 } |
| 98 |
| 99 |
| 85 v8::Handle<v8::ObjectTemplate> CreateGlobalTemplate( | 100 v8::Handle<v8::ObjectTemplate> CreateGlobalTemplate( |
| 86 v8::InvocationCallback terminate) { | 101 v8::InvocationCallback terminate, |
| 102 v8::InvocationCallback doloop) { |
| 87 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 103 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 88 global->Set(v8::String::New("terminate"), | 104 global->Set(v8::String::New("terminate"), |
| 89 v8::FunctionTemplate::New(terminate)); | 105 v8::FunctionTemplate::New(terminate)); |
| 90 global->Set(v8::String::New("fail"), v8::FunctionTemplate::New(Fail)); | 106 global->Set(v8::String::New("fail"), v8::FunctionTemplate::New(Fail)); |
| 91 global->Set(v8::String::New("loop"), v8::FunctionTemplate::New(Loop)); | 107 global->Set(v8::String::New("loop"), v8::FunctionTemplate::New(Loop)); |
| 92 global->Set(v8::String::New("doloop"), v8::FunctionTemplate::New(DoLoop)); | 108 global->Set(v8::String::New("doloop"), v8::FunctionTemplate::New(doloop)); |
| 93 return global; | 109 return global; |
| 94 } | 110 } |
| 95 | 111 |
| 96 | 112 |
| 97 // Test that a single thread of JavaScript execution can terminate | 113 // Test that a single thread of JavaScript execution can terminate |
| 98 // itself. | 114 // itself. |
| 99 TEST(TerminateOnlyV8ThreadFromThreadItself) { | 115 TEST(TerminateOnlyV8ThreadFromThreadItself) { |
| 100 v8::HandleScope scope; | 116 v8::HandleScope scope; |
| 101 v8::Handle<v8::ObjectTemplate> global = | 117 v8::Handle<v8::ObjectTemplate> global = |
| 102 CreateGlobalTemplate(TerminateCurrentThread); | 118 CreateGlobalTemplate(TerminateCurrentThread, DoLoop); |
| 103 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); | 119 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 104 v8::Context::Scope context_scope(context); | 120 v8::Context::Scope context_scope(context); |
| 105 // Run a loop that will be infinite if thread termination does not work. | 121 // Run a loop that will be infinite if thread termination does not work. |
| 106 v8::Handle<v8::String> source = | 122 v8::Handle<v8::String> source = |
| 107 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); | 123 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 108 v8::Script::Compile(source)->Run(); | 124 v8::Script::Compile(source)->Run(); |
| 109 // Test that we can run the code again after thread termination. | 125 // Test that we can run the code again after thread termination. |
| 110 v8::Script::Compile(source)->Run(); | 126 v8::Script::Compile(source)->Run(); |
| 111 context.Dispose(); | 127 context.Dispose(); |
| 112 } | 128 } |
| 113 | 129 |
| 114 | 130 |
| 131 // Test that a single thread of JavaScript execution can terminate |
| 132 // itself in a loop that performs no calls. |
| 133 TEST(TerminateOnlyV8ThreadFromThreadItselfNoLoop) { |
| 134 v8::HandleScope scope; |
| 135 v8::Handle<v8::ObjectTemplate> global = |
| 136 CreateGlobalTemplate(TerminateCurrentThread, DoLoopNoCall); |
| 137 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 138 v8::Context::Scope context_scope(context); |
| 139 // Run a loop that will be infinite if thread termination does not work. |
| 140 v8::Handle<v8::String> source = |
| 141 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 142 v8::Script::Compile(source)->Run(); |
| 143 // Test that we can run the code again after thread termination. |
| 144 v8::Script::Compile(source)->Run(); |
| 145 context.Dispose(); |
| 146 } |
| 147 |
| 148 |
| 115 class TerminatorThread : public v8::internal::Thread { | 149 class TerminatorThread : public v8::internal::Thread { |
| 116 void Run() { | 150 void Run() { |
| 117 semaphore->Wait(); | 151 semaphore->Wait(); |
| 118 v8::V8::TerminateExecution(); | 152 v8::V8::TerminateExecution(); |
| 119 } | 153 } |
| 120 }; | 154 }; |
| 121 | 155 |
| 122 | 156 |
| 123 // Test that a single thread of JavaScript execution can be terminated | 157 // Test that a single thread of JavaScript execution can be terminated |
| 124 // from the side by another thread. | 158 // from the side by another thread. |
| 125 TEST(TerminateOnlyV8ThreadFromOtherThread) { | 159 TEST(TerminateOnlyV8ThreadFromOtherThread) { |
| 126 semaphore = v8::internal::OS::CreateSemaphore(0); | 160 semaphore = v8::internal::OS::CreateSemaphore(0); |
| 127 TerminatorThread thread; | 161 TerminatorThread thread; |
| 128 thread.Start(); | 162 thread.Start(); |
| 129 | 163 |
| 130 v8::HandleScope scope; | 164 v8::HandleScope scope; |
| 131 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal); | 165 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal, DoLoop); |
| 132 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); | 166 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 133 v8::Context::Scope context_scope(context); | 167 v8::Context::Scope context_scope(context); |
| 134 // Run a loop that will be infinite if thread termination does not work. | 168 // Run a loop that will be infinite if thread termination does not work. |
| 135 v8::Handle<v8::String> source = | 169 v8::Handle<v8::String> source = |
| 136 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); | 170 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 137 v8::Script::Compile(source)->Run(); | 171 v8::Script::Compile(source)->Run(); |
| 138 | 172 |
| 139 thread.Join(); | 173 thread.Join(); |
| 140 delete semaphore; | 174 delete semaphore; |
| 141 semaphore = NULL; | 175 semaphore = NULL; |
| 142 context.Dispose(); | 176 context.Dispose(); |
| 143 } | 177 } |
| 144 | 178 |
| 145 | 179 |
| 146 class LoopingThread : public v8::internal::Thread { | 180 class LoopingThread : public v8::internal::Thread { |
| 147 public: | 181 public: |
| 148 void Run() { | 182 void Run() { |
| 149 v8::Locker locker; | 183 v8::Locker locker; |
| 150 v8::HandleScope scope; | 184 v8::HandleScope scope; |
| 151 v8_thread_id_ = v8::V8::GetCurrentThreadId(); | 185 v8_thread_id_ = v8::V8::GetCurrentThreadId(); |
| 152 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal); | 186 v8::Handle<v8::ObjectTemplate> global = |
| 187 CreateGlobalTemplate(Signal, DoLoop); |
| 153 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); | 188 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global); |
| 154 v8::Context::Scope context_scope(context); | 189 v8::Context::Scope context_scope(context); |
| 155 // Run a loop that will be infinite if thread termination does not work. | 190 // Run a loop that will be infinite if thread termination does not work. |
| 156 v8::Handle<v8::String> source = | 191 v8::Handle<v8::String> source = |
| 157 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); | 192 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 158 v8::Script::Compile(source)->Run(); | 193 v8::Script::Compile(source)->Run(); |
| 159 context.Dispose(); | 194 context.Dispose(); |
| 160 } | 195 } |
| 161 | 196 |
| 162 int GetV8ThreadId() { return v8_thread_id_; } | 197 int GetV8ThreadId() { return v8_thread_id_; } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 // Run a loop that will be infinite if thread termination does not work. | 281 // Run a loop that will be infinite if thread termination does not work. |
| 247 v8::Handle<v8::String> source = | 282 v8::Handle<v8::String> source = |
| 248 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); | 283 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }"); |
| 249 call_count = 0; | 284 call_count = 0; |
| 250 v8::Script::Compile(source)->Run(); | 285 v8::Script::Compile(source)->Run(); |
| 251 // Test that we can run the code again after thread termination. | 286 // Test that we can run the code again after thread termination. |
| 252 call_count = 0; | 287 call_count = 0; |
| 253 v8::Script::Compile(source)->Run(); | 288 v8::Script::Compile(source)->Run(); |
| 254 context.Dispose(); | 289 context.Dispose(); |
| 255 } | 290 } |
| OLD | NEW |