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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
/* Property descriptors and flags. */
#ifndef js_PropertyDescriptor_h
#define js_PropertyDescriptor_h
#include "mozilla/Assertions.h" // MOZ_ASSERT, MOZ_ASSERT_IF
#include "mozilla/EnumSet.h" // mozilla::EnumSet
#include "mozilla/Maybe.h" // mozilla::Maybe
#include <stddef.h> // size_t
#include <stdint.h> // uint8_t
#include "jstypes.h" // JS_PUBLIC_API
#include "js/Id.h" // jsid
#include "js/RootingAPI.h" // JS::Handle, js::{,Mutable}WrappedPtrOperations
#include "js/Value.h" // JS::Value
struct JS_PUBLIC_API JSContext;
class JS_PUBLIC_API JSObject;
class JS_PUBLIC_API JSTracer;
/* Property attributes, set in JSPropertySpec and passed to API functions.
*
* The data structure in which some of these values are stored only uses a
* uint8_t to store the relevant information. Proceed with caution if trying to
* reorder or change the the first byte worth of flags.
*/
/** The property is visible in for/in loops. */
static constexpr uint8_t JSPROP_ENUMERATE = 0x01;
/**
* The property is non-writable. This flag is only valid for data properties.
*/
static constexpr uint8_t JSPROP_READONLY = 0x02;
/**
* The property is non-configurable: it can't be deleted, and if it's an
* accessor descriptor, its getter and setter can't be changed.
*/
static constexpr uint8_t JSPROP_PERMANENT = 0x04;
/**
* Resolve hooks and enumerate hooks must pass this flag when calling
* JS_Define* APIs to reify lazily-defined properties.
*
* JSPROP_RESOLVING is used only with property-defining APIs. It tells the
* engine to skip the resolve hook when performing the lookup at the beginning
* of property definition. This keeps the resolve hook from accidentally
* triggering itself: unchecked recursion.
*
* For enumerate hooks, triggering the resolve hook would be merely silly, not
* fatal, except in some cases involving non-configurable properties.
*/
static constexpr unsigned JSPROP_RESOLVING = 0x08;
/* (higher flags are unused; add to JSPROP_FLAGS_MASK if ever defined) */
static constexpr unsigned JSPROP_FLAGS_MASK =
JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_RESOLVING;
namespace JS {
// 6.1.7.1 Property Attributes
enum class PropertyAttribute : uint8_t {
// The descriptor is [[Configurable]] := true.
Configurable,
// The descriptor is [[Enumerable]] := true.
Enumerable,
// The descriptor is [[Writable]] := true. Only valid for data descriptors.
Writable
};
class PropertyAttributes : public mozilla::EnumSet<PropertyAttribute> {
// Re-use all EnumSet constructors.
using mozilla::EnumSet<PropertyAttribute>::EnumSet;
public:
bool configurable() const {
return contains(PropertyAttribute::Configurable);
}
bool enumerable() const { return contains(PropertyAttribute::Enumerable); }
bool writable() const { return contains(PropertyAttribute::Writable); }
};
/**
* A structure that represents a property on an object, or the absence of a
* property. Use {,Mutable}Handle<PropertyDescriptor> to interact with
* instances of this structure rather than interacting directly with member
* fields.
*/
class JS_PUBLIC_API PropertyDescriptor {
private:
bool hasConfigurable_ : 1;
bool configurable_ : 1;
bool hasEnumerable_ : 1;
bool enumerable_ : 1;
bool hasWritable_ : 1;
bool writable_ : 1;
bool hasValue_ : 1;
bool hasGetter_ : 1;
bool hasSetter_ : 1;
bool resolving_ : 1;
JSObject* getter_;
JSObject* setter_;
Value value_;
public:
PropertyDescriptor()
: hasConfigurable_(false),
configurable_(false),
hasEnumerable_(false),
enumerable_(false),
hasWritable_(false),
writable_(false),
hasValue_(false),
hasGetter_(false),
hasSetter_(false),
resolving_(false),
getter_(nullptr),
setter_(nullptr),
value_(UndefinedValue()) {}
void trace(JSTracer* trc);
// Construct a new complete DataDescriptor.
static PropertyDescriptor Data(const Value& value,
PropertyAttributes attributes = {}) {
PropertyDescriptor desc;
desc.setConfigurable(attributes.configurable());
desc.setEnumerable(attributes.enumerable());
desc.setWritable(attributes.writable());
desc.setValue(value);
desc.assertComplete();
return desc;
}
// This constructor is only provided for legacy code!
static PropertyDescriptor Data(const Value& value, unsigned attrs) {
MOZ_ASSERT((attrs & ~(JSPROP_PERMANENT | JSPROP_ENUMERATE |
JSPROP_READONLY | JSPROP_RESOLVING)) == 0);
PropertyDescriptor desc;
desc.setConfigurable(!(attrs & JSPROP_PERMANENT));
desc.setEnumerable(attrs & JSPROP_ENUMERATE);
desc.setWritable(!(attrs & JSPROP_READONLY));
desc.setValue(value);
desc.setResolving(attrs & JSPROP_RESOLVING);
desc.assertComplete();
return desc;
}
// Construct a new complete AccessorDescriptor.
// Note: This means JSPROP_GETTER and JSPROP_SETTER are always set.
static PropertyDescriptor Accessor(JSObject* getter, JSObject* setter,
PropertyAttributes attributes = {}) {
MOZ_ASSERT(!attributes.writable());
PropertyDescriptor desc;
desc.setConfigurable(attributes.configurable());
desc.setEnumerable(attributes.enumerable());
desc.setGetter(getter);
desc.setSetter(setter);
desc.assertComplete();
return desc;
}
// This constructor is only provided for legacy code!
static PropertyDescriptor Accessor(JSObject* getter, JSObject* setter,
unsigned attrs) {
MOZ_ASSERT((attrs & ~(JSPROP_PERMANENT | JSPROP_ENUMERATE |
JSPROP_RESOLVING)) == 0);
PropertyDescriptor desc;
desc.setConfigurable(!(attrs & JSPROP_PERMANENT));
desc.setEnumerable(attrs & JSPROP_ENUMERATE);
desc.setGetter(getter);
desc.setSetter(setter);
desc.setResolving(attrs & JSPROP_RESOLVING);
desc.assertComplete();
return desc;
}
static PropertyDescriptor Accessor(mozilla::Maybe<JSObject*> getter,
mozilla::Maybe<JSObject*> setter,
unsigned attrs) {
MOZ_ASSERT((attrs & ~(JSPROP_PERMANENT | JSPROP_ENUMERATE |
JSPROP_RESOLVING)) == 0);
PropertyDescriptor desc;
desc.setConfigurable(!(attrs & JSPROP_PERMANENT));
desc.setEnumerable(attrs & JSPROP_ENUMERATE);
if (getter) {
desc.setGetter(*getter);
}
if (setter) {
desc.setSetter(*setter);
}
desc.setResolving(attrs & JSPROP_RESOLVING);
desc.assertValid();
return desc;
}
// Construct a new incomplete empty PropertyDescriptor.
// Using the spec syntax this would be { }. Specific fields like [[Value]]
// can be added with e.g., setValue.
static PropertyDescriptor Empty() {
PropertyDescriptor desc;
desc.assertValid();
MOZ_ASSERT(!desc.hasConfigurable() && !desc.hasEnumerable() &&
!desc.hasWritable() && !desc.hasValue() && !desc.hasGetter() &&
!desc.hasSetter());
return desc;
}
public:
bool isAccessorDescriptor() const {
MOZ_ASSERT_IF(hasGetter_ || hasSetter_, !isDataDescriptor());
return hasGetter_ || hasSetter_;
}
bool isGenericDescriptor() const {
return !isAccessorDescriptor() && !isDataDescriptor();
}
bool isDataDescriptor() const {
MOZ_ASSERT_IF(hasWritable_ || hasValue_, !isAccessorDescriptor());
return hasWritable_ || hasValue_;
}
bool hasConfigurable() const { return hasConfigurable_; }
bool configurable() const {
MOZ_ASSERT(hasConfigurable());
return configurable_;
}
void setConfigurable(bool configurable) {
hasConfigurable_ = true;
configurable_ = configurable;
}
bool hasEnumerable() const { return hasEnumerable_; }
bool enumerable() const {
MOZ_ASSERT(hasEnumerable());
return enumerable_;
}
void setEnumerable(bool enumerable) {
hasEnumerable_ = true;
enumerable_ = enumerable;
}
bool hasValue() const { return hasValue_; }
Value value() const {
MOZ_ASSERT(hasValue());
return value_;
}
void setValue(const Value& v) {
MOZ_ASSERT(!isAccessorDescriptor());
hasValue_ = true;
value_ = v;
}
bool hasWritable() const { return hasWritable_; }
bool writable() const {
MOZ_ASSERT(hasWritable());
return writable_;
}
void setWritable(bool writable) {
MOZ_ASSERT(!isAccessorDescriptor());
hasWritable_ = true;
writable_ = writable;
}
bool hasGetter() const { return hasGetter_; }
JSObject* getter() const {
MOZ_ASSERT(hasGetter());
return getter_;
}
void setGetter(JSObject* obj) {
MOZ_ASSERT(!isDataDescriptor());
hasGetter_ = true;
getter_ = obj;
}
bool hasSetter() const { return hasSetter_; }
JSObject* setter() const {
MOZ_ASSERT(hasSetter());
return setter_;
}
void setSetter(JSObject* obj) {
MOZ_ASSERT(!isDataDescriptor());
hasSetter_ = true;
setter_ = obj;
}
// Non-standard flag, which is set when defining properties in a resolve hook.
bool resolving() const { return resolving_; }
void setResolving(bool resolving) { resolving_ = resolving; }
Value* valueDoNotUse() { return &value_; }
Value const* valueDoNotUse() const { return &value_; }
JSObject** getterDoNotUse() { return &getter_; }
JSObject* const* getterDoNotUse() const { return &getter_; }
void setGetterDoNotUse(JSObject* obj) { getter_ = obj; }
JSObject** setterDoNotUse() { return &setter_; }
JSObject* const* setterDoNotUse() const { return &setter_; }
void setSetterDoNotUse(JSObject* obj) { setter_ = obj; }
void assertValid() const {
#ifdef DEBUG
if (isAccessorDescriptor()) {
MOZ_ASSERT(!hasWritable_);
MOZ_ASSERT(!hasValue_);
} else {
MOZ_ASSERT(isGenericDescriptor() || isDataDescriptor());
MOZ_ASSERT(!hasGetter_);
MOZ_ASSERT(!hasSetter_);
}
MOZ_ASSERT_IF(!hasConfigurable_, !configurable_);
MOZ_ASSERT_IF(!hasEnumerable_, !enumerable_);
MOZ_ASSERT_IF(!hasWritable_, !writable_);
MOZ_ASSERT_IF(!hasValue_, value_.isUndefined());
MOZ_ASSERT_IF(!hasGetter_, !getter_);
MOZ_ASSERT_IF(!hasSetter_, !setter_);
MOZ_ASSERT_IF(resolving_, !isGenericDescriptor());
#endif
}
void assertComplete() const {
#ifdef DEBUG
assertValid();
MOZ_ASSERT(hasConfigurable());
MOZ_ASSERT(hasEnumerable());
MOZ_ASSERT(!isGenericDescriptor());
MOZ_ASSERT_IF(isDataDescriptor(), hasValue() && hasWritable());
MOZ_ASSERT_IF(isAccessorDescriptor(), hasGetter() && hasSetter());
#endif
}
};
} // namespace JS
namespace js {
template <typename Wrapper>
class WrappedPtrOperations<JS::PropertyDescriptor, Wrapper> {
const JS::PropertyDescriptor& desc() const {
return static_cast<const Wrapper*>(this)->get();
}
public:
bool isAccessorDescriptor() const { return desc().isAccessorDescriptor(); }
bool isGenericDescriptor() const { return desc().isGenericDescriptor(); }
bool isDataDescriptor() const { return desc().isDataDescriptor(); }
bool hasConfigurable() const { return desc().hasConfigurable(); }
bool configurable() const { return desc().configurable(); }
bool hasEnumerable() const { return desc().hasEnumerable(); }
bool enumerable() const { return desc().enumerable(); }
bool hasValue() const { return desc().hasValue(); }
JS::Handle<JS::Value> value() const {
MOZ_ASSERT(hasValue());
return JS::Handle<JS::Value>::fromMarkedLocation(desc().valueDoNotUse());
}
bool hasWritable() const { return desc().hasWritable(); }
bool writable() const { return desc().writable(); }
bool hasGetter() const { return desc().hasGetter(); }
JS::Handle<JSObject*> getter() const {
MOZ_ASSERT(hasGetter());
return JS::Handle<JSObject*>::fromMarkedLocation(desc().getterDoNotUse());
}
bool hasSetter() const { return desc().hasSetter(); }
JS::Handle<JSObject*> setter() const {
MOZ_ASSERT(hasSetter());
return JS::Handle<JSObject*>::fromMarkedLocation(desc().setterDoNotUse());
}
bool resolving() const { return desc().resolving(); }
void assertValid() const { desc().assertValid(); }
void assertComplete() const { desc().assertComplete(); }
};
template <typename Wrapper>
class MutableWrappedPtrOperations<JS::PropertyDescriptor, Wrapper>
: public js::WrappedPtrOperations<JS::PropertyDescriptor, Wrapper> {
JS::PropertyDescriptor& desc() { return static_cast<Wrapper*>(this)->get(); }
public:
JS::MutableHandle<JS::Value> value() {
MOZ_ASSERT(desc().hasValue());
return JS::MutableHandle<JS::Value>::fromMarkedLocation(
desc().valueDoNotUse());
}
void setValue(JS::Handle<JS::Value> v) { desc().setValue(v); }
void setConfigurable(bool configurable) {
desc().setConfigurable(configurable);
}
void setEnumerable(bool enumerable) { desc().setEnumerable(enumerable); }
void setWritable(bool writable) { desc().setWritable(writable); }
void setGetter(JSObject* obj) { desc().setGetter(obj); }
void setSetter(JSObject* obj) { desc().setSetter(obj); }
JS::MutableHandle<JSObject*> getter() {
MOZ_ASSERT(desc().hasGetter());
return JS::MutableHandle<JSObject*>::fromMarkedLocation(
desc().getterDoNotUse());
}
JS::MutableHandle<JSObject*> setter() {
MOZ_ASSERT(desc().hasSetter());
return JS::MutableHandle<JSObject*>::fromMarkedLocation(
desc().setterDoNotUse());
}
void setResolving(bool resolving) { desc().setResolving(resolving); }
};
} // namespace js
/**
* Get a description of one of obj's own properties. If no such property exists
* on obj, return true with desc.object() set to null.
*
* Implements: ES6 [[GetOwnProperty]] internal method.
*/
extern JS_PUBLIC_API bool JS_GetOwnPropertyDescriptorById(
JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc);
extern JS_PUBLIC_API bool JS_GetOwnPropertyDescriptor(
JSContext* cx, JS::Handle<JSObject*> obj, const char* name,
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc);
extern JS_PUBLIC_API bool JS_GetOwnUCPropertyDescriptor(
JSContext* cx, JS::Handle<JSObject*> obj, const char16_t* name,
size_t namelen,
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc);
/**
* DEPRECATED
*
* Like JS_GetOwnPropertyDescriptorById, but also searches the prototype chain
* if no own property is found directly on obj. The object on which the
* property is found is returned in holder. If the property is not found
* on the prototype chain, then desc is Nothing.
*/
extern JS_PUBLIC_API bool JS_GetPropertyDescriptorById(
JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc,
JS::MutableHandle<JSObject*> holder);
extern JS_PUBLIC_API bool JS_GetPropertyDescriptor(
JSContext* cx, JS::Handle<JSObject*> obj, const char* name,
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc,
JS::MutableHandle<JSObject*> holder);
extern JS_PUBLIC_API bool JS_GetUCPropertyDescriptor(
JSContext* cx, JS::Handle<JSObject*> obj, const char16_t* name,
size_t namelen,
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc,
JS::MutableHandle<JSObject*> holder);
namespace JS {
extern JS_PUBLIC_API bool ObjectToCompletePropertyDescriptor(
JSContext* cx, Handle<JSObject*> obj, Handle<Value> descriptor,
MutableHandle<PropertyDescriptor> desc);
/*
* ES6 draft rev 32 (2015 Feb 2) 6.2.4.4 FromPropertyDescriptor(Desc).
*
* If desc.isNothing(), then vp is set to undefined.
*/
extern JS_PUBLIC_API bool FromPropertyDescriptor(
JSContext* cx, Handle<mozilla::Maybe<PropertyDescriptor>> desc,
MutableHandle<Value> vp);
} // namespace JS
#endif /* js_PropertyDescriptor_h */
|