js/src/jsobjinlines.h

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef jsobjinlines_h
michael@0 8 #define jsobjinlines_h
michael@0 9
michael@0 10 #include "jsobj.h"
michael@0 11
michael@0 12 #include "vm/ArrayObject.h"
michael@0 13 #include "vm/DateObject.h"
michael@0 14 #include "vm/NumberObject.h"
michael@0 15 #include "vm/Probes.h"
michael@0 16 #include "vm/ScopeObject.h"
michael@0 17 #include "vm/StringObject.h"
michael@0 18
michael@0 19 #include "jsatominlines.h"
michael@0 20 #include "jscompartmentinlines.h"
michael@0 21 #include "jsgcinlines.h"
michael@0 22 #include "jsinferinlines.h"
michael@0 23
michael@0 24 #include "vm/ObjectImpl-inl.h"
michael@0 25
michael@0 26 /* static */ inline bool
michael@0 27 JSObject::setGenericAttributes(JSContext *cx, js::HandleObject obj,
michael@0 28 js::HandleId id, unsigned *attrsp)
michael@0 29 {
michael@0 30 js::types::MarkTypePropertyNonData(cx, obj, id);
michael@0 31 js::GenericAttributesOp op = obj->getOps()->setGenericAttributes;
michael@0 32 return (op ? op : js::baseops::SetAttributes)(cx, obj, id, attrsp);
michael@0 33 }
michael@0 34
michael@0 35 /* static */ inline bool
michael@0 36 JSObject::changePropertyAttributes(JSContext *cx, js::HandleObject obj,
michael@0 37 js::HandleShape shape, unsigned attrs)
michael@0 38 {
michael@0 39 return !!changeProperty<js::SequentialExecution>(cx, obj, shape, attrs, 0,
michael@0 40 shape->getter(), shape->setter());
michael@0 41 }
michael@0 42
michael@0 43 /* static */ inline bool
michael@0 44 JSObject::deleteProperty(JSContext *cx, js::HandleObject obj, js::HandlePropertyName name,
michael@0 45 bool *succeeded)
michael@0 46 {
michael@0 47 JS::RootedId id(cx, js::NameToId(name));
michael@0 48 js::types::MarkTypePropertyNonData(cx, obj, id);
michael@0 49 js::DeletePropertyOp op = obj->getOps()->deleteProperty;
michael@0 50 return (op ? op : js::baseops::DeleteProperty)(cx, obj, name, succeeded);
michael@0 51 }
michael@0 52
michael@0 53 /* static */ inline bool
michael@0 54 JSObject::deleteElement(JSContext *cx, js::HandleObject obj, uint32_t index, bool *succeeded)
michael@0 55 {
michael@0 56 JS::RootedId id(cx);
michael@0 57 if (!js::IndexToId(cx, index, &id))
michael@0 58 return false;
michael@0 59 js::types::MarkTypePropertyNonData(cx, obj, id);
michael@0 60 js::DeleteElementOp op = obj->getOps()->deleteElement;
michael@0 61 return (op ? op : js::baseops::DeleteElement)(cx, obj, index, succeeded);
michael@0 62 }
michael@0 63
michael@0 64 /* static */ inline bool
michael@0 65 JSObject::watch(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
michael@0 66 JS::HandleObject callable)
michael@0 67 {
michael@0 68 js::WatchOp op = obj->getOps()->watch;
michael@0 69 return (op ? op : js::baseops::Watch)(cx, obj, id, callable);
michael@0 70 }
michael@0 71
michael@0 72 /* static */ inline bool
michael@0 73 JSObject::unwatch(JSContext *cx, JS::HandleObject obj, JS::HandleId id)
michael@0 74 {
michael@0 75 js::UnwatchOp op = obj->getOps()->unwatch;
michael@0 76 return (op ? op : js::baseops::Unwatch)(cx, obj, id);
michael@0 77 }
michael@0 78
michael@0 79 inline void
michael@0 80 JSObject::finalize(js::FreeOp *fop)
michael@0 81 {
michael@0 82 js::probes::FinalizeObject(this);
michael@0 83
michael@0 84 #ifdef DEBUG
michael@0 85 JS_ASSERT(isTenured());
michael@0 86 if (!IsBackgroundFinalized(tenuredGetAllocKind())) {
michael@0 87 /* Assert we're on the main thread. */
michael@0 88 JS_ASSERT(CurrentThreadCanAccessRuntime(fop->runtime()));
michael@0 89 }
michael@0 90 #endif
michael@0 91 const js::Class *clasp = getClass();
michael@0 92 if (clasp->finalize)
michael@0 93 clasp->finalize(fop, this);
michael@0 94
michael@0 95 finish(fop);
michael@0 96 }
michael@0 97
michael@0 98 inline void
michael@0 99 JSObject::setLastPropertyInfallible(js::Shape *shape)
michael@0 100 {
michael@0 101 JS_ASSERT(!shape->inDictionary());
michael@0 102 JS_ASSERT(shape->compartment() == compartment());
michael@0 103 JS_ASSERT(!inDictionaryMode());
michael@0 104 JS_ASSERT(slotSpan() == shape->slotSpan());
michael@0 105 JS_ASSERT(numFixedSlots() == shape->numFixedSlots());
michael@0 106
michael@0 107 shape_ = shape;
michael@0 108 }
michael@0 109
michael@0 110 inline void
michael@0 111 JSObject::removeLastProperty(js::ExclusiveContext *cx)
michael@0 112 {
michael@0 113 JS_ASSERT(canRemoveLastProperty());
michael@0 114 JS::RootedObject self(cx, this);
michael@0 115 js::RootedShape prev(cx, lastProperty()->previous());
michael@0 116 JS_ALWAYS_TRUE(setLastProperty(cx, self, prev));
michael@0 117 }
michael@0 118
michael@0 119 inline bool
michael@0 120 JSObject::canRemoveLastProperty()
michael@0 121 {
michael@0 122 /*
michael@0 123 * Check that the information about the object stored in the last
michael@0 124 * property's base shape is consistent with that stored in the previous
michael@0 125 * shape. If not consistent, then the last property cannot be removed as it
michael@0 126 * will induce a change in the object itself, and the object must be
michael@0 127 * converted to dictionary mode instead. See BaseShape comment in jsscope.h
michael@0 128 */
michael@0 129 JS_ASSERT(!inDictionaryMode());
michael@0 130 js::Shape *previous = lastProperty()->previous().get();
michael@0 131 return previous->getObjectParent() == lastProperty()->getObjectParent()
michael@0 132 && previous->getObjectMetadata() == lastProperty()->getObjectMetadata()
michael@0 133 && previous->getObjectFlags() == lastProperty()->getObjectFlags();
michael@0 134 }
michael@0 135
michael@0 136 inline void
michael@0 137 JSObject::setShouldConvertDoubleElements()
michael@0 138 {
michael@0 139 JS_ASSERT(is<js::ArrayObject>() && !hasEmptyElements());
michael@0 140 getElementsHeader()->setShouldConvertDoubleElements();
michael@0 141 }
michael@0 142
michael@0 143 inline void
michael@0 144 JSObject::clearShouldConvertDoubleElements()
michael@0 145 {
michael@0 146 JS_ASSERT(is<js::ArrayObject>() && !hasEmptyElements());
michael@0 147 getElementsHeader()->clearShouldConvertDoubleElements();
michael@0 148 }
michael@0 149
michael@0 150 inline bool
michael@0 151 JSObject::setDenseElementIfHasType(uint32_t index, const js::Value &val)
michael@0 152 {
michael@0 153 if (!js::types::HasTypePropertyId(this, JSID_VOID, val))
michael@0 154 return false;
michael@0 155 setDenseElementMaybeConvertDouble(index, val);
michael@0 156 return true;
michael@0 157 }
michael@0 158
michael@0 159 inline void
michael@0 160 JSObject::setDenseElementWithType(js::ExclusiveContext *cx, uint32_t index,
michael@0 161 const js::Value &val)
michael@0 162 {
michael@0 163 // Avoid a slow AddTypePropertyId call if the type is the same as the type
michael@0 164 // of the previous element.
michael@0 165 js::types::Type thisType = js::types::GetValueType(val);
michael@0 166 if (index == 0 || js::types::GetValueType(elements[index - 1]) != thisType)
michael@0 167 js::types::AddTypePropertyId(cx, this, JSID_VOID, thisType);
michael@0 168 setDenseElementMaybeConvertDouble(index, val);
michael@0 169 }
michael@0 170
michael@0 171 inline void
michael@0 172 JSObject::initDenseElementWithType(js::ExclusiveContext *cx, uint32_t index,
michael@0 173 const js::Value &val)
michael@0 174 {
michael@0 175 JS_ASSERT(!shouldConvertDoubleElements());
michael@0 176 js::types::AddTypePropertyId(cx, this, JSID_VOID, val);
michael@0 177 initDenseElement(index, val);
michael@0 178 }
michael@0 179
michael@0 180 inline void
michael@0 181 JSObject::setDenseElementHole(js::ExclusiveContext *cx, uint32_t index)
michael@0 182 {
michael@0 183 js::types::MarkTypeObjectFlags(cx, this, js::types::OBJECT_FLAG_NON_PACKED);
michael@0 184 setDenseElement(index, js::MagicValue(JS_ELEMENTS_HOLE));
michael@0 185 }
michael@0 186
michael@0 187 /* static */ inline void
michael@0 188 JSObject::removeDenseElementForSparseIndex(js::ExclusiveContext *cx,
michael@0 189 js::HandleObject obj, uint32_t index)
michael@0 190 {
michael@0 191 js::types::MarkTypeObjectFlags(cx, obj,
michael@0 192 js::types::OBJECT_FLAG_NON_PACKED |
michael@0 193 js::types::OBJECT_FLAG_SPARSE_INDEXES);
michael@0 194 if (obj->containsDenseElement(index))
michael@0 195 obj->setDenseElement(index, js::MagicValue(JS_ELEMENTS_HOLE));
michael@0 196 }
michael@0 197
michael@0 198 inline bool
michael@0 199 JSObject::writeToIndexWouldMarkNotPacked(uint32_t index)
michael@0 200 {
michael@0 201 return getElementsHeader()->initializedLength < index;
michael@0 202 }
michael@0 203
michael@0 204 inline void
michael@0 205 JSObject::markDenseElementsNotPacked(js::ExclusiveContext *cx)
michael@0 206 {
michael@0 207 JS_ASSERT(isNative());
michael@0 208 MarkTypeObjectFlags(cx, this, js::types::OBJECT_FLAG_NON_PACKED);
michael@0 209 }
michael@0 210
michael@0 211 inline void
michael@0 212 JSObject::ensureDenseInitializedLengthNoPackedCheck(js::ThreadSafeContext *cx, uint32_t index,
michael@0 213 uint32_t extra)
michael@0 214 {
michael@0 215 JS_ASSERT(cx->isThreadLocal(this));
michael@0 216
michael@0 217 /*
michael@0 218 * Ensure that the array's contents have been initialized up to index, and
michael@0 219 * mark the elements through 'index + extra' as initialized in preparation
michael@0 220 * for a write.
michael@0 221 */
michael@0 222 JS_ASSERT(index + extra <= getDenseCapacity());
michael@0 223 uint32_t &initlen = getElementsHeader()->initializedLength;
michael@0 224
michael@0 225 if (initlen < index + extra) {
michael@0 226 JSRuntime *rt = runtimeFromAnyThread();
michael@0 227 size_t offset = initlen;
michael@0 228 for (js::HeapSlot *sp = elements + initlen;
michael@0 229 sp != elements + (index + extra);
michael@0 230 sp++, offset++)
michael@0 231 sp->init(rt, this, js::HeapSlot::Element, offset, js::MagicValue(JS_ELEMENTS_HOLE));
michael@0 232 initlen = index + extra;
michael@0 233 }
michael@0 234 }
michael@0 235
michael@0 236 inline void
michael@0 237 JSObject::ensureDenseInitializedLength(js::ExclusiveContext *cx, uint32_t index, uint32_t extra)
michael@0 238 {
michael@0 239 if (writeToIndexWouldMarkNotPacked(index))
michael@0 240 markDenseElementsNotPacked(cx);
michael@0 241 ensureDenseInitializedLengthNoPackedCheck(cx, index, extra);
michael@0 242 }
michael@0 243
michael@0 244 inline void
michael@0 245 JSObject::ensureDenseInitializedLengthPreservePackedFlag(js::ThreadSafeContext *cx,
michael@0 246 uint32_t index, uint32_t extra)
michael@0 247 {
michael@0 248 JS_ASSERT(!writeToIndexWouldMarkNotPacked(index));
michael@0 249 ensureDenseInitializedLengthNoPackedCheck(cx, index, extra);
michael@0 250 }
michael@0 251
michael@0 252 JSObject::EnsureDenseResult
michael@0 253 JSObject::extendDenseElements(js::ThreadSafeContext *cx,
michael@0 254 uint32_t requiredCapacity, uint32_t extra)
michael@0 255 {
michael@0 256 JS_ASSERT(cx->isThreadLocal(this));
michael@0 257
michael@0 258 /*
michael@0 259 * Don't grow elements for non-extensible objects or watched objects. Dense
michael@0 260 * elements can be added/written with no extensible or watchpoint checks as
michael@0 261 * long as there is capacity for them.
michael@0 262 */
michael@0 263 if (!nonProxyIsExtensible() || watched()) {
michael@0 264 JS_ASSERT(getDenseCapacity() == 0);
michael@0 265 return ED_SPARSE;
michael@0 266 }
michael@0 267
michael@0 268 /*
michael@0 269 * Don't grow elements for objects which already have sparse indexes.
michael@0 270 * This avoids needing to count non-hole elements in willBeSparseElements
michael@0 271 * every time a new index is added.
michael@0 272 */
michael@0 273 if (isIndexed())
michael@0 274 return ED_SPARSE;
michael@0 275
michael@0 276 /*
michael@0 277 * We use the extra argument also as a hint about number of non-hole
michael@0 278 * elements to be inserted.
michael@0 279 */
michael@0 280 if (requiredCapacity > MIN_SPARSE_INDEX &&
michael@0 281 willBeSparseElements(requiredCapacity, extra)) {
michael@0 282 return ED_SPARSE;
michael@0 283 }
michael@0 284
michael@0 285 if (!growElements(cx, requiredCapacity))
michael@0 286 return ED_FAILED;
michael@0 287
michael@0 288 return ED_OK;
michael@0 289 }
michael@0 290
michael@0 291 inline JSObject::EnsureDenseResult
michael@0 292 JSObject::ensureDenseElementsNoPackedCheck(js::ThreadSafeContext *cx, uint32_t index, uint32_t extra)
michael@0 293 {
michael@0 294 JS_ASSERT(isNative());
michael@0 295
michael@0 296 uint32_t currentCapacity = getDenseCapacity();
michael@0 297
michael@0 298 uint32_t requiredCapacity;
michael@0 299 if (extra == 1) {
michael@0 300 /* Optimize for the common case. */
michael@0 301 if (index < currentCapacity) {
michael@0 302 ensureDenseInitializedLengthNoPackedCheck(cx, index, 1);
michael@0 303 return ED_OK;
michael@0 304 }
michael@0 305 requiredCapacity = index + 1;
michael@0 306 if (requiredCapacity == 0) {
michael@0 307 /* Overflow. */
michael@0 308 return ED_SPARSE;
michael@0 309 }
michael@0 310 } else {
michael@0 311 requiredCapacity = index + extra;
michael@0 312 if (requiredCapacity < index) {
michael@0 313 /* Overflow. */
michael@0 314 return ED_SPARSE;
michael@0 315 }
michael@0 316 if (requiredCapacity <= currentCapacity) {
michael@0 317 ensureDenseInitializedLengthNoPackedCheck(cx, index, extra);
michael@0 318 return ED_OK;
michael@0 319 }
michael@0 320 }
michael@0 321
michael@0 322 EnsureDenseResult edr = extendDenseElements(cx, requiredCapacity, extra);
michael@0 323 if (edr != ED_OK)
michael@0 324 return edr;
michael@0 325
michael@0 326 ensureDenseInitializedLengthNoPackedCheck(cx, index, extra);
michael@0 327 return ED_OK;
michael@0 328 }
michael@0 329
michael@0 330 inline JSObject::EnsureDenseResult
michael@0 331 JSObject::ensureDenseElements(js::ExclusiveContext *cx, uint32_t index, uint32_t extra)
michael@0 332 {
michael@0 333 if (writeToIndexWouldMarkNotPacked(index))
michael@0 334 markDenseElementsNotPacked(cx);
michael@0 335 return ensureDenseElementsNoPackedCheck(cx, index, extra);
michael@0 336 }
michael@0 337
michael@0 338 inline JSObject::EnsureDenseResult
michael@0 339 JSObject::ensureDenseElementsPreservePackedFlag(js::ThreadSafeContext *cx, uint32_t index,
michael@0 340 uint32_t extra)
michael@0 341 {
michael@0 342 JS_ASSERT(!writeToIndexWouldMarkNotPacked(index));
michael@0 343 return ensureDenseElementsNoPackedCheck(cx, index, extra);
michael@0 344 }
michael@0 345
michael@0 346 inline js::Value
michael@0 347 JSObject::getDenseOrTypedArrayElement(uint32_t idx)
michael@0 348 {
michael@0 349 if (is<js::TypedArrayObject>())
michael@0 350 return as<js::TypedArrayObject>().getElement(idx);
michael@0 351 return getDenseElement(idx);
michael@0 352 }
michael@0 353
michael@0 354 /* static */ inline bool
michael@0 355 JSObject::setSingletonType(js::ExclusiveContext *cx, js::HandleObject obj)
michael@0 356 {
michael@0 357 JS_ASSERT_IF(cx->isJSContext(),
michael@0 358 !IsInsideNursery(cx->asJSContext()->runtime(), obj.get()));
michael@0 359
michael@0 360 js::types::TypeObject *type = cx->getSingletonType(obj->getClass(), obj->getTaggedProto());
michael@0 361 if (!type)
michael@0 362 return false;
michael@0 363
michael@0 364 obj->type_ = type;
michael@0 365 return true;
michael@0 366 }
michael@0 367
michael@0 368 inline js::types::TypeObject*
michael@0 369 JSObject::getType(JSContext *cx)
michael@0 370 {
michael@0 371 JS_ASSERT(cx->compartment() == compartment());
michael@0 372 if (hasLazyType()) {
michael@0 373 JS::RootedObject self(cx, this);
michael@0 374 if (cx->compartment() != compartment())
michael@0 375 MOZ_CRASH();
michael@0 376 return makeLazyType(cx, self);
michael@0 377 }
michael@0 378 return static_cast<js::types::TypeObject*>(type_);
michael@0 379 }
michael@0 380
michael@0 381 /* static */ inline bool
michael@0 382 JSObject::clearType(JSContext *cx, js::HandleObject obj)
michael@0 383 {
michael@0 384 JS_ASSERT(!obj->hasSingletonType());
michael@0 385 JS_ASSERT(cx->compartment() == obj->compartment());
michael@0 386
michael@0 387 js::types::TypeObject *type = cx->getNewType(obj->getClass(), nullptr);
michael@0 388 if (!type)
michael@0 389 return false;
michael@0 390
michael@0 391 obj->type_ = type;
michael@0 392 return true;
michael@0 393 }
michael@0 394
michael@0 395 inline void
michael@0 396 JSObject::setType(js::types::TypeObject *newType)
michael@0 397 {
michael@0 398 JS_ASSERT(newType);
michael@0 399 JS_ASSERT(!hasSingletonType());
michael@0 400 type_ = newType;
michael@0 401 }
michael@0 402
michael@0 403 /* static */ inline bool
michael@0 404 JSObject::getProto(JSContext *cx, js::HandleObject obj, js::MutableHandleObject protop)
michael@0 405 {
michael@0 406 if (obj->getTaggedProto().isLazy()) {
michael@0 407 JS_ASSERT(obj->is<js::ProxyObject>());
michael@0 408 return js::Proxy::getPrototypeOf(cx, obj, protop);
michael@0 409 } else {
michael@0 410 protop.set(obj->getTaggedProto().toObjectOrNull());
michael@0 411 return true;
michael@0 412 }
michael@0 413 }
michael@0 414
michael@0 415 /* static */ inline bool
michael@0 416 JSObject::setProto(JSContext *cx, JS::HandleObject obj, JS::HandleObject proto, bool *succeeded)
michael@0 417 {
michael@0 418 /* Proxies live in their own little world. */
michael@0 419 if (obj->getTaggedProto().isLazy()) {
michael@0 420 JS_ASSERT(obj->is<js::ProxyObject>());
michael@0 421 return js::Proxy::setPrototypeOf(cx, obj, proto, succeeded);
michael@0 422 }
michael@0 423
michael@0 424 /*
michael@0 425 * Disallow mutating the [[Prototype]] on ArrayBuffer objects, which
michael@0 426 * due to their complicated delegate-object shenanigans can't easily
michael@0 427 * have a mutable [[Prototype]].
michael@0 428 */
michael@0 429 if (obj->is<js::ArrayBufferObject>()) {
michael@0 430 JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_SETPROTOTYPEOF_FAIL,
michael@0 431 "incompatible ArrayBuffer");
michael@0 432 return false;
michael@0 433 }
michael@0 434
michael@0 435 /*
michael@0 436 * Disallow mutating the [[Prototype]] on Typed Objects, per the spec.
michael@0 437 */
michael@0 438 if (obj->is<js::TypedObject>()) {
michael@0 439 JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_SETPROTOTYPEOF_FAIL,
michael@0 440 "incompatible TypedObject");
michael@0 441 return false;
michael@0 442 }
michael@0 443
michael@0 444 /*
michael@0 445 * Explicitly disallow mutating the [[Prototype]] of Location objects
michael@0 446 * for flash-related security reasons.
michael@0 447 */
michael@0 448 if (!strcmp(obj->getClass()->name, "Location")) {
michael@0 449 JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_SETPROTOTYPEOF_FAIL,
michael@0 450 "incompatible Location object");
michael@0 451 return false;
michael@0 452 }
michael@0 453
michael@0 454 /* ES6 9.1.2 step 5 forbids changing [[Prototype]] if not [[Extensible]]. */
michael@0 455 bool extensible;
michael@0 456 if (!JSObject::isExtensible(cx, obj, &extensible))
michael@0 457 return false;
michael@0 458 if (!extensible) {
michael@0 459 *succeeded = false;
michael@0 460 return true;
michael@0 461 }
michael@0 462
michael@0 463 /* ES6 9.1.2 step 6 forbids generating cyclical prototype chains. */
michael@0 464 js::RootedObject obj2(cx);
michael@0 465 for (obj2 = proto; obj2; ) {
michael@0 466 if (obj2 == obj) {
michael@0 467 *succeeded = false;
michael@0 468 return true;
michael@0 469 }
michael@0 470
michael@0 471 if (!JSObject::getProto(cx, obj2, &obj2))
michael@0 472 return false;
michael@0 473 }
michael@0 474
michael@0 475 return SetClassAndProto(cx, obj, obj->getClass(), proto, succeeded);
michael@0 476 }
michael@0 477
michael@0 478 inline bool
michael@0 479 JSObject::isVarObj()
michael@0 480 {
michael@0 481 if (is<js::DebugScopeObject>())
michael@0 482 return as<js::DebugScopeObject>().scope().isVarObj();
michael@0 483 return lastProperty()->hasObjectFlag(js::BaseShape::VAROBJ);
michael@0 484 }
michael@0 485
michael@0 486 /* static */ inline JSObject *
michael@0 487 JSObject::create(js::ExclusiveContext *cx, js::gc::AllocKind kind, js::gc::InitialHeap heap,
michael@0 488 js::HandleShape shape, js::HandleTypeObject type,
michael@0 489 js::HeapSlot *extantSlots /* = nullptr */)
michael@0 490 {
michael@0 491 /*
michael@0 492 * Callers must use dynamicSlotsCount to size the initial slot array of the
michael@0 493 * object. We can't check the allocated capacity of the dynamic slots, but
michael@0 494 * make sure their presence is consistent with the shape.
michael@0 495 */
michael@0 496 JS_ASSERT(shape && type);
michael@0 497 JS_ASSERT(type->clasp() == shape->getObjectClass());
michael@0 498 JS_ASSERT(type->clasp() != &js::ArrayObject::class_);
michael@0 499 JS_ASSERT_IF(!ClassCanHaveFixedData(type->clasp()),
michael@0 500 js::gc::GetGCKindSlots(kind, type->clasp()) == shape->numFixedSlots());
michael@0 501 JS_ASSERT_IF(type->clasp()->flags & JSCLASS_BACKGROUND_FINALIZE, IsBackgroundFinalized(kind));
michael@0 502 JS_ASSERT_IF(type->clasp()->finalize, heap == js::gc::TenuredHeap);
michael@0 503 JS_ASSERT_IF(extantSlots, dynamicSlotsCount(shape->numFixedSlots(), shape->slotSpan(),
michael@0 504 type->clasp()));
michael@0 505
michael@0 506 const js::Class *clasp = type->clasp();
michael@0 507 size_t nDynamicSlots = 0;
michael@0 508 if (!extantSlots)
michael@0 509 nDynamicSlots = dynamicSlotsCount(shape->numFixedSlots(), shape->slotSpan(), clasp);
michael@0 510
michael@0 511 JSObject *obj = js::NewGCObject<js::CanGC>(cx, kind, nDynamicSlots, heap);
michael@0 512 if (!obj)
michael@0 513 return nullptr;
michael@0 514
michael@0 515 obj->shape_.init(shape);
michael@0 516 obj->type_.init(type);
michael@0 517 if (extantSlots) {
michael@0 518 #ifdef JSGC_GENERATIONAL
michael@0 519 if (cx->isJSContext())
michael@0 520 cx->asJSContext()->runtime()->gcNursery.notifyInitialSlots(obj, extantSlots);
michael@0 521 #endif
michael@0 522 obj->slots = extantSlots;
michael@0 523 }
michael@0 524 obj->elements = js::emptyObjectElements;
michael@0 525
michael@0 526 if (clasp->hasPrivate())
michael@0 527 obj->privateRef(shape->numFixedSlots()) = nullptr;
michael@0 528
michael@0 529 size_t span = shape->slotSpan();
michael@0 530 if (span)
michael@0 531 obj->initializeSlotRange(0, span);
michael@0 532
michael@0 533 // JSFunction's fixed slots expect POD-style initialization.
michael@0 534 if (type->clasp()->isJSFunction())
michael@0 535 memset(obj->fixedSlots(), 0, sizeof(js::HeapSlot) * GetGCKindSlots(kind));
michael@0 536
michael@0 537 return obj;
michael@0 538 }
michael@0 539
michael@0 540 /* static */ inline js::ArrayObject *
michael@0 541 JSObject::createArray(js::ExclusiveContext *cx, js::gc::AllocKind kind, js::gc::InitialHeap heap,
michael@0 542 js::HandleShape shape, js::HandleTypeObject type,
michael@0 543 uint32_t length)
michael@0 544 {
michael@0 545 JS_ASSERT(shape && type);
michael@0 546 JS_ASSERT(type->clasp() == shape->getObjectClass());
michael@0 547 JS_ASSERT(type->clasp() == &js::ArrayObject::class_);
michael@0 548 JS_ASSERT_IF(type->clasp()->finalize, heap == js::gc::TenuredHeap);
michael@0 549
michael@0 550 /*
michael@0 551 * Arrays use their fixed slots to store elements, and must have enough
michael@0 552 * space for the elements header and also be marked as having no space for
michael@0 553 * named properties stored in those fixed slots.
michael@0 554 */
michael@0 555 JS_ASSERT(shape->numFixedSlots() == 0);
michael@0 556 size_t nDynamicSlots = dynamicSlotsCount(0, shape->slotSpan(), type->clasp());
michael@0 557 JSObject *obj = js::NewGCObject<js::CanGC>(cx, kind, nDynamicSlots, heap);
michael@0 558 if (!obj)
michael@0 559 return nullptr;
michael@0 560
michael@0 561 uint32_t capacity = js::gc::GetGCKindSlots(kind) - js::ObjectElements::VALUES_PER_HEADER;
michael@0 562
michael@0 563 obj->shape_.init(shape);
michael@0 564 obj->type_.init(type);
michael@0 565 obj->setFixedElements();
michael@0 566 new (obj->getElementsHeader()) js::ObjectElements(capacity, length);
michael@0 567
michael@0 568 size_t span = shape->slotSpan();
michael@0 569 if (span)
michael@0 570 obj->initializeSlotRange(0, span);
michael@0 571
michael@0 572 return &obj->as<js::ArrayObject>();
michael@0 573 }
michael@0 574
michael@0 575 inline void
michael@0 576 JSObject::finish(js::FreeOp *fop)
michael@0 577 {
michael@0 578 if (hasDynamicSlots())
michael@0 579 fop->free_(slots);
michael@0 580
michael@0 581 if (hasDynamicElements()) {
michael@0 582 js::ObjectElements *elements = getElementsHeader();
michael@0 583 fop->free_(elements);
michael@0 584 }
michael@0 585 }
michael@0 586
michael@0 587 /* static */ inline bool
michael@0 588 JSObject::hasProperty(JSContext *cx, js::HandleObject obj,
michael@0 589 js::HandleId id, bool *foundp)
michael@0 590 {
michael@0 591 JS::RootedObject pobj(cx);
michael@0 592 js::RootedShape prop(cx);
michael@0 593 if (!lookupGeneric(cx, obj, id, &pobj, &prop)) {
michael@0 594 *foundp = false; /* initialize to shut GCC up */
michael@0 595 return false;
michael@0 596 }
michael@0 597 *foundp = !!prop;
michael@0 598 return true;
michael@0 599 }
michael@0 600
michael@0 601 inline bool
michael@0 602 JSObject::nativeSetSlotIfHasType(js::Shape *shape, const js::Value &value)
michael@0 603 {
michael@0 604 if (!js::types::HasTypePropertyId(this, shape->propid(), value))
michael@0 605 return false;
michael@0 606 nativeSetSlot(shape->slot(), value);
michael@0 607 return true;
michael@0 608 }
michael@0 609
michael@0 610 inline void
michael@0 611 JSObject::nativeSetSlotWithType(js::ExclusiveContext *cx, js::Shape *shape,
michael@0 612 const js::Value &value)
michael@0 613 {
michael@0 614 nativeSetSlot(shape->slot(), value);
michael@0 615 js::types::AddTypePropertyId(cx, this, shape->propid(), value);
michael@0 616 }
michael@0 617
michael@0 618 /* static */ inline bool
michael@0 619 JSObject::getElement(JSContext *cx, js::HandleObject obj, js::HandleObject receiver,
michael@0 620 uint32_t index, js::MutableHandleValue vp)
michael@0 621 {
michael@0 622 js::ElementIdOp op = obj->getOps()->getElement;
michael@0 623 if (op)
michael@0 624 return op(cx, obj, receiver, index, vp);
michael@0 625
michael@0 626 JS::RootedId id(cx);
michael@0 627 if (!js::IndexToId(cx, index, &id))
michael@0 628 return false;
michael@0 629 return getGeneric(cx, obj, receiver, id, vp);
michael@0 630 }
michael@0 631
michael@0 632 /* static */ inline bool
michael@0 633 JSObject::getElementNoGC(JSContext *cx, JSObject *obj, JSObject *receiver,
michael@0 634 uint32_t index, js::Value *vp)
michael@0 635 {
michael@0 636 js::ElementIdOp op = obj->getOps()->getElement;
michael@0 637 if (op)
michael@0 638 return false;
michael@0 639
michael@0 640 if (index > JSID_INT_MAX)
michael@0 641 return false;
michael@0 642 return getGenericNoGC(cx, obj, receiver, INT_TO_JSID(index), vp);
michael@0 643 }
michael@0 644
michael@0 645 inline js::GlobalObject &
michael@0 646 JSObject::global() const
michael@0 647 {
michael@0 648 #ifdef DEBUG
michael@0 649 JSObject *obj = const_cast<JSObject *>(this);
michael@0 650 while (JSObject *parent = obj->getParent())
michael@0 651 obj = parent;
michael@0 652 #endif
michael@0 653 return *compartment()->maybeGlobal();
michael@0 654 }
michael@0 655
michael@0 656 inline bool
michael@0 657 JSObject::isOwnGlobal() const
michael@0 658 {
michael@0 659 return &global() == this;
michael@0 660 }
michael@0 661
michael@0 662 namespace js {
michael@0 663
michael@0 664 PropDesc::PropDesc(const Value &getter, const Value &setter,
michael@0 665 Enumerability enumerable, Configurability configurable)
michael@0 666 : pd_(UndefinedValue()),
michael@0 667 value_(UndefinedValue()),
michael@0 668 get_(getter), set_(setter),
michael@0 669 attrs(JSPROP_GETTER | JSPROP_SETTER | JSPROP_SHARED |
michael@0 670 (enumerable ? JSPROP_ENUMERATE : 0) |
michael@0 671 (configurable ? 0 : JSPROP_PERMANENT)),
michael@0 672 hasGet_(true), hasSet_(true),
michael@0 673 hasValue_(false), hasWritable_(false), hasEnumerable_(true), hasConfigurable_(true),
michael@0 674 isUndefined_(false)
michael@0 675 {
michael@0 676 MOZ_ASSERT(getter.isUndefined() || js_IsCallable(getter));
michael@0 677 MOZ_ASSERT(setter.isUndefined() || js_IsCallable(setter));
michael@0 678 }
michael@0 679
michael@0 680 static MOZ_ALWAYS_INLINE bool
michael@0 681 IsFunctionObject(const js::Value &v)
michael@0 682 {
michael@0 683 return v.isObject() && v.toObject().is<JSFunction>();
michael@0 684 }
michael@0 685
michael@0 686 static MOZ_ALWAYS_INLINE bool
michael@0 687 IsFunctionObject(const js::Value &v, JSFunction **fun)
michael@0 688 {
michael@0 689 if (v.isObject() && v.toObject().is<JSFunction>()) {
michael@0 690 *fun = &v.toObject().as<JSFunction>();
michael@0 691 return true;
michael@0 692 }
michael@0 693 return false;
michael@0 694 }
michael@0 695
michael@0 696 static MOZ_ALWAYS_INLINE bool
michael@0 697 IsNativeFunction(const js::Value &v)
michael@0 698 {
michael@0 699 JSFunction *fun;
michael@0 700 return IsFunctionObject(v, &fun) && fun->isNative();
michael@0 701 }
michael@0 702
michael@0 703 static MOZ_ALWAYS_INLINE bool
michael@0 704 IsNativeFunction(const js::Value &v, JSFunction **fun)
michael@0 705 {
michael@0 706 return IsFunctionObject(v, fun) && (*fun)->isNative();
michael@0 707 }
michael@0 708
michael@0 709 static MOZ_ALWAYS_INLINE bool
michael@0 710 IsNativeFunction(const js::Value &v, JSNative native)
michael@0 711 {
michael@0 712 JSFunction *fun;
michael@0 713 return IsFunctionObject(v, &fun) && fun->maybeNative() == native;
michael@0 714 }
michael@0 715
michael@0 716 /*
michael@0 717 * When we have an object of a builtin class, we don't quite know what its
michael@0 718 * valueOf/toString methods are, since these methods may have been overwritten
michael@0 719 * or shadowed. However, we can still do better than the general case by
michael@0 720 * hard-coding the necessary properties for us to find the native we expect.
michael@0 721 *
michael@0 722 * TODO: a per-thread shape-based cache would be faster and simpler.
michael@0 723 */
michael@0 724 static MOZ_ALWAYS_INLINE bool
michael@0 725 ClassMethodIsNative(JSContext *cx, JSObject *obj, const Class *clasp, jsid methodid, JSNative native)
michael@0 726 {
michael@0 727 JS_ASSERT(!obj->is<ProxyObject>());
michael@0 728 JS_ASSERT(obj->getClass() == clasp);
michael@0 729
michael@0 730 Value v;
michael@0 731 if (!HasDataProperty(cx, obj, methodid, &v)) {
michael@0 732 JSObject *proto = obj->getProto();
michael@0 733 if (!proto || proto->getClass() != clasp || !HasDataProperty(cx, proto, methodid, &v))
michael@0 734 return false;
michael@0 735 }
michael@0 736
michael@0 737 return js::IsNativeFunction(v, native);
michael@0 738 }
michael@0 739
michael@0 740 /* ES5 9.1 ToPrimitive(input). */
michael@0 741 static MOZ_ALWAYS_INLINE bool
michael@0 742 ToPrimitive(JSContext *cx, MutableHandleValue vp)
michael@0 743 {
michael@0 744 if (vp.isPrimitive())
michael@0 745 return true;
michael@0 746
michael@0 747 JSObject *obj = &vp.toObject();
michael@0 748
michael@0 749 /* Optimize new String(...).valueOf(). */
michael@0 750 if (obj->is<StringObject>()) {
michael@0 751 jsid id = NameToId(cx->names().valueOf);
michael@0 752 if (ClassMethodIsNative(cx, obj, &StringObject::class_, id, js_str_toString)) {
michael@0 753 vp.setString(obj->as<StringObject>().unbox());
michael@0 754 return true;
michael@0 755 }
michael@0 756 }
michael@0 757
michael@0 758 /* Optimize new Number(...).valueOf(). */
michael@0 759 if (obj->is<NumberObject>()) {
michael@0 760 jsid id = NameToId(cx->names().valueOf);
michael@0 761 if (ClassMethodIsNative(cx, obj, &NumberObject::class_, id, js_num_valueOf)) {
michael@0 762 vp.setNumber(obj->as<NumberObject>().unbox());
michael@0 763 return true;
michael@0 764 }
michael@0 765 }
michael@0 766
michael@0 767 RootedObject objRoot(cx, obj);
michael@0 768 return JSObject::defaultValue(cx, objRoot, JSTYPE_VOID, vp);
michael@0 769 }
michael@0 770
michael@0 771 /* ES5 9.1 ToPrimitive(input, PreferredType). */
michael@0 772 static MOZ_ALWAYS_INLINE bool
michael@0 773 ToPrimitive(JSContext *cx, JSType preferredType, MutableHandleValue vp)
michael@0 774 {
michael@0 775 JS_ASSERT(preferredType != JSTYPE_VOID); /* Use the other ToPrimitive! */
michael@0 776 if (vp.isPrimitive())
michael@0 777 return true;
michael@0 778 RootedObject obj(cx, &vp.toObject());
michael@0 779 return JSObject::defaultValue(cx, obj, preferredType, vp);
michael@0 780 }
michael@0 781
michael@0 782 /*
michael@0 783 * Return true if this is a compiler-created internal function accessed by
michael@0 784 * its own object. Such a function object must not be accessible to script
michael@0 785 * or embedding code.
michael@0 786 */
michael@0 787 inline bool
michael@0 788 IsInternalFunctionObject(JSObject *funobj)
michael@0 789 {
michael@0 790 JSFunction *fun = &funobj->as<JSFunction>();
michael@0 791 return fun->isLambda() && !funobj->getParent();
michael@0 792 }
michael@0 793
michael@0 794 class AutoPropDescArrayRooter : private AutoGCRooter
michael@0 795 {
michael@0 796 public:
michael@0 797 AutoPropDescArrayRooter(JSContext *cx)
michael@0 798 : AutoGCRooter(cx, DESCRIPTORS), descriptors(cx)
michael@0 799 { }
michael@0 800
michael@0 801 PropDesc *append() {
michael@0 802 if (!descriptors.append(PropDesc()))
michael@0 803 return nullptr;
michael@0 804 return &descriptors.back();
michael@0 805 }
michael@0 806
michael@0 807 bool reserve(size_t n) {
michael@0 808 return descriptors.reserve(n);
michael@0 809 }
michael@0 810
michael@0 811 PropDesc& operator[](size_t i) {
michael@0 812 JS_ASSERT(i < descriptors.length());
michael@0 813 return descriptors[i];
michael@0 814 }
michael@0 815
michael@0 816 friend void AutoGCRooter::trace(JSTracer *trc);
michael@0 817
michael@0 818 private:
michael@0 819 PropDescArray descriptors;
michael@0 820 };
michael@0 821
michael@0 822 /*
michael@0 823 * Make an object with the specified prototype. If parent is null, it will
michael@0 824 * default to the prototype's global if the prototype is non-null.
michael@0 825 */
michael@0 826 JSObject *
michael@0 827 NewObjectWithGivenProto(ExclusiveContext *cx, const js::Class *clasp, TaggedProto proto, JSObject *parent,
michael@0 828 gc::AllocKind allocKind, NewObjectKind newKind);
michael@0 829
michael@0 830 inline JSObject *
michael@0 831 NewObjectWithGivenProto(ExclusiveContext *cx, const js::Class *clasp, TaggedProto proto, JSObject *parent,
michael@0 832 NewObjectKind newKind = GenericObject)
michael@0 833 {
michael@0 834 gc::AllocKind allocKind = gc::GetGCObjectKind(clasp);
michael@0 835 return NewObjectWithGivenProto(cx, clasp, proto, parent, allocKind, newKind);
michael@0 836 }
michael@0 837
michael@0 838 inline JSObject *
michael@0 839 NewObjectWithGivenProto(ExclusiveContext *cx, const js::Class *clasp, JSObject *proto, JSObject *parent,
michael@0 840 NewObjectKind newKind = GenericObject)
michael@0 841 {
michael@0 842 return NewObjectWithGivenProto(cx, clasp, TaggedProto(proto), parent, newKind);
michael@0 843 }
michael@0 844
michael@0 845 inline JSProtoKey
michael@0 846 GetClassProtoKey(const js::Class *clasp)
michael@0 847 {
michael@0 848 JSProtoKey key = JSCLASS_CACHED_PROTO_KEY(clasp);
michael@0 849 if (key != JSProto_Null)
michael@0 850 return key;
michael@0 851 if (clasp->flags & JSCLASS_IS_ANONYMOUS)
michael@0 852 return JSProto_Object;
michael@0 853 return JSProto_Null;
michael@0 854 }
michael@0 855
michael@0 856 inline bool
michael@0 857 FindProto(ExclusiveContext *cx, const js::Class *clasp, MutableHandleObject proto)
michael@0 858 {
michael@0 859 if (!FindClassPrototype(cx, proto, clasp))
michael@0 860 return false;
michael@0 861
michael@0 862 if (!proto) {
michael@0 863 // We're looking for the prototype of a class that is currently being
michael@0 864 // resolved; the global object's resolve hook is on the
michael@0 865 // stack. js::FindClassPrototype detects this goofy case and returns
michael@0 866 // true with proto null. Fall back on Object.prototype.
michael@0 867 JS_ASSERT(JSCLASS_CACHED_PROTO_KEY(clasp) == JSProto_Null);
michael@0 868 return GetBuiltinPrototype(cx, JSProto_Object, proto);
michael@0 869 }
michael@0 870 return true;
michael@0 871 }
michael@0 872
michael@0 873 /*
michael@0 874 * Make an object with the prototype set according to the specified prototype or class:
michael@0 875 *
michael@0 876 * if proto is non-null:
michael@0 877 * use the specified proto
michael@0 878 * for a built-in class:
michael@0 879 * use the memoized original value of the class constructor .prototype
michael@0 880 * property object
michael@0 881 * else if available
michael@0 882 * the current value of .prototype
michael@0 883 * else
michael@0 884 * Object.prototype.
michael@0 885 *
michael@0 886 * The class prototype will be fetched from the parent's global. If global is
michael@0 887 * null, the context's active global will be used, and the resulting object's
michael@0 888 * parent will be that global.
michael@0 889 */
michael@0 890 JSObject *
michael@0 891 NewObjectWithClassProtoCommon(ExclusiveContext *cx, const js::Class *clasp, JSObject *proto, JSObject *parent,
michael@0 892 gc::AllocKind allocKind, NewObjectKind newKind);
michael@0 893
michael@0 894 inline JSObject *
michael@0 895 NewObjectWithClassProto(ExclusiveContext *cx, const js::Class *clasp, JSObject *proto, JSObject *parent,
michael@0 896 gc::AllocKind allocKind, NewObjectKind newKind = GenericObject)
michael@0 897 {
michael@0 898 return NewObjectWithClassProtoCommon(cx, clasp, proto, parent, allocKind, newKind);
michael@0 899 }
michael@0 900
michael@0 901 inline JSObject *
michael@0 902 NewObjectWithClassProto(ExclusiveContext *cx, const js::Class *clasp, JSObject *proto, JSObject *parent,
michael@0 903 NewObjectKind newKind = GenericObject)
michael@0 904 {
michael@0 905 gc::AllocKind allocKind = gc::GetGCObjectKind(clasp);
michael@0 906 return NewObjectWithClassProto(cx, clasp, proto, parent, allocKind, newKind);
michael@0 907 }
michael@0 908
michael@0 909 template<typename T>
michael@0 910 inline T *
michael@0 911 NewObjectWithProto(ExclusiveContext *cx, JSObject *proto, JSObject *parent,
michael@0 912 NewObjectKind newKind = GenericObject)
michael@0 913 {
michael@0 914 JSObject *obj = NewObjectWithClassProto(cx, &T::class_, proto, parent, newKind);
michael@0 915 if (!obj)
michael@0 916 return nullptr;
michael@0 917
michael@0 918 return &obj->as<T>();
michael@0 919 }
michael@0 920
michael@0 921 /*
michael@0 922 * Create a native instance of the given class with parent and proto set
michael@0 923 * according to the context's active global.
michael@0 924 */
michael@0 925 inline JSObject *
michael@0 926 NewBuiltinClassInstance(ExclusiveContext *cx, const Class *clasp, gc::AllocKind allocKind,
michael@0 927 NewObjectKind newKind = GenericObject)
michael@0 928 {
michael@0 929 return NewObjectWithClassProto(cx, clasp, nullptr, nullptr, allocKind, newKind);
michael@0 930 }
michael@0 931
michael@0 932 inline JSObject *
michael@0 933 NewBuiltinClassInstance(ExclusiveContext *cx, const Class *clasp, NewObjectKind newKind = GenericObject)
michael@0 934 {
michael@0 935 gc::AllocKind allocKind = gc::GetGCObjectKind(clasp);
michael@0 936 return NewBuiltinClassInstance(cx, clasp, allocKind, newKind);
michael@0 937 }
michael@0 938
michael@0 939 template<typename T>
michael@0 940 inline T *
michael@0 941 NewBuiltinClassInstance(ExclusiveContext *cx, NewObjectKind newKind = GenericObject)
michael@0 942 {
michael@0 943 JSObject *obj = NewBuiltinClassInstance(cx, &T::class_, newKind);
michael@0 944 if (!obj)
michael@0 945 return nullptr;
michael@0 946
michael@0 947 return &obj->as<T>();
michael@0 948 }
michael@0 949
michael@0 950 template<typename T>
michael@0 951 inline T *
michael@0 952 NewBuiltinClassInstance(ExclusiveContext *cx, gc::AllocKind allocKind, NewObjectKind newKind = GenericObject)
michael@0 953 {
michael@0 954 JSObject *obj = NewBuiltinClassInstance(cx, &T::class_, allocKind, newKind);
michael@0 955 if (!obj)
michael@0 956 return nullptr;
michael@0 957
michael@0 958 return &obj->as<T>();
michael@0 959 }
michael@0 960
michael@0 961 // Used to optimize calls to (new Object())
michael@0 962 bool
michael@0 963 NewObjectScriptedCall(JSContext *cx, MutableHandleObject obj);
michael@0 964
michael@0 965 /* Make an object with pregenerated shape from a NEWOBJECT bytecode. */
michael@0 966 static inline JSObject *
michael@0 967 CopyInitializerObject(JSContext *cx, HandleObject baseobj, NewObjectKind newKind = GenericObject)
michael@0 968 {
michael@0 969 JS_ASSERT(baseobj->getClass() == &JSObject::class_);
michael@0 970 JS_ASSERT(!baseobj->inDictionaryMode());
michael@0 971
michael@0 972 gc::AllocKind allocKind = gc::GetGCObjectFixedSlotsKind(baseobj->numFixedSlots());
michael@0 973 allocKind = gc::GetBackgroundAllocKind(allocKind);
michael@0 974 JS_ASSERT_IF(baseobj->isTenured(), allocKind == baseobj->tenuredGetAllocKind());
michael@0 975 RootedObject obj(cx);
michael@0 976 obj = NewBuiltinClassInstance(cx, &JSObject::class_, allocKind, newKind);
michael@0 977 if (!obj)
michael@0 978 return nullptr;
michael@0 979
michael@0 980 RootedObject metadata(cx, obj->getMetadata());
michael@0 981 RootedShape lastProp(cx, baseobj->lastProperty());
michael@0 982 if (!JSObject::setLastProperty(cx, obj, lastProp))
michael@0 983 return nullptr;
michael@0 984 if (metadata && !JSObject::setMetadata(cx, obj, metadata))
michael@0 985 return nullptr;
michael@0 986
michael@0 987 return obj;
michael@0 988 }
michael@0 989
michael@0 990 JSObject *
michael@0 991 NewObjectWithType(JSContext *cx, HandleTypeObject type, JSObject *parent, gc::AllocKind allocKind,
michael@0 992 NewObjectKind newKind = GenericObject);
michael@0 993
michael@0 994 inline JSObject *
michael@0 995 NewObjectWithType(JSContext *cx, HandleTypeObject type, JSObject *parent,
michael@0 996 NewObjectKind newKind = GenericObject)
michael@0 997 {
michael@0 998 gc::AllocKind allocKind = gc::GetGCObjectKind(type->clasp());
michael@0 999 return NewObjectWithType(cx, type, parent, allocKind, newKind);
michael@0 1000 }
michael@0 1001
michael@0 1002 JSObject *
michael@0 1003 NewReshapedObject(JSContext *cx, HandleTypeObject type, JSObject *parent,
michael@0 1004 gc::AllocKind allocKind, HandleShape shape,
michael@0 1005 NewObjectKind newKind = GenericObject);
michael@0 1006
michael@0 1007 /*
michael@0 1008 * As for gc::GetGCObjectKind, where numSlots is a guess at the final size of
michael@0 1009 * the object, zero if the final size is unknown. This should only be used for
michael@0 1010 * objects that do not require any fixed slots.
michael@0 1011 */
michael@0 1012 static inline gc::AllocKind
michael@0 1013 GuessObjectGCKind(size_t numSlots)
michael@0 1014 {
michael@0 1015 if (numSlots)
michael@0 1016 return gc::GetGCObjectKind(numSlots);
michael@0 1017 return gc::FINALIZE_OBJECT4;
michael@0 1018 }
michael@0 1019
michael@0 1020 static inline gc::AllocKind
michael@0 1021 GuessArrayGCKind(size_t numSlots)
michael@0 1022 {
michael@0 1023 if (numSlots)
michael@0 1024 return gc::GetGCArrayKind(numSlots);
michael@0 1025 return gc::FINALIZE_OBJECT8;
michael@0 1026 }
michael@0 1027
michael@0 1028 inline bool
michael@0 1029 ObjectClassIs(HandleObject obj, ESClassValue classValue, JSContext *cx)
michael@0 1030 {
michael@0 1031 if (MOZ_UNLIKELY(obj->is<ProxyObject>()))
michael@0 1032 return Proxy::objectClassIs(obj, classValue, cx);
michael@0 1033
michael@0 1034 switch (classValue) {
michael@0 1035 case ESClass_Array: return obj->is<ArrayObject>();
michael@0 1036 case ESClass_Number: return obj->is<NumberObject>();
michael@0 1037 case ESClass_String: return obj->is<StringObject>();
michael@0 1038 case ESClass_Boolean: return obj->is<BooleanObject>();
michael@0 1039 case ESClass_RegExp: return obj->is<RegExpObject>();
michael@0 1040 case ESClass_ArrayBuffer:
michael@0 1041 return obj->is<ArrayBufferObject>() || obj->is<SharedArrayBufferObject>();
michael@0 1042 case ESClass_Date: return obj->is<DateObject>();
michael@0 1043 }
michael@0 1044 MOZ_ASSUME_UNREACHABLE("bad classValue");
michael@0 1045 }
michael@0 1046
michael@0 1047 inline bool
michael@0 1048 IsObjectWithClass(const Value &v, ESClassValue classValue, JSContext *cx)
michael@0 1049 {
michael@0 1050 if (!v.isObject())
michael@0 1051 return false;
michael@0 1052 RootedObject obj(cx, &v.toObject());
michael@0 1053 return ObjectClassIs(obj, classValue, cx);
michael@0 1054 }
michael@0 1055
michael@0 1056 static MOZ_ALWAYS_INLINE bool
michael@0 1057 NewObjectMetadata(ExclusiveContext *cxArg, JSObject **pmetadata)
michael@0 1058 {
michael@0 1059 // The metadata callback is invoked before each created object, except when
michael@0 1060 // analysis/compilation is active, to avoid recursion.
michael@0 1061 JS_ASSERT(!*pmetadata);
michael@0 1062 if (JSContext *cx = cxArg->maybeJSContext()) {
michael@0 1063 if (MOZ_UNLIKELY((size_t)cx->compartment()->hasObjectMetadataCallback()) &&
michael@0 1064 !cx->compartment()->activeAnalysis)
michael@0 1065 {
michael@0 1066 // Use AutoEnterAnalysis to prohibit both any GC activity under the
michael@0 1067 // callback, and any reentering of JS via Invoke() etc.
michael@0 1068 types::AutoEnterAnalysis enter(cx);
michael@0 1069
michael@0 1070 if (!cx->compartment()->callObjectMetadataCallback(cx, pmetadata))
michael@0 1071 return false;
michael@0 1072 }
michael@0 1073 }
michael@0 1074 return true;
michael@0 1075 }
michael@0 1076
michael@0 1077 inline bool
michael@0 1078 DefineNativeProperty(ExclusiveContext *cx, HandleObject obj, PropertyName *name, HandleValue value,
michael@0 1079 PropertyOp getter, StrictPropertyOp setter, unsigned attrs)
michael@0 1080 {
michael@0 1081 Rooted<jsid> id(cx, NameToId(name));
michael@0 1082 return DefineNativeProperty(cx, obj, id, value, getter, setter, attrs);
michael@0 1083 }
michael@0 1084
michael@0 1085 namespace baseops {
michael@0 1086
michael@0 1087 inline bool
michael@0 1088 LookupProperty(ExclusiveContext *cx, HandleObject obj, PropertyName *name,
michael@0 1089 MutableHandleObject objp, MutableHandleShape propp)
michael@0 1090 {
michael@0 1091 Rooted<jsid> id(cx, NameToId(name));
michael@0 1092 return LookupProperty<CanGC>(cx, obj, id, objp, propp);
michael@0 1093 }
michael@0 1094
michael@0 1095 inline bool
michael@0 1096 DefineProperty(ExclusiveContext *cx, HandleObject obj, PropertyName *name, HandleValue value,
michael@0 1097 JSPropertyOp getter, JSStrictPropertyOp setter, unsigned attrs)
michael@0 1098 {
michael@0 1099 Rooted<jsid> id(cx, NameToId(name));
michael@0 1100 return DefineGeneric(cx, obj, id, value, getter, setter, attrs);
michael@0 1101 }
michael@0 1102
michael@0 1103 } /* namespace baseops */
michael@0 1104
michael@0 1105 } /* namespace js */
michael@0 1106
michael@0 1107 extern JSObject *
michael@0 1108 js_InitClass(JSContext *cx, js::HandleObject obj, JSObject *parent_proto,
michael@0 1109 const js::Class *clasp, JSNative constructor, unsigned nargs,
michael@0 1110 const JSPropertySpec *ps, const JSFunctionSpec *fs,
michael@0 1111 const JSPropertySpec *static_ps, const JSFunctionSpec *static_fs,
michael@0 1112 JSObject **ctorp = nullptr,
michael@0 1113 js::gc::AllocKind ctorKind = JSFunction::FinalizeKind);
michael@0 1114
michael@0 1115 #endif /* jsobjinlines_h */

mercurial