Refactor DOM Iterator next signature
authoryouenn.fablet@crf.canon.fr <youenn.fablet@crf.canon.fr@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 23 Feb 2016 09:39:30 +0000 (09:39 +0000)
committeryouenn.fablet@crf.canon.fr <youenn.fablet@crf.canon.fr@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 23 Feb 2016 09:39:30 +0000 (09:39 +0000)
https://bugs.webkit.org/show_bug.cgi?id=154531

Reviewed by Myles C. Maxfield.

Covered by existing tests.

* Modules/fetch/FetchHeaders.cpp:
(WebCore::FetchHeaders::Iterator::next): Using Optional<KeyValuePair> to return iterator value.
* Modules/fetch/FetchHeaders.h:
* bindings/js/JSKeyValueIterator.h: Using Optional<KeyValuePair> as returned iterator value.
(WebCore::keyValueIteratorForEach):
(WebCore::JSKeyValueIterator<JSWrapper>::next):
* css/FontFaceSet.cpp:
(WebCore::FontFaceSet::Iterator::next): Using Optional<KeyValuePair> to return iterator value.
* css/FontFaceSet.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@196973 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/Modules/fetch/FetchHeaders.cpp
Source/WebCore/Modules/fetch/FetchHeaders.h
Source/WebCore/bindings/js/JSKeyValueIterator.h
Source/WebCore/css/FontFaceSet.cpp
Source/WebCore/css/FontFaceSet.h

index cc9c5d1..2335910 100644 (file)
@@ -1,3 +1,22 @@
+2016-02-23  Youenn Fablet  <youenn.fablet@crf.canon.fr>
+
+        Refactor DOM Iterator next signature
+        https://bugs.webkit.org/show_bug.cgi?id=154531
+
+        Reviewed by Myles C. Maxfield.
+
+        Covered by existing tests.
+
+        * Modules/fetch/FetchHeaders.cpp:
+        (WebCore::FetchHeaders::Iterator::next): Using Optional<KeyValuePair> to return iterator value.
+        * Modules/fetch/FetchHeaders.h:
+        * bindings/js/JSKeyValueIterator.h: Using Optional<KeyValuePair> as returned iterator value.
+        (WebCore::keyValueIteratorForEach):
+        (WebCore::JSKeyValueIterator<JSWrapper>::next):
+        * css/FontFaceSet.cpp:
+        (WebCore::FontFaceSet::Iterator::next): Using Optional<KeyValuePair> to return iterator value.
+        * css/FontFaceSet.h:
+
 2016-02-22  Gavin Barraclough  <barraclough@apple.com>
 
         Some timer alignment cleanup.
index 108fa7b..f4bc1a3 100644 (file)
@@ -179,19 +179,16 @@ void FetchHeaders::fill(const FetchHeaders* headers)
     }
 }
 
-bool FetchHeaders::Iterator::next(JSC::ExecState&, String& nextKey, String& nextValue)
+Optional<WTF::KeyValuePair<String, String>> FetchHeaders::Iterator::next(JSC::ExecState&)
 {
     while (m_currentIndex < m_keys.size()) {
-        auto& key = m_keys[m_currentIndex++];
+        String key = m_keys[m_currentIndex++];
         String value = m_headers->m_headers.get(key);
-        if (!value.isNull()) {
-            nextKey = key;
-            nextValue = WTFMove(value);
-            return false;
-        }
+        if (!value.isNull())
+            return WTF::KeyValuePair<String, String>(WTFMove(key), WTFMove(value));
     }
     m_keys.clear();
-    return true;
+    return Nullopt;
 }
 
 FetchHeaders::Iterator::Iterator(FetchHeaders& headers)
index 80d14e3..311678d 100644 (file)
@@ -32,6 +32,8 @@
 #if ENABLE(FETCH_API)
 
 #include "HTTPHeaderMap.h"
+#include <wtf/HashTraits.h>
+#include <wtf/Optional.h>
 
 namespace JSC {
 class ExecState;
@@ -69,7 +71,7 @@ public:
     class Iterator {
     public:
         explicit Iterator(FetchHeaders&);
-        bool next(JSC::ExecState&, String& nextKey, String& nextValue);
+        Optional<WTF::KeyValuePair<String, String>> next(JSC::ExecState&);
 
     private:
         Ref<FetchHeaders> m_headers;
index c5f84ac..62272f5 100644 (file)
@@ -88,7 +88,7 @@ public:
             JSKeyValueIteratorPrototype<JSWrapper>::createStructure(vm, globalObject, globalObject->objectPrototype()));
     }
 
-    bool next(JSC::ExecState&, JSC::JSValue&);
+    JSC::JSValue next(JSC::ExecState&);
 
 private:
     JSKeyValueIterator(JSC::Structure* structure, JSWrapper& iteratedObject, IterationKind kind)
@@ -126,13 +126,11 @@ JSC::EncodedJSValue keyValueIteratorForEach(JSC::ExecState& state, const char* p
     if (callType == JSC::CallTypeNone)
         return throwVMTypeError(&state);
 
-    typename JSWrapper::IteratorKey nextKey;
-    typename JSWrapper::IteratorValue nextValue;
     auto iterator = wrapper->wrapped().createIterator();
-    while (!iterator.next(state, nextKey, nextValue)) {
+    while (auto value = iterator.next(state)) {
         JSC::MarkedArgumentBuffer arguments;
-        arguments.append(toJS(&state, wrapper->globalObject(), nextValue));
-        arguments.append(toJS(&state, wrapper->globalObject(), nextKey));
+        arguments.append(toJS(&state, wrapper->globalObject(), value.value().value));
+        arguments.append(toJS(&state, wrapper->globalObject(), value.value().key));
         arguments.append(wrapper);
         JSC::call(&state, state.argument(0), callType, callData, wrapper, arguments);
         if (state.hadException())
@@ -149,33 +147,31 @@ void JSKeyValueIterator<JSWrapper>::destroy(JSCell* cell)
 }
 
 template<typename JSWrapper>
-bool JSKeyValueIterator<JSWrapper>::next(JSC::ExecState& state, JSC::JSValue& value)
+JSC::JSValue JSKeyValueIterator<JSWrapper>::next(JSC::ExecState& state)
 {
-    typename JSWrapper::IteratorKey nextKey;
-    typename JSWrapper::IteratorValue nextValue;
-    if (m_iterator.next(state, nextKey, nextValue)) {
-        value = JSC::jsUndefined();
-        return true;
-    }
+    auto iteratorValue = m_iterator.next(state);
+    if (!iteratorValue)
+        return createIteratorResultObject(&state, JSC::jsUndefined(), true);
+
+    JSC::JSValue value;
     if (m_kind == IterationKind::Value)
-        value = toJS(&state, globalObject(), nextValue);
+        value = toJS(&state, globalObject(), iteratorValue.value().value);
     else if (m_kind == IterationKind::Key)
-        value = toJS(&state, globalObject(), nextKey);
+        value = toJS(&state, globalObject(), iteratorValue.value().key);
     else
-        value = jsPair(state, globalObject(), nextKey, nextValue);
-    return false;
+        value = jsPair(state, globalObject(), iteratorValue.value().key, iteratorValue.value().value);
+
+    return createIteratorResultObject(&state, value, false);
 }
 
 template<typename JSWrapper>
 JSC::EncodedJSValue JSC_HOST_CALL JSKeyValueIteratorPrototype<JSWrapper>::next(JSC::ExecState* state)
 {
-    JSKeyValueIterator<JSWrapper>* iterator = JSC::jsDynamicCast<JSKeyValueIterator<JSWrapper>*>(state->thisValue());
+    auto iterator = JSC::jsDynamicCast<JSKeyValueIterator<JSWrapper>*>(state->thisValue());
     if (!iterator)
         return JSC::JSValue::encode(throwTypeError(state, ASCIILiteral("Cannot call next() on a non-Iterator object")));
 
-    JSC::JSValue result;
-    bool isDone = iterator->next(*state, result);
-    return JSC::JSValue::encode(createIteratorResultObject(state, result, isDone));
+    return JSC::JSValue::encode(iterator->next(*state));
 }
 
 template<typename JSWrapper>
index 4094b19..ad5aa48 100644 (file)
@@ -82,13 +82,12 @@ FontFaceSet::Iterator::Iterator(FontFaceSet& set)
 {
 }
 
-bool FontFaceSet::Iterator::next(JSC::ExecState& execState, RefPtr<FontFace>& key, RefPtr<FontFace>& value)
+Optional<WTF::KeyValuePair<RefPtr<FontFace>, RefPtr<FontFace>>> FontFaceSet::Iterator::next(JSC::ExecState& state)
 {
     if (m_index == m_target->size())
-        return true;
-    key = m_target->backing()[m_index++].wrapper(execState);
-    value = key;
-    return false;
+        return Nullopt;
+    RefPtr<FontFace> item = m_target->backing()[m_index++].wrapper(state);
+    return WTF::KeyValuePair<RefPtr<FontFace>, RefPtr<FontFace>>(item, item);
 }
 
 FontFaceSet::PendingPromise::PendingPromise(Promise&& promise)
index 1fdecb8..871f3bc 100644 (file)
@@ -31,6 +31,7 @@
 #include "DOMCoreException.h"
 #include "EventTarget.h"
 #include "JSDOMPromise.h"
+#include <wtf/HashTraits.h>
 #include <wtf/Optional.h>
 #include <wtf/Ref.h>
 #include <wtf/RefCounted.h>
@@ -74,7 +75,7 @@ public:
     class Iterator {
     public:
         explicit Iterator(FontFaceSet&);
-        bool next(JSC::ExecState&, RefPtr<FontFace>& nextKey, RefPtr<FontFace>& nextValue);
+        Optional<WTF::KeyValuePair<RefPtr<FontFace>, RefPtr<FontFace>>> next(JSC::ExecState&);
 
     private:
         Ref<FontFaceSet> m_target;