forked from BrowserWorks/Waterfox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathId.h
253 lines (214 loc) · 7.01 KB
/
Id.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* 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 js_Id_h
#define js_Id_h
// A jsid is an identifier for a property or method of an object which is
// either a 31-bit unsigned integer, interned string or symbol.
//
// Also, there is an additional jsid value, JSID_VOID, which does not occur in
// JS scripts but may be used to indicate the absence of a valid jsid. A void
// jsid is not a valid id and only arises as an exceptional API return value,
// such as in JS_NextProperty. Embeddings must not pass JSID_VOID into JSAPI
// entry points expecting a jsid and do not need to handle JSID_VOID in hooks
// receiving a jsid except when explicitly noted in the API contract.
//
// A jsid is not implicitly convertible to or from a Value; JS_ValueToId or
// JS_IdToValue must be used instead.
#include "jstypes.h"
#include "js/HeapAPI.h"
#include "js/RootingAPI.h"
#include "js/TypeDecls.h"
#include "js/Utility.h"
// All jsids with the low bit set are integer ids. This means the other type
// tags must all be even.
#define JSID_TYPE_INT_BIT 0x1
// Use 0 for JSID_TYPE_STRING to avoid a bitwise op for atom <-> id conversions.
#define JSID_TYPE_STRING 0x0
#define JSID_TYPE_VOID 0x2
#define JSID_TYPE_SYMBOL 0x4
#define JSID_TYPE_EMPTY 0x6
#define JSID_TYPE_MASK 0x7
struct jsid
{
size_t asBits;
constexpr jsid() : asBits(JSID_TYPE_VOID) {}
static constexpr MOZ_ALWAYS_INLINE jsid fromRawBits(size_t bits) {
jsid id;
id.asBits = bits;
return id;
}
bool operator==(const jsid& rhs) const { return asBits == rhs.asBits; }
bool operator!=(const jsid& rhs) const { return asBits != rhs.asBits; }
} JS_HAZ_GC_POINTER;
#define JSID_BITS(id) (id.asBits)
// Avoid using canonical 'id' for jsid parameters since this is a magic word in
// Objective-C++ which, apparently, wants to be able to #include jsapi.h.
#define id iden
static MOZ_ALWAYS_INLINE bool
JSID_IS_STRING(jsid id)
{
return (JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_STRING;
}
static MOZ_ALWAYS_INLINE JSString*
JSID_TO_STRING(jsid id)
{
// Use XOR instead of `& ~JSID_TYPE_MASK` because small immediates can be
// encoded more efficiently on some platorms.
MOZ_ASSERT(JSID_IS_STRING(id));
return (JSString*)(JSID_BITS(id) ^ JSID_TYPE_STRING);
}
/**
* Only JSStrings that have been interned via the JSAPI can be turned into
* jsids by API clients.
*
* N.B. if a jsid is backed by a string which has not been interned, that
* string must be appropriately rooted to avoid being collected by the GC.
*/
JS_PUBLIC_API(jsid)
INTERNED_STRING_TO_JSID(JSContext* cx, JSString* str);
static MOZ_ALWAYS_INLINE bool
JSID_IS_INT(jsid id)
{
return !!(JSID_BITS(id) & JSID_TYPE_INT_BIT);
}
static MOZ_ALWAYS_INLINE int32_t
JSID_TO_INT(jsid id)
{
MOZ_ASSERT(JSID_IS_INT(id));
uint32_t bits = static_cast<uint32_t>(JSID_BITS(id)) >> 1;
return static_cast<int32_t>(bits);
}
#define JSID_INT_MIN 0
#define JSID_INT_MAX INT32_MAX
static MOZ_ALWAYS_INLINE bool
INT_FITS_IN_JSID(int32_t i)
{
return i >= 0;
}
static MOZ_ALWAYS_INLINE jsid
INT_TO_JSID(int32_t i)
{
jsid id;
MOZ_ASSERT(INT_FITS_IN_JSID(i));
uint32_t bits = (static_cast<uint32_t>(i) << 1) | JSID_TYPE_INT_BIT;
JSID_BITS(id) = static_cast<size_t>(bits);
return id;
}
static MOZ_ALWAYS_INLINE bool
JSID_IS_SYMBOL(jsid id)
{
return (JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_SYMBOL;
}
static MOZ_ALWAYS_INLINE JS::Symbol*
JSID_TO_SYMBOL(jsid id)
{
MOZ_ASSERT(JSID_IS_SYMBOL(id));
return (JS::Symbol*)(JSID_BITS(id) ^ JSID_TYPE_SYMBOL);
}
static MOZ_ALWAYS_INLINE jsid
SYMBOL_TO_JSID(JS::Symbol* sym)
{
jsid id;
MOZ_ASSERT(sym != nullptr);
MOZ_ASSERT((size_t(sym) & JSID_TYPE_MASK) == 0);
MOZ_ASSERT(!js::gc::IsInsideNursery(reinterpret_cast<js::gc::Cell*>(sym)));
JSID_BITS(id) = (size_t(sym) | JSID_TYPE_SYMBOL);
return id;
}
static MOZ_ALWAYS_INLINE bool
JSID_IS_GCTHING(jsid id)
{
return JSID_IS_STRING(id) || JSID_IS_SYMBOL(id);
}
static MOZ_ALWAYS_INLINE JS::GCCellPtr
JSID_TO_GCTHING(jsid id)
{
void* thing = (void*)(JSID_BITS(id) & ~(size_t)JSID_TYPE_MASK);
if (JSID_IS_STRING(id)) {
return JS::GCCellPtr(thing, JS::TraceKind::String);
}
MOZ_ASSERT(JSID_IS_SYMBOL(id));
return JS::GCCellPtr(thing, JS::TraceKind::Symbol);
}
static MOZ_ALWAYS_INLINE bool
JSID_IS_VOID(const jsid id)
{
MOZ_ASSERT_IF((JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_VOID,
JSID_BITS(id) == JSID_TYPE_VOID);
return JSID_BITS(id) == JSID_TYPE_VOID;
}
static MOZ_ALWAYS_INLINE bool
JSID_IS_EMPTY(const jsid id)
{
MOZ_ASSERT_IF((JSID_BITS(id) & JSID_TYPE_MASK) == JSID_TYPE_EMPTY,
JSID_BITS(id) == JSID_TYPE_EMPTY);
return JSID_BITS(id) == JSID_TYPE_EMPTY;
}
constexpr const jsid JSID_VOID;
constexpr const jsid JSID_EMPTY = jsid::fromRawBits(JSID_TYPE_EMPTY);
extern JS_PUBLIC_DATA(const JS::HandleId) JSID_VOIDHANDLE;
extern JS_PUBLIC_DATA(const JS::HandleId) JSID_EMPTYHANDLE;
namespace JS {
template <>
struct GCPolicy<jsid>
{
static void trace(JSTracer* trc, jsid* idp, const char* name) {
js::UnsafeTraceManuallyBarrieredEdge(trc, idp, name);
}
static bool isValid(jsid id) {
return !JSID_IS_GCTHING(id) || js::gc::IsCellPointerValid(JSID_TO_GCTHING(id).asCell());
}
};
#ifdef DEBUG
MOZ_ALWAYS_INLINE bool
IdIsNotGray(jsid id)
{
if (!JSID_IS_GCTHING(id)) {
return true;
}
return CellIsNotGray(JSID_TO_GCTHING(id).asCell());
}
#endif
} // namespace JS
namespace js {
template <>
struct BarrierMethods<jsid>
{
static gc::Cell* asGCThingOrNull(jsid id) {
if (JSID_IS_STRING(id)) {
return reinterpret_cast<gc::Cell*>(JSID_TO_STRING(id));
}
if (JSID_IS_SYMBOL(id)) {
return reinterpret_cast<gc::Cell*>(JSID_TO_SYMBOL(id));
}
return nullptr;
}
static void postBarrier(jsid* idp, jsid prev, jsid next) {}
static void exposeToJS(jsid id) {
if (JSID_IS_GCTHING(id)) {
js::gc::ExposeGCThingToActiveJS(JSID_TO_GCTHING(id));
}
}
};
// If the jsid is a GC pointer type, convert to that type and call |f| with
// the pointer. If the jsid is not a GC type, calls F::defaultValue.
template <typename F, typename... Args>
auto
DispatchTyped(F f, const jsid& id, Args&&... args)
-> decltype(f(static_cast<JSString*>(nullptr), std::forward<Args>(args)...))
{
if (JSID_IS_STRING(id)) {
return f(JSID_TO_STRING(id), std::forward<Args>(args)...);
}
if (JSID_IS_SYMBOL(id)) {
return f(JSID_TO_SYMBOL(id), std::forward<Args>(args)...);
}
MOZ_ASSERT(!JSID_IS_GCTHING(id));
return F::defaultValue(id);
}
#undef id
} // namespace js
#endif /* js_Id_h */