/
attributes.h
300 lines (242 loc) · 6.86 KB
/
attributes.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#ifndef IV_LV5_ATTRIBUTES_H_
#define IV_LV5_ATTRIBUTES_H_
namespace iv {
namespace lv5 {
class Attributes {
public:
enum Attribute {
NONE = 0,
WRITABLE = 1,
ENUMERABLE = 2,
CONFIGURABLE = 4,
DATA = 8,
ACCESSOR = 16,
EMPTY = 32,
UNDEF_WRITABLE = 64,
UNDEF_ENUMERABLE = 128,
UNDEF_CONFIGURABLE = 256,
UNDEF_VALUE = 512,
UNDEF_GETTER = 1024,
UNDEF_SETTER = 2048,
// short options
N = NONE,
W = WRITABLE,
E = ENUMERABLE,
C = CONFIGURABLE
};
typedef uint32_t Raw;
static const Raw TYPE_MASK = DATA | ACCESSOR;
static const Raw DATA_ATTR_MASK = DATA | WRITABLE | ENUMERABLE | CONFIGURABLE;
static const Raw ACCESSOR_ATTR_MASK = ACCESSOR | ENUMERABLE | CONFIGURABLE;
static const Raw DEFAULT =
UNDEF_WRITABLE | UNDEF_ENUMERABLE | UNDEF_CONFIGURABLE |
UNDEF_VALUE | UNDEF_GETTER | UNDEF_SETTER;
static const Raw UNDEFS = EMPTY | DEFAULT;
static const Raw BOTH = CONFIGURABLE | ENUMERABLE;
static bool IsStored(Raw attributes) {
// undef attributes should not be here
if (attributes & UNDEFS) {
return false;
}
if (attributes & DATA) {
return !(attributes & ACCESSOR);
}
if (attributes & ACCESSOR) {
return !(attributes & WRITABLE);
}
return false;
}
static Raw RemoveUndefs(Raw attributes) {
return attributes & (~UNDEFS);
}
class External {
public:
friend class Attributes;
External(Raw attributes) : attributes_(attributes) { } // NOLINT
External() : attributes_(NONE) { }
Raw type() const {
return attributes_ & TYPE_MASK;
}
inline bool IsEnumerable() const {
return attributes_ & ENUMERABLE;
}
inline bool IsEnumerableAbsent() const {
return attributes_ & UNDEF_ENUMERABLE;
}
inline void set_enumerable(bool val) {
if (val) {
attributes_ = (attributes_ & ~UNDEF_ENUMERABLE) | ENUMERABLE;
} else {
attributes_ = (attributes_ & ~UNDEF_ENUMERABLE) & ~ENUMERABLE;
}
}
inline bool IsConfigurable() const {
return attributes_ & CONFIGURABLE;
}
inline bool IsConfigurableAbsent() const {
return attributes_ & UNDEF_CONFIGURABLE;
}
inline void set_configurable(bool val) {
if (val) {
attributes_ = (attributes_ & ~UNDEF_CONFIGURABLE) | CONFIGURABLE;
} else {
attributes_ = (attributes_ & ~UNDEF_CONFIGURABLE) & ~CONFIGURABLE;
}
}
inline bool IsData() const {
return attributes_ & DATA;
}
void set_data() {
attributes_ &= ~ACCESSOR;
attributes_ |= DATA;
}
inline bool IsAccessor() const {
return attributes_ & ACCESSOR;
}
void set_accessor() {
attributes_ &= ~(DATA | WRITABLE);
attributes_ |= ACCESSOR;
}
inline bool IsGeneric() const {
return !(attributes_ & (DATA | ACCESSOR | EMPTY));
}
inline bool IsEmpty() const { return attributes_ & EMPTY; }
inline bool IsWritable() const {
return attributes_ & WRITABLE;
}
inline bool IsWritableAbsent() const {
return attributes_ & UNDEF_WRITABLE;
}
inline void set_writable(bool val) {
if (val) {
attributes_ = (attributes_ & ~UNDEF_WRITABLE) | WRITABLE;
} else {
attributes_ = (attributes_ & ~UNDEF_WRITABLE) & ~WRITABLE;
}
}
inline bool IsValueAbsent() const {
return attributes_ & UNDEF_VALUE;
}
inline bool IsGetterAbsent() const {
return attributes_ & UNDEF_GETTER;
}
inline bool IsSetterAbsent() const {
return attributes_ & UNDEF_SETTER;
}
bool IsAbsent() const {
return
IsConfigurableAbsent() &&
IsEnumerableAbsent() &&
IsGeneric();
}
inline bool IsDefault() const {
const Raw kDefault =
(CONFIGURABLE | ENUMERABLE | DATA | WRITABLE);
return (attributes_ & kDefault) == kDefault;
}
Raw raw() const { return attributes_; }
inline friend bool operator==(External lhs, External rhs) {
return lhs.raw() == rhs.raw();
}
inline friend bool operator!=(External lhs, External rhs) {
return lhs.raw() != rhs.raw();
}
private:
void FillEnumerableAndConfigurable() {
if (IsConfigurableAbsent()) {
assert(!(attributes_ & CONFIGURABLE));
attributes_ &= ~UNDEF_CONFIGURABLE;
}
if (IsEnumerableAbsent()) {
assert(!(attributes_ & ENUMERABLE));
attributes_ &= ~UNDEF_ENUMERABLE;
}
}
Raw attributes_;
};
class Safe : protected Attributes::External {
public:
friend class Attributes;
typedef Attributes::External super_type;
using super_type::IsEnumerable;
using super_type::IsConfigurable;
using super_type::IsData;
using super_type::IsAccessor;
using super_type::IsGeneric;
using super_type::IsWritable;
using super_type::raw;
using super_type::type;
static Safe NotFound() {
return Safe();
}
bool IsNotFound() const { return raw() == NONE; }
inline friend bool operator==(Safe lhs, Safe rhs) {
return lhs.raw() == rhs.raw();
}
inline friend bool operator!=(Safe lhs, Safe rhs) {
return lhs.raw() != rhs.raw();
}
bool IsSimpleData() const {
// we can ignore ENUMERABLE
const Raw value = DATA | WRITABLE;
return (raw() & value) == value;
}
static Safe UnSafe(External attr) {
return Safe(attr);
}
private:
explicit Safe(Raw attr)
: External(RemoveUndefs(attr)) {
assert(Attributes::IsStored(raw()));
}
explicit Safe(External attr)
: External(RemoveUndefs(attr.raw())) {
assert(Attributes::IsStored(raw()));
}
Safe() : External() { }
};
static Safe CreateData(External attributes) {
assert(!attributes.IsEmpty());
assert(!attributes.IsAccessor());
attributes.FillEnumerableAndConfigurable();
attributes.set_data();
if (attributes.IsWritableAbsent()) {
assert(!attributes.IsWritable());
attributes.set_writable(false);
}
return Safe(attributes);
}
static Safe CreateAccessor(External attributes) {
assert(!attributes.IsEmpty());
assert(!attributes.IsWritable());
assert(!attributes.IsData());
attributes.FillEnumerableAndConfigurable();
attributes.set_accessor();
return Safe(attributes);
}
class Object {
public:
static Safe Data() {
return Attributes::CreateData(WRITABLE | ENUMERABLE | CONFIGURABLE);
}
static Safe Accessor() {
return Attributes::CreateAccessor(ENUMERABLE | CONFIGURABLE);
}
};
class String {
public:
static Safe Length() {
return CreateData(NONE);
}
static Safe Indexed() {
return CreateData(ENUMERABLE);
}
};
private:
static bool CheckUndefs(Raw attributes) {
return attributes & UNDEFS;
}
};
typedef Attributes ATTR;
} } // namespace iv::lv5
#endif // IV_LV5_ATTRIBUTES_H_