1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef vm_JSObject_inl_h
8 #define vm_JSObject_inl_h
9
10 #include "vm/JSObject.h"
11
12 #include "js/Object.h" // JS::GetBuiltinClass
13 #include "vm/ArrayObject.h"
14 #include "vm/EnvironmentObject.h"
15 #include "vm/JSFunction.h"
16 #include "vm/Probes.h"
17 #include "vm/PropertyResult.h"
18 #include "vm/TypedArrayObject.h"
19
20 #include "gc/FreeOp-inl.h"
21 #include "gc/Marking-inl.h"
22 #include "gc/ObjectKind-inl.h"
23 #include "vm/ObjectOperations-inl.h" // js::MaybeHasInterestingSymbolProperty
24 #include "vm/Realm-inl.h"
25
26 namespace js {
27
28 // Get the GC kind to use for scripted 'new', empty object literals ({}), and
29 // the |Object| constructor.
NewObjectGCKind()30 static inline gc::AllocKind NewObjectGCKind() { return gc::AllocKind::OBJECT4; }
31
32 } // namespace js
33
numDynamicSlots()34 MOZ_ALWAYS_INLINE uint32_t js::NativeObject::numDynamicSlots() const {
35 uint32_t slots = getSlotsHeader()->capacity();
36 MOZ_ASSERT(slots == calculateDynamicSlots());
37 MOZ_ASSERT_IF(hasDynamicSlots(), slots != 0);
38
39 return slots;
40 }
41
calculateDynamicSlots()42 MOZ_ALWAYS_INLINE uint32_t js::NativeObject::calculateDynamicSlots() const {
43 return calculateDynamicSlots(numFixedSlots(), slotSpan(), getClass());
44 }
45
calculateDynamicSlots(uint32_t nfixed,uint32_t span,const JSClass * clasp)46 /* static */ MOZ_ALWAYS_INLINE uint32_t js::NativeObject::calculateDynamicSlots(
47 uint32_t nfixed, uint32_t span, const JSClass* clasp) {
48 if (span <= nfixed) {
49 return 0;
50 }
51
52 uint32_t ndynamic = span - nfixed;
53
54 // Increase the slots to SLOT_CAPACITY_MIN to decrease the likelihood
55 // the dynamic slots need to get increased again. ArrayObjects ignore
56 // this because slots are uncommon in that case.
57 if (clasp != &ArrayObject::class_ && ndynamic <= SLOT_CAPACITY_MIN) {
58 return SLOT_CAPACITY_MIN;
59 }
60
61 uint32_t count =
62 mozilla::RoundUpPow2(ndynamic + ObjectSlots::VALUES_PER_HEADER);
63
64 uint32_t slots = count - ObjectSlots::VALUES_PER_HEADER;
65 MOZ_ASSERT(slots >= ndynamic);
66 return slots;
67 }
68
69 /* static */ MOZ_ALWAYS_INLINE uint32_t
calculateDynamicSlots(Shape * shape)70 js::NativeObject::calculateDynamicSlots(Shape* shape) {
71 return calculateDynamicSlots(shape->numFixedSlots(), shape->slotSpan(),
72 shape->getObjectClass());
73 }
74
finalize(JSFreeOp * fop)75 inline void JSObject::finalize(JSFreeOp* fop) {
76 js::probes::FinalizeObject(this);
77
78 #ifdef DEBUG
79 MOZ_ASSERT(isTenured());
80 if (!IsBackgroundFinalized(asTenured().getAllocKind())) {
81 /* Assert we're on the main thread. */
82 MOZ_ASSERT(CurrentThreadCanAccessZone(zone()));
83 }
84 #endif
85
86 const JSClass* clasp = getClass();
87 js::NativeObject* nobj =
88 clasp->isNativeObject() ? &as<js::NativeObject>() : nullptr;
89
90 if (clasp->hasFinalize()) {
91 clasp->doFinalize(fop, this);
92 }
93
94 if (!nobj) {
95 return;
96 }
97
98 if (nobj->hasDynamicSlots()) {
99 js::ObjectSlots* slotsHeader = nobj->getSlotsHeader();
100 size_t size = js::ObjectSlots::allocSize(slotsHeader->capacity());
101 fop->free_(this, slotsHeader, size, js::MemoryUse::ObjectSlots);
102 }
103
104 if (nobj->hasDynamicElements()) {
105 js::ObjectElements* elements = nobj->getElementsHeader();
106 size_t size = elements->numAllocatedElements() * sizeof(js::HeapSlot);
107 fop->free_(this, nobj->getUnshiftedElementsHeader(), size,
108 js::MemoryUse::ObjectElements);
109 }
110 }
111
isQualifiedVarObj()112 inline bool JSObject::isQualifiedVarObj() const {
113 if (is<js::DebugEnvironmentProxy>()) {
114 return as<js::DebugEnvironmentProxy>().environment().isQualifiedVarObj();
115 }
116 bool rv = hasFlag(js::ObjectFlag::QualifiedVarObj);
117 MOZ_ASSERT_IF(rv, is<js::GlobalObject>() || is<js::CallObject>() ||
118 is<js::VarEnvironmentObject>() ||
119 is<js::ModuleEnvironmentObject>() ||
120 is<js::NonSyntacticVariablesObject>() ||
121 (is<js::WithEnvironmentObject>() &&
122 !as<js::WithEnvironmentObject>().isSyntactic()));
123 return rv;
124 }
125
isUnqualifiedVarObj()126 inline bool JSObject::isUnqualifiedVarObj() const {
127 if (is<js::DebugEnvironmentProxy>()) {
128 return as<js::DebugEnvironmentProxy>().environment().isUnqualifiedVarObj();
129 }
130 return is<js::GlobalObject>() || is<js::NonSyntacticVariablesObject>();
131 }
132
133 namespace js {
134
ClassCanHaveFixedData(const JSClass * clasp)135 inline bool ClassCanHaveFixedData(const JSClass* clasp) {
136 // Normally, the number of fixed slots given an object is the maximum
137 // permitted for its size class. For array buffers and non-shared typed
138 // arrays we only use enough to cover the class reserved slots, so that
139 // the remaining space in the object's allocation is available for the
140 // buffer's data.
141 return !clasp->isNativeObject() || clasp == &js::ArrayBufferObject::class_ ||
142 js::IsTypedArrayClass(clasp);
143 }
144
145 class MOZ_RAII AutoSuppressAllocationMetadataBuilder {
146 JS::Zone* zone;
147 bool saved;
148
149 public:
AutoSuppressAllocationMetadataBuilder(JSContext * cx)150 explicit AutoSuppressAllocationMetadataBuilder(JSContext* cx)
151 : zone(cx->zone()), saved(zone->suppressAllocationMetadataBuilder) {
152 zone->suppressAllocationMetadataBuilder = true;
153 }
154
~AutoSuppressAllocationMetadataBuilder()155 ~AutoSuppressAllocationMetadataBuilder() {
156 zone->suppressAllocationMetadataBuilder = saved;
157 }
158 };
159
160 // This function is meant to be called from allocation fast paths.
161 //
162 // If we do have an allocation metadata builder, it can cause a GC, so the
163 // object must be rooted. The usual way to do this would be to make our callers
164 // pass a HandleObject, but that would require them to pay the cost of rooting
165 // the object unconditionally, even though collecting metadata is rare. Instead,
166 // SetNewObjectMetadata's contract is that the caller must use the pointer
167 // returned in place of the pointer passed. If a GC occurs, the returned pointer
168 // may be the passed pointer, relocated by GC. If no GC could occur, it's just
169 // passed through. We root nothing unless necessary.
170 template <typename T>
SetNewObjectMetadata(JSContext * cx,T * obj)171 [[nodiscard]] static MOZ_ALWAYS_INLINE T* SetNewObjectMetadata(JSContext* cx,
172 T* obj) {
173 MOZ_ASSERT(!cx->realm()->hasObjectPendingMetadata());
174
175 // The metadata builder is invoked for each object created on the active
176 // thread, except when analysis/compilation is active, to avoid recursion.
177 if (!cx->isHelperThreadContext()) {
178 if (MOZ_UNLIKELY(cx->realm()->hasAllocationMetadataBuilder()) &&
179 !cx->zone()->suppressAllocationMetadataBuilder) {
180 // Don't collect metadata on objects that represent metadata.
181 AutoSuppressAllocationMetadataBuilder suppressMetadata(cx);
182
183 Rooted<T*> rooted(cx, obj);
184 cx->realm()->setNewObjectMetadata(cx, rooted);
185 return rooted;
186 }
187 }
188
189 return obj;
190 }
191
192 } // namespace js
193
nonCCWGlobal()194 inline js::GlobalObject& JSObject::nonCCWGlobal() const {
195 /*
196 * The global is read-barriered so that it is kept live by access through
197 * the Realm. When accessed through a JSObject, however, the global will be
198 * already kept live by the black JSObject's group pointer, so does not
199 * need to be read-barriered.
200 */
201 return *nonCCWRealm()->unsafeUnbarrieredMaybeGlobal();
202 }
203
nonProxyIsExtensible()204 inline bool JSObject::nonProxyIsExtensible() const {
205 MOZ_ASSERT(!uninlinedIsProxyObject());
206
207 // [[Extensible]] for ordinary non-proxy objects is an object flag.
208 return !hasFlag(js::ObjectFlag::NotExtensible);
209 }
210
isBoundFunction()211 inline bool JSObject::isBoundFunction() const {
212 return is<JSFunction>() && as<JSFunction>().isBoundFunction();
213 }
214
hasUncacheableProto()215 inline bool JSObject::hasUncacheableProto() const {
216 return hasFlag(js::ObjectFlag::UncacheableProto);
217 }
218
maybeHasInterestingSymbolProperty()219 MOZ_ALWAYS_INLINE bool JSObject::maybeHasInterestingSymbolProperty() const {
220 if (is<js::NativeObject>()) {
221 return as<js::NativeObject>().hasInterestingSymbol();
222 }
223 return true;
224 }
225
staticPrototypeIsImmutable()226 inline bool JSObject::staticPrototypeIsImmutable() const {
227 MOZ_ASSERT(hasStaticPrototype());
228 return hasFlag(js::ObjectFlag::ImmutablePrototype);
229 }
230
231 namespace js {
232
IsFunctionObject(const js::Value & v)233 static MOZ_ALWAYS_INLINE bool IsFunctionObject(const js::Value& v) {
234 return v.isObject() && v.toObject().is<JSFunction>();
235 }
236
IsFunctionObject(const js::Value & v,JSFunction ** fun)237 static MOZ_ALWAYS_INLINE bool IsFunctionObject(const js::Value& v,
238 JSFunction** fun) {
239 if (v.isObject() && v.toObject().is<JSFunction>()) {
240 *fun = &v.toObject().as<JSFunction>();
241 return true;
242 }
243 return false;
244 }
245
IsNativeFunction(const js::Value & v,JSNative native)246 static MOZ_ALWAYS_INLINE bool IsNativeFunction(const js::Value& v,
247 JSNative native) {
248 JSFunction* fun;
249 return IsFunctionObject(v, &fun) && fun->maybeNative() == native;
250 }
251
IsNativeFunction(const JSObject * obj,JSNative native)252 static MOZ_ALWAYS_INLINE bool IsNativeFunction(const JSObject* obj,
253 JSNative native) {
254 return obj->is<JSFunction>() && obj->as<JSFunction>().maybeNative() == native;
255 }
256
257 // Return whether looking up a method on 'obj' definitely resolves to the
258 // original specified native function. The method may conservatively return
259 // 'false' in the case of proxies or other non-native objects.
HasNativeMethodPure(JSObject * obj,PropertyName * name,JSNative native,JSContext * cx)260 static MOZ_ALWAYS_INLINE bool HasNativeMethodPure(JSObject* obj,
261 PropertyName* name,
262 JSNative native,
263 JSContext* cx) {
264 Value v;
265 if (!GetPropertyPure(cx, obj, NameToId(name), &v)) {
266 return false;
267 }
268
269 return IsNativeFunction(v, native);
270 }
271
272 // Return whether 'obj' definitely has no @@toPrimitive method.
HasNoToPrimitiveMethodPure(JSObject * obj,JSContext * cx)273 static MOZ_ALWAYS_INLINE bool HasNoToPrimitiveMethodPure(JSObject* obj,
274 JSContext* cx) {
275 JS::Symbol* toPrimitive = cx->wellKnownSymbols().toPrimitive;
276 JSObject* holder;
277 if (!MaybeHasInterestingSymbolProperty(cx, obj, toPrimitive, &holder)) {
278 #ifdef DEBUG
279 NativeObject* pobj;
280 PropertyResult prop;
281 MOZ_ASSERT(
282 LookupPropertyPure(cx, obj, SYMBOL_TO_JSID(toPrimitive), &pobj, &prop));
283 MOZ_ASSERT(prop.isNotFound());
284 #endif
285 return true;
286 }
287
288 NativeObject* pobj;
289 PropertyResult prop;
290 if (!LookupPropertyPure(cx, holder, SYMBOL_TO_JSID(toPrimitive), &pobj,
291 &prop)) {
292 return false;
293 }
294
295 return prop.isNotFound();
296 }
297
298 extern bool ToPropertyKeySlow(JSContext* cx, HandleValue argument,
299 MutableHandleId result);
300
301 /* ES6 draft rev 28 (2014 Oct 14) 7.1.14 */
ToPropertyKey(JSContext * cx,HandleValue argument,MutableHandleId result)302 MOZ_ALWAYS_INLINE bool ToPropertyKey(JSContext* cx, HandleValue argument,
303 MutableHandleId result) {
304 if (MOZ_LIKELY(argument.isPrimitive())) {
305 return PrimitiveValueToId<CanGC>(cx, argument, result);
306 }
307
308 return ToPropertyKeySlow(cx, argument, result);
309 }
310
311 /*
312 * Return true if this is a compiler-created internal function accessed by
313 * its own object. Such a function object must not be accessible to script
314 * or embedding code.
315 */
IsInternalFunctionObject(JSObject & funobj)316 inline bool IsInternalFunctionObject(JSObject& funobj) {
317 JSFunction& fun = funobj.as<JSFunction>();
318 return fun.isInterpreted() && !fun.environment();
319 }
320
321 inline gc::InitialHeap GetInitialHeap(NewObjectKind newKind,
322 const JSClass* clasp,
323 gc::AllocSite* site = nullptr) {
324 if (newKind != GenericObject) {
325 return gc::TenuredHeap;
326 }
327 if (clasp->hasFinalize() && !CanNurseryAllocateFinalizedClass(clasp)) {
328 return gc::TenuredHeap;
329 }
330 if (site) {
331 return site->initialHeap();
332 }
333 return gc::DefaultHeap;
334 }
335
336 /*
337 * Make an object with the specified prototype. If parent is null, it will
338 * default to the prototype's global if the prototype is non-null.
339 */
340 JSObject* NewObjectWithGivenTaggedProto(JSContext* cx, const JSClass* clasp,
341 Handle<TaggedProto> proto,
342 gc::AllocKind allocKind,
343 NewObjectKind newKind,
344 ObjectFlags objectFlags = {});
345
346 template <NewObjectKind NewKind>
NewObjectWithGivenTaggedProto(JSContext * cx,const JSClass * clasp,Handle<TaggedProto> proto)347 inline JSObject* NewObjectWithGivenTaggedProto(JSContext* cx,
348 const JSClass* clasp,
349 Handle<TaggedProto> proto) {
350 gc::AllocKind allocKind = gc::GetGCObjectKind(clasp);
351 return NewObjectWithGivenTaggedProto(cx, clasp, proto, allocKind, NewKind);
352 }
353
354 namespace detail {
355
356 template <typename T, NewObjectKind NewKind>
NewObjectWithGivenTaggedProtoForKind(JSContext * cx,Handle<TaggedProto> proto)357 inline T* NewObjectWithGivenTaggedProtoForKind(JSContext* cx,
358 Handle<TaggedProto> proto) {
359 JSObject* obj = NewObjectWithGivenTaggedProto<NewKind>(cx, &T::class_, proto);
360 return obj ? &obj->as<T>() : nullptr;
361 }
362
363 } // namespace detail
364
365 template <typename T>
NewObjectWithGivenTaggedProto(JSContext * cx,Handle<TaggedProto> proto)366 inline T* NewObjectWithGivenTaggedProto(JSContext* cx,
367 Handle<TaggedProto> proto) {
368 return detail::NewObjectWithGivenTaggedProtoForKind<T, GenericObject>(cx,
369 proto);
370 }
371
372 inline JSObject* NewObjectWithGivenProto(
373 JSContext* cx, const JSClass* clasp, HandleObject proto,
374 gc::AllocKind allocKind, NewObjectKind newKind = GenericObject) {
375 return NewObjectWithGivenTaggedProto(cx, clasp, AsTaggedProto(proto),
376 allocKind, newKind);
377 }
378
NewObjectWithGivenProto(JSContext * cx,const JSClass * clasp,HandleObject proto)379 inline JSObject* NewObjectWithGivenProto(JSContext* cx, const JSClass* clasp,
380 HandleObject proto) {
381 return NewObjectWithGivenTaggedProto<GenericObject>(cx, clasp,
382 AsTaggedProto(proto));
383 }
384
NewTenuredObjectWithGivenProto(JSContext * cx,const JSClass * clasp,HandleObject proto)385 inline JSObject* NewTenuredObjectWithGivenProto(JSContext* cx,
386 const JSClass* clasp,
387 HandleObject proto) {
388 return NewObjectWithGivenTaggedProto<TenuredObject>(cx, clasp,
389 AsTaggedProto(proto));
390 }
391
392 template <typename T>
NewObjectWithGivenProto(JSContext * cx,HandleObject proto)393 inline T* NewObjectWithGivenProto(JSContext* cx, HandleObject proto) {
394 return detail::NewObjectWithGivenTaggedProtoForKind<T, GenericObject>(
395 cx, AsTaggedProto(proto));
396 }
397
398 template <typename T>
NewTenuredObjectWithGivenProto(JSContext * cx,HandleObject proto)399 inline T* NewTenuredObjectWithGivenProto(JSContext* cx, HandleObject proto) {
400 return detail::NewObjectWithGivenTaggedProtoForKind<T, TenuredObject>(
401 cx, AsTaggedProto(proto));
402 }
403
404 template <typename T>
NewObjectWithGivenProtoAndKinds(JSContext * cx,HandleObject proto,gc::AllocKind allocKind,NewObjectKind newKind)405 inline T* NewObjectWithGivenProtoAndKinds(JSContext* cx, HandleObject proto,
406 gc::AllocKind allocKind,
407 NewObjectKind newKind) {
408 JSObject* obj = NewObjectWithGivenTaggedProto(
409 cx, &T::class_, AsTaggedProto(proto), allocKind, newKind);
410 return obj ? &obj->as<T>() : nullptr;
411 }
412
413 // Make an object with the prototype set according to the cached prototype or
414 // Object.prototype.
415 JSObject* NewObjectWithClassProto(JSContext* cx, const JSClass* clasp,
416 HandleObject proto, gc::AllocKind allocKind,
417 NewObjectKind newKind = GenericObject);
418
419 inline JSObject* NewObjectWithClassProto(
420 JSContext* cx, const JSClass* clasp, HandleObject proto,
421 NewObjectKind newKind = GenericObject) {
422 gc::AllocKind allocKind = gc::GetGCObjectKind(clasp);
423 return NewObjectWithClassProto(cx, clasp, proto, allocKind, newKind);
424 }
425
426 template <class T>
NewObjectWithClassProto(JSContext * cx,HandleObject proto)427 inline T* NewObjectWithClassProto(JSContext* cx, HandleObject proto) {
428 JSObject* obj = NewObjectWithClassProto(cx, &T::class_, proto, GenericObject);
429 return obj ? &obj->as<T>() : nullptr;
430 }
431
432 template <class T>
NewObjectWithClassProtoAndKind(JSContext * cx,HandleObject proto,NewObjectKind newKind)433 inline T* NewObjectWithClassProtoAndKind(JSContext* cx, HandleObject proto,
434 NewObjectKind newKind) {
435 JSObject* obj = NewObjectWithClassProto(cx, &T::class_, proto, newKind);
436 return obj ? &obj->as<T>() : nullptr;
437 }
438
439 template <class T>
440 inline T* NewObjectWithClassProto(JSContext* cx, HandleObject proto,
441 gc::AllocKind allocKind,
442 NewObjectKind newKind = GenericObject) {
443 JSObject* obj =
444 NewObjectWithClassProto(cx, &T::class_, proto, allocKind, newKind);
445 return obj ? &obj->as<T>() : nullptr;
446 }
447
448 /*
449 * Create a native instance of the given class with parent and proto set
450 * according to the context's active global.
451 */
452 inline JSObject* NewBuiltinClassInstance(
453 JSContext* cx, const JSClass* clasp, gc::AllocKind allocKind,
454 NewObjectKind newKind = GenericObject) {
455 return NewObjectWithClassProto(cx, clasp, nullptr, allocKind, newKind);
456 }
457
458 inline JSObject* NewBuiltinClassInstance(
459 JSContext* cx, const JSClass* clasp,
460 NewObjectKind newKind = GenericObject) {
461 gc::AllocKind allocKind = gc::GetGCObjectKind(clasp);
462 return NewBuiltinClassInstance(cx, clasp, allocKind, newKind);
463 }
464
465 template <typename T>
NewBuiltinClassInstance(JSContext * cx)466 inline T* NewBuiltinClassInstance(JSContext* cx) {
467 JSObject* obj = NewBuiltinClassInstance(cx, &T::class_, GenericObject);
468 return obj ? &obj->as<T>() : nullptr;
469 }
470
471 template <typename T>
NewTenuredBuiltinClassInstance(JSContext * cx)472 inline T* NewTenuredBuiltinClassInstance(JSContext* cx) {
473 JSObject* obj = NewBuiltinClassInstance(cx, &T::class_, TenuredObject);
474 return obj ? &obj->as<T>() : nullptr;
475 }
476
477 template <typename T>
NewBuiltinClassInstanceWithKind(JSContext * cx,NewObjectKind newKind)478 inline T* NewBuiltinClassInstanceWithKind(JSContext* cx,
479 NewObjectKind newKind) {
480 JSObject* obj = NewBuiltinClassInstance(cx, &T::class_, newKind);
481 return obj ? &obj->as<T>() : nullptr;
482 }
483
484 template <typename T>
485 inline T* NewBuiltinClassInstance(JSContext* cx, gc::AllocKind allocKind,
486 NewObjectKind newKind = GenericObject) {
487 JSObject* obj = NewBuiltinClassInstance(cx, &T::class_, allocKind, newKind);
488 return obj ? &obj->as<T>() : nullptr;
489 }
490
491 // Used to optimize calls to (new Object())
492 bool NewObjectScriptedCall(JSContext* cx, MutableHandleObject obj);
493
494 /*
495 * As for gc::GetGCObjectKind, where numElements is a guess at the final size of
496 * the object, zero if the final size is unknown. This should only be used for
497 * objects that do not require any fixed slots.
498 */
GuessObjectGCKind(size_t numElements)499 static inline gc::AllocKind GuessObjectGCKind(size_t numElements) {
500 if (numElements) {
501 return gc::GetGCObjectKind(numElements);
502 }
503 return gc::AllocKind::OBJECT4;
504 }
505
GuessArrayGCKind(size_t numElements)506 static inline gc::AllocKind GuessArrayGCKind(size_t numElements) {
507 if (numElements) {
508 return gc::GetGCArrayKind(numElements);
509 }
510 return gc::AllocKind::OBJECT8;
511 }
512
513 // Returns ESClass::Other if the value isn't an object, or if the object
514 // isn't of one of the enumerated classes. Otherwise returns the appropriate
515 // class.
GetClassOfValue(JSContext * cx,HandleValue v,ESClass * cls)516 inline bool GetClassOfValue(JSContext* cx, HandleValue v, ESClass* cls) {
517 if (!v.isObject()) {
518 *cls = ESClass::Other;
519 return true;
520 }
521
522 RootedObject obj(cx, &v.toObject());
523 return JS::GetBuiltinClass(cx, obj, cls);
524 }
525
526 extern NativeObject* InitClass(JSContext* cx, HandleObject obj,
527 HandleObject parent_proto, const JSClass* clasp,
528 JSNative constructor, unsigned nargs,
529 const JSPropertySpec* ps,
530 const JSFunctionSpec* fs,
531 const JSPropertySpec* static_ps,
532 const JSFunctionSpec* static_fs,
533 NativeObject** ctorp = nullptr);
534
GetObjectClassName(JSContext * cx,HandleObject obj)535 MOZ_ALWAYS_INLINE const char* GetObjectClassName(JSContext* cx,
536 HandleObject obj) {
537 if (obj->is<ProxyObject>()) {
538 return Proxy::className(cx, obj);
539 }
540
541 return obj->getClass()->name;
542 }
543
IsCallable(const Value & v)544 inline bool IsCallable(const Value& v) {
545 return v.isObject() && v.toObject().isCallable();
546 }
547
548 // ES6 rev 24 (2014 April 27) 7.2.5 IsConstructor
IsConstructor(const Value & v)549 inline bool IsConstructor(const Value& v) {
550 return v.isObject() && v.toObject().isConstructor();
551 }
552
MaybePreserveDOMWrapper(JSContext * cx,HandleObject obj)553 static inline bool MaybePreserveDOMWrapper(JSContext* cx, HandleObject obj) {
554 if (!obj->getClass()->isDOMClass()) {
555 return true;
556 }
557
558 MOZ_ASSERT(cx->runtime()->preserveWrapperCallback);
559 return cx->runtime()->preserveWrapperCallback(cx, obj);
560 }
561
562 } /* namespace js */
563
isCallable()564 MOZ_ALWAYS_INLINE bool JSObject::isCallable() const {
565 if (is<JSFunction>()) {
566 return true;
567 }
568 if (is<js::ProxyObject>()) {
569 const js::ProxyObject& p = as<js::ProxyObject>();
570 return p.handler()->isCallable(const_cast<JSObject*>(this));
571 }
572 return callHook() != nullptr;
573 }
574
isConstructor()575 MOZ_ALWAYS_INLINE bool JSObject::isConstructor() const {
576 if (is<JSFunction>()) {
577 const JSFunction& fun = as<JSFunction>();
578 return fun.isConstructor();
579 }
580 if (is<js::ProxyObject>()) {
581 const js::ProxyObject& p = as<js::ProxyObject>();
582 return p.handler()->isConstructor(const_cast<JSObject*>(this));
583 }
584 return constructHook() != nullptr;
585 }
586
callHook()587 MOZ_ALWAYS_INLINE JSNative JSObject::callHook() const {
588 return getClass()->getCall();
589 }
590
constructHook()591 MOZ_ALWAYS_INLINE JSNative JSObject::constructHook() const {
592 return getClass()->getConstruct();
593 }
594
595 #endif /* vm_JSObject_inl_h */
596