2009-08-19 15:14:11 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "cctest.h"
|
|
|
|
|
|
|
|
|
|
|
|
v8::internal::Semaphore* semaphore = NULL;
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void Signal(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-08-19 15:14:11 +00:00
|
|
|
semaphore->Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void TerminateCurrentThread(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::V8::TerminateExecution();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void Fail(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-08-19 15:14:11 +00:00
|
|
|
CHECK(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void Loop(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Handle<v8::String> source =
|
|
|
|
v8::String::New("try { doloop(); fail(); } catch(e) { fail(); }");
|
2010-03-04 12:13:04 +00:00
|
|
|
v8::Handle<v8::Value> result = v8::Script::Compile(source)->Run();
|
|
|
|
CHECK(result.IsEmpty());
|
|
|
|
CHECK(v8::V8::IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DoLoop(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::TryCatch try_catch;
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Script::Compile(v8::String::New("function f() {"
|
|
|
|
" var term = true;"
|
|
|
|
" try {"
|
|
|
|
" while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
" }"
|
|
|
|
" fail();"
|
|
|
|
" } catch(e) {"
|
|
|
|
" fail();"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
"f()"))->Run();
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(try_catch.Exception()->IsNull());
|
|
|
|
CHECK(try_catch.Message().IsEmpty());
|
|
|
|
CHECK(!try_catch.CanContinue());
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(v8::V8::IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DoLoopNoCall(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-11-24 12:23:29 +00:00
|
|
|
v8::TryCatch try_catch;
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-11-24 12:23:29 +00:00
|
|
|
v8::Script::Compile(v8::String::New("var term = true;"
|
|
|
|
"while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
"}"))->Run();
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(try_catch.Exception()->IsNull());
|
|
|
|
CHECK(try_catch.Message().IsEmpty());
|
|
|
|
CHECK(!try_catch.CanContinue());
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(v8::V8::IsExecutionTerminating());
|
2009-11-24 12:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> CreateGlobalTemplate(
|
2013-06-20 12:28:27 +00:00
|
|
|
v8::FunctionCallback terminate,
|
|
|
|
v8::FunctionCallback doloop) {
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
|
|
|
|
global->Set(v8::String::New("terminate"),
|
|
|
|
v8::FunctionTemplate::New(terminate));
|
|
|
|
global->Set(v8::String::New("fail"), v8::FunctionTemplate::New(Fail));
|
|
|
|
global->Set(v8::String::New("loop"), v8::FunctionTemplate::New(Loop));
|
2009-11-24 12:23:29 +00:00
|
|
|
global->Set(v8::String::New("doloop"), v8::FunctionTemplate::New(doloop));
|
2009-08-19 15:14:11 +00:00
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test that a single thread of JavaScript execution can terminate
|
|
|
|
// itself.
|
|
|
|
TEST(TerminateOnlyV8ThreadFromThreadItself) {
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(v8::Isolate::GetCurrent());
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global =
|
2009-11-24 12:23:29 +00:00
|
|
|
CreateGlobalTemplate(TerminateCurrentThread, DoLoop);
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
|
|
|
v8::Context::New(v8::Isolate::GetCurrent(), NULL, global);
|
2009-11-24 12:23:29 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-11-24 12:23:29 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
|
|
|
v8::Handle<v8::String> source =
|
|
|
|
v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
|
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
// Test that we can run the code again after thread termination.
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-11-24 12:23:29 +00:00
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test that a single thread of JavaScript execution can terminate
|
|
|
|
// itself in a loop that performs no calls.
|
|
|
|
TEST(TerminateOnlyV8ThreadFromThreadItselfNoLoop) {
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(v8::Isolate::GetCurrent());
|
2009-11-24 12:23:29 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global =
|
|
|
|
CreateGlobalTemplate(TerminateCurrentThread, DoLoopNoCall);
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
|
|
|
v8::Context::New(v8::Isolate::GetCurrent(), NULL, global);
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
|
|
|
v8::Handle<v8::String> source =
|
|
|
|
v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
|
|
|
|
v8::Script::Compile(source)->Run();
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
// Test that we can run the code again after thread termination.
|
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class TerminatorThread : public v8::internal::Thread {
|
2011-03-18 20:35:07 +00:00
|
|
|
public:
|
2011-06-10 09:36:35 +00:00
|
|
|
explicit TerminatorThread(i::Isolate* isolate)
|
2011-06-10 09:54:04 +00:00
|
|
|
: Thread("TerminatorThread"),
|
|
|
|
isolate_(reinterpret_cast<v8::Isolate*>(isolate)) { }
|
2009-08-19 15:14:11 +00:00
|
|
|
void Run() {
|
|
|
|
semaphore->Wait();
|
2011-06-10 09:54:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating(isolate_));
|
|
|
|
v8::V8::TerminateExecution(isolate_);
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
2011-06-10 09:54:04 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
v8::Isolate* isolate_;
|
2009-08-19 15:14:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Test that a single thread of JavaScript execution can be terminated
|
|
|
|
// from the side by another thread.
|
|
|
|
TEST(TerminateOnlyV8ThreadFromOtherThread) {
|
2013-09-02 12:26:06 +00:00
|
|
|
semaphore = new v8::internal::Semaphore(0);
|
2011-06-10 09:36:35 +00:00
|
|
|
TerminatorThread thread(i::Isolate::Current());
|
2009-08-19 15:14:11 +00:00
|
|
|
thread.Start();
|
|
|
|
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(v8::Isolate::GetCurrent());
|
2009-11-24 12:23:29 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal, DoLoop);
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
|
|
|
v8::Context::New(v8::Isolate::GetCurrent(), NULL, global);
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
|
|
|
v8::Handle<v8::String> source =
|
|
|
|
v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
|
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
|
|
|
|
thread.Join();
|
|
|
|
delete semaphore;
|
|
|
|
semaphore = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class LoopingThread : public v8::internal::Thread {
|
|
|
|
public:
|
2011-06-10 09:54:04 +00:00
|
|
|
LoopingThread() : Thread("LoopingThread") { }
|
2009-08-19 15:14:11 +00:00
|
|
|
void Run() {
|
2013-01-18 07:20:17 +00:00
|
|
|
v8::Locker locker(CcTest::default_isolate());
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(CcTest::default_isolate());
|
2009-08-19 15:14:11 +00:00
|
|
|
v8_thread_id_ = v8::V8::GetCurrentThreadId();
|
2009-11-24 12:23:29 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global =
|
|
|
|
CreateGlobalTemplate(Signal, DoLoop);
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
|
|
|
v8::Context::New(v8::Isolate::GetCurrent(), NULL, global);
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-08-19 15:14:11 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
|
|
|
v8::Handle<v8::String> source =
|
|
|
|
v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
|
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetV8ThreadId() { return v8_thread_id_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int v8_thread_id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-05-05 18:55:31 +00:00
|
|
|
// Test that multiple threads using default isolate can be terminated
|
|
|
|
// from another thread when using Lockers and preemption.
|
|
|
|
TEST(TerminateMultipleV8ThreadsDefaultIsolate) {
|
2009-08-19 15:14:11 +00:00
|
|
|
{
|
2013-01-18 07:20:17 +00:00
|
|
|
v8::Locker locker(CcTest::default_isolate());
|
2009-08-19 15:14:11 +00:00
|
|
|
v8::V8::Initialize();
|
|
|
|
v8::Locker::StartPreemption(1);
|
2013-09-02 12:26:06 +00:00
|
|
|
semaphore = new v8::internal::Semaphore(0);
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
2011-05-05 18:55:31 +00:00
|
|
|
const int kThreads = 2;
|
|
|
|
i::List<LoopingThread*> threads(kThreads);
|
|
|
|
for (int i = 0; i < kThreads; i++) {
|
2011-06-10 09:54:04 +00:00
|
|
|
threads.Add(new LoopingThread());
|
2011-05-05 18:55:31 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < kThreads; i++) {
|
|
|
|
threads[i]->Start();
|
|
|
|
}
|
|
|
|
// Wait until all threads have signaled the semaphore.
|
|
|
|
for (int i = 0; i < kThreads; i++) {
|
|
|
|
semaphore->Wait();
|
|
|
|
}
|
2009-08-19 15:14:11 +00:00
|
|
|
{
|
2013-01-18 07:20:17 +00:00
|
|
|
v8::Locker locker(CcTest::default_isolate());
|
2011-05-05 18:55:31 +00:00
|
|
|
for (int i = 0; i < kThreads; i++) {
|
|
|
|
v8::V8::TerminateExecution(threads[i]->GetV8ThreadId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < kThreads; i++) {
|
|
|
|
threads[i]->Join();
|
|
|
|
delete threads[i];
|
2009-08-19 15:14:11 +00:00
|
|
|
}
|
2012-04-03 10:15:12 +00:00
|
|
|
{
|
2013-01-18 07:20:17 +00:00
|
|
|
v8::Locker locker(CcTest::default_isolate());
|
2012-04-03 10:15:12 +00:00
|
|
|
v8::Locker::StopPreemption();
|
|
|
|
}
|
2009-08-19 15:14:11 +00:00
|
|
|
|
|
|
|
delete semaphore;
|
|
|
|
semaphore = NULL;
|
|
|
|
}
|
2009-09-01 13:55:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
int call_count = 0;
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void TerminateOrReturnObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-09-01 13:55:45 +00:00
|
|
|
if (++call_count == 10) {
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-09-01 13:55:45 +00:00
|
|
|
v8::V8::TerminateExecution();
|
2013-06-20 12:28:27 +00:00
|
|
|
return;
|
2009-09-01 13:55:45 +00:00
|
|
|
}
|
|
|
|
v8::Local<v8::Object> result = v8::Object::New();
|
|
|
|
result->Set(v8::String::New("x"), v8::Integer::New(42));
|
2013-06-20 12:28:27 +00:00
|
|
|
args.GetReturnValue().Set(result);
|
2009-09-01 13:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void LoopGetProperty(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2009-09-01 13:55:45 +00:00
|
|
|
v8::TryCatch try_catch;
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-09-01 13:55:45 +00:00
|
|
|
v8::Script::Compile(v8::String::New("function f() {"
|
|
|
|
" try {"
|
|
|
|
" while(true) {"
|
|
|
|
" terminate_or_return_object().x;"
|
|
|
|
" }"
|
|
|
|
" fail();"
|
|
|
|
" } catch(e) {"
|
|
|
|
" fail();"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
"f()"))->Run();
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(try_catch.Exception()->IsNull());
|
|
|
|
CHECK(try_catch.Message().IsEmpty());
|
|
|
|
CHECK(!try_catch.CanContinue());
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(v8::V8::IsExecutionTerminating());
|
2009-09-01 13:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test that we correctly handle termination exceptions if they are
|
|
|
|
// triggered by the creation of error objects in connection with ICs.
|
|
|
|
TEST(TerminateLoadICException) {
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(v8::Isolate::GetCurrent());
|
2009-09-01 13:55:45 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
|
|
|
|
global->Set(v8::String::New("terminate_or_return_object"),
|
|
|
|
v8::FunctionTemplate::New(TerminateOrReturnObject));
|
|
|
|
global->Set(v8::String::New("fail"), v8::FunctionTemplate::New(Fail));
|
|
|
|
global->Set(v8::String::New("loop"),
|
|
|
|
v8::FunctionTemplate::New(LoopGetProperty));
|
|
|
|
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
|
|
|
v8::Context::New(v8::Isolate::GetCurrent(), NULL, global);
|
2009-09-01 13:55:45 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-09-01 13:55:45 +00:00
|
|
|
// Run a loop that will be infinite if thread termination does not work.
|
|
|
|
v8::Handle<v8::String> source =
|
|
|
|
v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
|
|
|
|
call_count = 0;
|
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
// Test that we can run the code again after thread termination.
|
2010-03-04 12:13:04 +00:00
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
2009-09-01 13:55:45 +00:00
|
|
|
call_count = 0;
|
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
}
|
2010-05-21 20:52:19 +00:00
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void ReenterAfterTermination(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2010-05-21 20:52:19 +00:00
|
|
|
v8::TryCatch try_catch;
|
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
|
|
|
v8::Script::Compile(v8::String::New("function f() {"
|
|
|
|
" var term = true;"
|
|
|
|
" try {"
|
|
|
|
" while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
" }"
|
|
|
|
" fail();"
|
|
|
|
" } catch(e) {"
|
|
|
|
" fail();"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
"f()"))->Run();
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(try_catch.Exception()->IsNull());
|
|
|
|
CHECK(try_catch.Message().IsEmpty());
|
|
|
|
CHECK(!try_catch.CanContinue());
|
|
|
|
CHECK(v8::V8::IsExecutionTerminating());
|
|
|
|
v8::Script::Compile(v8::String::New("function f() { fail(); } f()"))->Run();
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2010-05-21 20:52:19 +00:00
|
|
|
// Test that reentry into V8 while the termination exception is still pending
|
|
|
|
// (has not yet unwound the 0-level JS frame) does not crash.
|
|
|
|
TEST(TerminateAndReenterFromThreadItself) {
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(v8::Isolate::GetCurrent());
|
2010-05-21 20:52:19 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> global =
|
|
|
|
CreateGlobalTemplate(TerminateCurrentThread, ReenterAfterTermination);
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
|
|
|
v8::Context::New(v8::Isolate::GetCurrent(), NULL, global);
|
2010-05-21 20:52:19 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
|
|
|
v8::Handle<v8::String> source =
|
|
|
|
v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
|
|
|
|
v8::Script::Compile(source)->Run();
|
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
|
|
|
// Check we can run JS again after termination.
|
|
|
|
CHECK(v8::Script::Compile(v8::String::New("function f() { return true; }"
|
|
|
|
"f()"))->Run()->IsTrue());
|
|
|
|
}
|
2013-04-22 15:01:45 +00:00
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-06-20 12:28:27 +00:00
|
|
|
void DoLoopCancelTerminate(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-04-22 15:01:45 +00:00
|
|
|
v8::TryCatch try_catch;
|
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
|
|
|
v8::Script::Compile(v8::String::New("var term = true;"
|
|
|
|
"while(true) {"
|
|
|
|
" if (term) terminate();"
|
|
|
|
" term = false;"
|
|
|
|
"}"
|
|
|
|
"fail();"))->Run();
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(try_catch.Exception()->IsNull());
|
|
|
|
CHECK(try_catch.Message().IsEmpty());
|
|
|
|
CHECK(!try_catch.CanContinue());
|
|
|
|
CHECK(v8::V8::IsExecutionTerminating());
|
|
|
|
CHECK(try_catch.HasTerminated());
|
|
|
|
v8::V8::CancelTerminateExecution(v8::Isolate::GetCurrent());
|
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2013-04-22 15:01:45 +00:00
|
|
|
// Test that a single thread of JavaScript execution can terminate
|
|
|
|
// itself and then resume execution.
|
|
|
|
TEST(TerminateCancelTerminateFromThreadItself) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
v8::Handle<v8::ObjectTemplate> global =
|
|
|
|
CreateGlobalTemplate(TerminateCurrentThread, DoLoopCancelTerminate);
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> context =
|
|
|
|
v8::Context::New(v8::Isolate::GetCurrent(), NULL, global);
|
2013-04-22 15:01:45 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
CHECK(!v8::V8::IsExecutionTerminating());
|
|
|
|
v8::Handle<v8::String> source =
|
|
|
|
v8::String::New("try { doloop(); } catch(e) { fail(); } 'completed';");
|
|
|
|
// Check that execution completed with correct return value.
|
|
|
|
CHECK(v8::Script::Compile(source)->Run()->Equals(v8_str("completed")));
|
|
|
|
}
|
|
|
|
|