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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
/* -*- 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_JSObject_inl_h
#define vm_JSObject_inl_h
#include "vm/JSObject.h"
#include "js/Object.h" // JS::GetBuiltinClass
#include "vm/ArrayObject.h"
#include "vm/BoundFunctionObject.h"
#include "vm/EnvironmentObject.h"
#include "vm/JSFunction.h"
#include "vm/Probes.h"
#include "vm/PropertyResult.h"
#include "vm/TypedArrayObject.h"
#ifdef ENABLE_RECORD_TUPLE
# include "vm/TupleType.h"
#endif
#include "gc/GCContext-inl.h"
#include "gc/ObjectKind-inl.h"
#include "vm/ObjectOperations-inl.h" // js::MaybeHasInterestingSymbolProperty
namespace js {
#ifdef ENABLE_RECORD_TUPLE
// Defined in vm/RecordTupleShared.{h,cpp}. We cannot include that file
// because it causes circular dependencies.
extern bool IsExtendedPrimitiveWrapper(const JSObject& obj);
#endif
// Get the GC kind to use for scripted 'new', empty object literals ({}), and
// the |Object| constructor.
static inline gc::AllocKind NewObjectGCKind() { return gc::AllocKind::OBJECT4; }
} // namespace js
MOZ_ALWAYS_INLINE uint32_t js::NativeObject::numDynamicSlots() const {
uint32_t slots = getSlotsHeader()->capacity();
MOZ_ASSERT(slots == calculateDynamicSlots());
MOZ_ASSERT_IF(hasDynamicSlots() && !hasUniqueId(), slots != 0);
return slots;
}
MOZ_ALWAYS_INLINE uint32_t js::NativeObject::calculateDynamicSlots() const {
return calculateDynamicSlots(numFixedSlots(), slotSpan(), getClass());
}
/* static */ MOZ_ALWAYS_INLINE uint32_t js::NativeObject::calculateDynamicSlots(
uint32_t nfixed, uint32_t span, const JSClass* clasp) {
if (span <= nfixed) {
return 0;
}
uint32_t ndynamic = span - nfixed;
// Increase the slots to SLOT_CAPACITY_MIN to decrease the likelihood
// the dynamic slots need to get increased again. ArrayObjects ignore
// this because slots are uncommon in that case.
if (clasp != &ArrayObject::class_ && ndynamic <= SLOT_CAPACITY_MIN) {
return SLOT_CAPACITY_MIN;
}
uint32_t count =
mozilla::RoundUpPow2(ndynamic + ObjectSlots::VALUES_PER_HEADER);
uint32_t slots = count - ObjectSlots::VALUES_PER_HEADER;
MOZ_ASSERT(slots >= ndynamic);
return slots;
}
/* static */ MOZ_ALWAYS_INLINE uint32_t
js::NativeObject::calculateDynamicSlots(SharedShape* shape) {
return calculateDynamicSlots(shape->numFixedSlots(), shape->slotSpan(),
shape->getObjectClass());
}
inline void JSObject::finalize(JS::GCContext* gcx) {
js::probes::FinalizeObject(this);
#ifdef DEBUG
MOZ_ASSERT(isTenured());
if (!IsBackgroundFinalized(asTenured().getAllocKind())) {
/* Assert we're on the main thread. */
MOZ_ASSERT(CurrentThreadCanAccessZone(zone()));
}
#endif
js::Shape* objShape = shape();
const JSClass* clasp = objShape->getObjectClass();
if (clasp->hasFinalize()) {
clasp->doFinalize(gcx, this);
}
if (!objShape->isNative()) {
return;
}
js::NativeObject* nobj = &as<js::NativeObject>();
if (nobj->hasDynamicSlots()) {
js::ObjectSlots* slotsHeader = nobj->getSlotsHeader();
size_t size = js::ObjectSlots::allocSize(slotsHeader->capacity());
gcx->free_(this, slotsHeader, size, js::MemoryUse::ObjectSlots);
}
if (nobj->hasDynamicElements()) {
js::ObjectElements* elements = nobj->getElementsHeader();
size_t size = elements->numAllocatedElements() * sizeof(js::HeapSlot);
gcx->free_(this, nobj->getUnshiftedElementsHeader(), size,
js::MemoryUse::ObjectElements);
}
}
inline bool JSObject::isQualifiedVarObj() const {
if (is<js::DebugEnvironmentProxy>()) {
return as<js::DebugEnvironmentProxy>().environment().isQualifiedVarObj();
}
bool rv = hasFlag(js::ObjectFlag::QualifiedVarObj);
MOZ_ASSERT_IF(rv, is<js::GlobalObject>() || is<js::CallObject>() ||
is<js::VarEnvironmentObject>() ||
is<js::ModuleEnvironmentObject>() ||
is<js::NonSyntacticVariablesObject>() ||
(is<js::WithEnvironmentObject>() &&
!as<js::WithEnvironmentObject>().isSyntactic()));
return rv;
}
inline bool JSObject::isUnqualifiedVarObj() const {
if (is<js::DebugEnvironmentProxy>()) {
return as<js::DebugEnvironmentProxy>().environment().isUnqualifiedVarObj();
}
return is<js::GlobalObject>() || is<js::NonSyntacticVariablesObject>();
}
inline bool JSObject::canHaveFixedElements() const {
return (is<js::ArrayObject>() || IF_RECORD_TUPLE(is<js::TupleType>(), false));
}
namespace js {
#ifdef DEBUG
inline bool ClassCanHaveFixedData(const JSClass* clasp) {
// Normally, the number of fixed slots given an object is the maximum
// permitted for its size class. For array buffers and non-shared typed
// arrays we only use enough to cover the class reserved slots, so that
// the remaining space in the object's allocation is available for the
// buffer's data.
return !clasp->isNativeObject() || clasp == &js::ArrayBufferObject::class_ ||
js::IsTypedArrayClass(clasp);
}
#endif
class MOZ_RAII AutoSuppressAllocationMetadataBuilder {
JS::Zone* zone;
bool saved;
public:
explicit AutoSuppressAllocationMetadataBuilder(JSContext* cx)
: zone(cx->zone()), saved(zone->suppressAllocationMetadataBuilder) {
zone->suppressAllocationMetadataBuilder = true;
}
~AutoSuppressAllocationMetadataBuilder() {
zone->suppressAllocationMetadataBuilder = saved;
}
};
// This function is meant to be called from allocation fast paths.
//
// If we do have an allocation metadata builder, it can cause a GC, so the
// object must be rooted. The usual way to do this would be to make our callers
// pass a HandleObject, but that would require them to pay the cost of rooting
// the object unconditionally, even though collecting metadata is rare. Instead,
// SetNewObjectMetadata's contract is that the caller must use the pointer
// returned in place of the pointer passed. If a GC occurs, the returned pointer
// may be the passed pointer, relocated by GC. If no GC could occur, it's just
// passed through. We root nothing unless necessary.
template <typename T>
[[nodiscard]] static inline T* SetNewObjectMetadata(JSContext* cx, T* obj) {
MOZ_ASSERT(cx->isMainThreadContext());
MOZ_ASSERT(cx->realm()->hasAllocationMetadataBuilder());
MOZ_ASSERT(!cx->realm()->hasObjectPendingMetadata());
// The metadata builder is invoked for each object created on the main thread,
// except when it's suppressed.
if (!cx->zone()->suppressAllocationMetadataBuilder) {
// Don't collect metadata on objects that represent metadata, to avoid
// recursion.
AutoSuppressAllocationMetadataBuilder suppressMetadata(cx);
Rooted<T*> rooted(cx, obj);
cx->realm()->setNewObjectMetadata(cx, rooted);
return rooted;
}
return obj;
}
} // namespace js
inline js::GlobalObject& JSObject::nonCCWGlobal() const {
/*
* The global is read-barriered so that it is kept live by access through
* the Realm. When accessed through a JSObject, however, the global will be
* already kept live by the black JSObject's group pointer, so does not
* need to be read-barriered.
*/
return *nonCCWRealm()->unsafeUnbarrieredMaybeGlobal();
}
inline bool JSObject::nonProxyIsExtensible() const {
MOZ_ASSERT(!uninlinedIsProxyObject());
#ifdef ENABLE_RECORD_TUPLE
if (js::IsExtendedPrimitiveWrapper(*this)) {
return false;
}
#endif
// [[Extensible]] for ordinary non-proxy objects is an object flag.
return !hasFlag(js::ObjectFlag::NotExtensible);
}
inline bool JSObject::hasInvalidatedTeleporting() const {
return hasFlag(js::ObjectFlag::InvalidatedTeleporting);
}
MOZ_ALWAYS_INLINE bool JSObject::maybeHasInterestingSymbolProperty() const {
if (is<js::NativeObject>()) {
return as<js::NativeObject>().hasInterestingSymbol();
}
return true;
}
inline bool JSObject::staticPrototypeIsImmutable() const {
MOZ_ASSERT(hasStaticPrototype());
return hasFlag(js::ObjectFlag::ImmutablePrototype);
}
namespace js {
static MOZ_ALWAYS_INLINE bool IsFunctionObject(const js::Value& v) {
return v.isObject() && v.toObject().is<JSFunction>();
}
static MOZ_ALWAYS_INLINE bool IsFunctionObject(const js::Value& v,
JSFunction** fun) {
if (v.isObject() && v.toObject().is<JSFunction>()) {
*fun = &v.toObject().as<JSFunction>();
return true;
}
return false;
}
static MOZ_ALWAYS_INLINE bool IsNativeFunction(const js::Value& v,
JSNative native) {
JSFunction* fun;
return IsFunctionObject(v, &fun) && fun->maybeNative() == native;
}
static MOZ_ALWAYS_INLINE bool IsNativeFunction(const JSObject* obj,
JSNative native) {
return obj->is<JSFunction>() && obj->as<JSFunction>().maybeNative() == native;
}
// Return whether looking up a method on 'obj' definitely resolves to the
// original specified native function. The method may conservatively return
// 'false' in the case of proxies or other non-native objects.
static MOZ_ALWAYS_INLINE bool HasNativeMethodPure(JSObject* obj,
PropertyName* name,
JSNative native,
JSContext* cx) {
Value v;
if (!GetPropertyPure(cx, obj, NameToId(name), &v)) {
return false;
}
return IsNativeFunction(v, native);
}
// Return whether 'obj' definitely has no @@toPrimitive method.
static MOZ_ALWAYS_INLINE bool HasNoToPrimitiveMethodPure(JSObject* obj,
JSContext* cx) {
JS::Symbol* toPrimitive = cx->wellKnownSymbols().toPrimitive;
JSObject* holder;
if (!MaybeHasInterestingSymbolProperty(cx, obj, toPrimitive, &holder)) {
#ifdef DEBUG
NativeObject* pobj;
PropertyResult prop;
MOZ_ASSERT(LookupPropertyPure(cx, obj, PropertyKey::Symbol(toPrimitive),
&pobj, &prop));
MOZ_ASSERT(prop.isNotFound());
#endif
return true;
}
NativeObject* pobj;
PropertyResult prop;
if (!LookupPropertyPure(cx, holder, PropertyKey::Symbol(toPrimitive), &pobj,
&prop)) {
return false;
}
return prop.isNotFound();
}
extern bool ToPropertyKeySlow(JSContext* cx, HandleValue argument,
MutableHandleId result);
/* ES6 draft rev 28 (2014 Oct 14) 7.1.14 */
MOZ_ALWAYS_INLINE bool ToPropertyKey(JSContext* cx, HandleValue argument,
MutableHandleId result) {
if (MOZ_LIKELY(argument.isPrimitive())) {
return PrimitiveValueToId<CanGC>(cx, argument, result);
}
return ToPropertyKeySlow(cx, argument, result);
}
/*
* Return true if this is a compiler-created internal function accessed by
* its own object. Such a function object must not be accessible to script
* or embedding code.
*/
inline bool IsInternalFunctionObject(JSObject& funobj) {
JSFunction& fun = funobj.as<JSFunction>();
return fun.isInterpreted() && !fun.environment();
}
inline gc::Heap GetInitialHeap(NewObjectKind newKind, const JSClass* clasp,
gc::AllocSite* site = nullptr) {
if (newKind != GenericObject) {
return gc::Heap::Tenured;
}
if (clasp->hasFinalize() && !CanNurseryAllocateFinalizedClass(clasp)) {
return gc::Heap::Tenured;
}
if (site) {
return site->initialHeap();
}
return gc::Heap::Default;
}
/*
* Make an object with the specified prototype. If parent is null, it will
* default to the prototype's global if the prototype is non-null.
*/
NativeObject* NewObjectWithGivenTaggedProto(JSContext* cx, const JSClass* clasp,
Handle<TaggedProto> proto,
gc::AllocKind allocKind,
NewObjectKind newKind);
template <NewObjectKind NewKind>
inline NativeObject* NewObjectWithGivenTaggedProto(JSContext* cx,
const JSClass* clasp,
Handle<TaggedProto> proto) {
gc::AllocKind allocKind = gc::GetGCObjectKind(clasp);
return NewObjectWithGivenTaggedProto(cx, clasp, proto, allocKind, NewKind);
}
namespace detail {
template <typename T, NewObjectKind NewKind>
inline T* NewObjectWithGivenTaggedProtoForKind(JSContext* cx,
Handle<TaggedProto> proto) {
JSObject* obj = NewObjectWithGivenTaggedProto<NewKind>(cx, &T::class_, proto);
return obj ? &obj->as<T>() : nullptr;
}
} // namespace detail
template <typename T>
inline T* NewObjectWithGivenTaggedProto(JSContext* cx,
Handle<TaggedProto> proto) {
return detail::NewObjectWithGivenTaggedProtoForKind<T, GenericObject>(cx,
proto);
}
inline NativeObject* NewObjectWithGivenProto(
JSContext* cx, const JSClass* clasp, HandleObject proto,
gc::AllocKind allocKind, NewObjectKind newKind = GenericObject) {
return NewObjectWithGivenTaggedProto(cx, clasp, AsTaggedProto(proto),
allocKind, newKind);
}
inline NativeObject* NewObjectWithGivenProto(JSContext* cx,
const JSClass* clasp,
HandleObject proto) {
return NewObjectWithGivenTaggedProto<GenericObject>(cx, clasp,
AsTaggedProto(proto));
}
inline NativeObject* NewTenuredObjectWithGivenProto(JSContext* cx,
const JSClass* clasp,
HandleObject proto) {
return NewObjectWithGivenTaggedProto<TenuredObject>(cx, clasp,
AsTaggedProto(proto));
}
template <typename T>
inline T* NewObjectWithGivenProto(JSContext* cx, HandleObject proto) {
return detail::NewObjectWithGivenTaggedProtoForKind<T, GenericObject>(
cx, AsTaggedProto(proto));
}
template <typename T>
inline T* NewTenuredObjectWithGivenProto(JSContext* cx, HandleObject proto) {
return detail::NewObjectWithGivenTaggedProtoForKind<T, TenuredObject>(
cx, AsTaggedProto(proto));
}
template <typename T>
inline T* NewObjectWithGivenProtoAndKinds(JSContext* cx, HandleObject proto,
gc::AllocKind allocKind,
NewObjectKind newKind) {
JSObject* obj = NewObjectWithGivenTaggedProto(
cx, &T::class_, AsTaggedProto(proto), allocKind, newKind);
return obj ? &obj->as<T>() : nullptr;
}
// Make an object with the prototype set according to the cached prototype or
// Object.prototype.
NativeObject* NewObjectWithClassProto(JSContext* cx, const JSClass* clasp,
HandleObject proto,
gc::AllocKind allocKind,
NewObjectKind newKind = GenericObject);
inline NativeObject* NewObjectWithClassProto(
JSContext* cx, const JSClass* clasp, HandleObject proto,
NewObjectKind newKind = GenericObject) {
gc::AllocKind allocKind = gc::GetGCObjectKind(clasp);
return NewObjectWithClassProto(cx, clasp, proto, allocKind, newKind);
}
template <class T>
inline T* NewObjectWithClassProto(JSContext* cx, HandleObject proto) {
JSObject* obj = NewObjectWithClassProto(cx, &T::class_, proto, GenericObject);
return obj ? &obj->as<T>() : nullptr;
}
template <class T>
inline T* NewObjectWithClassProtoAndKind(JSContext* cx, HandleObject proto,
NewObjectKind newKind) {
JSObject* obj = NewObjectWithClassProto(cx, &T::class_, proto, newKind);
return obj ? &obj->as<T>() : nullptr;
}
template <class T>
inline T* NewObjectWithClassProto(JSContext* cx, HandleObject proto,
gc::AllocKind allocKind,
NewObjectKind newKind = GenericObject) {
NativeObject* obj =
NewObjectWithClassProto(cx, &T::class_, proto, allocKind, newKind);
return obj ? &obj->as<T>() : nullptr;
}
/*
* Create a native instance of the given class with parent and proto set
* according to the context's active global.
*/
inline NativeObject* NewBuiltinClassInstance(
JSContext* cx, const JSClass* clasp, gc::AllocKind allocKind,
NewObjectKind newKind = GenericObject) {
return NewObjectWithClassProto(cx, clasp, nullptr, allocKind, newKind);
}
inline NativeObject* NewBuiltinClassInstance(
JSContext* cx, const JSClass* clasp,
NewObjectKind newKind = GenericObject) {
gc::AllocKind allocKind = gc::GetGCObjectKind(clasp);
return NewBuiltinClassInstance(cx, clasp, allocKind, newKind);
}
template <typename T>
inline T* NewBuiltinClassInstance(JSContext* cx) {
JSObject* obj = NewBuiltinClassInstance(cx, &T::class_, GenericObject);
return obj ? &obj->as<T>() : nullptr;
}
template <typename T>
inline T* NewTenuredBuiltinClassInstance(JSContext* cx) {
JSObject* obj = NewBuiltinClassInstance(cx, &T::class_, TenuredObject);
return obj ? &obj->as<T>() : nullptr;
}
template <typename T>
inline T* NewBuiltinClassInstanceWithKind(JSContext* cx,
NewObjectKind newKind) {
JSObject* obj = NewBuiltinClassInstance(cx, &T::class_, newKind);
return obj ? &obj->as<T>() : nullptr;
}
template <typename T>
inline T* NewBuiltinClassInstance(JSContext* cx, gc::AllocKind allocKind,
NewObjectKind newKind = GenericObject) {
JSObject* obj = NewBuiltinClassInstance(cx, &T::class_, allocKind, newKind);
return obj ? &obj->as<T>() : nullptr;
}
static inline gc::AllocKind GuessArrayGCKind(size_t numElements) {
if (numElements) {
return gc::GetGCArrayKind(numElements);
}
return gc::AllocKind::OBJECT8;
}
// Returns ESClass::Other if the value isn't an object, or if the object
// isn't of one of the enumerated classes. Otherwise returns the appropriate
// class.
inline bool GetClassOfValue(JSContext* cx, HandleValue v, ESClass* cls) {
if (!v.isObject()) {
*cls = ESClass::Other;
return true;
}
RootedObject obj(cx, &v.toObject());
return JS::GetBuiltinClass(cx, obj, cls);
}
extern NativeObject* InitClass(
JSContext* cx, HandleObject obj, const JSClass* protoClass,
HandleObject protoProto, const char* name, JSNative constructor,
unsigned nargs, const JSPropertySpec* ps, const JSFunctionSpec* fs,
const JSPropertySpec* static_ps, const JSFunctionSpec* static_fs,
NativeObject** ctorp = nullptr);
MOZ_ALWAYS_INLINE const char* GetObjectClassName(JSContext* cx,
HandleObject obj) {
if (obj->is<ProxyObject>()) {
return Proxy::className(cx, obj);
}
return obj->getClass()->name;
}
inline bool IsCallable(const Value& v) {
return v.isObject() && v.toObject().isCallable();
}
// ES6 rev 24 (2014 April 27) 7.2.5 IsConstructor
inline bool IsConstructor(const Value& v) {
return v.isObject() && v.toObject().isConstructor();
}
static inline bool MaybePreserveDOMWrapper(JSContext* cx, HandleObject obj) {
if (!obj->getClass()->isDOMClass()) {
return true;
}
MOZ_ASSERT(cx->runtime()->preserveWrapperCallback);
return cx->runtime()->preserveWrapperCallback(cx, obj);
}
} /* namespace js */
MOZ_ALWAYS_INLINE bool JSObject::isCallable() const {
if (is<JSFunction>()) {
return true;
}
if (is<js::ProxyObject>()) {
const js::ProxyObject& p = as<js::ProxyObject>();
return p.handler()->isCallable(const_cast<JSObject*>(this));
}
return callHook() != nullptr;
}
MOZ_ALWAYS_INLINE bool JSObject::isConstructor() const {
if (is<JSFunction>()) {
const JSFunction& fun = as<JSFunction>();
return fun.isConstructor();
}
if (is<js::BoundFunctionObject>()) {
const js::BoundFunctionObject& bound = as<js::BoundFunctionObject>();
return bound.isConstructor();
}
if (is<js::ProxyObject>()) {
const js::ProxyObject& p = as<js::ProxyObject>();
return p.handler()->isConstructor(const_cast<JSObject*>(this));
}
return constructHook() != nullptr;
}
MOZ_ALWAYS_INLINE JSNative JSObject::callHook() const {
return getClass()->getCall();
}
MOZ_ALWAYS_INLINE JSNative JSObject::constructHook() const {
return getClass()->getConstruct();
}
#endif /* vm_JSObject_inl_h */
|