JSImmutableButterfly.h [plain text]
#pragma once
#include "Butterfly.h"
#include "IndexingHeader.h"
#include "JSCJSValueInlines.h"
#include "JSCell.h"
#include "Structure.h"
#include "VirtualRegister.h"
namespace JSC {
class JSImmutableButterfly : public JSCell {
using Base = JSCell;
public:
static constexpr unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
DECLARE_INFO;
static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype, IndexingType indexingType)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(JSImmutableButterflyType, StructureFlags), info(), indexingType);
}
ALWAYS_INLINE static JSImmutableButterfly* tryCreate(VM& vm, Structure* structure, unsigned length)
{
if (UNLIKELY(length > IndexingHeader::maximumLength))
return nullptr;
void* buffer = tryAllocateCell<JSImmutableButterfly>(vm.heap, allocationSize(length).unsafeGet());
if (UNLIKELY(!buffer))
return nullptr;
JSImmutableButterfly* result = new (NotNull, buffer) JSImmutableButterfly(vm, structure, length);
result->finishCreation(vm);
return result;
}
static JSImmutableButterfly* create(VM& vm, IndexingType indexingType, unsigned length)
{
auto* array = tryCreate(vm, vm.immutableButterflyStructures[arrayIndexFromIndexingType(indexingType) - NumberOfIndexingShapes].get(), length);
RELEASE_ASSERT(array);
return array;
}
ALWAYS_INLINE static JSImmutableButterfly* createFromArray(JSGlobalObject* globalObject, VM& vm, JSArray* array)
{
auto throwScope = DECLARE_THROW_SCOPE(vm);
IndexingType indexingType = array->indexingType() & IndexingShapeMask;
unsigned length = array->length();
if (isCopyOnWrite(indexingType)) {
if (hasContiguous(indexingType))
return JSImmutableButterfly::fromButterfly(array->butterfly());
}
JSImmutableButterfly* result = JSImmutableButterfly::tryCreate(vm, vm.immutableButterflyStructures[arrayIndexFromIndexingType(CopyOnWriteArrayWithContiguous) - NumberOfIndexingShapes].get(), length);
if (UNLIKELY(!result)) {
throwOutOfMemoryError(globalObject, throwScope);
return nullptr;
}
if (!length)
return result;
if (indexingType == ContiguousShape || indexingType == Int32Shape) {
for (unsigned i = 0; i < length; i++) {
JSValue value = array->butterfly()->contiguous().at(array, i).get();
value = !!value ? value : jsUndefined();
result->setIndex(vm, i, value);
}
return result;
}
if (indexingType == DoubleShape) {
for (unsigned i = 0; i < length; i++) {
double d = array->butterfly()->contiguousDouble().at(array, i);
JSValue value = std::isnan(d) ? jsUndefined() : JSValue(JSValue::EncodeAsDouble, d);
result->setIndex(vm, i, value);
}
return result;
}
for (unsigned i = 0; i < length; i++) {
JSValue value = array->getDirectIndex(globalObject, i);
if (!value) {
value = jsUndefined();
}
RETURN_IF_EXCEPTION(throwScope, nullptr);
result->setIndex(vm, i, value);
}
return result;
}
unsigned publicLength() const { return m_header.publicLength(); }
unsigned vectorLength() const { return m_header.vectorLength(); }
unsigned length() const { return m_header.publicLength(); }
Butterfly* toButterfly() const { return bitwise_cast<Butterfly*>(bitwise_cast<char*>(this) + offsetOfData()); }
static JSImmutableButterfly* fromButterfly(Butterfly* butterfly) { return bitwise_cast<JSImmutableButterfly*>(bitwise_cast<char*>(butterfly) - offsetOfData()); }
JSValue get(unsigned index) const
{
if (!hasDouble(indexingMode()))
return toButterfly()->contiguous().at(this, index).get();
double value = toButterfly()->contiguousDouble().at(this, index);
ASSERT(!std::isnan(value));
return jsDoubleNumber(value);
}
static void visitChildren(JSCell*, SlotVisitor&);
void copyToArguments(JSGlobalObject*, JSValue* firstElementDest, unsigned offset, unsigned length);
template<typename, SubspaceAccess>
static CompleteSubspace* subspaceFor(VM& vm)
{
return &vm.immutableButterflyJSValueGigacageAuxiliarySpace;
}
void setIndex(VM& vm, unsigned index, JSValue value)
{
if (hasDouble(indexingType()))
toButterfly()->contiguousDouble().atUnsafe(index) = value.asNumber();
else
toButterfly()->contiguous().atUnsafe(index).set(vm, this, value);
}
static constexpr size_t offsetOfData()
{
return WTF::roundUpToMultipleOf<sizeof(WriteBarrier<Unknown>)>(sizeof(JSImmutableButterfly));
}
static ptrdiff_t offsetOfPublicLength()
{
return OBJECT_OFFSETOF(JSImmutableButterfly, m_header) + IndexingHeader::offsetOfPublicLength();
}
static ptrdiff_t offsetOfVectorLength()
{
return OBJECT_OFFSETOF(JSImmutableButterfly, m_header) + IndexingHeader::offsetOfVectorLength();
}
static Checked<size_t> allocationSize(Checked<size_t> numItems)
{
return offsetOfData() + numItems * sizeof(WriteBarrier<Unknown>);
}
private:
JSImmutableButterfly(VM& vm, Structure* structure, unsigned length)
: Base(vm, structure)
{
m_header.setVectorLength(length);
m_header.setPublicLength(length);
if (hasContiguous(indexingType())) {
for (unsigned index = 0; index < length; ++index)
toButterfly()->contiguous().at(this, index).setStartingValue(JSValue());
}
}
IndexingHeader m_header;
};
}