Chromium Code Reviews| Index: Source/bindings/v8/ThenableCoercions.cpp |
| diff --git a/Source/bindings/v8/ThenableCoercions.cpp b/Source/bindings/v8/ThenableCoercions.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..95a29dbbe3b07e564bef28084556704dd59a2d0f |
| --- /dev/null |
| +++ b/Source/bindings/v8/ThenableCoercions.cpp |
| @@ -0,0 +1,136 @@ |
| +/* |
| + * Copyright (C) 2013 Google Inc. 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 "config.h" |
| +#include "bindings/v8/ThenableCoercions.h" |
| + |
| +namespace WebCore { |
| + |
| +PassOwnPtr<ThenableCoercions> ThenableCoercions::create() |
| +{ |
| + return adoptPtr(new ThenableCoercions()); |
| +} |
| + |
| +ThenableCoercions::ThenableCoercions() |
| + : m_entries() |
| + , m_entriesToDelete() |
| + , m_entriesListGuarded(false) |
| +{ |
| +} |
| + |
| +ThenableCoercions::~ThenableCoercions() |
| +{ |
| + Entry* entry = m_entries.head(); |
| + while (entry) { |
| + Entry* nextEntry = entry->next(); |
| + delete entry; |
| + entry = nextEntry; |
| + } |
| +} |
| + |
| +bool ThenableCoercions::has(v8::Handle<v8::Object> thenable) |
| +{ |
| + return !get(thenable).IsEmpty(); |
| +} |
| + |
| +v8::Handle<v8::Object> ThenableCoercions::get(v8::Handle<v8::Object> thenable) |
| +{ |
| + // GuardEntriesListAgainstGC guarantees that |m_entries| linked list won't |
| + // be corrupt when GC collects the weak references during the iteration. |
| + // If the entry's thenable is collected during the iteration, |
| + // its thenable and promise become empty, but node is not unlinked from the |
| + // |m_entries|. |
| + // Nodes will be destroyed when GuardEntriesListAgainstGC is destroyed. |
| + GuardEntriesListAgainstGC guard(this); |
| + Entry* entry = m_entries.head(); |
| + while (entry) { |
| + v8::Local<v8::Object> storedThenable = entry->thenable(); |
| + if (!storedThenable.IsEmpty() && storedThenable->SameValue(thenable)) |
|
yusukesuzuki
2013/09/26 02:44:14
To follow the WeakMap spec of ES6 draft, use SameV
|
| + return entry->promise(); |
| + entry = entry->next(); |
| + } |
| + return v8::Handle<v8::Object>(); |
| +} |
| + |
| +void ThenableCoercions::set(v8::Handle<v8::Object> thenable, v8::Handle<v8::Object> promise, v8::Isolate* isolate) |
| +{ |
| + GuardEntriesListAgainstGC guard(this); |
| + Entry* entry = Entry::create(thenable, promise, this, isolate); |
| + m_entries.append(entry); |
| +} |
| + |
| +void ThenableCoercions::tryToDelete(ThenableCoercions::Entry* entry) |
| +{ |
| + if (m_entriesListGuarded) { |
| + m_entriesToDelete.append(entry); |
| + return; |
| + } |
| + deleteInternal(entry); |
| +} |
| + |
| +void ThenableCoercions::deleteInternal(Entry* entry) |
| +{ |
| + m_entries.remove(entry); |
| + delete entry; |
| +} |
| + |
| +ThenableCoercions::Entry::Entry(v8::Handle<v8::Object> thenable, v8::Handle<v8::Object> promise, ThenableCoercions* thenableCoercions, v8::Isolate* isolate) |
| + : m_next(0) |
| + , m_prev(0) |
| + , m_thenable(isolate, thenable) |
| + , m_promise(isolate, promise) |
| + , m_thenableCoercions(thenableCoercions) |
| + , m_isolate(isolate) |
| +{ |
| + m_thenable.makeWeak(this, &makeWeakCallback); |
| +} |
| + |
| +void ThenableCoercions::Entry::makeWeakCallback(v8::Isolate* isolate, v8::Persistent<v8::Object>* thenable, ThenableCoercions::Entry* entry) |
| +{ |
| + entry->m_thenableCoercions->tryToDelete(entry); |
| + entry->m_thenable.clear(); |
| + entry->m_promise.clear(); |
| +} |
| + |
| +ThenableCoercions::GuardEntriesListAgainstGC::GuardEntriesListAgainstGC(ThenableCoercions* thenableCoercions) |
| + : m_thenableCoercions(thenableCoercions) |
| +{ |
| + thenableCoercions->m_entriesListGuarded = true; |
| +} |
| + |
| +ThenableCoercions::GuardEntriesListAgainstGC::~GuardEntriesListAgainstGC() |
| +{ |
| + for (size_t i = 0; i < m_thenableCoercions->m_entriesToDelete.size(); ++i) |
| + m_thenableCoercions->deleteInternal(m_thenableCoercions->m_entriesToDelete[i]); |
| + m_thenableCoercions->m_entriesToDelete.clear(); |
| + m_thenableCoercions->m_entriesListGuarded = false; |
| +} |
| + |
| +} // namespace WebCore |