2017-10-27 01:08:41 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2015-10-15 20:06:44 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The IterableIterator class is used for WebIDL interfaces that have a
|
2016-02-18 06:58:07 +03:00
|
|
|
* iterable<> member defined with two types (so a pair iterator). It handles
|
|
|
|
* the ES6 Iterator-like functions that are generated for the iterable
|
|
|
|
* interface.
|
2015-10-15 20:06:44 +03:00
|
|
|
*
|
2016-02-18 06:58:07 +03:00
|
|
|
* For iterable interfaces with a pair iterator, the implementation class will
|
|
|
|
* need to implement these two functions:
|
2015-10-15 20:06:44 +03:00
|
|
|
*
|
|
|
|
* - size_t GetIterableLength()
|
|
|
|
* - Returns the number of elements available to iterate over
|
2015-10-16 22:54:51 +03:00
|
|
|
* - [type] GetValueAtIndex(size_t index)
|
|
|
|
* - Returns the value at the requested index.
|
2015-10-15 20:06:44 +03:00
|
|
|
* - [type] GetKeyAtIndex(size_t index)
|
|
|
|
* - Returns the key at the requested index
|
|
|
|
*
|
|
|
|
* Examples of iterable interface implementations can be found in the bindings
|
|
|
|
* test directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_IterableIterator_h
|
|
|
|
#define mozilla_dom_IterableIterator_h
|
|
|
|
|
2020-11-23 19:12:02 +03:00
|
|
|
#include "js/TypeDecls.h"
|
|
|
|
#include "js/Value.h"
|
2015-10-15 20:06:44 +03:00
|
|
|
#include "nsISupports.h"
|
2020-11-23 19:12:02 +03:00
|
|
|
#include "mozilla/dom/IterableIteratorBinding.h"
|
2020-07-15 18:34:49 +03:00
|
|
|
#include "mozilla/dom/RootedDictionary.h"
|
2015-10-15 20:06:44 +03:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class IterableIteratorBase : public nsISupports {
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(IterableIteratorBase)
|
|
|
|
typedef enum { Keys = 0, Values, Entries } IterableIteratorType;
|
|
|
|
|
2020-02-20 18:47:53 +03:00
|
|
|
IterableIteratorBase() = default;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2015-10-15 20:06:44 +03:00
|
|
|
protected:
|
2020-02-20 18:47:53 +03:00
|
|
|
virtual ~IterableIteratorBase() = default;
|
2015-10-15 20:06:44 +03:00
|
|
|
virtual void UnlinkHelper() = 0;
|
|
|
|
virtual void TraverseHelper(nsCycleCollectionTraversalCallback& cb) = 0;
|
|
|
|
};
|
|
|
|
|
2018-07-18 23:09:04 +03:00
|
|
|
// Helpers to call iterator getter methods with the correct arguments, depending
|
|
|
|
// on the types they return, and convert the result to JS::Values.
|
|
|
|
|
|
|
|
// Helper for Get[Key,Value]AtIndex(uint32_t) methods, which accept an index and
|
|
|
|
// return a type supported by ToJSValue.
|
|
|
|
template <typename T, typename U>
|
|
|
|
bool CallIterableGetter(JSContext* aCx, U (T::*aMethod)(uint32_t), T* aInst,
|
|
|
|
uint32_t aIndex, JS::MutableHandle<JS::Value> aResult) {
|
|
|
|
return ToJSValue(aCx, (aInst->*aMethod)(aIndex), aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
bool CallIterableGetter(JSContext* aCx, U (T::*aMethod)(uint32_t) const,
|
|
|
|
const T* aInst, uint32_t aIndex,
|
|
|
|
JS::MutableHandle<JS::Value> aResult) {
|
|
|
|
return ToJSValue(aCx, (aInst->*aMethod)(aIndex), aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper for Get[Key,Value]AtIndex(JSContext*, uint32_t, MutableHandleValue)
|
|
|
|
// methods, which accept a JS context, index, and mutable result value handle,
|
|
|
|
// and return true on success or false on failure.
|
|
|
|
template <typename T>
|
|
|
|
bool CallIterableGetter(JSContext* aCx,
|
|
|
|
bool (T::*aMethod)(JSContext*, uint32_t,
|
|
|
|
JS::MutableHandle<JS::Value>),
|
|
|
|
T* aInst, uint32_t aIndex,
|
|
|
|
JS::MutableHandle<JS::Value> aResult) {
|
|
|
|
return (aInst->*aMethod)(aCx, aIndex, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool CallIterableGetter(JSContext* aCx,
|
|
|
|
bool (T::*aMethod)(JSContext*, uint32_t,
|
|
|
|
JS::MutableHandle<JS::Value>) const,
|
|
|
|
const T* aInst, uint32_t aIndex,
|
|
|
|
JS::MutableHandle<JS::Value> aResult) {
|
|
|
|
return (aInst->*aMethod)(aCx, aIndex, aResult);
|
|
|
|
}
|
|
|
|
|
2015-10-15 20:06:44 +03:00
|
|
|
template <typename T>
|
2016-02-18 06:58:07 +03:00
|
|
|
class IterableIterator final : public IterableIteratorBase {
|
2015-10-16 22:54:51 +03:00
|
|
|
public:
|
|
|
|
typedef bool (*WrapFunc)(JSContext* aCx, IterableIterator<T>* aObject,
|
|
|
|
JS::Handle<JSObject*> aGivenProto,
|
|
|
|
JS::MutableHandle<JSObject*> aReflector);
|
|
|
|
|
2016-02-18 06:58:07 +03:00
|
|
|
explicit IterableIterator(T* aIterableObj, IterableIteratorType aIteratorType,
|
|
|
|
WrapFunc aWrapFunc)
|
|
|
|
: mIterableObj(aIterableObj),
|
2015-10-16 22:54:51 +03:00
|
|
|
mIteratorType(aIteratorType),
|
|
|
|
mWrapFunc(aWrapFunc),
|
|
|
|
mIndex(0) {
|
2016-02-18 06:58:07 +03:00
|
|
|
MOZ_ASSERT(mIterableObj);
|
2015-10-16 22:54:51 +03:00
|
|
|
MOZ_ASSERT(mWrapFunc);
|
|
|
|
}
|
|
|
|
|
2018-07-18 23:09:04 +03:00
|
|
|
bool GetKeyAtIndex(JSContext* aCx, uint32_t aIndex,
|
|
|
|
JS::MutableHandle<JS::Value> aResult) {
|
|
|
|
return CallIterableGetter(aCx, &T::GetKeyAtIndex, mIterableObj.get(),
|
|
|
|
aIndex, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetValueAtIndex(JSContext* aCx, uint32_t aIndex,
|
|
|
|
JS::MutableHandle<JS::Value> aResult) {
|
|
|
|
return CallIterableGetter(aCx, &T::GetValueAtIndex, mIterableObj.get(),
|
|
|
|
aIndex, aResult);
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:54:51 +03:00
|
|
|
void Next(JSContext* aCx, JS::MutableHandle<JSObject*> aResult,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::UndefinedValue());
|
|
|
|
if (mIndex >= this->mIterableObj->GetIterableLength()) {
|
|
|
|
DictReturn(aCx, aResult, true, value, aRv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (mIteratorType) {
|
|
|
|
case IterableIteratorType::Keys: {
|
2018-07-18 23:09:04 +03:00
|
|
|
if (!GetKeyAtIndex(aCx, mIndex, &value)) {
|
2015-10-15 20:06:44 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-16 22:54:51 +03:00
|
|
|
DictReturn(aCx, aResult, false, value, aRv);
|
|
|
|
break;
|
2015-10-15 20:06:44 +03:00
|
|
|
}
|
2015-10-16 22:54:51 +03:00
|
|
|
case IterableIteratorType::Values: {
|
2018-07-18 23:09:04 +03:00
|
|
|
if (!GetValueAtIndex(aCx, mIndex, &value)) {
|
2015-10-15 20:06:44 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-10-16 22:54:51 +03:00
|
|
|
DictReturn(aCx, aResult, false, value, aRv);
|
|
|
|
break;
|
2015-10-16 23:16:36 +03:00
|
|
|
}
|
2015-10-16 22:54:51 +03:00
|
|
|
case IterableIteratorType::Entries: {
|
|
|
|
JS::Rooted<JS::Value> key(aCx);
|
2018-07-18 23:09:04 +03:00
|
|
|
if (!GetKeyAtIndex(aCx, mIndex, &key)) {
|
2015-10-15 20:06:44 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-07-18 23:09:04 +03:00
|
|
|
if (!GetValueAtIndex(aCx, mIndex, &value)) {
|
2015-10-15 20:06:44 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-10-16 22:54:51 +03:00
|
|
|
KeyAndValueReturn(aCx, key, value, aResult, aRv);
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2015-10-15 20:06:44 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Invalid iterator type!");
|
|
|
|
}
|
|
|
|
++mIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
|
|
|
|
JS::MutableHandle<JSObject*> aObj) {
|
|
|
|
return (*mWrapFunc)(aCx, this, aGivenProto, aObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-02-18 06:58:07 +03:00
|
|
|
static void DictReturn(JSContext* aCx, JS::MutableHandle<JSObject*> aResult,
|
|
|
|
bool aDone, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
RootedDictionary<IterableKeyOrValueResult> dict(aCx);
|
|
|
|
dict.mDone = aDone;
|
|
|
|
dict.mValue = aValue;
|
|
|
|
JS::Rooted<JS::Value> dictValue(aCx);
|
|
|
|
if (!ToJSValue(aCx, dict, &dictValue)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
aResult.set(&dictValue.toObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void KeyAndValueReturn(JSContext* aCx, JS::Handle<JS::Value> aKey,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
JS::MutableHandle<JSObject*> aResult,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
RootedDictionary<IterableKeyAndValueResult> dict(aCx);
|
|
|
|
dict.mDone = false;
|
|
|
|
// Dictionary values are a Sequence, which is a FallibleTArray, so we need
|
|
|
|
// to check returns when appending.
|
|
|
|
if (!dict.mValue.AppendElement(aKey, mozilla::fallible)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!dict.mValue.AppendElement(aValue, mozilla::fallible)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JS::Rooted<JS::Value> dictValue(aCx);
|
|
|
|
if (!ToJSValue(aCx, dict, &dictValue)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
aResult.set(&dictValue.toObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-02-20 18:47:53 +03:00
|
|
|
virtual ~IterableIterator() = default;
|
2016-02-18 06:58:07 +03:00
|
|
|
|
|
|
|
// Since we're templated on a binding, we need to possibly CC it, but can't do
|
|
|
|
// that through macros. So it happens here.
|
2018-02-09 08:22:43 +03:00
|
|
|
void UnlinkHelper() final { mIterableObj = nullptr; }
|
2016-02-18 06:58:07 +03:00
|
|
|
|
|
|
|
virtual void TraverseHelper(nsCycleCollectionTraversalCallback& cb) override {
|
|
|
|
IterableIterator<T>* tmp = this;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIterableObj);
|
|
|
|
}
|
2015-10-16 22:54:51 +03:00
|
|
|
|
2016-02-18 06:58:07 +03:00
|
|
|
// Binding Implementation object that we're iterating over.
|
|
|
|
RefPtr<T> mIterableObj;
|
2015-10-15 20:06:44 +03:00
|
|
|
// Tells whether this is a key, value, or entries iterator.
|
|
|
|
IterableIteratorType mIteratorType;
|
2015-10-16 23:16:36 +03:00
|
|
|
// Function pointer to binding-type-specific Wrap() call for this iterator.
|
|
|
|
WrapFunc mWrapFunc;
|
2015-10-16 22:54:51 +03:00
|
|
|
// Current index of iteration.
|
|
|
|
uint32_t mIndex;
|
2015-10-15 20:06:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_IterableIterator_h
|