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.

Implementation

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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * 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/. */

/*
 * Iterators for various data structures.
 */

#ifndef gc_PublicIterators_h
#define gc_PublicIterators_h

#include "mozilla/Maybe.h"

#include "gc/GCRuntime.h"
#include "gc/Zone.h"
#include "vm/Compartment.h"

namespace JS {
class Realm;
}

namespace js {

// Accessing the atoms zone can be dangerous because helper threads may be
// accessing it concurrently to the main thread, so it's better to skip the
// atoms zone when iterating over zones. If you need to iterate over the atoms
// zone, consider using AutoLockAllAtoms.
enum ZoneSelector { WithAtoms, SkipAtoms };

// Iterate over all zones in the runtime, except those which may be in use by
// parse threads.
class ZonesIter {
  gc::AutoEnterIteration iterMarker;
  JS::Zone* atomsZone;
  JS::Zone** it;
  JS::Zone** end;

 public:
  ZonesIter(JSRuntime* rt, ZoneSelector selector)
      : iterMarker(&rt->gc),
        atomsZone(selector == WithAtoms ? rt->gc.atomsZone.ref() : nullptr),
        it(rt->gc.zones().begin()),
        end(rt->gc.zones().end()) {
    if (!atomsZone) {
      skipHelperThreadZones();
    }
  }

  bool done() const { return !atomsZone && it == end; }

  void next() {
    MOZ_ASSERT(!done());
    if (atomsZone) {
      atomsZone = nullptr;
    } else {
      it++;
    }
    skipHelperThreadZones();
  }

  void skipHelperThreadZones() {
    while (!done() && get()->usedByHelperThread()) {
      it++;
    }
  }

  JS::Zone* get() const {
    MOZ_ASSERT(!done());
    return atomsZone ? atomsZone : *it;
  }

  operator JS::Zone*() const { return get(); }
  JS::Zone* operator->() const { return get(); }
};

struct CompartmentsInZoneIter {
  using ItemType = JS::Compartment;

  explicit CompartmentsInZoneIter(JS::Zone* zone) : zone(zone) {
    it = zone->compartments().begin();
  }

  bool done() const {
    MOZ_ASSERT(it);
    return it < zone->compartments().begin() ||
           it >= zone->compartments().end();
  }
  void next() {
    MOZ_ASSERT(!done());
    it++;
  }

  JS::Compartment* get() const {
    MOZ_ASSERT(it);
    return *it;
  }

  operator JS::Compartment*() const { return get(); }
  JS::Compartment* operator->() const { return get(); }

 private:
  JS::Zone* zone;
  JS::Compartment** it;
};

class RealmsInCompartmentIter {
  JS::Compartment* comp;
  JS::Realm** it;

 public:
  explicit RealmsInCompartmentIter(JS::Compartment* comp) : comp(comp) {
    it = comp->realms().begin();
  }

  bool done() const {
    MOZ_ASSERT(it);
    return it < comp->realms().begin() || it >= comp->realms().end();
  }
  void next() {
    MOZ_ASSERT(!done());
    it++;
  }

  JS::Realm* get() const {
    MOZ_ASSERT(!done());
    return *it;
  }

  operator JS::Realm*() const { return get(); }
  JS::Realm* operator->() const { return get(); }
};

class RealmsInZoneIter {
  CompartmentsInZoneIter comp;
  mozilla::Maybe<RealmsInCompartmentIter> realm;

 public:
  using ItemType = JS::Realm;

  explicit RealmsInZoneIter(JS::Zone* zone) : comp(zone) {
    settleOnCompartment();
  }

  void settleOnCompartment() {
    if (!comp.done()) {
      realm.emplace(comp.get());
      MOZ_ASSERT(!realm->done(), "compartment must have at least one realm");
    }
  }

  bool done() const {
    MOZ_ASSERT(comp.done() == realm.isNothing());
    return comp.done();
  }
  void next() {
    MOZ_ASSERT(!done());

    realm->next();

    if (realm->done()) {
      realm.reset();
      comp.next();
      settleOnCompartment();
    }
  }

  JS::Realm* get() const { return realm->get(); }

  operator JS::Realm*() const { return get(); }
  JS::Realm* operator->() const { return get(); }
};

// This iterator iterates over all the compartments or realms in a given set of
// zones. The set of zones is determined by iterating ZoneIterT. The set of
// compartments or realms is determined by InnerIterT.
template <class ZonesIterT, class InnerIterT>
class CompartmentsOrRealmsIterT {
  using T = typename InnerIterT::ItemType;

  gc::AutoEnterIteration iterMarker;
  ZonesIterT zone;
  mozilla::Maybe<InnerIterT> inner;

 public:
  explicit CompartmentsOrRealmsIterT(JSRuntime* rt)
      : iterMarker(&rt->gc), zone(rt, SkipAtoms) {
    if (!zone.done()) {
      inner.emplace(zone);
    }
  }

  bool done() const { return zone.done(); }

  void next() {
    MOZ_ASSERT(!done());
    MOZ_ASSERT(!inner.ref().done());
    inner->next();
    if (inner->done()) {
      inner.reset();
      zone.next();
      if (!zone.done()) {
        inner.emplace(zone);
      }
    }
  }

  T* get() const {
    MOZ_ASSERT(!done());
    return *inner;
  }

  operator T*() const { return get(); }
  T* operator->() const { return get(); }
};

using CompartmentsIter =
    CompartmentsOrRealmsIterT<ZonesIter, CompartmentsInZoneIter>;
using RealmsIter = CompartmentsOrRealmsIterT<ZonesIter, RealmsInZoneIter>;

}  // namespace js

#endif  // gc_PublicIterators_h