OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "bindings/v8/ThenableCoercions.h" |
| 33 |
| 34 namespace WebCore { |
| 35 |
| 36 PassOwnPtr<ThenableCoercions> ThenableCoercions::create() |
| 37 { |
| 38 return adoptPtr(new ThenableCoercions()); |
| 39 } |
| 40 |
| 41 ThenableCoercions::ThenableCoercions() |
| 42 : m_entries() |
| 43 , m_entriesToDelete() |
| 44 , m_entriesListGuarded(false) |
| 45 { |
| 46 } |
| 47 |
| 48 ThenableCoercions::~ThenableCoercions() |
| 49 { |
| 50 Entry* entry = m_entries.head(); |
| 51 while (entry) { |
| 52 Entry* nextEntry = entry->next(); |
| 53 delete entry; |
| 54 entry = nextEntry; |
| 55 } |
| 56 } |
| 57 |
| 58 bool ThenableCoercions::has(v8::Handle<v8::Object> thenable) |
| 59 { |
| 60 return !get(thenable).IsEmpty(); |
| 61 } |
| 62 |
| 63 v8::Handle<v8::Object> ThenableCoercions::get(v8::Handle<v8::Object> thenable) |
| 64 { |
| 65 // GuardEntriesListAgainstGC guarantees that |m_entries| linked list won't |
| 66 // be corrupt when GC collects the weak references during the iteration. |
| 67 // If the entry's thenable is collected during the iteration, |
| 68 // its thenable and promise become empty, but node is not unlinked from the |
| 69 // |m_entries|. |
| 70 // Nodes will be destroyed when GuardEntriesListAgainstGC is destroyed. |
| 71 GuardEntriesListAgainstGC guard(this); |
| 72 Entry* entry = m_entries.head(); |
| 73 while (entry) { |
| 74 v8::Local<v8::Object> storedThenable = entry->thenable(); |
| 75 if (!storedThenable.IsEmpty() && storedThenable->SameValue(thenable)) |
| 76 return entry->promise(); |
| 77 entry = entry->next(); |
| 78 } |
| 79 return v8::Handle<v8::Object>(); |
| 80 } |
| 81 |
| 82 void ThenableCoercions::set(v8::Handle<v8::Object> thenable, v8::Handle<v8::Obje
ct> promise, v8::Isolate* isolate) |
| 83 { |
| 84 GuardEntriesListAgainstGC guard(this); |
| 85 Entry* entry = Entry::create(thenable, promise, this, isolate); |
| 86 m_entries.append(entry); |
| 87 } |
| 88 |
| 89 void ThenableCoercions::tryToDelete(ThenableCoercions::Entry* entry) |
| 90 { |
| 91 if (m_entriesListGuarded) { |
| 92 m_entriesToDelete.append(entry); |
| 93 return; |
| 94 } |
| 95 deleteInternal(entry); |
| 96 } |
| 97 |
| 98 void ThenableCoercions::deleteInternal(Entry* entry) |
| 99 { |
| 100 m_entries.remove(entry); |
| 101 delete entry; |
| 102 } |
| 103 |
| 104 ThenableCoercions::Entry::Entry(v8::Handle<v8::Object> thenable, v8::Handle<v8::
Object> promise, ThenableCoercions* thenableCoercions, v8::Isolate* isolate) |
| 105 : m_next(0) |
| 106 , m_prev(0) |
| 107 , m_thenable(isolate, thenable) |
| 108 , m_promise(isolate, promise) |
| 109 , m_thenableCoercions(thenableCoercions) |
| 110 , m_isolate(isolate) |
| 111 { |
| 112 m_thenable.makeWeak(this, &makeWeakCallback); |
| 113 } |
| 114 |
| 115 void ThenableCoercions::Entry::makeWeakCallback(v8::Isolate* isolate, v8::Persis
tent<v8::Object>* thenable, ThenableCoercions::Entry* entry) |
| 116 { |
| 117 entry->m_thenableCoercions->tryToDelete(entry); |
| 118 entry->m_thenable.clear(); |
| 119 entry->m_promise.clear(); |
| 120 } |
| 121 |
| 122 ThenableCoercions::GuardEntriesListAgainstGC::GuardEntriesListAgainstGC(Thenable
Coercions* thenableCoercions) |
| 123 : m_thenableCoercions(thenableCoercions) |
| 124 { |
| 125 thenableCoercions->m_entriesListGuarded = true; |
| 126 } |
| 127 |
| 128 ThenableCoercions::GuardEntriesListAgainstGC::~GuardEntriesListAgainstGC() |
| 129 { |
| 130 for (size_t i = 0; i < m_thenableCoercions->m_entriesToDelete.size(); ++i) |
| 131 m_thenableCoercions->deleteInternal(m_thenableCoercions->m_entriesToDele
te[i]); |
| 132 m_thenableCoercions->m_entriesToDelete.clear(); |
| 133 m_thenableCoercions->m_entriesListGuarded = false; |
| 134 } |
| 135 |
| 136 } // namespace WebCore |
OLD | NEW |