void WorkerGlobalScope::dispose()
{
    ASSERT(thread()->isCurrentThread());
    stopActiveDOMObjects();

    // Event listeners would keep DOMWrapperWorld objects alive for too long. Also, they have references to JS objects,
    // which become dangling once Heap is destroyed.
    for (auto it = m_eventListeners.begin(); it != m_eventListeners.end(); ) {
        RawPtr<V8AbstractEventListener> listener = *it;
        // clearListenerObject() will unregister the listener from
        // m_eventListeners, and invalidate the iterator, so we have to advance
        // it first.
        ++it;
        listener->clearListenerObject();
    }
    removeAllEventListeners();

    clearScript();
    clearInspector();
    m_eventQueue->close();

    // We do not clear the thread field of the
    // WorkerGlobalScope. Other objects keep the worker global scope
    // alive because they need its thread field to check that work is
    // being carried out on the right thread. We therefore cannot clear
    // the thread field before all references to the worker global
    // scope are gone.
}