-
Notifications
You must be signed in to change notification settings - Fork 54
/
List.h
71 lines (58 loc) · 2.18 KB
/
List.h
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
/* -*- 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/. */
#ifndef vm_List_h
#define vm_List_h
#include "NamespaceImports.h"
#include "js/Value.h"
#include "vm/NativeObject.h"
namespace js {
/**
* The List specification type, ECMA-262 6.2.1.
* <https://tc39.github.io/ecma262/#sec-list-and-record-specification-type>
*
* Lists are simple mutable sequences of values. Many standards use them.
* Abstractly, they're not objects; they don't have properties or prototypes;
* they're for internal specification use only. ListObject is our most direct
* implementation of a List: store the values in the slots of a JSObject.
*
* We often implement Lists in other ways. For example, builtin/Utilities.js
* contains a completely unrelated List constructor that's used in self-hosted
* code. And AsyncGeneratorObject optimizes away the ListObject in the common
* case where its internal queue never holds more than one element.
*
* ListObjects must not be exposed to content scripts.
*/
class ListObject : public NativeObject {
public:
static const Class class_;
inline static MOZ_MUST_USE ListObject* create(JSContext* cx);
uint32_t length() const { return getDenseInitializedLength(); }
const Value& get(uint32_t index) const { return getDenseElement(index); }
template <class T>
T& getAs(uint32_t index) const {
return get(index).toObject().as<T>();
}
/**
* Add an element to the end of the list. Returns false on OOM.
*/
inline MOZ_MUST_USE bool append(JSContext* cx, HandleValue value);
/**
* Remove and return the first element of the list.
*
* Precondition: This list is not empty.
*/
inline JS::Value popFirst(JSContext* cx);
/**
* Remove and return the first element of the list.
*
* Precondition: This list is not empty, and the first element
* is an object of class T.
*/
template <class T>
inline T& popFirstAs(JSContext* cx);
};
} // namespace js
#endif // vm_List_h