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