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 "mozilla/Try.h"
14 #include "nsContentUtils.h"
16 using namespace mozilla
;
17 using namespace mozilla::dom
;
21 class JSEnumerator final
: public nsIJSEnumerator
{
23 NS_DECL_NSIJSENUMERATOR
25 explicit JSEnumerator(nsISimpleEnumerator
* aEnumerator
, const nsID
& aIID
)
26 : mEnumerator(aEnumerator
), mIID(aIID
) {}
29 ~JSEnumerator() = default;
31 nsCOMPtr
<nsISimpleEnumerator
> mEnumerator
;
35 } // anonymous namespace
37 nsresult
JSEnumerator::Iterator(nsIJSEnumerator
** aResult
) {
38 RefPtr
<JSEnumerator
> result(this);
39 result
.forget(aResult
);
43 nsresult
JSEnumerator::Next(JSContext
* aCx
, JS::MutableHandleValue aResult
) {
44 RootedDictionary
<IteratorResult
> result(aCx
);
46 nsCOMPtr
<nsISupports
> elem
;
47 if (NS_FAILED(mEnumerator
->GetNext(getter_AddRefs(elem
)))) {
52 JS::RootedValue
value(aCx
);
53 MOZ_TRY(nsContentUtils::WrapNative(aCx
, elem
, &mIID
, &value
));
54 result
.mValue
= value
;
57 if (!ToJSValue(aCx
, result
, aResult
)) {
58 return NS_ERROR_OUT_OF_MEMORY
;
63 NS_IMPL_ISUPPORTS(JSEnumerator
, nsIJSEnumerator
)
65 nsresult
nsSimpleEnumerator::Iterator(nsIJSEnumerator
** aResult
) {
66 auto result
= MakeRefPtr
<JSEnumerator
>(this, DefaultInterface());
67 result
.forget(aResult
);
71 nsresult
nsSimpleEnumerator::Entries(const nsIID
& aIface
,
72 nsIJSEnumerator
** aResult
) {
73 auto result
= MakeRefPtr
<JSEnumerator
>(this, aIface
);
74 result
.forget(aResult
);
78 NS_IMPL_ISUPPORTS(nsSimpleEnumerator
, nsISimpleEnumerator
,
79 nsISimpleEnumeratorBase
)