DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#ifndef AddonManagerStartup_inlines_h
#define AddonManagerStartup_inlines_h

#include "jsapi.h"
#include "nsJSUtils.h"

#include "mozilla/Maybe.h"
#include "mozilla/Move.h"

namespace mozilla {

class ArrayIterElem;
class PropertyIterElem;

/*****************************************************************************
 * Object iterator base classes
 *****************************************************************************/

template <class T, class PropertyType>
class MOZ_STACK_CLASS BaseIter {
 public:
  typedef T SelfType;

  PropertyType begin() const {
    PropertyType elem(Self());
    return std::move(elem);
  }

  PropertyType end() const {
    PropertyType elem(Self());
    return elem.End();
  }

  void* Context() const { return mContext; }

 protected:
  BaseIter(JSContext* cx, JS::HandleObject object, void* context = nullptr)
      : mCx(cx), mObject(object), mContext(context) {}

  const SelfType& Self() const { return *static_cast<const SelfType*>(this); }
  SelfType& Self() { return *static_cast<SelfType*>(this); }

  JSContext* mCx;

  JS::HandleObject mObject;

  void* mContext;
};

template <class T, class IterType>
class MOZ_STACK_CLASS BaseIterElem {
 public:
  typedef T SelfType;

  explicit BaseIterElem(const IterType& iter, uint32_t index = 0)
      : mIter(iter), mIndex(index) {}

  uint32_t Length() const { return mIter.Length(); }

  JS::Value Value() {
    JS::RootedValue value(mIter.mCx, JS::UndefinedValue());

    auto& self = Self();
    if (!self.GetValue(&value)) {
      JS_ClearPendingException(mIter.mCx);
    }

    return value;
  }

  SelfType& operator*() { return Self(); }

  SelfType& operator++() {
    MOZ_ASSERT(mIndex < Length());
    mIndex++;
    return Self();
  }

  bool operator!=(const SelfType& other) const {
    return &mIter != &other.mIter || mIndex != other.mIndex;
  }

  SelfType End() const {
    SelfType end(mIter);
    end.mIndex = Length();
    return std::move(end);
  }

  void* Context() const { return mIter.Context(); }

 protected:
  const SelfType& Self() const { return *static_cast<const SelfType*>(this); }
  SelfType& Self() { return *static_cast<SelfType*>(this); }

  const IterType& mIter;

  uint32_t mIndex;
};

/*****************************************************************************
 * Property iteration
 *****************************************************************************/

class MOZ_STACK_CLASS PropertyIter
    : public BaseIter<PropertyIter, PropertyIterElem> {
  friend class PropertyIterElem;
  friend class BaseIterElem<PropertyIterElem, PropertyIter>;

 public:
  PropertyIter(JSContext* cx, JS::HandleObject object, void* context = nullptr)
      : BaseIter(cx, object, context), mIds(cx, JS::IdVector(cx)) {
    if (!JS_Enumerate(cx, object, &mIds)) {
      JS_ClearPendingException(cx);
    }
  }

  PropertyIter(const PropertyIter& other)
      : PropertyIter(other.mCx, other.mObject, other.mContext) {}

  PropertyIter& operator=(const PropertyIter& other) {
    MOZ_ASSERT(other.mObject == mObject);
    mCx = other.mCx;
    mContext = other.mContext;

    mIds.clear();
    if (!JS_Enumerate(mCx, mObject, &mIds)) {
      JS_ClearPendingException(mCx);
    }
    return *this;
  }

  int32_t Length() const { return mIds.length(); }

 protected:
  JS::Rooted<JS::IdVector> mIds;
};

class MOZ_STACK_CLASS PropertyIterElem
    : public BaseIterElem<PropertyIterElem, PropertyIter> {
  friend class BaseIterElem<PropertyIterElem, PropertyIter>;

 public:
  using BaseIterElem::BaseIterElem;

  PropertyIterElem(const PropertyIterElem& other)
      : BaseIterElem(other.mIter, other.mIndex) {}

  jsid Id() {
    MOZ_ASSERT(mIndex < mIter.mIds.length());

    return mIter.mIds[mIndex];
  }

  const nsAString& Name() {
    if (mName.isNothing()) {
      mName.emplace();
      mName.ref().init(mIter.mCx, Id());
    }
    return mName.ref();
  }

  JSContext* Cx() { return mIter.mCx; }

 protected:
  bool GetValue(JS::MutableHandleValue value) {
    MOZ_ASSERT(mIndex < Length());
    JS::Rooted<jsid> id(mIter.mCx, Id());

    return JS_GetPropertyById(mIter.mCx, mIter.mObject, id, value);
  }

 private:
  Maybe<nsAutoJSString> mName;
};

/*****************************************************************************
 * Array iteration
 *****************************************************************************/

class MOZ_STACK_CLASS ArrayIter : public BaseIter<ArrayIter, ArrayIterElem> {
  friend class ArrayIterElem;
  friend class BaseIterElem<ArrayIterElem, ArrayIter>;

 public:
  ArrayIter(JSContext* cx, JS::HandleObject object)
      : BaseIter(cx, object), mLength(0) {
    bool isArray;
    if (!JS_IsArrayObject(cx, object, &isArray) || !isArray) {
      JS_ClearPendingException(cx);
      return;
    }

    if (!JS_GetArrayLength(cx, object, &mLength)) {
      JS_ClearPendingException(cx);
    }
  }

  uint32_t Length() const { return mLength; }

 private:
  uint32_t mLength;
};

class MOZ_STACK_CLASS ArrayIterElem
    : public BaseIterElem<ArrayIterElem, ArrayIter> {
  friend class BaseIterElem<ArrayIterElem, ArrayIter>;

 public:
  using BaseIterElem::BaseIterElem;

  ArrayIterElem(const ArrayIterElem& other)
      : BaseIterElem(other.mIter, other.mIndex) {}

 protected:
  bool GetValue(JS::MutableHandleValue value) {
    MOZ_ASSERT(mIndex < Length());
    return JS_GetElement(mIter.mCx, mIter.mObject, mIndex, value);
  }
};

}  // namespace mozilla

#endif  // AddonManagerStartup_inlines_h