Chromium Code Reviews| 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 |
| 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 // TODO(mythria): Remove this define after this flag is turned on globally | |
| 29 #define V8_IMMINENT_DEPRECATION_WARNINGS | |
| 30 | |
| 28 #include "src/v8.h" | 31 #include "src/v8.h" |
| 29 #include "test/cctest/cctest.h" | 32 #include "test/cctest/cctest.h" |
| 30 | 33 |
| 31 #include "src/base/platform/platform.h" | 34 #include "src/base/platform/platform.h" |
| 32 | 35 |
| 33 | 36 |
| 34 v8::base::Semaphore* semaphore = NULL; | 37 v8::base::Semaphore* semaphore = NULL; |
| 35 | 38 |
| 36 | 39 |
| 37 void Signal(const v8::FunctionCallbackInfo<v8::Value>& args) { | 40 void Signal(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 38 semaphore->Signal(); | 41 semaphore->Signal(); |
| 39 } | 42 } |
| 40 | 43 |
| 41 | 44 |
| 42 void TerminateCurrentThread(const v8::FunctionCallbackInfo<v8::Value>& args) { | 45 void TerminateCurrentThread(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 43 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate())); | 46 CHECK(!args.GetIsolate()->IsExecutionTerminating()); |
| 44 v8::V8::TerminateExecution(args.GetIsolate()); | 47 args.GetIsolate()->TerminateExecution(); |
| 45 } | 48 } |
| 46 | 49 |
| 47 | 50 |
| 48 void Fail(const v8::FunctionCallbackInfo<v8::Value>& args) { | 51 void Fail(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 49 CHECK(false); | 52 CHECK(false); |
| 50 } | 53 } |
| 51 | 54 |
| 52 | 55 |
| 53 void Loop(const v8::FunctionCallbackInfo<v8::Value>& args) { | 56 void Loop(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 54 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate())); | 57 CHECK(!args.GetIsolate()->IsExecutionTerminating()); |
| 55 v8::Handle<v8::String> source = v8::String::NewFromUtf8( | 58 v8::Local<v8::String> source = |
| 56 args.GetIsolate(), "try { doloop(); fail(); } catch(e) { fail(); }"); | 59 v8::String::NewFromUtf8(args.GetIsolate(), |
| 57 v8::Handle<v8::Value> result = v8::Script::Compile(source)->Run(); | 60 "try { doloop(); fail(); } catch(e) { fail(); }", |
| 61 v8::NewStringType::kNormal) | |
| 62 .ToLocalChecked(); | |
| 63 v8::MaybeLocal<v8::Value> result = | |
| 64 v8::Script::Compile(args.GetIsolate()->GetCurrentContext(), source) | |
| 65 .ToLocalChecked() | |
| 66 ->Run(args.GetIsolate()->GetCurrentContext()); | |
| 58 CHECK(result.IsEmpty()); | 67 CHECK(result.IsEmpty()); |
| 59 CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate())); | 68 CHECK(args.GetIsolate()->IsExecutionTerminating()); |
| 60 } | 69 } |
| 61 | 70 |
| 62 | 71 |
| 63 void DoLoop(const v8::FunctionCallbackInfo<v8::Value>& args) { | 72 void DoLoop(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 64 v8::TryCatch try_catch(args.GetIsolate()); | 73 v8::TryCatch try_catch(args.GetIsolate()); |
| 65 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate())); | 74 CHECK(!args.GetIsolate()->IsExecutionTerminating()); |
| 66 v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(), | 75 v8::MaybeLocal<v8::Value> result = |
| 67 "function f() {" | 76 v8::Script::Compile(args.GetIsolate()->GetCurrentContext(), |
| 68 " var term = true;" | 77 v8::String::NewFromUtf8(args.GetIsolate(), |
| 69 " try {" | 78 "function f() {" |
| 70 " while(true) {" | 79 " var term = true;" |
| 71 " if (term) terminate();" | 80 " try {" |
| 72 " term = false;" | 81 " while(true) {" |
| 73 " }" | 82 " if (term) terminate();" |
| 74 " fail();" | 83 " term = false;" |
| 75 " } catch(e) {" | 84 " }" |
| 76 " fail();" | 85 " fail();" |
| 77 " }" | 86 " } catch(e) {" |
| 78 "}" | 87 " fail();" |
| 79 "f()"))->Run(); | 88 " }" |
| 89 "}" | |
| 90 "f()", | |
| 91 v8::NewStringType::kNormal) | |
| 92 .ToLocalChecked()) | |
| 93 .ToLocalChecked() | |
| 94 ->Run(args.GetIsolate()->GetCurrentContext()); | |
| 95 CHECK(result.IsEmpty()); | |
|
mythria
2015/09/14 15:43:59
Added a check to see if the result is empty here i
rmcilroy
2015/09/15 10:29:47
This seems correct to me. The test seems like it i
| |
| 80 CHECK(try_catch.HasCaught()); | 96 CHECK(try_catch.HasCaught()); |
| 81 CHECK(try_catch.Exception()->IsNull()); | 97 CHECK(try_catch.Exception()->IsNull()); |
| 82 CHECK(try_catch.Message().IsEmpty()); | 98 CHECK(try_catch.Message().IsEmpty()); |
| 83 CHECK(!try_catch.CanContinue()); | 99 CHECK(!try_catch.CanContinue()); |
| 84 CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate())); | 100 CHECK(args.GetIsolate()->IsExecutionTerminating()); |
| 85 } | 101 } |
| 86 | 102 |
| 87 | 103 |
| 88 void DoLoopNoCall(const v8::FunctionCallbackInfo<v8::Value>& args) { | 104 void DoLoopNoCall(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 89 v8::TryCatch try_catch(args.GetIsolate()); | 105 v8::TryCatch try_catch(args.GetIsolate()); |
| 90 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate())); | 106 CHECK(!args.GetIsolate()->IsExecutionTerminating()); |
| 91 v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(), | 107 v8::MaybeLocal<v8::Value> result = |
| 92 "var term = true;" | 108 v8::Script::Compile(args.GetIsolate()->GetCurrentContext(), |
| 93 "while(true) {" | 109 v8::String::NewFromUtf8(args.GetIsolate(), |
| 94 " if (term) terminate();" | 110 "var term = true;" |
| 95 " term = false;" | 111 "while(true) {" |
| 96 "}"))->Run(); | 112 " if (term) terminate();" |
| 113 " term = false;" | |
| 114 "}", | |
| 115 v8::NewStringType::kNormal) | |
| 116 .ToLocalChecked()) | |
| 117 .ToLocalChecked() | |
| 118 ->Run(args.GetIsolate()->GetCurrentContext()); | |
| 119 CHECK(result.IsEmpty()); | |
| 97 CHECK(try_catch.HasCaught()); | 120 CHECK(try_catch.HasCaught()); |
| 98 CHECK(try_catch.Exception()->IsNull()); | 121 CHECK(try_catch.Exception()->IsNull()); |
| 99 CHECK(try_catch.Message().IsEmpty()); | 122 CHECK(try_catch.Message().IsEmpty()); |
| 100 CHECK(!try_catch.CanContinue()); | 123 CHECK(!try_catch.CanContinue()); |
| 101 CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate())); | 124 CHECK(args.GetIsolate()->IsExecutionTerminating()); |
| 102 } | 125 } |
| 103 | 126 |
| 104 | 127 |
| 105 v8::Handle<v8::ObjectTemplate> CreateGlobalTemplate( | 128 v8::Local<v8::ObjectTemplate> CreateGlobalTemplate( |
| 106 v8::Isolate* isolate, | 129 v8::Isolate* isolate, v8::FunctionCallback terminate, |
| 107 v8::FunctionCallback terminate, | |
| 108 v8::FunctionCallback doloop) { | 130 v8::FunctionCallback doloop) { |
| 109 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); | 131 v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); |
| 110 global->Set(v8::String::NewFromUtf8(isolate, "terminate"), | 132 global->Set( |
| 111 v8::FunctionTemplate::New(isolate, terminate)); | 133 v8::String::NewFromUtf8(isolate, "terminate", v8::NewStringType::kNormal) |
| 112 global->Set(v8::String::NewFromUtf8(isolate, "fail"), | 134 .ToLocalChecked(), |
| 113 v8::FunctionTemplate::New(isolate, Fail)); | 135 v8::FunctionTemplate::New(isolate, terminate)); |
| 114 global->Set(v8::String::NewFromUtf8(isolate, "loop"), | 136 global->Set( |
| 115 v8::FunctionTemplate::New(isolate, Loop)); | 137 v8::String::NewFromUtf8(isolate, "fail", v8::NewStringType::kNormal) |
| 116 global->Set(v8::String::NewFromUtf8(isolate, "doloop"), | 138 .ToLocalChecked(), |
| 117 v8::FunctionTemplate::New(isolate, doloop)); | 139 v8::FunctionTemplate::New(isolate, Fail)); |
| 140 global->Set( | |
| 141 v8::String::NewFromUtf8(isolate, "loop", v8::NewStringType::kNormal) | |
| 142 .ToLocalChecked(), | |
| 143 v8::FunctionTemplate::New(isolate, Loop)); | |
| 144 global->Set( | |
| 145 v8::String::NewFromUtf8(isolate, "doloop", v8::NewStringType::kNormal) | |
| 146 .ToLocalChecked(), | |
| 147 v8::FunctionTemplate::New(isolate, doloop)); | |
| 118 return global; | 148 return global; |
| 119 } | 149 } |
| 120 | 150 |
| 121 | 151 |
| 122 // Test that a single thread of JavaScript execution can terminate | 152 // Test that a single thread of JavaScript execution can terminate |
| 123 // itself. | 153 // itself. |
| 124 TEST(TerminateOnlyV8ThreadFromThreadItself) { | 154 TEST(TerminateOnlyV8ThreadFromThreadItself) { |
| 125 v8::HandleScope scope(CcTest::isolate()); | 155 v8::HandleScope scope(CcTest::isolate()); |
| 126 v8::Handle<v8::ObjectTemplate> global = | 156 v8::Local<v8::ObjectTemplate> global = |
| 127 CreateGlobalTemplate(CcTest::isolate(), TerminateCurrentThread, DoLoop); | 157 CreateGlobalTemplate(CcTest::isolate(), TerminateCurrentThread, DoLoop); |
| 128 v8::Handle<v8::Context> context = | 158 v8::Local<v8::Context> context = |
| 129 v8::Context::New(CcTest::isolate(), NULL, global); | 159 v8::Context::New(CcTest::isolate(), NULL, global); |
| 130 v8::Context::Scope context_scope(context); | 160 v8::Context::Scope context_scope(context); |
| 131 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate())); | 161 CHECK(!CcTest::isolate()->IsExecutionTerminating()); |
| 132 // Run a loop that will be infinite if thread termination does not work. | 162 // Run a loop that will be infinite if thread termination does not work. |
| 133 v8::Handle<v8::String> source = v8::String::NewFromUtf8( | 163 v8::Local<v8::String> source = |
| 134 CcTest::isolate(), "try { loop(); fail(); } catch(e) { fail(); }"); | 164 v8::String::NewFromUtf8(CcTest::isolate(), |
| 135 v8::Script::Compile(source)->Run(); | 165 "try { loop(); fail(); } catch(e) { fail(); }", |
| 166 v8::NewStringType::kNormal) | |
| 167 .ToLocalChecked(); | |
| 168 v8::MaybeLocal<v8::Value> result = | |
| 169 v8::Script::Compile(CcTest::isolate()->GetCurrentContext(), source) | |
| 170 .ToLocalChecked() | |
| 171 ->Run(CcTest::isolate()->GetCurrentContext()); | |
| 172 CHECK(result.IsEmpty()); | |
| 136 // Test that we can run the code again after thread termination. | 173 // Test that we can run the code again after thread termination. |
| 137 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate())); | 174 CHECK(!CcTest::isolate()->IsExecutionTerminating()); |
| 138 v8::Script::Compile(source)->Run(); | 175 result = v8::Script::Compile(CcTest::isolate()->GetCurrentContext(), source) |
| 176 .ToLocalChecked() | |
| 177 ->Run(CcTest::isolate()->GetCurrentContext()); | |
|
rmcilroy
2015/09/15 10:29:47
nit - how about having a helper CompileAndRun func
mythria
2015/09/17 11:21:48
Done.
| |
| 178 CHECK(result.IsEmpty()); | |
| 139 } | 179 } |
| 140 | 180 |
| 141 | 181 |
| 142 // Test that a single thread of JavaScript execution can terminate | 182 // Test that a single thread of JavaScript execution can terminate |
| 143 // itself in a loop that performs no calls. | 183 // itself in a loop that performs no calls. |
| 144 TEST(TerminateOnlyV8ThreadFromThreadItselfNoLoop) { | 184 TEST(TerminateOnlyV8ThreadFromThreadItselfNoLoop) { |
| 145 v8::HandleScope scope(CcTest::isolate()); | 185 v8::HandleScope scope(CcTest::isolate()); |
| 146 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate( | 186 v8::Local<v8::ObjectTemplate> global = CreateGlobalTemplate( |
| 147 CcTest::isolate(), TerminateCurrentThread, DoLoopNoCall); | 187 CcTest::isolate(), TerminateCurrentThread, DoLoopNoCall); |
| 148 v8::Handle<v8::Context> context = | 188 v8::Local<v8::Context> context = |
| 149 v8::Context::New(CcTest::isolate(), NULL, global); | 189 v8::Context::New(CcTest::isolate(), NULL, global); |
| 150 v8::Context::Scope context_scope(context); | 190 v8::Context::Scope context_scope(context); |
| 151 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate())); | 191 CHECK(!CcTest::isolate()->IsExecutionTerminating()); |
| 152 // Run a loop that will be infinite if thread termination does not work. | 192 // Run a loop that will be infinite if thread termination does not work. |
| 153 v8::Handle<v8::String> source = v8::String::NewFromUtf8( | 193 v8::Local<v8::String> source = |
| 154 CcTest::isolate(), "try { loop(); fail(); } catch(e) { fail(); }"); | 194 v8::String::NewFromUtf8(CcTest::isolate(), |
| 155 v8::Script::Compile(source)->Run(); | 195 "try { loop(); fail(); } catch(e) { fail(); }", |
| 156 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate())); | 196 v8::NewStringType::kNormal) |
| 197 .ToLocalChecked(); | |
| 198 v8::MaybeLocal<v8::Value> result = | |
| 199 v8::Script::Compile(CcTest::isolate()->GetCurrentContext(), source) | |
| 200 .ToLocalChecked() | |
| 201 ->Run(CcTest::isolate()->GetCurrentContext()); | |
| 202 CHECK(result.IsEmpty()); | |
| 203 CHECK(!CcTest::isolate()->IsExecutionTerminating()); | |
| 157 // Test that we can run the code again after thread termination. | 204 // Test that we can run the code again after thread termination. |
| 158 v8::Script::Compile(source)->Run(); | 205 result = v8::Script::Compile(CcTest::isolate()->GetCurrentContext(), source) |
| 206 .ToLocalChecked() | |
| 207 ->Run(CcTest::isolate()->GetCurrentContext()); | |
| 208 CHECK(result.IsEmpty()); | |
| 159 } | 209 } |
| 160 | 210 |
| 161 | 211 |
| 162 class TerminatorThread : public v8::base::Thread { | 212 class TerminatorThread : public v8::base::Thread { |
| 163 public: | 213 public: |
| 164 explicit TerminatorThread(i::Isolate* isolate) | 214 explicit TerminatorThread(i::Isolate* isolate) |
| 165 : Thread(Options("TerminatorThread")), | 215 : Thread(Options("TerminatorThread")), |
| 166 isolate_(reinterpret_cast<v8::Isolate*>(isolate)) {} | 216 isolate_(reinterpret_cast<v8::Isolate*>(isolate)) {} |
| 167 void Run() { | 217 void Run() { |
| 168 semaphore->Wait(); | 218 semaphore->Wait(); |
| 169 CHECK(!v8::V8::IsExecutionTerminating(isolate_)); | 219 CHECK(!isolate_->IsExecutionTerminating()); |
| 170 v8::V8::TerminateExecution(isolate_); | 220 isolate_->TerminateExecution(); |
| 171 } | 221 } |
| 172 | 222 |
| 173 private: | 223 private: |
| 174 v8::Isolate* isolate_; | 224 v8::Isolate* isolate_; |
| 175 }; | 225 }; |
| 176 | 226 |
| 177 | 227 |
| 178 // Test that a single thread of JavaScript execution can be terminated | 228 // Test that a single thread of JavaScript execution can be terminated |
| 179 // from the side by another thread. | 229 // from the side by another thread. |
| 180 TEST(TerminateOnlyV8ThreadFromOtherThread) { | 230 TEST(TerminateOnlyV8ThreadFromOtherThread) { |
| 181 semaphore = new v8::base::Semaphore(0); | 231 semaphore = new v8::base::Semaphore(0); |
| 182 TerminatorThread thread(CcTest::i_isolate()); | 232 TerminatorThread thread(CcTest::i_isolate()); |
| 183 thread.Start(); | 233 thread.Start(); |
| 184 | 234 |
| 185 v8::HandleScope scope(CcTest::isolate()); | 235 v8::HandleScope scope(CcTest::isolate()); |
| 186 v8::Handle<v8::ObjectTemplate> global = | 236 v8::Local<v8::ObjectTemplate> global = |
| 187 CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop); | 237 CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop); |
| 188 v8::Handle<v8::Context> context = | 238 v8::Local<v8::Context> context = |
| 189 v8::Context::New(CcTest::isolate(), NULL, global); | 239 v8::Context::New(CcTest::isolate(), NULL, global); |
| 190 v8::Context::Scope context_scope(context); | 240 v8::Context::Scope context_scope(context); |
| 191 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate())); | 241 CHECK(!CcTest::isolate()->IsExecutionTerminating()); |
| 192 // Run a loop that will be infinite if thread termination does not work. | 242 // Run a loop that will be infinite if thread termination does not work. |
| 193 v8::Handle<v8::String> source = v8::String::NewFromUtf8( | 243 v8::Local<v8::String> source = |
| 194 CcTest::isolate(), "try { loop(); fail(); } catch(e) { fail(); }"); | 244 v8::String::NewFromUtf8(CcTest::isolate(), |
| 195 v8::Script::Compile(source)->Run(); | 245 "try { loop(); fail(); } catch(e) { fail(); }", |
| 196 | 246 v8::NewStringType::kNormal) |
| 247 .ToLocalChecked(); | |
| 248 v8::MaybeLocal<v8::Value> result = | |
| 249 v8::Script::Compile(CcTest::isolate()->GetCurrentContext(), source) | |
| 250 .ToLocalChecked() | |
| 251 ->Run(CcTest::isolate()->GetCurrentContext()); | |
| 252 CHECK(result.IsEmpty()); | |
| 197 thread.Join(); | 253 thread.Join(); |
| 198 delete semaphore; | 254 delete semaphore; |
| 199 semaphore = NULL; | 255 semaphore = NULL; |
| 200 } | 256 } |
| 201 | 257 |
| 202 | 258 |
| 203 int call_count = 0; | 259 int call_count = 0; |
| 204 | 260 |
| 205 | 261 |
| 206 void TerminateOrReturnObject(const v8::FunctionCallbackInfo<v8::Value>& args) { | 262 void TerminateOrReturnObject(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 207 if (++call_count == 10) { | 263 if (++call_count == 10) { |
| 208 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate())); | 264 CHECK(!args.GetIsolate()->IsExecutionTerminating()); |
| 209 v8::V8::TerminateExecution(args.GetIsolate()); | 265 args.GetIsolate()->TerminateExecution(); |
| 210 return; | 266 return; |
| 211 } | 267 } |
| 212 v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate()); | 268 v8::Local<v8::Object> result = v8::Object::New(args.GetIsolate()); |
| 213 result->Set(v8::String::NewFromUtf8(args.GetIsolate(), "x"), | 269 CHECK(result->Set(args.GetIsolate()->GetCurrentContext(), |
| 214 v8::Integer::New(args.GetIsolate(), 42)); | 270 v8::String::NewFromUtf8(args.GetIsolate(), "x", |
| 271 v8::NewStringType::kNormal) | |
| 272 .ToLocalChecked(), | |
| 273 v8::Integer::New(args.GetIsolate(), 42)) | |
| 274 .FromJust()); | |
| 215 args.GetReturnValue().Set(result); | 275 args.GetReturnValue().Set(result); |
| 216 } | 276 } |
| 217 | 277 |
| 218 | 278 |
| 219 void LoopGetProperty(const v8::FunctionCallbackInfo<v8::Value>& args) { | 279 void LoopGetProperty(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 220 v8::TryCatch try_catch(args.GetIsolate()); | 280 v8::TryCatch try_catch(args.GetIsolate()); |
| 221 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate())); | 281 CHECK(!args.GetIsolate()->IsExecutionTerminating()); |
| 222 v8::Script::Compile( | 282 v8::MaybeLocal<v8::Value> result = |
| 223 v8::String::NewFromUtf8(args.GetIsolate(), | 283 v8::Script::Compile(args.GetIsolate()->GetCurrentContext(), |
| 284 v8::String::NewFromUtf8( | |
| 285 args.GetIsolate(), | |
| 224 "function f() {" | 286 "function f() {" |
| 225 " try {" | 287 " try {" |
| 226 " while(true) {" | 288 " while(true) {" |
| 227 " terminate_or_return_object().x;" | 289 " terminate_or_return_object().x;" |
| 228 " }" | 290 " }" |
| 229 " fail();" | 291 " fail();" |
| 230 " } catch(e) {" | 292 " } catch(e) {" |
| 231 " (function() {})();" // trigger stack check. | 293 " (function() {})();" // trigger stack check. |
| 232 " fail();" | 294 " fail();" |
| 233 " }" | 295 " }" |
| 234 "}" | 296 "}" |
| 235 "f()"))->Run(); | 297 "f()", |
| 298 v8::NewStringType::kNormal) | |
| 299 .ToLocalChecked()) | |
| 300 .ToLocalChecked() | |
| 301 ->Run(args.GetIsolate()->GetCurrentContext()); | |
| 302 CHECK(result.IsEmpty()); | |
| 236 CHECK(try_catch.HasCaught()); | 303 CHECK(try_catch.HasCaught()); |
| 237 CHECK(try_catch.Exception()->IsNull()); | 304 CHECK(try_catch.Exception()->IsNull()); |
| 238 CHECK(try_catch.Message().IsEmpty()); | 305 CHECK(try_catch.Message().IsEmpty()); |
| 239 CHECK(!try_catch.CanContinue()); | 306 CHECK(!try_catch.CanContinue()); |
| 240 CHECK(v8::V8::IsExecutionTerminating(args.GetIsolate())); | 307 CHECK(args.GetIsolate()->IsExecutionTerminating()); |
| 241 } | 308 } |
| 242 | 309 |
| 243 | 310 |
| 244 // Test that we correctly handle termination exceptions if they are | 311 // Test that we correctly handle termination exceptions if they are |
| 245 // triggered by the creation of error objects in connection with ICs. | 312 // triggered by the creation of error objects in connection with ICs. |
| 246 TEST(TerminateLoadICException) { | 313 TEST(TerminateLoadICException) { |
| 247 v8::Isolate* isolate = CcTest::isolate(); | 314 v8::Isolate* isolate = CcTest::isolate(); |
| 248 v8::HandleScope scope(isolate); | 315 v8::HandleScope scope(isolate); |
| 249 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); | 316 v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); |
| 317 global->Set(v8::String::NewFromUtf8(isolate, "terminate_or_return_object", | |
| 318 v8::NewStringType::kNormal) | |
| 319 .ToLocalChecked(), | |
|
rmcilroy
2015/09/15 10:29:47
nit - you could also have a helper for:
v8::Local
mythria
2015/09/17 11:21:48
There was already a helper function. I did not not
mythria
2015/09/17 11:21:48
Done.
| |
| 320 v8::FunctionTemplate::New(isolate, TerminateOrReturnObject)); | |
| 250 global->Set( | 321 global->Set( |
| 251 v8::String::NewFromUtf8(isolate, "terminate_or_return_object"), | 322 v8::String::NewFromUtf8(isolate, "fail", v8::NewStringType::kNormal) |
| 252 v8::FunctionTemplate::New(isolate, TerminateOrReturnObject)); | 323 .ToLocalChecked(), |
| 253 global->Set(v8::String::NewFromUtf8(isolate, "fail"), | 324 v8::FunctionTemplate::New(isolate, Fail)); |
| 254 v8::FunctionTemplate::New(isolate, Fail)); | 325 global->Set( |
| 255 global->Set(v8::String::NewFromUtf8(isolate, "loop"), | 326 v8::String::NewFromUtf8(isolate, "loop", v8::NewStringType::kNormal) |
| 256 v8::FunctionTemplate::New(isolate, LoopGetProperty)); | 327 .ToLocalChecked(), |
| 328 v8::FunctionTemplate::New(isolate, LoopGetProperty)); | |
| 257 | 329 |
| 258 v8::Handle<v8::Context> context = | 330 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
| 259 v8::Context::New(isolate, NULL, global); | |
| 260 v8::Context::Scope context_scope(context); | 331 v8::Context::Scope context_scope(context); |
| 261 CHECK(!v8::V8::IsExecutionTerminating(isolate)); | 332 CHECK(!isolate->IsExecutionTerminating()); |
| 262 // Run a loop that will be infinite if thread termination does not work. | 333 // Run a loop that will be infinite if thread termination does not work. |
| 263 v8::Handle<v8::String> source = v8::String::NewFromUtf8( | 334 v8::Local<v8::String> source = |
| 264 isolate, "try { loop(); fail(); } catch(e) { fail(); }"); | 335 v8::String::NewFromUtf8(isolate, |
| 336 "try { loop(); fail(); } catch(e) { fail(); }", | |
| 337 v8::NewStringType::kNormal) | |
| 338 .ToLocalChecked(); | |
| 265 call_count = 0; | 339 call_count = 0; |
| 266 v8::Script::Compile(source)->Run(); | 340 v8::MaybeLocal<v8::Value> result = |
| 341 v8::Script::Compile(isolate->GetCurrentContext(), source) | |
| 342 .ToLocalChecked() | |
| 343 ->Run(isolate->GetCurrentContext()); | |
| 344 CHECK(result.IsEmpty()); | |
| 267 // Test that we can run the code again after thread termination. | 345 // Test that we can run the code again after thread termination. |
| 268 CHECK(!v8::V8::IsExecutionTerminating(isolate)); | 346 CHECK(!isolate->IsExecutionTerminating()); |
| 269 call_count = 0; | 347 call_count = 0; |
| 270 v8::Script::Compile(source)->Run(); | 348 result = v8::Script::Compile(isolate->GetCurrentContext(), source) |
| 349 .ToLocalChecked() | |
| 350 ->Run(isolate->GetCurrentContext()); | |
| 351 CHECK(result.IsEmpty()); | |
| 271 } | 352 } |
| 272 | 353 |
| 273 | 354 |
| 274 v8::Persistent<v8::String> reenter_script_1; | 355 v8::Persistent<v8::String> reenter_script_1; |
| 275 v8::Persistent<v8::String> reenter_script_2; | 356 v8::Persistent<v8::String> reenter_script_2; |
| 276 | 357 |
| 277 void ReenterAfterTermination(const v8::FunctionCallbackInfo<v8::Value>& args) { | 358 void ReenterAfterTermination(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 278 v8::TryCatch try_catch(args.GetIsolate()); | 359 v8::TryCatch try_catch(args.GetIsolate()); |
| 279 v8::Isolate* isolate = args.GetIsolate(); | 360 v8::Isolate* isolate = args.GetIsolate(); |
| 280 CHECK(!v8::V8::IsExecutionTerminating(isolate)); | 361 CHECK(!isolate->IsExecutionTerminating()); |
| 281 v8::Local<v8::String> script = | 362 v8::Local<v8::String> script = |
| 282 v8::Local<v8::String>::New(isolate, reenter_script_1); | 363 v8::Local<v8::String>::New(isolate, reenter_script_1); |
| 283 v8::Script::Compile(script)->Run(); | 364 v8::MaybeLocal<v8::Value> result = |
| 365 v8::Script::Compile(isolate->GetCurrentContext(), script) | |
| 366 .ToLocalChecked() | |
| 367 ->Run(isolate->GetCurrentContext()); | |
| 368 CHECK(result.IsEmpty()); | |
| 284 CHECK(try_catch.HasCaught()); | 369 CHECK(try_catch.HasCaught()); |
| 285 CHECK(try_catch.Exception()->IsNull()); | 370 CHECK(try_catch.Exception()->IsNull()); |
| 286 CHECK(try_catch.Message().IsEmpty()); | 371 CHECK(try_catch.Message().IsEmpty()); |
| 287 CHECK(!try_catch.CanContinue()); | 372 CHECK(!try_catch.CanContinue()); |
| 288 CHECK(v8::V8::IsExecutionTerminating(isolate)); | 373 CHECK(isolate->IsExecutionTerminating()); |
| 289 script = v8::Local<v8::String>::New(isolate, reenter_script_2); | 374 script = v8::Local<v8::String>::New(isolate, reenter_script_2); |
| 290 v8::Script::Compile(script)->Run(); | 375 v8::MaybeLocal<v8::Script> compiled_script = |
| 376 v8::Script::Compile(isolate->GetCurrentContext(), script); | |
| 377 CHECK(compiled_script.IsEmpty()); | |
| 291 } | 378 } |
| 292 | 379 |
| 293 | 380 |
| 294 // Test that reentry into V8 while the termination exception is still pending | 381 // Test that reentry into V8 while the termination exception is still pending |
| 295 // (has not yet unwound the 0-level JS frame) does not crash. | 382 // (has not yet unwound the 0-level JS frame) does not crash. |
| 296 TEST(TerminateAndReenterFromThreadItself) { | 383 TEST(TerminateAndReenterFromThreadItself) { |
| 297 v8::Isolate* isolate = CcTest::isolate(); | 384 v8::Isolate* isolate = CcTest::isolate(); |
| 298 v8::HandleScope scope(isolate); | 385 v8::HandleScope scope(isolate); |
| 299 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate( | 386 v8::Local<v8::ObjectTemplate> global = CreateGlobalTemplate( |
| 300 isolate, TerminateCurrentThread, ReenterAfterTermination); | 387 isolate, TerminateCurrentThread, ReenterAfterTermination); |
| 301 v8::Handle<v8::Context> context = | 388 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
| 302 v8::Context::New(isolate, NULL, global); | |
| 303 v8::Context::Scope context_scope(context); | 389 v8::Context::Scope context_scope(context); |
| 304 CHECK(!v8::V8::IsExecutionTerminating()); | 390 CHECK(!v8::Isolate::GetCurrent()->IsExecutionTerminating()); |
| 305 // Create script strings upfront as it won't work when terminating. | 391 // Create script strings upfront as it won't work when terminating. |
| 306 reenter_script_1.Reset(isolate, v8_str( | 392 reenter_script_1.Reset(isolate, v8_str( |
| 307 "function f() {" | 393 "function f() {" |
| 308 " var term = true;" | 394 " var term = true;" |
| 309 " try {" | 395 " try {" |
| 310 " while(true) {" | 396 " while(true) {" |
| 311 " if (term) terminate();" | 397 " if (term) terminate();" |
| 312 " term = false;" | 398 " term = false;" |
| 313 " }" | 399 " }" |
| 314 " fail();" | 400 " fail();" |
| 315 " } catch(e) {" | 401 " } catch(e) {" |
| 316 " fail();" | 402 " fail();" |
| 317 " }" | 403 " }" |
| 318 "}" | 404 "}" |
| 319 "f()")); | 405 "f()")); |
| 320 reenter_script_2.Reset(isolate, v8_str("function f() { fail(); } f()")); | 406 reenter_script_2.Reset(isolate, v8_str("function f() { fail(); } f()")); |
| 321 CompileRun("try { loop(); fail(); } catch(e) { fail(); }"); | 407 CompileRun("try { loop(); fail(); } catch(e) { fail(); }"); |
| 322 CHECK(!v8::V8::IsExecutionTerminating(isolate)); | 408 CHECK(!isolate->IsExecutionTerminating()); |
| 323 // Check we can run JS again after termination. | 409 // Check we can run JS again after termination. |
| 324 CHECK(CompileRun("function f() { return true; } f()")->IsTrue()); | 410 CHECK(CompileRun("function f() { return true; } f()")->IsTrue()); |
| 325 reenter_script_1.Reset(); | 411 reenter_script_1.Reset(); |
| 326 reenter_script_2.Reset(); | 412 reenter_script_2.Reset(); |
| 327 } | 413 } |
| 328 | 414 |
| 329 | 415 |
| 330 void DoLoopCancelTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) { | 416 void DoLoopCancelTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 331 v8::TryCatch try_catch(args.GetIsolate()); | 417 v8::TryCatch try_catch(args.GetIsolate()); |
| 332 CHECK(!v8::V8::IsExecutionTerminating()); | 418 CHECK(!v8::Isolate::GetCurrent()->IsExecutionTerminating()); |
| 333 v8::Script::Compile(v8::String::NewFromUtf8(args.GetIsolate(), | 419 v8::MaybeLocal<v8::Value> result = |
| 334 "var term = true;" | 420 v8::Script::Compile(args.GetIsolate()->GetCurrentContext(), |
| 335 "while(true) {" | 421 v8::String::NewFromUtf8(args.GetIsolate(), |
| 336 " if (term) terminate();" | 422 "var term = true;" |
| 337 " term = false;" | 423 "while(true) {" |
| 338 "}" | 424 " if (term) terminate();" |
| 339 "fail();"))->Run(); | 425 " term = false;" |
| 426 "}" | |
| 427 "fail();", | |
| 428 v8::NewStringType::kNormal) | |
| 429 .ToLocalChecked()) | |
| 430 .ToLocalChecked() | |
| 431 ->Run(args.GetIsolate()->GetCurrentContext()); | |
| 432 CHECK(result.IsEmpty()); | |
| 340 CHECK(try_catch.HasCaught()); | 433 CHECK(try_catch.HasCaught()); |
| 341 CHECK(try_catch.Exception()->IsNull()); | 434 CHECK(try_catch.Exception()->IsNull()); |
| 342 CHECK(try_catch.Message().IsEmpty()); | 435 CHECK(try_catch.Message().IsEmpty()); |
| 343 CHECK(!try_catch.CanContinue()); | 436 CHECK(!try_catch.CanContinue()); |
| 344 CHECK(v8::V8::IsExecutionTerminating()); | 437 CHECK(v8::Isolate::GetCurrent()->IsExecutionTerminating()); |
| 345 CHECK(try_catch.HasTerminated()); | 438 CHECK(try_catch.HasTerminated()); |
| 346 v8::V8::CancelTerminateExecution(CcTest::isolate()); | 439 CcTest::isolate()->CancelTerminateExecution(); |
| 347 CHECK(!v8::V8::IsExecutionTerminating()); | 440 CHECK(!v8::Isolate::GetCurrent()->IsExecutionTerminating()); |
| 348 } | 441 } |
| 349 | 442 |
| 350 | 443 |
| 351 // Test that a single thread of JavaScript execution can terminate | 444 // Test that a single thread of JavaScript execution can terminate |
| 352 // itself and then resume execution. | 445 // itself and then resume execution. |
| 353 TEST(TerminateCancelTerminateFromThreadItself) { | 446 TEST(TerminateCancelTerminateFromThreadItself) { |
| 354 v8::Isolate* isolate = CcTest::isolate(); | 447 v8::Isolate* isolate = CcTest::isolate(); |
| 355 v8::HandleScope scope(isolate); | 448 v8::HandleScope scope(isolate); |
| 356 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate( | 449 v8::Local<v8::ObjectTemplate> global = CreateGlobalTemplate( |
| 357 isolate, TerminateCurrentThread, DoLoopCancelTerminate); | 450 isolate, TerminateCurrentThread, DoLoopCancelTerminate); |
| 358 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global); | 451 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
| 359 v8::Context::Scope context_scope(context); | 452 v8::Context::Scope context_scope(context); |
| 360 CHECK(!v8::V8::IsExecutionTerminating(CcTest::isolate())); | 453 CHECK(!CcTest::isolate()->IsExecutionTerminating()); |
| 361 v8::Handle<v8::String> source = v8::String::NewFromUtf8( | 454 v8::Local<v8::String> source = |
| 362 isolate, "try { doloop(); } catch(e) { fail(); } 'completed';"); | 455 v8::String::NewFromUtf8( |
| 456 isolate, "try { doloop(); } catch(e) { fail(); } 'completed';", | |
| 457 v8::NewStringType::kNormal) | |
| 458 .ToLocalChecked(); | |
| 363 // Check that execution completed with correct return value. | 459 // Check that execution completed with correct return value. |
| 364 CHECK(v8::Script::Compile(source)->Run()->Equals(v8_str("completed"))); | 460 CHECK(v8::Script::Compile(isolate->GetCurrentContext(), source) |
| 461 .ToLocalChecked() | |
| 462 ->Run(isolate->GetCurrentContext()) | |
| 463 .ToLocalChecked() | |
| 464 ->Equals(isolate->GetCurrentContext(), v8_str("completed")) | |
| 465 .FromJust()); | |
| 365 } | 466 } |
| 366 | 467 |
| 367 | 468 |
| 368 void MicrotaskShouldNotRun(const v8::FunctionCallbackInfo<v8::Value>& info) { | 469 void MicrotaskShouldNotRun(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 369 CHECK(false); | 470 CHECK(false); |
| 370 } | 471 } |
| 371 | 472 |
| 372 | 473 |
| 373 void MicrotaskLoopForever(const v8::FunctionCallbackInfo<v8::Value>& info) { | 474 void MicrotaskLoopForever(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 374 v8::Isolate* isolate = info.GetIsolate(); | 475 v8::Isolate* isolate = info.GetIsolate(); |
| 375 v8::HandleScope scope(isolate); | 476 v8::HandleScope scope(isolate); |
| 376 // Enqueue another should-not-run task to ensure we clean out the queue | 477 // Enqueue another should-not-run task to ensure we clean out the queue |
| 377 // when we terminate. | 478 // when we terminate. |
| 378 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun)); | 479 isolate->EnqueueMicrotask( |
| 480 v8::Function::New(isolate->GetCurrentContext(), MicrotaskShouldNotRun) | |
| 481 .ToLocalChecked()); | |
| 379 CompileRun("terminate(); while (true) { }"); | 482 CompileRun("terminate(); while (true) { }"); |
| 380 CHECK(v8::V8::IsExecutionTerminating()); | 483 CHECK(v8::Isolate::GetCurrent()->IsExecutionTerminating()); |
| 381 } | 484 } |
| 382 | 485 |
| 383 | 486 |
| 384 TEST(TerminateFromOtherThreadWhileMicrotaskRunning) { | 487 TEST(TerminateFromOtherThreadWhileMicrotaskRunning) { |
| 385 semaphore = new v8::base::Semaphore(0); | 488 semaphore = new v8::base::Semaphore(0); |
| 386 TerminatorThread thread(CcTest::i_isolate()); | 489 TerminatorThread thread(CcTest::i_isolate()); |
| 387 thread.Start(); | 490 thread.Start(); |
| 388 | 491 |
| 389 v8::Isolate* isolate = CcTest::isolate(); | 492 v8::Isolate* isolate = CcTest::isolate(); |
| 390 isolate->SetAutorunMicrotasks(false); | 493 isolate->SetAutorunMicrotasks(false); |
| 391 v8::HandleScope scope(isolate); | 494 v8::HandleScope scope(isolate); |
| 392 v8::Handle<v8::ObjectTemplate> global = | 495 v8::Local<v8::ObjectTemplate> global = |
| 393 CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop); | 496 CreateGlobalTemplate(CcTest::isolate(), Signal, DoLoop); |
| 394 v8::Handle<v8::Context> context = | 497 v8::Local<v8::Context> context = |
| 395 v8::Context::New(CcTest::isolate(), NULL, global); | 498 v8::Context::New(CcTest::isolate(), NULL, global); |
| 396 v8::Context::Scope context_scope(context); | 499 v8::Context::Scope context_scope(context); |
| 397 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskLoopForever)); | 500 isolate->EnqueueMicrotask( |
| 501 v8::Function::New(isolate->GetCurrentContext(), MicrotaskLoopForever) | |
| 502 .ToLocalChecked()); | |
| 398 // The second task should never be run because we bail out if we're | 503 // The second task should never be run because we bail out if we're |
| 399 // terminating. | 504 // terminating. |
| 400 isolate->EnqueueMicrotask(v8::Function::New(isolate, MicrotaskShouldNotRun)); | 505 isolate->EnqueueMicrotask( |
| 506 v8::Function::New(isolate->GetCurrentContext(), MicrotaskShouldNotRun) | |
| 507 .ToLocalChecked()); | |
| 401 isolate->RunMicrotasks(); | 508 isolate->RunMicrotasks(); |
| 402 | 509 |
| 403 v8::V8::CancelTerminateExecution(isolate); | 510 isolate->CancelTerminateExecution(); |
| 404 isolate->RunMicrotasks(); // should not run MicrotaskShouldNotRun | 511 isolate->RunMicrotasks(); // should not run MicrotaskShouldNotRun |
| 405 | 512 |
| 406 thread.Join(); | 513 thread.Join(); |
| 407 delete semaphore; | 514 delete semaphore; |
| 408 semaphore = NULL; | 515 semaphore = NULL; |
| 409 } | 516 } |
| 410 | 517 |
| 411 | 518 |
| 412 static int callback_counter = 0; | 519 static int callback_counter = 0; |
| 413 | 520 |
| 414 | 521 |
| 415 static void CounterCallback(v8::Isolate* isolate, void* data) { | 522 static void CounterCallback(v8::Isolate* isolate, void* data) { |
| 416 callback_counter++; | 523 callback_counter++; |
| 417 } | 524 } |
| 418 | 525 |
| 419 | 526 |
| 420 TEST(PostponeTerminateException) { | 527 TEST(PostponeTerminateException) { |
| 421 v8::Isolate* isolate = CcTest::isolate(); | 528 v8::Isolate* isolate = CcTest::isolate(); |
| 422 v8::HandleScope scope(isolate); | 529 v8::HandleScope scope(isolate); |
| 423 v8::Handle<v8::ObjectTemplate> global = | 530 v8::Local<v8::ObjectTemplate> global = |
| 424 CreateGlobalTemplate(CcTest::isolate(), TerminateCurrentThread, DoLoop); | 531 CreateGlobalTemplate(CcTest::isolate(), TerminateCurrentThread, DoLoop); |
| 425 v8::Handle<v8::Context> context = | 532 v8::Local<v8::Context> context = |
| 426 v8::Context::New(CcTest::isolate(), NULL, global); | 533 v8::Context::New(CcTest::isolate(), NULL, global); |
| 427 v8::Context::Scope context_scope(context); | 534 v8::Context::Scope context_scope(context); |
| 428 | 535 |
| 429 v8::TryCatch try_catch(isolate); | 536 v8::TryCatch try_catch(isolate); |
| 430 static const char* terminate_and_loop = | 537 static const char* terminate_and_loop = |
| 431 "terminate(); for (var i = 0; i < 10000; i++);"; | 538 "terminate(); for (var i = 0; i < 10000; i++);"; |
| 432 | 539 |
| 433 { // Postpone terminate execution interrupts. | 540 { // Postpone terminate execution interrupts. |
| 434 i::PostponeInterruptsScope p1(CcTest::i_isolate(), | 541 i::PostponeInterruptsScope p1(CcTest::i_isolate(), |
| 435 i::StackGuard::TERMINATE_EXECUTION) ; | 542 i::StackGuard::TERMINATE_EXECUTION) ; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 461 // Now the previously requested terminate execution interrupt should trigger. | 568 // Now the previously requested terminate execution interrupt should trigger. |
| 462 CompileRun("for (var i = 0; i < 10000; i++);"); | 569 CompileRun("for (var i = 0; i < 10000; i++);"); |
| 463 CHECK(try_catch.HasTerminated()); | 570 CHECK(try_catch.HasTerminated()); |
| 464 CHECK_EQ(2, callback_counter); | 571 CHECK_EQ(2, callback_counter); |
| 465 } | 572 } |
| 466 | 573 |
| 467 | 574 |
| 468 TEST(ErrorObjectAfterTermination) { | 575 TEST(ErrorObjectAfterTermination) { |
| 469 v8::Isolate* isolate = CcTest::isolate(); | 576 v8::Isolate* isolate = CcTest::isolate(); |
| 470 v8::HandleScope scope(isolate); | 577 v8::HandleScope scope(isolate); |
| 471 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); | 578 v8::Local<v8::Context> context = v8::Context::New(CcTest::isolate()); |
| 472 v8::Context::Scope context_scope(context); | 579 v8::Context::Scope context_scope(context); |
| 473 v8::V8::TerminateExecution(isolate); | 580 isolate->TerminateExecution(); |
| 474 v8::Local<v8::Value> error = v8::Exception::Error(v8_str("error")); | 581 v8::Local<v8::Value> error = v8::Exception::Error(v8_str("error")); |
| 475 // TODO(yangguo): crbug/403509. Check for empty handle instead. | 582 // TODO(yangguo): crbug/403509. Check for empty handle instead. |
| 476 CHECK(error->IsUndefined()); | 583 CHECK(error->IsUndefined()); |
| 477 } | 584 } |
| 478 | 585 |
| 479 | 586 |
| 480 void InnerTryCallTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) { | 587 void InnerTryCallTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 481 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate())); | 588 CHECK(!args.GetIsolate()->IsExecutionTerminating()); |
| 482 v8::Handle<v8::Object> global = CcTest::global(); | 589 v8::Local<v8::Object> global = CcTest::global(); |
| 483 v8::Handle<v8::Function> loop = | 590 v8::Local<v8::Function> loop = v8::Local<v8::Function>::Cast( |
| 484 v8::Handle<v8::Function>::Cast(global->Get(v8_str("loop"))); | 591 global->Get(CcTest::isolate()->GetCurrentContext(), v8_str("loop")) |
| 592 .ToLocalChecked()); | |
| 485 i::MaybeHandle<i::Object> result = | 593 i::MaybeHandle<i::Object> result = |
| 486 i::Execution::TryCall(v8::Utils::OpenHandle((*loop)), | 594 i::Execution::TryCall(v8::Utils::OpenHandle((*loop)), |
| 487 v8::Utils::OpenHandle((*global)), 0, NULL, NULL); | 595 v8::Utils::OpenHandle((*global)), 0, NULL, NULL); |
| 488 CHECK(result.is_null()); | 596 CHECK(result.is_null()); |
| 489 // TryCall ignores terminate execution, but rerequests the interrupt. | 597 // TryCall ignores terminate execution, but rerequests the interrupt. |
| 490 CHECK(!v8::V8::IsExecutionTerminating(args.GetIsolate())); | 598 CHECK(!args.GetIsolate()->IsExecutionTerminating()); |
| 491 CHECK(CompileRun("1 + 1;").IsEmpty()); | 599 CHECK(CompileRun("1 + 1;").IsEmpty()); |
| 492 } | 600 } |
| 493 | 601 |
| 494 | 602 |
| 495 TEST(TerminationInInnerTryCall) { | 603 TEST(TerminationInInnerTryCall) { |
| 496 v8::Isolate* isolate = CcTest::isolate(); | 604 v8::Isolate* isolate = CcTest::isolate(); |
| 497 v8::HandleScope scope(isolate); | 605 v8::HandleScope scope(isolate); |
| 498 v8::Handle<v8::ObjectTemplate> global_template = CreateGlobalTemplate( | 606 v8::Local<v8::ObjectTemplate> global_template = CreateGlobalTemplate( |
| 499 CcTest::isolate(), TerminateCurrentThread, DoLoopNoCall); | 607 CcTest::isolate(), TerminateCurrentThread, DoLoopNoCall); |
| 500 global_template->Set( | 608 global_template->Set( |
| 501 v8_str("inner_try_call_terminate"), | 609 v8_str("inner_try_call_terminate"), |
| 502 v8::FunctionTemplate::New(isolate, InnerTryCallTerminate)); | 610 v8::FunctionTemplate::New(isolate, InnerTryCallTerminate)); |
| 503 v8::Handle<v8::Context> context = | 611 v8::Local<v8::Context> context = |
| 504 v8::Context::New(CcTest::isolate(), NULL, global_template); | 612 v8::Context::New(CcTest::isolate(), NULL, global_template); |
| 505 v8::Context::Scope context_scope(context); | 613 v8::Context::Scope context_scope(context); |
| 506 { | 614 { |
| 507 v8::TryCatch try_catch(isolate); | 615 v8::TryCatch try_catch(isolate); |
| 508 CompileRun("inner_try_call_terminate()"); | 616 CompileRun("inner_try_call_terminate()"); |
| 509 CHECK(try_catch.HasTerminated()); | 617 CHECK(try_catch.HasTerminated()); |
| 510 } | 618 } |
| 511 CHECK_EQ(4, CompileRun("2 + 2")->ToInt32()->Int32Value()); | 619 CHECK_EQ(4, CompileRun("2 + 2") |
| 512 CHECK(!v8::V8::IsExecutionTerminating()); | 620 ->ToInt32(v8::Isolate::GetCurrent()->GetCurrentContext()) |
| 621 .ToLocalChecked() | |
| 622 ->Int32Value(v8::Isolate::GetCurrent()->GetCurrentContext()) | |
| 623 .FromJust()); | |
| 624 CHECK(!v8::Isolate::GetCurrent()->IsExecutionTerminating()); | |
| 513 } | 625 } |
| 514 | 626 |
| 515 | 627 |
| 516 TEST(TerminateAndTryCall) { | 628 TEST(TerminateAndTryCall) { |
| 517 i::FLAG_allow_natives_syntax = true; | 629 i::FLAG_allow_natives_syntax = true; |
| 518 v8::Isolate* isolate = CcTest::isolate(); | 630 v8::Isolate* isolate = CcTest::isolate(); |
| 519 v8::HandleScope scope(isolate); | 631 v8::HandleScope scope(isolate); |
| 520 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate( | 632 v8::Local<v8::ObjectTemplate> global = CreateGlobalTemplate( |
| 521 isolate, TerminateCurrentThread, DoLoopCancelTerminate); | 633 isolate, TerminateCurrentThread, DoLoopCancelTerminate); |
| 522 v8::Handle<v8::Context> context = v8::Context::New(isolate, NULL, global); | 634 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
| 523 v8::Context::Scope context_scope(context); | 635 v8::Context::Scope context_scope(context); |
| 524 CHECK(!v8::V8::IsExecutionTerminating(isolate)); | 636 CHECK(!isolate->IsExecutionTerminating()); |
| 525 v8::TryCatch try_catch(isolate); | 637 v8::TryCatch try_catch(isolate); |
| 526 CHECK(!v8::V8::IsExecutionTerminating(isolate)); | 638 CHECK(!isolate->IsExecutionTerminating()); |
| 527 // Terminate execution has been triggered inside TryCall, but re-requested | 639 // Terminate execution has been triggered inside TryCall, but re-requested |
| 528 // to trigger later. | 640 // to trigger later. |
| 529 CHECK(CompileRun("terminate(); reference_error();").IsEmpty()); | 641 CHECK(CompileRun("terminate(); reference_error();").IsEmpty()); |
| 530 CHECK(try_catch.HasCaught()); | 642 CHECK(try_catch.HasCaught()); |
| 531 CHECK(!v8::V8::IsExecutionTerminating(isolate)); | 643 CHECK(!isolate->IsExecutionTerminating()); |
| 532 CHECK(CcTest::global()->Get(v8_str("terminate"))->IsFunction()); | 644 CHECK(CcTest::global() |
| 645 ->Get(isolate->GetCurrentContext(), v8_str("terminate")) | |
| 646 .ToLocalChecked() | |
| 647 ->IsFunction()); | |
| 533 // The first stack check after terminate has been re-requested fails. | 648 // The first stack check after terminate has been re-requested fails. |
| 534 CHECK(CompileRun("1 + 1").IsEmpty()); | 649 CHECK(CompileRun("1 + 1").IsEmpty()); |
| 535 CHECK(!v8::V8::IsExecutionTerminating(isolate)); | 650 CHECK(!isolate->IsExecutionTerminating()); |
| 536 // V8 then recovers. | 651 // V8 then recovers. |
| 537 CHECK_EQ(4, CompileRun("2 + 2")->ToInt32()->Int32Value()); | 652 CHECK_EQ(4, CompileRun("2 + 2") |
| 538 CHECK(!v8::V8::IsExecutionTerminating(isolate)); | 653 ->ToInt32(v8::Isolate::GetCurrent()->GetCurrentContext()) |
| 654 .ToLocalChecked() | |
| 655 ->Int32Value(v8::Isolate::GetCurrent()->GetCurrentContext()) | |
| 656 .FromJust()); | |
| 657 CHECK(!isolate->IsExecutionTerminating()); | |
| 539 } | 658 } |
| OLD | NEW |