Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Source/JavaScriptCore/runtime/MicrotaskQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ void MicrotaskQueue::enqueue(QueuedTask&& task)
}
}

bool MarkedMicrotaskDeque::hasTasks(JSGlobalObject* globalObject) const
{
for (const auto& task : m_queue) {
if (task.globalObject() == globalObject)
return true;
}
return false;
}

bool MicrotaskQueue::isEmpty(JSGlobalObject* globalObject) const
{
return !m_queue.hasTasks(globalObject);
}

bool MarkedMicrotaskDeque::hasMicrotasksForFullyActiveDocument() const
{
for (auto& task : m_queue) {
Expand Down
3 changes: 3 additions & 0 deletions Source/JavaScriptCore/runtime/MicrotaskQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class MarkedMicrotaskDeque {
std::swap(m_markedBefore, other.m_markedBefore);
}

JS_EXPORT_PRIVATE bool hasTasks(JSGlobalObject*) const;
JS_EXPORT_PRIVATE bool hasMicrotasksForFullyActiveDocument() const;

DECLARE_VISIT_AGGREGATE;
Expand All @@ -177,6 +178,8 @@ class MicrotaskQueue final : public BasicRawSentinelNode<MicrotaskQueue> {
return m_queue.isEmpty();
}

JS_EXPORT_PRIVATE bool isEmpty(JSGlobalObject*) const;

size_t size() const { return m_queue.size(); }

void clear()
Expand Down
29 changes: 29 additions & 0 deletions Source/JavaScriptCore/runtime/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,35 @@ void VM::drainMicrotasks()
finalizeSynchronousJSExecution();
}

void VM::drainMicrotasks(JSGlobalObject* globalObject)
{
if (m_drainMicrotaskDelayScopeCount) [[unlikely]]
return;

if (executionForbidden()) [[unlikely]]
m_defaultMicrotaskQueue.clear();
else {
do {
m_defaultMicrotaskQueue.performMicrotaskCheckpoint(*this,
[&](QueuedTask& task) ALWAYS_INLINE_LAMBDA {
if (task.globalObject() != globalObject)
return QueuedTask::Result::Suspended;
if (RefPtr dispatcher = task.dispatcher())
return dispatcher->run(task);

runJSMicrotask(task.globalObject(), task.identifier(), task.job(), task.arguments());
return QueuedTask::Result::Executed;
});
if (hasPendingTerminationException()) [[unlikely]]
return;
didExhaustMicrotaskQueue();
if (hasPendingTerminationException()) [[unlikely]]
return;
} while (!m_defaultMicrotaskQueue.isEmpty(globalObject));
}
finalizeSynchronousJSExecution();
}

void sanitizeStackForVM(VM& vm)
{
auto& thread = Thread::currentSingleton();
Expand Down
1 change: 1 addition & 0 deletions Source/JavaScriptCore/runtime/VM.h
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ class VM : public ThreadSafeRefCountedWithSuppressingSaferCPPChecking<VM>, publi

DrainMicrotaskDelayScope drainMicrotaskDelayScope() { return DrainMicrotaskDelayScope { *this }; }
JS_EXPORT_PRIVATE void drainMicrotasks();
JS_EXPORT_PRIVATE void drainMicrotasks(JSGlobalObject*);
void setOnEachMicrotaskTick(WTF::Function<void(VM&)>&& func) { m_onEachMicrotaskTick = WTFMove(func); }
void callOnEachMicrotaskTick()
{
Expand Down