IterableIterator.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim:set ts=2 sw=2 sts=2 et cindent: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /**
  7. * The IterableIterator class is used for WebIDL interfaces that have a
  8. * iterable<> member defined with two types (so a pair iterator). It handles
  9. * the ES6 Iterator-like functions that are generated for the iterable
  10. * interface.
  11. *
  12. * For iterable interfaces with a pair iterator, the implementation class will
  13. * need to implement these two functions:
  14. *
  15. * - size_t GetIterableLength()
  16. * - Returns the number of elements available to iterate over
  17. * - [type] GetValueAtIndex(size_t index)
  18. * - Returns the value at the requested index.
  19. * - [type] GetKeyAtIndex(size_t index)
  20. * - Returns the key at the requested index
  21. *
  22. * Examples of iterable interface implementations can be found in the bindings
  23. * test directory.
  24. */
  25. #ifndef mozilla_dom_IterableIterator_h
  26. #define mozilla_dom_IterableIterator_h
  27. #include "nsISupports.h"
  28. #include "nsWrapperCache.h"
  29. #include "nsPIDOMWindow.h"
  30. #include "nsCOMPtr.h"
  31. #include "mozilla/dom/ToJSValue.h"
  32. #include "jswrapper.h"
  33. #include "mozilla/dom/IterableIteratorBinding.h"
  34. namespace mozilla {
  35. namespace dom {
  36. class IterableIteratorBase : public nsISupports
  37. {
  38. public:
  39. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  40. NS_DECL_CYCLE_COLLECTION_CLASS(IterableIteratorBase)
  41. typedef enum {
  42. Keys = 0,
  43. Values,
  44. Entries
  45. } IterableIteratorType;
  46. IterableIteratorBase() {}
  47. protected:
  48. virtual ~IterableIteratorBase() {}
  49. virtual void UnlinkHelper() = 0;
  50. virtual void TraverseHelper(nsCycleCollectionTraversalCallback& cb) = 0;
  51. };
  52. template <typename T>
  53. class IterableIterator final : public IterableIteratorBase
  54. {
  55. public:
  56. typedef bool (*WrapFunc)(JSContext* aCx,
  57. IterableIterator<T>* aObject,
  58. JS::Handle<JSObject*> aGivenProto,
  59. JS::MutableHandle<JSObject*> aReflector);
  60. explicit IterableIterator(T* aIterableObj,
  61. IterableIteratorType aIteratorType,
  62. WrapFunc aWrapFunc)
  63. : mIterableObj(aIterableObj)
  64. , mIteratorType(aIteratorType)
  65. , mWrapFunc(aWrapFunc)
  66. , mIndex(0)
  67. {
  68. MOZ_ASSERT(mIterableObj);
  69. MOZ_ASSERT(mWrapFunc);
  70. }
  71. void
  72. Next(JSContext* aCx, JS::MutableHandle<JSObject*> aResult, ErrorResult& aRv)
  73. {
  74. JS::Rooted<JS::Value> value(aCx, JS::UndefinedValue());
  75. if (mIndex >= this->mIterableObj->GetIterableLength()) {
  76. DictReturn(aCx, aResult, true, value, aRv);
  77. return;
  78. }
  79. switch (mIteratorType) {
  80. case IterableIteratorType::Keys:
  81. {
  82. if (!ToJSValue(aCx, this->mIterableObj->GetKeyAtIndex(mIndex), &value)) {
  83. aRv.Throw(NS_ERROR_FAILURE);
  84. return;
  85. }
  86. DictReturn(aCx, aResult, false, value, aRv);
  87. break;
  88. }
  89. case IterableIteratorType::Values:
  90. {
  91. if (!ToJSValue(aCx, this->mIterableObj->GetValueAtIndex(mIndex), &value)) {
  92. aRv.Throw(NS_ERROR_FAILURE);
  93. return;
  94. }
  95. DictReturn(aCx, aResult, false, value, aRv);
  96. break;
  97. }
  98. case IterableIteratorType::Entries:
  99. {
  100. JS::Rooted<JS::Value> key(aCx);
  101. if (!ToJSValue(aCx, this->mIterableObj->GetKeyAtIndex(mIndex), &key)) {
  102. aRv.Throw(NS_ERROR_FAILURE);
  103. return;
  104. }
  105. if (!ToJSValue(aCx, this->mIterableObj->GetValueAtIndex(mIndex), &value)) {
  106. aRv.Throw(NS_ERROR_FAILURE);
  107. return;
  108. }
  109. KeyAndValueReturn(aCx, key, value, aResult, aRv);
  110. break;
  111. }
  112. default:
  113. MOZ_CRASH("Invalid iterator type!");
  114. }
  115. ++mIndex;
  116. }
  117. bool
  118. WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aObj)
  119. {
  120. return (*mWrapFunc)(aCx, this, aGivenProto, aObj);
  121. }
  122. protected:
  123. static void
  124. DictReturn(JSContext* aCx, JS::MutableHandle<JSObject*> aResult,
  125. bool aDone, JS::Handle<JS::Value> aValue, ErrorResult& aRv)
  126. {
  127. RootedDictionary<IterableKeyOrValueResult> dict(aCx);
  128. dict.mDone = aDone;
  129. dict.mValue = aValue;
  130. JS::Rooted<JS::Value> dictValue(aCx);
  131. if (!ToJSValue(aCx, dict, &dictValue)) {
  132. aRv.Throw(NS_ERROR_FAILURE);
  133. return;
  134. }
  135. aResult.set(&dictValue.toObject());
  136. }
  137. static void
  138. KeyAndValueReturn(JSContext* aCx, JS::Handle<JS::Value> aKey,
  139. JS::Handle<JS::Value> aValue,
  140. JS::MutableHandle<JSObject*> aResult, ErrorResult& aRv)
  141. {
  142. RootedDictionary<IterableKeyAndValueResult> dict(aCx);
  143. dict.mDone = false;
  144. // Dictionary values are a Sequence, which is a FallibleTArray, so we need
  145. // to check returns when appending.
  146. if (!dict.mValue.AppendElement(aKey, mozilla::fallible)) {
  147. aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
  148. return;
  149. }
  150. if (!dict.mValue.AppendElement(aValue, mozilla::fallible)) {
  151. aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
  152. return;
  153. }
  154. JS::Rooted<JS::Value> dictValue(aCx);
  155. if (!ToJSValue(aCx, dict, &dictValue)) {
  156. aRv.Throw(NS_ERROR_FAILURE);
  157. return;
  158. }
  159. aResult.set(&dictValue.toObject());
  160. }
  161. protected:
  162. virtual ~IterableIterator() {}
  163. // Since we're templated on a binding, we need to possibly CC it, but can't do
  164. // that through macros. So it happens here.
  165. virtual void UnlinkHelper() final
  166. {
  167. mIterableObj = nullptr;
  168. }
  169. virtual void TraverseHelper(nsCycleCollectionTraversalCallback& cb) override
  170. {
  171. IterableIterator<T>* tmp = this;
  172. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIterableObj);
  173. }
  174. // Binding Implementation object that we're iterating over.
  175. RefPtr<T> mIterableObj;
  176. // Tells whether this is a key, value, or entries iterator.
  177. IterableIteratorType mIteratorType;
  178. // Function pointer to binding-type-specific Wrap() call for this iterator.
  179. WrapFunc mWrapFunc;
  180. // Current index of iteration.
  181. uint32_t mIndex;
  182. };
  183. } // namespace dom
  184. } // namespace mozilla
  185. #endif // mozilla_dom_IterableIterator_h