tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

List.h (2276B)


      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/. */
      6 
      7 #ifndef vm_List_h
      8 #define vm_List_h
      9 
     10 #include "NamespaceImports.h"
     11 #include "js/Value.h"
     12 #include "vm/NativeObject.h"
     13 
     14 namespace js {
     15 
     16 /**
     17 * The List specification type, ECMA-262 6.2.1.
     18 * <https://tc39.github.io/ecma262/#sec-list-and-record-specification-type>
     19 *
     20 * Lists are simple mutable sequences of values. Many standards use them.
     21 * Abstractly, they're not objects; they don't have properties or prototypes;
     22 * they're for internal specification use only. ListObject is our most direct
     23 * implementation of a List: store the values in the slots of a JSObject.
     24 *
     25 * We often implement Lists in other ways. For example, builtin/Utilities.js
     26 * contains a completely unrelated List constructor that's used in self-hosted
     27 * code. And AsyncGeneratorObject optimizes away the ListObject in the common
     28 * case where its internal queue never holds more than one element.
     29 *
     30 * ListObjects must not be exposed to content scripts.
     31 */
     32 class ListObject : public NativeObject {
     33 public:
     34  static const JSClass class_;
     35 
     36  [[nodiscard]] inline static ListObject* create(JSContext* cx);
     37 
     38  uint32_t length() const { return getDenseInitializedLength(); }
     39 
     40  bool isEmpty() const { return length() == 0; }
     41 
     42  const Value& get(uint32_t index) const { return getDenseElement(index); }
     43 
     44  template <class T>
     45  T& getAs(uint32_t index) const {
     46    return get(index).toObject().as<T>();
     47  }
     48 
     49  /**
     50   * Add an element to the end of the list. Returns false on OOM.
     51   */
     52  [[nodiscard]] inline bool append(JSContext* cx, Value value);
     53 
     54  /**
     55   * Remove and return the first element of the list.
     56   *
     57   * Precondition: This list is not empty.
     58   */
     59  inline JS::Value popFirst(JSContext* cx);
     60 
     61  /**
     62   * Remove and return the first element of the list.
     63   *
     64   * Precondition: This list is not empty, and the first element
     65   * is an object of class T.
     66   */
     67  template <class T>
     68  inline T& popFirstAs(JSContext* cx);
     69 };
     70 
     71 }  // namespace js
     72 
     73 #endif  // vm_List_h