11cb0ef41Sopenharmony_ci// Copyright 2019 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_OBJECTS_JS_COLLECTION_ITERATOR_H_
61cb0ef41Sopenharmony_ci#define V8_OBJECTS_JS_COLLECTION_ITERATOR_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/common/globals.h"
91cb0ef41Sopenharmony_ci#include "src/objects/js-objects.h"
101cb0ef41Sopenharmony_ci#include "src/objects/objects.h"
111cb0ef41Sopenharmony_ci#include "src/objects/smi.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci// Has to be the last include (doesn't have include guards):
141cb0ef41Sopenharmony_ci#include "src/objects/object-macros.h"
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cinamespace v8 {
171cb0ef41Sopenharmony_cinamespace internal {
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci#include "torque-generated/src/objects/js-collection-iterator-tq.inc"
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciclass JSCollectionIterator
221cb0ef41Sopenharmony_ci    : public TorqueGeneratedJSCollectionIterator<JSCollectionIterator,
231cb0ef41Sopenharmony_ci                                                 JSObject> {
241cb0ef41Sopenharmony_ci public:
251cb0ef41Sopenharmony_ci  void JSCollectionIteratorPrint(std::ostream& os, const char* name);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  TQ_OBJECT_CONSTRUCTORS(JSCollectionIterator)
281cb0ef41Sopenharmony_ci};
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci// OrderedHashTableIterator is an iterator that iterates over the keys and
311cb0ef41Sopenharmony_ci// values of an OrderedHashTable.
321cb0ef41Sopenharmony_ci//
331cb0ef41Sopenharmony_ci// The iterator has a reference to the underlying OrderedHashTable data,
341cb0ef41Sopenharmony_ci// [table], as well as the current [index] the iterator is at.
351cb0ef41Sopenharmony_ci//
361cb0ef41Sopenharmony_ci// When the OrderedHashTable is rehashed it adds a reference from the old table
371cb0ef41Sopenharmony_ci// to the new table as well as storing enough data about the changes so that the
381cb0ef41Sopenharmony_ci// iterator [index] can be adjusted accordingly.
391cb0ef41Sopenharmony_ci//
401cb0ef41Sopenharmony_ci// When the [Next] result from the iterator is requested, the iterator checks if
411cb0ef41Sopenharmony_ci// there is a newer table that it needs to transition to.
421cb0ef41Sopenharmony_citemplate <class Derived, class TableType>
431cb0ef41Sopenharmony_ciclass OrderedHashTableIterator : public JSCollectionIterator {
441cb0ef41Sopenharmony_ci public:
451cb0ef41Sopenharmony_ci  // Whether the iterator has more elements. This needs to be called before
461cb0ef41Sopenharmony_ci  // calling |CurrentKey| and/or |CurrentValue|.
471cb0ef41Sopenharmony_ci  bool HasMore();
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  // Move the index forward one.
501cb0ef41Sopenharmony_ci  void MoveNext() { set_index(Smi::FromInt(Smi::ToInt(index()) + 1)); }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  // Returns the current key of the iterator. This should only be called when
531cb0ef41Sopenharmony_ci  // |HasMore| returns true.
541cb0ef41Sopenharmony_ci  inline Object CurrentKey();
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci private:
571cb0ef41Sopenharmony_ci  // Transitions the iterator to the non obsolete backing store. This is a NOP
581cb0ef41Sopenharmony_ci  // if the [table] is not obsolete.
591cb0ef41Sopenharmony_ci  void Transition();
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  OBJECT_CONSTRUCTORS(OrderedHashTableIterator, JSCollectionIterator);
621cb0ef41Sopenharmony_ci};
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci}  // namespace internal
651cb0ef41Sopenharmony_ci}  // namespace v8
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci#include "src/objects/object-macros-undef.h"
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci#endif  // V8_OBJECTS_JS_COLLECTION_ITERATOR_H_
70