Source code

Revision control

Other Tools

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* JS object implementation.
*/
#include "vm/JSObject-inl.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/Maybe.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/TemplateLib.h"
#include <algorithm>
#include <string.h>
#include "jsapi.h"
#include "jsexn.h"
#include "jsfriendapi.h"
#include "jsnum.h"
#include "jstypes.h"
#include "builtin/Array.h"
#include "builtin/BigInt.h"
#include "builtin/Eval.h"
#include "builtin/Object.h"
#include "builtin/String.h"
#include "builtin/Symbol.h"
#include "builtin/WeakSetObject.h"
#include "frontend/BytecodeCompiler.h"
#include "gc/Policy.h"
#include "jit/BaselineJIT.h"
#include "js/CharacterEncoding.h"
#include "js/friend/DumpFunctions.h" // js::DumpObject
#include "js/friend/ErrorMessages.h" // JSErrNum, js::GetErrorMessage, JSMSG_*
#include "js/friend/WindowProxy.h" // js::IsWindow, js::ToWindowProxyIfWindow
#include "js/MemoryMetrics.h"
#include "js/PropertyDescriptor.h" // JS::FromPropertyDescriptor
#include "js/PropertySpec.h" // JSPropertySpec
#include "js/Proxy.h"
#include "js/Result.h"
#include "js/UbiNode.h"
#include "js/UniquePtr.h"
#include "js/Wrapper.h"
#include "util/Memory.h"
#include "util/Text.h"
#include "util/Windows.h"
#include "vm/ArgumentsObject.h"
#include "vm/BytecodeUtil.h"
#include "vm/DateObject.h"
#include "vm/Interpreter.h"
#include "vm/Iteration.h"
#include "vm/JSAtom.h"
#include "vm/JSContext.h"
#include "vm/JSFunction.h"
#include "vm/JSScript.h"
#include "vm/ProxyObject.h"
#include "vm/RegExpStaticsObject.h"
#include "vm/Shape.h"
#include "vm/TypedArrayObject.h"
#include "vm/WellKnownAtom.h" // js_*_str
#include "builtin/Boolean-inl.h"
#include "gc/Marking-inl.h"
#include "vm/ArrayObject-inl.h"
#include "vm/BooleanObject-inl.h"
#include "vm/Caches-inl.h"
#include "vm/Compartment-inl.h"
#include "vm/Interpreter-inl.h"
#include "vm/JSAtom-inl.h"
#include "vm/JSContext-inl.h"
#include "vm/JSFunction-inl.h"
#include "vm/NativeObject-inl.h"
#include "vm/NumberObject-inl.h"
#include "vm/PlainObject-inl.h" // js::CopyInitializerObject
#include "vm/Realm-inl.h"
#include "vm/Shape-inl.h"
#include "vm/StringObject-inl.h"
#include "vm/TypedArrayObject-inl.h"
#include "wasm/TypedObject-inl.h"
using namespace js;
void js::ReportNotObject(JSContext* cx, JSErrNum err, int spindex,
HandleValue v) {
MOZ_ASSERT(!v.isObject());
ReportValueError(cx, err, spindex, v, nullptr);
}
void js::ReportNotObject(JSContext* cx, JSErrNum err, HandleValue v) {
ReportNotObject(cx, err, JSDVG_SEARCH_STACK, v);
}
void js::ReportNotObject(JSContext* cx, const Value& v) {
RootedValue value(cx, v);
ReportNotObject(cx, JSMSG_OBJECT_REQUIRED, value);
}
void js::ReportNotObjectArg(JSContext* cx, const char* nth, const char* fun,
HandleValue v) {
MOZ_ASSERT(!v.isObject());
UniqueChars bytes;
if (const char* chars = ValueToSourceForError(cx, v, bytes)) {
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr,
JSMSG_OBJECT_REQUIRED_ARG, nth, fun, chars);
}
}
JS_PUBLIC_API const char* JS::InformalValueTypeName(const Value& v) {
switch (v.type()) {
case ValueType::Double:
case ValueType::Int32:
return "number";
case ValueType::Boolean:
return "boolean";
case ValueType::Undefined:
return "undefined";
case ValueType::Null:
return "null";
case ValueType::String:
return "string";
case ValueType::Symbol:
return "symbol";
case ValueType::BigInt:
return "bigint";
case ValueType::Object:
return v.toObject().getClass()->name;
case ValueType::Magic:
return "magic";
case ValueType::PrivateGCThing:
break;
}
MOZ_CRASH("unexpected type");
}
// ES6 draft rev37 6.2.4.4 FromPropertyDescriptor
JS_PUBLIC_API bool JS::FromPropertyDescriptor(JSContext* cx,
Handle<PropertyDescriptor> desc,
MutableHandleValue vp) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
cx->check(desc);
// Step 1.
if (!desc.object()) {
vp.setUndefined();
return true;
}
return FromPropertyDescriptorToObject(cx, desc, vp);
}
bool js::FromPropertyDescriptorToObject(JSContext* cx,
Handle<PropertyDescriptor> desc,
MutableHandleValue vp) {
// Step 2-3.
RootedObject obj(cx, NewBuiltinClassInstance<PlainObject>(cx));
if (!obj) {
return false;
}
const JSAtomState& names = cx->names();
// Step 4.
if (desc.hasValue()) {
if (!DefineDataProperty(cx, obj, names.value, desc.value())) {
return false;
}
}
// Step 5.
RootedValue v(cx);
if (desc.hasWritable()) {
v.setBoolean(desc.writable());
if (!DefineDataProperty(cx, obj, names.writable, v)) {
return false;
}
}
// Step 6.
if (desc.hasGetterObject()) {
if (JSObject* get = desc.getterObject()) {
v.setObject(*get);
} else {
v.setUndefined();
}
if (!DefineDataProperty(cx, obj, names.get, v)) {
return false;
}
}
// Step 7.
if (desc.hasSetterObject()) {
if (JSObject* set = desc.setterObject()) {
v.setObject(*set);
} else {
v.setUndefined();
}
if (!DefineDataProperty(cx, obj, names.set, v)) {
return false;
}
}
// Step 8.
if (desc.hasEnumerable()) {
v.setBoolean(desc.enumerable());
if (!DefineDataProperty(cx, obj, names.enumerable, v)) {
return false;
}
}
// Step 9.
if (desc.hasConfigurable()) {
v.setBoolean(desc.configurable());
if (!DefineDataProperty(cx, obj, names.configurable, v)) {
return false;
}
}
vp.setObject(*obj);
return true;
}
bool js::GetFirstArgumentAsObject(JSContext* cx, const CallArgs& args,
const char* method,
MutableHandleObject objp) {
if (!args.requireAtLeast(cx, method, 1)) {
return false;
}
HandleValue v = args[0];
if (!v.isObject()) {
UniqueChars bytes =
DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, v, nullptr);
if (!bytes) {
return false;
}
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
JSMSG_UNEXPECTED_TYPE, bytes.get(),
"not an object");
return false;
}
objp.set(&v.toObject());
return true;
}
static bool GetPropertyIfPresent(JSContext* cx, HandleObject obj, HandleId id,
MutableHandleValue vp, bool* foundp) {
if (!HasProperty(cx, obj, id, foundp)) {
return false;
}
if (!*foundp) {
vp.setUndefined();
return true;
}
return GetProperty(cx, obj, obj, id, vp);
}
bool js::Throw(JSContext* cx, HandleId id, unsigned errorNumber,
const char* details) {
MOZ_ASSERT(js_ErrorFormatString[errorNumber].argCount == (details ? 2 : 1));
MOZ_ASSERT_IF(details, JS::StringIsASCII(details));
UniqueChars bytes =
IdToPrintableUTF8(cx, id, IdToPrintableBehavior::IdIsPropertyKey);
if (!bytes) {
return false;
}
if (details) {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, errorNumber,
bytes.get(), details);
} else {
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, errorNumber,
bytes.get());
}
return false;
}
/*** PropertyDescriptor operations and DefineProperties *********************/
static const char js_getter_str[] = "getter";
static const char js_setter_str[] = "setter";
static Result<> CheckCallable(JSContext* cx, JSObject* obj,
const char* fieldName) {
if (obj && !obj->isCallable()) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_BAD_GET_SET_FIELD, fieldName);
return cx->alreadyReportedError();
}
return Ok();
}
bool js::ToPropertyDescriptor(JSContext* cx, HandleValue descval,
bool checkAccessors,
MutableHandle<PropertyDescriptor> desc) {
// step 2
RootedObject obj(cx,
RequireObject(cx, JSMSG_OBJECT_REQUIRED_PROP_DESC, descval));
if (!obj) {
return false;
}
// step 3
desc.clear();
bool found = false;
RootedId id(cx);
RootedValue v(cx);
unsigned attrs = 0;
// step 4
id = NameToId(cx->names().enumerable);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
if (found) {
if (ToBoolean(v)) {
attrs |= JSPROP_ENUMERATE;
}
} else {
attrs |= JSPROP_IGNORE_ENUMERATE;
}
// step 5
id = NameToId(cx->names().configurable);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
if (found) {
if (!ToBoolean(v)) {
attrs |= JSPROP_PERMANENT;
}
} else {
attrs |= JSPROP_IGNORE_PERMANENT;
}
// step 6
id = NameToId(cx->names().value);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
if (found) {
desc.value().set(v);
} else {
attrs |= JSPROP_IGNORE_VALUE;
}
// step 7
id = NameToId(cx->names().writable);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
if (found) {
if (!ToBoolean(v)) {
attrs |= JSPROP_READONLY;
}
} else {
attrs |= JSPROP_IGNORE_READONLY;
}
// step 8
bool hasGetOrSet;
id = NameToId(cx->names().get);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
hasGetOrSet = found;
if (found) {
if (v.isObject()) {
if (checkAccessors) {
JS_TRY_OR_RETURN_FALSE(cx,
CheckCallable(cx, &v.toObject(), js_getter_str));
}
desc.setGetterObject(&v.toObject());
} else if (!v.isUndefined()) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_BAD_GET_SET_FIELD, js_getter_str);
return false;
}
attrs |= JSPROP_GETTER;
}
// step 9
id = NameToId(cx->names().set);
if (!GetPropertyIfPresent(cx, obj, id, &v, &found)) {
return false;
}
hasGetOrSet |= found;
if (found) {
if (v.isObject()) {
if (checkAccessors) {
JS_TRY_OR_RETURN_FALSE(cx,
CheckCallable(cx, &v.toObject(), js_setter_str));
}
desc.setSetterObject(&v.toObject());
} else if (!v.isUndefined()) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_BAD_GET_SET_FIELD, js_setter_str);
return false;
}
attrs |= JSPROP_SETTER;
}
// step 10
if (hasGetOrSet) {
if (!(attrs & JSPROP_IGNORE_READONLY) || !(attrs & JSPROP_IGNORE_VALUE)) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_INVALID_DESCRIPTOR);
return false;
}
// By convention, these bits are not used on accessor descriptors.
attrs &= ~(JSPROP_IGNORE_READONLY | JSPROP_IGNORE_VALUE);
}
desc.setAttributes(attrs);
MOZ_ASSERT_IF(attrs & JSPROP_READONLY,
!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
return true;
}
Result<> js::CheckPropertyDescriptorAccessors(JSContext* cx,
Handle<PropertyDescriptor> desc) {
if (desc.hasGetterObject()) {
MOZ_TRY(CheckCallable(cx, desc.getterObject(), js_getter_str));
}
if (desc.hasSetterObject()) {
MOZ_TRY(CheckCallable(cx, desc.setterObject(), js_setter_str));
}
return Ok();
}
void js::CompletePropertyDescriptor(MutableHandle<PropertyDescriptor> desc) {
desc.assertValid();
if (desc.isGenericDescriptor() || desc.isDataDescriptor()) {
if (!desc.hasWritable()) {
desc.attributesRef() |= JSPROP_READONLY;
}
desc.attributesRef() &= ~(JSPROP_IGNORE_READONLY | JSPROP_IGNORE_VALUE);
} else {
if (!desc.hasGetterObject()) {
desc.setGetterObject(nullptr);
}
if (!desc.hasSetterObject()) {
desc.setSetterObject(nullptr);
}
desc.attributesRef() |= JSPROP_GETTER | JSPROP_SETTER;
}
if (!desc.hasConfigurable()) {
desc.attributesRef() |= JSPROP_PERMANENT;
}
desc.attributesRef() &= ~(JSPROP_IGNORE_PERMANENT | JSPROP_IGNORE_ENUMERATE);
desc.assertComplete();
}
bool js::ReadPropertyDescriptors(
JSContext* cx, HandleObject props, bool checkAccessors,
MutableHandleIdVector ids, MutableHandle<PropertyDescriptorVector> descs) {
if (!GetPropertyKeys(cx, props, JSITER_OWNONLY | JSITER_SYMBOLS, ids)) {
return false;
}
RootedId id(cx);
for (size_t i = 0, len = ids.length(); i < len; i++) {
id = ids[i];
Rooted<PropertyDescriptor> desc(cx);
RootedValue v(cx);
if (!GetProperty(cx, props, props, id, &v) ||
!ToPropertyDescriptor(cx, v, checkAccessors, &desc) ||
!descs.append(desc)) {
return false;
}
}
return true;
}
/*** Seal and freeze ********************************************************/
static unsigned GetSealedOrFrozenAttributes(unsigned attrs,
IntegrityLevel level) {
// Make all attributes permanent; if freezing, make data attributes
// read-only.
if (level == IntegrityLevel::Frozen &&
!(attrs & (JSPROP_GETTER | JSPROP_SETTER))) {
return JSPROP_PERMANENT | JSPROP_READONLY;
}
return JSPROP_PERMANENT;
}
/* ES6 draft rev 29 (6 Dec 2014) 7.3.13. */
bool js::SetIntegrityLevel(JSContext* cx, HandleObject obj,
IntegrityLevel level) {
cx->check(obj);
// Steps 3-5. (Steps 1-2 are redundant assertions.)
if (!PreventExtensions(cx, obj)) {
return false;
}
// Steps 6-9, loosely interpreted.
if (obj->is<NativeObject>() && !obj->as<NativeObject>().inDictionaryMode() &&
!obj->is<TypedArrayObject>() && !obj->is<MappedArgumentsObject>()) {
HandleNativeObject nobj = obj.as<NativeObject>();
// Seal/freeze non-dictionary objects by constructing a new shape
// hierarchy mirroring the original one, which can be shared if many
// objects with the same structure are sealed/frozen. If we use the
// generic path below then any non-empty object will be converted to
// dictionary mode.
RootedShape last(
cx, EmptyShape::getInitialShape(
cx, nobj->getClass(), nobj->realm(), nobj->taggedProto(),
nobj->numFixedSlots(), nobj->lastProperty()->objectFlags()));
if (!last) {
return false;
}
// Get an in-order list of the shapes in this object.
using ShapeVec = GCVector<Shape*, 8>;
Rooted<ShapeVec> shapes(cx, ShapeVec(cx));
for (Shape::Range<NoGC> r(nobj->lastProperty()); !r.empty(); r.popFront()) {
if (!shapes.append(&r.front())) {
return false;
}
}
std::reverse(shapes.begin(), shapes.end());
for (Shape* shape : shapes) {
Rooted<StackShape> child(cx, StackShape(shape));
bool isPrivate = JSID_IS_SYMBOL(child.get().propid) &&
JSID_TO_SYMBOL(child.get().propid)->isPrivateName();
// Private fields are not visible to SetIntegrity.
if (!isPrivate) {
child.setAttrs(child.attrs() |
GetSealedOrFrozenAttributes(child.attrs(), level));
}
last = cx->zone()->propertyTree().getChild(cx, last, child);
if (!last) {
return false;
}
}
MOZ_ASSERT(nobj->lastProperty()->slotSpan() == last->slotSpan());
MOZ_ALWAYS_TRUE(nobj->setLastProperty(cx, last));
// Ordinarily ArraySetLength handles this, but we're going behind its back
// right now, so we must do this manually.
if (level == IntegrityLevel::Frozen && obj->is<ArrayObject>()) {
obj->as<ArrayObject>().setNonWritableLength(cx);
}
} else {
// Steps 6-7.
RootedIdVector keys(cx);
if (!GetPropertyKeys(
cx, obj, JSITER_HIDDEN | JSITER_OWNONLY | JSITER_SYMBOLS, &keys)) {
return false;
}
RootedId id(cx);
Rooted<PropertyDescriptor> desc(cx);
const unsigned AllowConfigure =
JSPROP_IGNORE_ENUMERATE | JSPROP_IGNORE_READONLY | JSPROP_IGNORE_VALUE;
const unsigned AllowConfigureAndWritable =
AllowConfigure & ~JSPROP_IGNORE_READONLY;
// 8.a/9.a. The two different loops are merged here.
for (size_t i = 0; i < keys.length(); i++) {
id = keys[i];
if (level == IntegrityLevel::Sealed) {
// 8.a.i.
desc.setAttributes(AllowConfigure | JSPROP_PERMANENT);
} else {
// 9.a.i-ii.
Rooted<PropertyDescriptor> currentDesc(cx);
if (!GetOwnPropertyDescriptor(cx, obj, id, &currentDesc)) {
return false;
}
// 9.a.iii.
if (!currentDesc.object()) {
continue;
}
// 9.a.iii.1-2
if (currentDesc.isAccessorDescriptor()) {
desc.setAttributes(AllowConfigure | JSPROP_PERMANENT);
} else {
desc.setAttributes(AllowConfigureAndWritable | JSPROP_PERMANENT |
JSPROP_READONLY);
}
}
// 8.a.i-ii. / 9.a.iii.3-4
if (!DefineProperty(cx, obj, id, desc)) {
return false;
}
}
}
// Finally, freeze or seal the dense elements.
if (obj->is<NativeObject>()) {
if (!ObjectElements::FreezeOrSeal(cx, obj.as<NativeObject>(), level)) {
return false;
}
}
return true;
}
static bool ResolveLazyProperties(JSContext* cx, HandleNativeObject obj) {
const JSClass* clasp = obj->getClass();
if (JSEnumerateOp enumerate = clasp->getEnumerate()) {
if (!enumerate(cx, obj)) {
return false;
}
}
if (clasp->getNewEnumerate() && clasp->getResolve()) {
RootedIdVector properties(cx);
if (!clasp->getNewEnumerate()(cx, obj, &properties,
/* enumerableOnly = */ false)) {
return false;
}
RootedId id(cx);
for (size_t i = 0; i < properties.length(); i++) {
id = properties[i];
bool found;
if (!HasOwnProperty(cx, obj, id, &found)) {
return false;
}
}
}
return true;
}
// ES6 draft rev33 (12 Feb 2015) 7.3.15
bool js::TestIntegrityLevel(JSContext* cx, HandleObject obj,
IntegrityLevel level, bool* result) {
// Steps 3-6. (Steps 1-2 are redundant assertions.)
bool status;
if (!IsExtensible(cx, obj, &status)) {
return false;
}
if (status) {
*result = false;
return true;
}
// Fast path for native objects.
if (obj->is<NativeObject>()) {
HandleNativeObject nobj = obj.as<NativeObject>();
// Force lazy properties to be resolved.
if (!ResolveLazyProperties(cx, nobj)) {
return false;
}
// Typed array elements are configurable, writable properties, so if any
// elements are present, the typed array can neither be sealed nor frozen.
if (nobj->is<TypedArrayObject>() &&
nobj->as<TypedArrayObject>().length().get() > 0) {
*result = false;
return true;
}
bool hasDenseElements = false;
for (size_t i = 0; i < nobj->getDenseInitializedLength(); i++) {
if (nobj->containsDenseElement(i)) {
hasDenseElements = true;
break;
}
}
if (hasDenseElements) {
// Unless the sealed flag is set, dense elements are configurable.
if (!nobj->denseElementsAreSealed()) {
*result = false;
return true;
}
// Unless the frozen flag is set, dense elements are writable.
if (level == IntegrityLevel::Frozen && !nobj->denseElementsAreFrozen()) {
*result = false;
return true;
}
}
// Steps 7-9.
for (Shape::Range<NoGC> r(nobj->lastProperty()); !r.empty(); r.popFront()) {
Shape* shape = &r.front();
// Steps 9.c.i-ii.
if (shape->configurable() ||
(level == IntegrityLevel::Frozen && shape->isDataDescriptor() &&
shape->writable())) {
*result = false;
return true;
}
}
} else {
// Steps 7-8.
RootedIdVector props(cx);
if (!GetPropertyKeys(
cx, obj, JSITER_HIDDEN | JSITER_OWNONLY | JSITER_SYMBOLS, &props)) {
return false;
}
// Step 9.
RootedId id(cx);
Rooted<PropertyDescriptor> desc(cx);
for (size_t i = 0, len = props.length(); i < len; i++) {
id = props[i];
// Steps 9.a-b.
if (!GetOwnPropertyDescriptor(cx, obj, id, &desc)) {
return false;
}
// Step 9.c.
if (!desc.object()) {
continue;
}
// Steps 9.c.i-ii.
if (desc.configurable() || (level == IntegrityLevel::Frozen &&
desc.isDataDescriptor() && desc.writable())) {
*result = false;
return true;
}
}
}
// Step 10.
*result = true;
return true;
}
/* * */
static inline JSObject* NewObject(JSContext* cx, Handle<TaggedProto> proto,
const JSClass* clasp, gc::AllocKind kind,
NewObjectKind newKind,
ObjectFlags objectFlags = {}) {
MOZ_ASSERT(clasp != &ArrayObject::class_);
MOZ_ASSERT_IF(clasp == &JSFunction::class_,
kind == gc::AllocKind::FUNCTION ||
kind == gc::AllocKind::FUNCTION_EXTENDED);
// For objects which can have fixed data following the object, only use
// enough fixed slots to cover the number of reserved slots in the object,
// regardless of the allocation kind specified.
size_t nfixed = ClassCanHaveFixedData(clasp)
? GetGCKindSlots(gc::GetGCObjectKind(clasp), clasp)
: GetGCKindSlots(kind, clasp);
RootedShape shape(
cx, EmptyShape::getInitialShape(cx, clasp, cx->realm(), proto, nfixed,
objectFlags));
if (!shape) {
return nullptr;
}
gc::InitialHeap heap = GetInitialHeap(newKind, clasp);
JSObject* obj;
if (clasp->isJSFunction()) {
JS_TRY_VAR_OR_RETURN_NULL(cx, obj,
JSFunction::create(cx, kind, heap, shape));
} else if (MOZ_LIKELY(clasp->isNativeObject())) {
JS_TRY_VAR_OR_RETURN_NULL(cx, obj,
NativeObject::create(cx, kind, heap, shape));
} else {
MOZ_ASSERT(IsTypedObjectClass(clasp));
JS_TRY_VAR_OR_RETURN_NULL(cx, obj,
TypedObject::create(cx, kind, heap, shape));
}
probes::CreateObject(cx, obj);
return obj;
}
void NewObjectCache::fillProto(EntryIndex entry, const JSClass* clasp,
js::TaggedProto proto, gc::AllocKind kind,
NativeObject* obj) {
MOZ_ASSERT_IF(proto.isObject(), !proto.toObject()->is<GlobalObject>());
MOZ_ASSERT(obj->taggedProto() == proto);
return fill(entry, clasp, proto.raw(), kind, obj);
}
bool js::NewObjectWithTaggedProtoIsCachable(JSContext* cx,
Handle<TaggedProto> proto,
NewObjectKind newKind,
const JSClass* clasp) {
return !cx->isHelperThreadContext() && proto.isObject() &&
newKind == GenericObject && clasp->isNativeObject() &&
!proto.toObject()->is<GlobalObject>();
}
JSObject* js::NewObjectWithGivenTaggedProto(JSContext* cx, const JSClass* clasp,
Handle<TaggedProto> proto,
gc::AllocKind allocKind,
NewObjectKind newKind,
ObjectFlags objectFlags) {
if (CanChangeToBackgroundAllocKind(allocKind, clasp)) {
allocKind = ForegroundToBackgroundAllocKind(allocKind);
}
bool isCachable =
NewObjectWithTaggedProtoIsCachable(cx, proto, newKind, clasp);
if (isCachable) {
NewObjectCache& cache = cx->caches().newObjectCache;
NewObjectCache::EntryIndex entry = -1;
if (cache.lookupProto(clasp, proto.toObject(), allocKind, &entry)) {
JSObject* obj =
cache.newObjectFromHit(cx, entry, GetInitialHeap(newKind, clasp));
if (obj) {
return obj;
}
}
}
RootedObject obj(
cx, NewObject(cx, proto, clasp, allocKind, newKind, objectFlags));
if (!obj) {
return nullptr;
}
if (isCachable && !obj->as<NativeObject>().hasDynamicSlots()) {
NewObjectCache& cache = cx->caches().newObjectCache;
NewObjectCache::EntryIndex entry = -1;
cache.lookupProto(clasp, proto.toObject(), allocKind, &entry);
cache.fillProto(entry, clasp, proto, allocKind, &obj->as<NativeObject>());
}
return obj;
}
static bool NewObjectIsCachable(JSContext* cx, NewObjectKind newKind,
const JSClass* clasp) {
return !cx->isHelperThreadContext() && newKind == GenericObject &&
clasp->isNativeObject();
}
JSObject* js::NewObjectWithClassProto(JSContext* cx, const JSClass* clasp,
HandleObject protoArg,
gc::AllocKind allocKind,
NewObjectKind newKind) {
if (protoArg) {
return NewObjectWithGivenTaggedProto(cx, clasp, AsTaggedProto(protoArg),
allocKind, newKind);
}
if (CanChangeToBackgroundAllocKind(allocKind, clasp)) {
allocKind = ForegroundToBackgroundAllocKind(allocKind);
}
Handle<GlobalObject*> global = cx->global();
bool isCachable = NewObjectIsCachable(cx, newKind, clasp);
if (isCachable) {
NewObjectCache& cache = cx->caches().newObjectCache;
NewObjectCache::EntryIndex entry = -1;
if (cache.lookupGlobal(clasp, global, allocKind, &entry)) {
gc::InitialHeap heap = GetInitialHeap(newKind, clasp);
JSObject* obj = cache.newObjectFromHit(cx, entry, heap);
if (obj) {
return obj;
}
}
}
// Find the appropriate proto for clasp. Built-in classes have a cached
// proto on cx->global(); all others get %ObjectPrototype%.
JSProtoKey protoKey = JSCLASS_CACHED_PROTO_KEY(clasp);
if (protoKey == JSProto_Null) {
protoKey = JSProto_Object;
}
JSObject* proto = GlobalObject::getOrCreatePrototype(cx, protoKey);
if (!proto) {
return nullptr;
}
Rooted<TaggedProto> taggedProto(cx, TaggedProto(proto));
JSObject* obj = NewObject(cx, taggedProto, clasp, allocKind, newKind);
if (!obj) {
return nullptr;
}
if (isCachable && !obj->as<NativeObject>().hasDynamicSlots()) {
NewObjectCache& cache = cx->caches().newObjectCache;
NewObjectCache::EntryIndex entry = -1;
cache.lookupGlobal(clasp, global, allocKind, &entry);
cache.fillGlobal(entry, clasp, global, allocKind, &obj->as<NativeObject>());
}
return obj;
}
bool js::NewObjectScriptedCall(JSContext* cx, MutableHandleObject pobj) {
gc::AllocKind allocKind = NewObjectGCKind();
NewObjectKind newKind = GenericObject;
JSObject* obj = NewBuiltinClassInstance<PlainObject>(cx, allocKind, newKind);
if (!obj) {
return false;
}
pobj.set(obj);
return true;
}
JSObject* js::CreateThis(JSContext* cx, const JSClass* newclasp,
HandleObject callee) {
RootedObject proto(cx);
if (!GetPrototypeFromConstructor(
cx, callee, JSCLASS_CACHED_PROTO_KEY(newclasp), &proto)) {
return nullptr;
}
gc::AllocKind kind = NewObjectGCKind();
return NewObjectWithClassProto(cx, newclasp, proto, kind);
}
bool js::GetPrototypeFromConstructor(JSContext* cx, HandleObject newTarget,
JSProtoKey intrinsicDefaultProto,
MutableHandleObject proto) {
RootedValue protov(cx);
if (!GetProperty(cx, newTarget, newTarget, cx->names().prototype, &protov)) {
return false;
}
if (protov.isObject()) {
proto.set(&protov.toObject());
} else if (newTarget->is<JSFunction>() &&
newTarget->as<JSFunction>().realm() == cx->realm()) {
// Steps 4.a-b fetch the builtin prototype of the current realm, which we
// represent as nullptr.
proto.set(nullptr);
} else if (intrinsicDefaultProto == JSProto_Null) {
// Bug 1317416. The caller did not pass a reasonable JSProtoKey, so let the
// caller select a prototype object. Most likely they will choose one from
// the wrong realm.
proto.set(nullptr);
} else {
// Step 4.a: Let realm be ? GetFunctionRealm(constructor);
Realm* realm = JS::GetFunctionRealm(cx, newTarget);
if (!realm) {
return false;
}
// Step 4.b: Set proto to realm's intrinsic object named
// intrinsicDefaultProto.
{
mozilla::Maybe<AutoRealm> ar;
if (cx->realm() != realm) {
ar.emplace(cx, realm->maybeGlobal());
}
proto.set(GlobalObject::getOrCreatePrototype(cx, intrinsicDefaultProto));
}
if (!proto) {
return false;
}
if (!cx->compartment()->wrap(cx, proto)) {
return false;
}
}
return true;
}
/* static */
bool JSObject::nonNativeSetProperty(JSContext* cx, HandleObject obj,
HandleId id, HandleValue v,
HandleValue receiver,
ObjectOpResult& result) {
return obj->getOpsSetProperty()(cx, obj, id, v, receiver, result);
}
/* static */
bool JSObject::nonNativeSetElement(JSContext* cx, HandleObject obj,
uint32_t index, HandleValue v,
HandleValue receiver,
ObjectOpResult& result) {
RootedId id(cx);
if (!IndexToId(cx, index, &id)) {
return false;
}
return nonNativeSetProperty(cx, obj, id, v, receiver, result);
}
static bool CopyPropertyFrom(JSContext* cx, HandleId id, HandleObject target,
HandleObject obj) {
// |target| must not be a CCW because we need to enter its realm below and
// CCWs are not associated with a single realm.
MOZ_ASSERT(!IsCrossCompartmentWrapper(target));
// |obj| and |cx| are generally not same-compartment with |target| here.
cx->check(obj, id);
Rooted<PropertyDescriptor> desc(cx);
if (!GetOwnPropertyDescriptor(cx, obj, id, &desc)) {
return false;
}
MOZ_ASSERT(desc.object());
// Silently skip JSGetterOp/JSSetterOp-implemented accessors.
if (desc.getter() && !desc.hasGetterObject()) {
return true;
}
if (desc.setter() && !desc.hasSetterObject()) {
return true;
}
JSAutoRealm ar(cx, target);
cx->markId(id);
RootedId wrappedId(cx, id);
if (!cx->compartment()->wrap(cx, &desc)) {
return false;
}
return DefineProperty(cx, target, wrappedId, desc);
}
JS_FRIEND_API bool JS_CopyOwnPropertiesAndPrivateFields(JSContext* cx,
HandleObject target,
HandleObject obj) {
// Both |obj| and |target| must not be CCWs because we need to enter their
// realms below and CCWs are not associated with a single realm.
MOZ_ASSERT(!IsCrossCompartmentWrapper(obj));
MOZ_ASSERT(!IsCrossCompartmentWrapper(target));
JSAutoRealm ar(cx, obj);
RootedIdVector props(cx);
if (!GetPropertyKeys(
cx, obj,
JSITER_PRIVATE | JSITER_OWNONLY | JSITER_HIDDEN | JSITER_SYMBOLS,
&props)) {
return false;
}
for (size_t i = 0; i < props.length(); ++i) {
if (!CopyPropertyFrom(cx, props[i], target, obj)) {
return false;
}
}
return true;
}
static bool GetScriptArrayObjectElements(
HandleArrayObject arr, MutableHandle<GCVector<Value>> values) {
MOZ_ASSERT(!arr->isIndexed());
size_t length = arr->length();
if (!values.appendN(MagicValue(JS_ELEMENTS_HOLE), length)) {
return false;
}
size_t initlen = arr->getDenseInitializedLength();
for (size_t i = 0; i < initlen; i++) {
values[i].set(arr->getDenseElement(i));
}
return true;
}
static bool GetScriptPlainObjectProperties(
HandleObject obj, MutableHandle<IdValueVector> properties) {
MOZ_ASSERT(obj->is<PlainObject>());
PlainObject* nobj = &obj->as<PlainObject>();
if (!properties.appendN(IdValuePair(), nobj->slotSpan())) {
return false;
}
for (Shape::Range<NoGC> r(nobj->lastProperty()); !r.empty(); r.popFront()) {
Shape& shape = r.front();
MOZ_ASSERT(shape.isDataDescriptor());
uint32_t slot = shape.slot();
properties[slot].get().id = shape.propid();
properties[slot].get().value = nobj->getSlot(slot);
}
for (size_t i = 0; i < nobj->getDenseInitializedLength(); i++) {
Value v = nobj->getDenseElement(i);
if (!v.isMagic(JS_ELEMENTS_HOLE) &&
!properties.emplaceBack(INT_TO_JSID(i), v)) {
return false;
}
}
return true;
}
static bool DeepCloneValue(JSContext* cx, Value* vp) {
if (vp->isObject()) {
RootedObject obj(cx, &vp->toObject());
obj = DeepCloneObjectLiteral(cx, obj);
if (!obj) {
return false;
}
vp->setObject(*obj);
} else {
cx->markAtomValue(*vp);
}
return true;
}
JSObject* js::DeepCloneObjectLiteral(JSContext* cx, HandleObject obj) {
/* NB: Keep this in sync with XDRObjectLiteral. */
MOZ_ASSERT(obj->is<PlainObject>() || obj->is<ArrayObject>());
if (obj->is<ArrayObject>()) {
Rooted<GCVector<Value>> values(cx, GCVector<Value>(cx));
if (!GetScriptArrayObjectElements(obj.as<ArrayObject>(), &values)) {
return nullptr;
}
// Deep clone any elements.
for (uint32_t i = 0; i < values.length(); ++i) {
if (!DeepCloneValue(cx, values[i].address())) {
return nullptr;
}
}
return NewDenseCopiedArray(cx, values.length(), values.begin(),
/* proto = */ nullptr, TenuredObject);
}
Rooted<IdValueVector> properties(cx, IdValueVector(cx));
if (!GetScriptPlainObjectProperties(obj, &properties)) {
return nullptr;
}
for (size_t i = 0; i < properties.length(); i++) {
cx->markId(properties[i].get().id);
if (!DeepCloneValue(cx, &properties[i].get().value)) {
return nullptr;
}
}
return NewPlainObjectWithProperties(cx, properties.begin(),
properties.length(), TenuredObject);
}
static bool InitializePropertiesFromCompatibleNativeObject(
JSContext* cx, HandleNativeObject dst, HandleNativeObject src) {
cx->check(src, dst);
MOZ_ASSERT(src->getClass() == dst->getClass());
MOZ_ASSERT(dst->lastProperty()->objectFlags().isEmpty());
MOZ_ASSERT(src->numFixedSlots() == dst->numFixedSlots());
if (!dst->ensureElements(cx, src->getDenseInitializedLength())) {
return false;
}
uint32_t initialized = src->getDenseInitializedLength();
for (uint32_t i = 0; i < initialized; ++i) {
dst->setDenseInitializedLength(i + 1);
dst->initDenseElement(i, src->getDenseElement(i));
}
MOZ_ASSERT(!src->hasPrivate());
RootedShape shape(cx);
if (src->staticPrototype() == dst->staticPrototype()) {
shape = src->lastProperty();
} else {
// We need to generate a new shape for dst that has dst's proto but all
// the property information from src. Note that we asserted above that
// dst's object flags are empty.
shape = EmptyShape::getInitialShape(cx, dst->getClass(), dst->realm(),
dst->taggedProto(),
dst->numFixedSlots(), ObjectFlags());
if (!shape) {
return false;
}
Rooted<BaseShape*> nbase(cx, shape->base());
// Get an in-order list of the shapes in the src object.
Rooted<ShapeVector> shapes(cx, ShapeVector(cx));
for (Shape::Range<NoGC> r(src->lastProperty()); !r.empty(); r.popFront()) {
if (!shapes.append(&r.front())) {
return false;
}
}
std::reverse(shapes.begin(), shapes.end());
for (Shape* shapeToClone : shapes) {
Rooted<StackShape> child(cx, StackShape(shapeToClone));
child.setBase(nbase);
shape = cx->zone()->propertyTree().getChild(cx, shape, child);
if (!shape) {
return false;
}
}
}
size_t span = shape->slotSpan();
if (!dst->setLastProperty(cx, shape)) {
return false;
}
for (size_t i = JSCLASS_RESERVED_SLOTS(src->getClass()); i < span; i++) {
dst->setSlot(i, src->getSlot(i));
}
return true;
}
JS_FRIEND_API bool JS_InitializePropertiesFromCompatibleNativeObject(
JSContext* cx, HandleObject dst, HandleObject src) {
return InitializePropertiesFromCompatibleNativeObject(
cx, dst.as<NativeObject>(), src.as<NativeObject>());
}
template <XDRMode mode>
XDRResult js::XDRObjectLiteral(XDRState<mode>* xdr, MutableHandleObject obj) {
/* NB: Keep this in sync with DeepCloneObjectLiteral. */
JSContext* cx = xdr->cx();
cx->check(obj);
// Distinguish between objects and array classes.
uint32_t isArray = 0;
{
if (mode == XDR_ENCODE) {
MOZ_ASSERT(obj->is<PlainObject>() || obj->is<ArrayObject>());
isArray = obj->is<ArrayObject>() ? 1 : 0;
}
MOZ_TRY(xdr->codeUint32(&isArray));
}
RootedValue tmpValue(cx), tmpIdValue(cx);
RootedId tmpId(cx);
if (isArray) {
Rooted<GCVector<Value>> values(cx, GCVector<Value>(cx));
if (mode == XDR_ENCODE) {
RootedArrayObject arr(cx, &obj->as<ArrayObject>());
if (!GetScriptArrayObjectElements(arr, &values)) {
return xdr->fail(JS::TranscodeResult::Throw);
}
}
uint32_t initialized;
if (mode == XDR_ENCODE) {
initialized = values.length();
}
MOZ_TRY(xdr->codeUint32(&initialized));
if (mode == XDR_DECODE &&
!values.appendN(MagicValue(JS_ELEMENTS_HOLE), initialized)) {
return xdr->fail(JS::TranscodeResult::Throw);
}
// Recursively copy dense elements.
for (unsigned i = 0; i < initialized; i++) {
MOZ_TRY(XDRScriptConst(xdr, values[i]));
}
if (mode == XDR_DECODE) {
obj.set(NewDenseCopiedArray(cx, values.length(), values.begin(),
/* proto = */ nullptr, TenuredObject));
if (!obj) {
return xdr->fail(JS::TranscodeResult::Throw);
}
}
return Ok();
}
// Code the properties in the object.
Rooted<IdValueVector> properties(cx, IdValueVector(cx));
if (mode == XDR_ENCODE && !GetScriptPlainObjectProperties(obj, &properties)) {
return xdr->fail(JS::TranscodeResult::Throw);
}
uint32_t nproperties = properties.length();
MOZ_TRY(xdr->codeUint32(&nproperties));
if (mode == XDR_DECODE && !properties.appendN(IdValuePair(), nproperties)) {
return xdr->fail(JS::TranscodeResult::Throw);
}
for (size_t i = 0; i < nproperties; i++) {
if (mode == XDR_ENCODE) {
tmpIdValue = IdToValue(properties[i].get().id);
tmpValue = properties[i].get().value;
}
MOZ_TRY(XDRScriptConst(xdr, &tmpIdValue));
MOZ_TRY(XDRScriptConst(xdr, &tmpValue));
if (mode == XDR_DECODE) {
if (!PrimitiveValueToId<CanGC>(cx, tmpIdValue, &tmpId)) {
return xdr->fail(JS::TranscodeResult::Throw);
}
properties[i].get().id = tmpId;
properties[i].get().value = tmpValue;
}
}
if (mode == XDR_DECODE) {
obj.set(NewPlainObjectWithProperties(cx, properties.begin(),
properties.length(), TenuredObject));
if (!obj) {
return xdr->fail(JS::TranscodeResult::Throw);
}
}
return Ok();
}
template XDRResult js::XDRObjectLiteral(XDRState<XDR_ENCODE>* xdr,
MutableHandleObject obj);
template XDRResult js::XDRObjectLiteral(XDRState<XDR_DECODE>* xdr,
MutableHandleObject obj);
/* static */
bool NativeObject::fillInAfterSwap(JSContext* cx, HandleNativeObject obj,
NativeObject* old, HandleValueVector values,
void* priv) {
// This object has just been swapped with some other object, and its shape
// no longer reflects its allocated size. Correct this information and
// fill the slots in with the specified values.
MOZ_ASSERT(obj->slotSpan() == values.length());
MOZ_ASSERT(!IsInsideNursery(obj));
// Make sure the shape's numFixedSlots() is correct.
size_t nfixed =
gc::GetGCKindSlots(obj->asTenured().getAllocKind(), obj->getClass());
if (nfixed != obj->shape()->numFixedSlots()) {
if (!NativeObject::generateOwnShape(cx, obj)) {
return false;
}
obj->shape()->setNumFixedSlots(nfixed);
}
if (obj->hasPrivate()) {
obj->setPrivate(priv);
} else {
MOZ_ASSERT(!priv);
}
uint32_t oldDictionarySlotSpan =
obj->inDictionaryMode() ? obj->dictionaryModeSlotSpan() : 0;
Zone* zone = obj->zone();
if (obj->hasDynamicSlots()) {
ObjectSlots* slotsHeader = obj->getSlotsHeader();
size_t size = ObjectSlots::allocSize(slotsHeader->capacity());
zone->removeCellMemory(old, size, MemoryUse::ObjectSlots);
js_free(slotsHeader);
obj->setEmptyDynamicSlots(0);
}
size_t ndynamic =
calculateDynamicSlots(nfixed, values.length(), obj->getClass());
size_t currentSlots = obj->getSlotsHeader()->capacity();
MOZ_ASSERT(ndynamic >= currentSlots);
if (ndynamic > currentSlots) {
if (!obj->growSlots(cx, currentSlots, ndynamic)) {
return false;
}
}
if (obj->inDictionaryMode()) {
obj->setDictionaryModeSlotSpan(oldDictionarySlotSpan);
}
obj->initSlots(values.begin(), values.length());
return true;
}
void JSObject::fixDictionaryShapeAfterSwap() {
// Dictionary shapes can point back to their containing objects, so after
// swapping the guts of those objects fix the pointers up.
if (is<NativeObject>() && as<NativeObject>().inDictionaryMode()) {
shape()->dictNext.setObject(this);
}
}
bool js::ObjectMayBeSwapped(const JSObject* obj) {
const JSClass* clasp = obj->getClass();
// We want to optimize Window/globals and Gecko doesn't require transplanting
// them (only the WindowProxy around them). A Window may be a DOMClass, so we
// explicitly check if this is a global.
if (clasp->isGlobal()) {
return false;
}
// WindowProxy, Wrapper, DeadProxyObject, DOMProxy, and DOMClass (non-global)
// types may be swapped. It is hard to detect DOMProxy from shell, so target
// proxies in general.
return clasp->isProxyObject() || clasp->isDOMClass();
}
[[nodiscard]] static bool CopyProxyValuesBeforeSwap(
JSContext* cx, ProxyObject* proxy, MutableHandleValueVector values) {
MOZ_ASSERT(values.empty());
// Remove the GCPtrValues we're about to swap from the store buffer, to
// ensure we don't trace bogus values.
gc::StoreBuffer& sb = cx->runtime()->gc.storeBuffer();
// Reserve space for the expando, private slot and the reserved slots.
if (!values.reserve(2 + proxy->numReservedSlots())) {
return false;
}
js::detail::ProxyValueArray* valArray =
js::detail::GetProxyDataLayout(proxy)->values();
sb.unputValue(&valArray->expandoSlot);
sb.unputValue(&valArray->privateSlot);
values.infallibleAppend(valArray->expandoSlot);
values.infallibleAppend(valArray->privateSlot);
for (size_t i = 0; i < proxy->numReservedSlots(); i++) {
sb.unputValue(&valArray->reservedSlots.slots[i]);
values.infallibleAppend(valArray->reservedSlots.slots[i]);
}
return true;
}
bool ProxyObject::initExternalValueArrayAfterSwap(
JSContext* cx, const HandleValueVector values) {
MOZ_ASSERT(getClass()->isProxyObject());
size_t nreserved = numReservedSlots();
// |values| contains the expando slot, private slot and the reserved slots.
MOZ_ASSERT(values.length() == 2 + nreserved);
size_t nbytes = js::detail::ProxyValueArray::sizeOf(nreserved);
auto* valArray = reinterpret_cast<js::detail::ProxyValueArray*>(
cx->zone()->pod_malloc<uint8_t>(nbytes));
if (!valArray) {
return false;
}
valArray->expandoSlot = values[0];
valArray->privateSlot = values[1];
for (size_t i = 0; i < nreserved; i++) {
valArray->reservedSlots.slots[i] = values[i + 2];
}
// Note: we allocate external slots iff the proxy had an inline
// ProxyValueArray, so at this point reservedSlots points into the
// old object and we don't have to free anything.
data.reservedSlots = &valArray->reservedSlots;
return true;
}
/* Use this method with extreme caution. It trades the guts of two objects. */
void JSObject::swap(JSContext* cx, HandleObject a, HandleObject b,
AutoEnterOOMUnsafeRegion& oomUnsafe) {
// Ensure swap doesn't cause a finalizer to not be run.
MOZ_ASSERT(IsBackgroundFinalized(a->asTenured().getAllocKind()) ==
IsBackgroundFinalized(b->asTenured().getAllocKind()));
MOZ_ASSERT(a->compartment() == b->compartment());
// You must have entered the objects' compartment before calling this.
MOZ_ASSERT(cx->compartment() == a->compartment());
// Only certain types of objects are allowed to be swapped. This allows the
// JITs to better optimize objects that can never swap.
MOZ_RELEASE_ASSERT(js::ObjectMayBeSwapped(a));
MOZ_RELEASE_ASSERT(js::ObjectMayBeSwapped(b));
/*
* Neither object may be in the nursery, but ensure we update any embedded
* nursery pointers in either object.
*/
MOZ_ASSERT(!IsInsideNursery(a) && !IsInsideNursery(b));
gc::StoreBuffer& storeBuffer = cx->runtime()->gc.storeBuffer();
storeBuffer.putWholeCell(a);
storeBuffer.putWholeCell(b);
if (a->zone()->wasGCStarted() || b->zone()->wasGCStarted()) {
storeBuffer.setMayHavePointersToDeadCells();
}
unsigned r = NotifyGCPreSwap(a, b);
// Do the fundamental swapping of the contents of two objects.
MOZ_ASSERT(a->compartment() == b->compartment());
MOZ_ASSERT(a->is<JSFunction>() == b->is<JSFunction>());
// Don't try to swap functions with different sizes.
MOZ_ASSERT_IF(a->is<JSFunction>(),
a->tenuredSizeOfThis() == b->tenuredSizeOfThis());
// Watch for oddball objects that have special organizational issues and
// can't be swapped.
MOZ_ASSERT(!a->is<RegExpObject>() && !b->is<RegExpObject>());
MOZ_ASSERT(!a->is<ArrayObject>() && !b->is<ArrayObject>());
MOZ_ASSERT(!a->is<ArrayBufferObject>() && !b->is<ArrayBufferObject>());
MOZ_ASSERT(!a->is<TypedArrayObject>() && !b->is<TypedArrayObject>());
MOZ_ASSERT(!a->is<TypedObject>() && !b->is<TypedObject>());
// Don't swap objects that may currently be participating in shape
// teleporting optimizations.
//
// See: ReshapeForProtoMutation, ReshapeForShadowedProp
MOZ_ASSERT_IF(a->is<NativeObject>() && a->isDelegate(),
a->taggedProto() == TaggedProto());
MOZ_ASSERT_IF(b->is<NativeObject>() && b->isDelegate(),
b->taggedProto() == TaggedProto());
bool aIsProxyWithInlineValues =
a->is<ProxyObject>() && a->as<ProxyObject>().usingInlineValueArray();
bool bIsProxyWithInlineValues =
b->is<ProxyObject>() && b->as<ProxyObject>().usingInlineValueArray();
// Swap element associations.
Zone* zone = a->zone();