1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
7 #include "nsSimpleEnumerator.h"
9 #include "mozilla/dom/IteratorResultBinding.h"
10 #include "mozilla/dom/RootedDictionary.h"
11 #include "mozilla/dom/ToJSValue.h"
12 #include "mozilla/ResultExtensions.h"
13 #include "nsContentUtils.h"
15 using namespace mozilla
;
16 using namespace mozilla::dom
;
20 class JSEnumerator final
: public nsIJSEnumerator
{
22 NS_DECL_NSIJSENUMERATOR
24 explicit JSEnumerator(nsISimpleEnumerator
* aEnumerator
, const nsID
& aIID
)
25 : mEnumerator(aEnumerator
), mIID(aIID
) {}
28 ~JSEnumerator() = default;
30 nsCOMPtr
<nsISimpleEnumerator
> mEnumerator
;
34 } // anonymous namespace
36 nsresult
JSEnumerator::Iterator(nsIJSEnumerator
** aResult
) {
37 RefPtr
<JSEnumerator
> result(this);
38 result
.forget(aResult
);
42 nsresult
JSEnumerator::Next(JSContext
* aCx
, JS::MutableHandleValue aResult
) {
43 RootedDictionary
<IteratorResult
> result(aCx
);
45 nsCOMPtr
<nsISupports
> elem
;
46 if (NS_FAILED(mEnumerator
->GetNext(getter_AddRefs(elem
)))) {
51 JS::RootedValue
value(aCx
);
52 MOZ_TRY(nsContentUtils::WrapNative(aCx
, elem
, &mIID
, &value
));
53 result
.mValue
= value
;
56 if (!ToJSValue(aCx
, result
, aResult
)) {
57 return NS_ERROR_OUT_OF_MEMORY
;
62 NS_IMPL_ISUPPORTS(JSEnumerator
, nsIJSEnumerator
)
64 nsresult
nsSimpleEnumerator::Iterator(nsIJSEnumerator
** aResult
) {
65 auto result
= MakeRefPtr
<JSEnumerator
>(this, DefaultInterface());
66 result
.forget(aResult
);
70 nsresult
nsSimpleEnumerator::Entries(const nsIID
& aIface
,
71 nsIJSEnumerator
** aResult
) {
72 auto result
= MakeRefPtr
<JSEnumerator
>(this, aIface
);
73 result
.forget(aResult
);
77 NS_IMPL_ISUPPORTS(nsSimpleEnumerator
, nsISimpleEnumerator
,
78 nsISimpleEnumeratorBase
)