Commit 4dd8b111 authored by jochen@chromium.org's avatar jochen@chromium.org

Introduce a microtask suppression scope and move microtask methods to isolate

BUG=369503
R=adamk@chromium.org
LOG=y
TEST=cctest/test-api/SetAutorunMicrotasks

Review URL: https://codereview.chromium.org/263933002

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@21128 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 72a090f3
...@@ -4112,6 +4112,24 @@ class V8_EXPORT Isolate { ...@@ -4112,6 +4112,24 @@ class V8_EXPORT Isolate {
const AllowJavascriptExecutionScope&); const AllowJavascriptExecutionScope&);
}; };
/**
* Do not run microtasks while this scope is active, even if microtasks are
* automatically executed otherwise.
*/
class V8_EXPORT SuppressMicrotaskExecutionScope {
public:
explicit SuppressMicrotaskExecutionScope(Isolate* isolate);
~SuppressMicrotaskExecutionScope();
private:
internal::Isolate* isolate_;
// Prevent copying of Scope objects.
SuppressMicrotaskExecutionScope(const SuppressMicrotaskExecutionScope&);
SuppressMicrotaskExecutionScope& operator=(
const SuppressMicrotaskExecutionScope&);
};
/** /**
* Types of garbage collections that can be requested via * Types of garbage collections that can be requested via
* RequestGarbageCollectionForTesting. * RequestGarbageCollectionForTesting.
...@@ -4361,6 +4379,22 @@ class V8_EXPORT Isolate { ...@@ -4361,6 +4379,22 @@ class V8_EXPORT Isolate {
*/ */
void RemoveCallCompletedCallback(CallCompletedCallback callback); void RemoveCallCompletedCallback(CallCompletedCallback callback);
/**
* Experimental: Runs the Microtask Work Queue until empty
*/
void RunMicrotasks();
/**
* Experimental: Enqueues the callback to the Microtask Work Queue
*/
void EnqueueMicrotask(Handle<Function> microtask);
/**
* Experimental: Controls whether the Microtask Work Queue is automatically
* run when the script call depth decrements to zero.
*/
void SetAutorunMicrotasks(bool autorun);
private: private:
template<class K, class V, class Traits> friend class PersistentValueMap; template<class K, class V, class Traits> friend class PersistentValueMap;
...@@ -4724,17 +4758,23 @@ class V8_EXPORT V8 { ...@@ -4724,17 +4758,23 @@ class V8_EXPORT V8 {
/** /**
* Experimental: Runs the Microtask Work Queue until empty * Experimental: Runs the Microtask Work Queue until empty
*
* Deprecated: Use methods on Isolate instead.
*/ */
static void RunMicrotasks(Isolate* isolate); static void RunMicrotasks(Isolate* isolate);
/** /**
* Experimental: Enqueues the callback to the Microtask Work Queue * Experimental: Enqueues the callback to the Microtask Work Queue
*
* Deprecated: Use methods on Isolate instead.
*/ */
static void EnqueueMicrotask(Isolate* isolate, Handle<Function> microtask); static void EnqueueMicrotask(Isolate* isolate, Handle<Function> microtask);
/** /**
* Experimental: Controls whether the Microtask Work Queue is automatically * Experimental: Controls whether the Microtask Work Queue is automatically
* run when the script call depth decrements to zero. * run when the script call depth decrements to zero.
*
* Deprecated: Use methods on Isolate instead.
*/ */
static void SetAutorunMicrotasks(Isolate *source, bool autorun); static void SetAutorunMicrotasks(Isolate *source, bool autorun);
......
...@@ -6455,21 +6455,17 @@ void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) { ...@@ -6455,21 +6455,17 @@ void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) {
void V8::RunMicrotasks(Isolate* isolate) { void V8::RunMicrotasks(Isolate* isolate) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); isolate->RunMicrotasks();
i::HandleScope scope(i_isolate);
i::V8::RunMicrotasks(i_isolate);
} }
void V8::EnqueueMicrotask(Isolate* isolate, Handle<Function> microtask) { void V8::EnqueueMicrotask(Isolate* isolate, Handle<Function> microtask) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); isolate->EnqueueMicrotask(microtask);
ENTER_V8(i_isolate);
i::Execution::EnqueueMicrotask(i_isolate, Utils::OpenHandle(*microtask));
} }
void V8::SetAutorunMicrotasks(Isolate* isolate, bool autorun) { void V8::SetAutorunMicrotasks(Isolate* isolate, bool autorun) {
reinterpret_cast<i::Isolate*>(isolate)->set_autorun_microtasks(autorun); isolate->SetAutorunMicrotasks(autorun);
} }
...@@ -6593,6 +6589,18 @@ Isolate::AllowJavascriptExecutionScope::~AllowJavascriptExecutionScope() { ...@@ -6593,6 +6589,18 @@ Isolate::AllowJavascriptExecutionScope::~AllowJavascriptExecutionScope() {
} }
Isolate::SuppressMicrotaskExecutionScope::SuppressMicrotaskExecutionScope(
Isolate* isolate)
: isolate_(reinterpret_cast<i::Isolate*>(isolate)) {
isolate_->handle_scope_implementer()->IncrementCallDepth();
}
Isolate::SuppressMicrotaskExecutionScope::~SuppressMicrotaskExecutionScope() {
isolate_->handle_scope_implementer()->DecrementCallDepth();
}
void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) { void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
if (!isolate->IsInitialized()) { if (!isolate->IsInitialized()) {
...@@ -6632,6 +6640,25 @@ void Isolate::RemoveCallCompletedCallback(CallCompletedCallback callback) { ...@@ -6632,6 +6640,25 @@ void Isolate::RemoveCallCompletedCallback(CallCompletedCallback callback) {
} }
void Isolate::RunMicrotasks() {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(this);
i::HandleScope scope(i_isolate);
i_isolate->RunMicrotasks();
}
void Isolate::EnqueueMicrotask(Handle<Function> microtask) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(this);
ENTER_V8(i_isolate);
i::Execution::EnqueueMicrotask(i_isolate, Utils::OpenHandle(*microtask));
}
void Isolate::SetAutorunMicrotasks(bool autorun) {
reinterpret_cast<i::Isolate*>(this)->set_autorun_microtasks(autorun);
}
String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj) String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj)
: str_(NULL), length_(0) { : str_(NULL), length_(0) {
i::Isolate* isolate = i::Isolate::Current(); i::Isolate* isolate = i::Isolate::Current();
......
...@@ -2252,4 +2252,17 @@ void Isolate::FireCallCompletedCallback() { ...@@ -2252,4 +2252,17 @@ void Isolate::FireCallCompletedCallback() {
} }
void Isolate::RunMicrotasks() {
if (!microtask_pending())
return;
ASSERT(handle_scope_implementer()->CallDepthIsZero());
// Increase call depth to prevent recursive callbacks.
handle_scope_implementer()->IncrementCallDepth();
Execution::RunMicrotasks(this);
handle_scope_implementer()->DecrementCallDepth();
}
} } // namespace v8::internal } } // namespace v8::internal
...@@ -1077,6 +1077,8 @@ class Isolate { ...@@ -1077,6 +1077,8 @@ class Isolate {
void RemoveCallCompletedCallback(CallCompletedCallback callback); void RemoveCallCompletedCallback(CallCompletedCallback callback);
void FireCallCompletedCallback(); void FireCallCompletedCallback();
void RunMicrotasks();
private: private:
Isolate(); Isolate();
......
...@@ -87,21 +87,6 @@ void V8::SetReturnAddressLocationResolver( ...@@ -87,21 +87,6 @@ void V8::SetReturnAddressLocationResolver(
} }
void V8::RunMicrotasks(Isolate* isolate) {
if (!isolate->microtask_pending())
return;
HandleScopeImplementer* handle_scope_implementer =
isolate->handle_scope_implementer();
ASSERT(handle_scope_implementer->CallDepthIsZero());
// Increase call depth to prevent recursive callbacks.
handle_scope_implementer->IncrementCallDepth();
Execution::RunMicrotasks(isolate);
handle_scope_implementer->DecrementCallDepth();
}
void V8::InitializeOncePerProcessImpl() { void V8::InitializeOncePerProcessImpl() {
FlagList::EnforceFlagImplications(); FlagList::EnforceFlagImplications();
Serializer::InitializeOncePerProcess(); Serializer::InitializeOncePerProcess();
......
...@@ -75,8 +75,6 @@ class V8 : public AllStatic { ...@@ -75,8 +75,6 @@ class V8 : public AllStatic {
// Support for entry hooking JITed code. // Support for entry hooking JITed code.
static void SetFunctionEntryHook(FunctionEntryHook entry_hook); static void SetFunctionEntryHook(FunctionEntryHook entry_hook);
static void RunMicrotasks(Isolate* isolate);
static v8::ArrayBuffer::Allocator* ArrayBufferAllocator() { static v8::ArrayBuffer::Allocator* ArrayBufferAllocator() {
return array_buffer_allocator_; return array_buffer_allocator_;
} }
......
...@@ -20717,22 +20717,22 @@ TEST(EnqueueMicrotask) { ...@@ -20717,22 +20717,22 @@ TEST(EnqueueMicrotask) {
CHECK_EQ(0, CompileRun("ext1Calls")->Int32Value()); CHECK_EQ(0, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(0, CompileRun("ext2Calls")->Int32Value()); CHECK_EQ(0, CompileRun("ext2Calls")->Int32Value());
v8::V8::EnqueueMicrotask(env->GetIsolate(), env->GetIsolate()->EnqueueMicrotask(
Function::New(env->GetIsolate(), MicrotaskOne)); Function::New(env->GetIsolate(), MicrotaskOne));
CompileRun("1+1;"); CompileRun("1+1;");
CHECK_EQ(1, CompileRun("ext1Calls")->Int32Value()); CHECK_EQ(1, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(0, CompileRun("ext2Calls")->Int32Value()); CHECK_EQ(0, CompileRun("ext2Calls")->Int32Value());
v8::V8::EnqueueMicrotask(env->GetIsolate(), env->GetIsolate()->EnqueueMicrotask(
Function::New(env->GetIsolate(), MicrotaskOne)); Function::New(env->GetIsolate(), MicrotaskOne));
v8::V8::EnqueueMicrotask(env->GetIsolate(), env->GetIsolate()->EnqueueMicrotask(
Function::New(env->GetIsolate(), MicrotaskTwo)); Function::New(env->GetIsolate(), MicrotaskTwo));
CompileRun("1+1;"); CompileRun("1+1;");
CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value()); CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(1, CompileRun("ext2Calls")->Int32Value()); CHECK_EQ(1, CompileRun("ext2Calls")->Int32Value());
v8::V8::EnqueueMicrotask(env->GetIsolate(), env->GetIsolate()->EnqueueMicrotask(
Function::New(env->GetIsolate(), MicrotaskTwo)); Function::New(env->GetIsolate(), MicrotaskTwo));
CompileRun("1+1;"); CompileRun("1+1;");
CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value()); CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(2, CompileRun("ext2Calls")->Int32Value()); CHECK_EQ(2, CompileRun("ext2Calls")->Int32Value());
...@@ -20753,41 +20753,54 @@ TEST(SetAutorunMicrotasks) { ...@@ -20753,41 +20753,54 @@ TEST(SetAutorunMicrotasks) {
CHECK_EQ(0, CompileRun("ext1Calls")->Int32Value()); CHECK_EQ(0, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(0, CompileRun("ext2Calls")->Int32Value()); CHECK_EQ(0, CompileRun("ext2Calls")->Int32Value());
v8::V8::EnqueueMicrotask(env->GetIsolate(), env->GetIsolate()->EnqueueMicrotask(
Function::New(env->GetIsolate(), MicrotaskOne)); Function::New(env->GetIsolate(), MicrotaskOne));
CompileRun("1+1;"); CompileRun("1+1;");
CHECK_EQ(1, CompileRun("ext1Calls")->Int32Value()); CHECK_EQ(1, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(0, CompileRun("ext2Calls")->Int32Value()); CHECK_EQ(0, CompileRun("ext2Calls")->Int32Value());
V8::SetAutorunMicrotasks(env->GetIsolate(), false); env->GetIsolate()->SetAutorunMicrotasks(false);
v8::V8::EnqueueMicrotask(env->GetIsolate(), env->GetIsolate()->EnqueueMicrotask(
Function::New(env->GetIsolate(), MicrotaskOne)); Function::New(env->GetIsolate(), MicrotaskOne));
v8::V8::EnqueueMicrotask(env->GetIsolate(), env->GetIsolate()->EnqueueMicrotask(
Function::New(env->GetIsolate(), MicrotaskTwo)); Function::New(env->GetIsolate(), MicrotaskTwo));
CompileRun("1+1;"); CompileRun("1+1;");
CHECK_EQ(1, CompileRun("ext1Calls")->Int32Value()); CHECK_EQ(1, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(0, CompileRun("ext2Calls")->Int32Value()); CHECK_EQ(0, CompileRun("ext2Calls")->Int32Value());
V8::RunMicrotasks(env->GetIsolate()); env->GetIsolate()->RunMicrotasks();
CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value()); CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(1, CompileRun("ext2Calls")->Int32Value()); CHECK_EQ(1, CompileRun("ext2Calls")->Int32Value());
v8::V8::EnqueueMicrotask(env->GetIsolate(), env->GetIsolate()->EnqueueMicrotask(
Function::New(env->GetIsolate(), MicrotaskTwo)); Function::New(env->GetIsolate(), MicrotaskTwo));
CompileRun("1+1;"); CompileRun("1+1;");
CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value()); CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(1, CompileRun("ext2Calls")->Int32Value()); CHECK_EQ(1, CompileRun("ext2Calls")->Int32Value());
V8::RunMicrotasks(env->GetIsolate()); env->GetIsolate()->RunMicrotasks();
CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value()); CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(2, CompileRun("ext2Calls")->Int32Value()); CHECK_EQ(2, CompileRun("ext2Calls")->Int32Value());
V8::SetAutorunMicrotasks(env->GetIsolate(), true); env->GetIsolate()->SetAutorunMicrotasks(true);
v8::V8::EnqueueMicrotask(env->GetIsolate(), env->GetIsolate()->EnqueueMicrotask(
Function::New(env->GetIsolate(), MicrotaskTwo)); Function::New(env->GetIsolate(), MicrotaskTwo));
CompileRun("1+1;"); CompileRun("1+1;");
CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value()); CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(3, CompileRun("ext2Calls")->Int32Value()); CHECK_EQ(3, CompileRun("ext2Calls")->Int32Value());
env->GetIsolate()->EnqueueMicrotask(
Function::New(env->GetIsolate(), MicrotaskTwo));
{
v8::Isolate::SuppressMicrotaskExecutionScope scope(env->GetIsolate());
CompileRun("1+1;");
CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(3, CompileRun("ext2Calls")->Int32Value());
}
CompileRun("1+1;");
CHECK_EQ(2, CompileRun("ext1Calls")->Int32Value());
CHECK_EQ(4, CompileRun("ext2Calls")->Int32Value());
} }
...@@ -22342,20 +22355,20 @@ TEST(Promises) { ...@@ -22342,20 +22355,20 @@ TEST(Promises) {
p->Chain(f1); p->Chain(f1);
CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value());
V8::RunMicrotasks(isolate); isolate->RunMicrotasks();
CHECK_EQ(1, global->Get(v8_str("x1"))->Int32Value()); CHECK_EQ(1, global->Get(v8_str("x1"))->Int32Value());
p->Catch(f2); p->Catch(f2);
V8::RunMicrotasks(isolate); isolate->RunMicrotasks();
CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value());
r->Catch(f2); r->Catch(f2);
CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value());
V8::RunMicrotasks(isolate); isolate->RunMicrotasks();
CHECK_EQ(2, global->Get(v8_str("x2"))->Int32Value()); CHECK_EQ(2, global->Get(v8_str("x2"))->Int32Value());
r->Chain(f1); r->Chain(f1);
V8::RunMicrotasks(isolate); isolate->RunMicrotasks();
CHECK_EQ(1, global->Get(v8_str("x1"))->Int32Value()); CHECK_EQ(1, global->Get(v8_str("x1"))->Int32Value());
// Chaining pending promises. // Chaining pending promises.
...@@ -22365,7 +22378,7 @@ TEST(Promises) { ...@@ -22365,7 +22378,7 @@ TEST(Promises) {
pr->GetPromise()->Chain(f1); pr->GetPromise()->Chain(f1);
rr->GetPromise()->Catch(f2); rr->GetPromise()->Catch(f2);
V8::RunMicrotasks(isolate); isolate->RunMicrotasks();
CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value());
CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value());
...@@ -22374,7 +22387,7 @@ TEST(Promises) { ...@@ -22374,7 +22387,7 @@ TEST(Promises) {
CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value());
CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value());
V8::RunMicrotasks(isolate); isolate->RunMicrotasks();
CHECK_EQ(1, global->Get(v8_str("x1"))->Int32Value()); CHECK_EQ(1, global->Get(v8_str("x1"))->Int32Value());
CHECK_EQ(2, global->Get(v8_str("x2"))->Int32Value()); CHECK_EQ(2, global->Get(v8_str("x2"))->Int32Value());
...@@ -22385,7 +22398,7 @@ TEST(Promises) { ...@@ -22385,7 +22398,7 @@ TEST(Promises) {
pr->Resolve(v8::Integer::New(isolate, 3)); pr->Resolve(v8::Integer::New(isolate, 3));
CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value());
CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value());
V8::RunMicrotasks(isolate); isolate->RunMicrotasks();
CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value()); CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value());
CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value()); CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value());
...@@ -22395,7 +22408,7 @@ TEST(Promises) { ...@@ -22395,7 +22408,7 @@ TEST(Promises) {
rr->Reject(v8::Integer::New(isolate, 3)); rr->Reject(v8::Integer::New(isolate, 3));
CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value());
CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value());
V8::RunMicrotasks(isolate); isolate->RunMicrotasks();
CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value()); CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value());
CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value()); CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value());
} }
......
...@@ -96,7 +96,7 @@ TEST(MicrotaskPerIsolateState) { ...@@ -96,7 +96,7 @@ TEST(MicrotaskPerIsolateState) {
HarmonyIsolate isolate; HarmonyIsolate isolate;
HandleScope scope(isolate.GetIsolate()); HandleScope scope(isolate.GetIsolate());
LocalContext context1(isolate.GetIsolate()); LocalContext context1(isolate.GetIsolate());
V8::SetAutorunMicrotasks(isolate.GetIsolate(), false); isolate.GetIsolate()->SetAutorunMicrotasks(false);
CompileRun( CompileRun(
"var obj = { calls: 0 };"); "var obj = { calls: 0 };");
Handle<Value> obj = CompileRun("obj"); Handle<Value> obj = CompileRun("obj");
...@@ -130,7 +130,7 @@ TEST(MicrotaskPerIsolateState) { ...@@ -130,7 +130,7 @@ TEST(MicrotaskPerIsolateState) {
LocalContext context4(isolate.GetIsolate()); LocalContext context4(isolate.GetIsolate());
context4->Global()->Set(String::NewFromUtf8(isolate.GetIsolate(), "obj"), context4->Global()->Set(String::NewFromUtf8(isolate.GetIsolate(), "obj"),
obj); obj);
V8::RunMicrotasks(isolate.GetIsolate()); isolate.GetIsolate()->RunMicrotasks();
CHECK_EQ(2, CompileRun("obj.calls")->Int32Value()); CHECK_EQ(2, CompileRun("obj.calls")->Int32Value());
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment