#pragma once
#include "GenericArguments.h"
#include "JSLexicalEnvironment.h"
namespace JSC {
class ScopedArguments : public GenericArguments<ScopedArguments> {
private:
ScopedArguments(VM&, Structure*, unsigned totalLength);
void finishCreation(VM&, JSFunction* callee, ScopedArgumentsTable*, JSLexicalEnvironment*);
public:
template<typename CellType>
static CompleteSubspace* subspaceFor(VM& vm)
{
RELEASE_ASSERT(!CellType::needsDestruction);
return &vm.jsValueGigacageCellSpace;
}
static ScopedArguments* createUninitialized(VM&, Structure*, JSFunction* callee, ScopedArgumentsTable*, JSLexicalEnvironment*, unsigned totalLength);
static ScopedArguments* create(VM&, Structure*, JSFunction* callee, ScopedArgumentsTable*, JSLexicalEnvironment*, unsigned totalLength);
static ScopedArguments* createByCopying(ExecState*, ScopedArgumentsTable*, JSLexicalEnvironment*);
static ScopedArguments* createByCopyingFrom(VM&, Structure*, Register* argumentsStart, unsigned totalLength, JSFunction* callee, ScopedArgumentsTable*, JSLexicalEnvironment*);
static void visitChildren(JSCell*, SlotVisitor&);
uint32_t internalLength() const
{
return m_totalLength;
}
uint32_t length(ExecState* exec) const
{
VM& vm = exec->vm();
if (UNLIKELY(m_overrodeThings))
return get(exec, vm.propertyNames->length).toUInt32(exec);
return internalLength();
}
bool isMappedArgument(uint32_t i) const
{
if (i >= m_totalLength)
return false;
unsigned namedLength = m_table->length();
if (i < namedLength)
return !!m_table->get(i);
return !!overflowStorage()[i - namedLength].get();
}
bool isMappedArgumentInDFG(uint32_t i) const
{
return isMappedArgument(i);
}
JSValue getIndexQuickly(uint32_t i) const
{
ASSERT_WITH_SECURITY_IMPLICATION(isMappedArgument(i));
unsigned namedLength = m_table->length();
if (i < namedLength)
return m_scope->variableAt(m_table->get(i)).get();
return overflowStorage()[i - namedLength].get();
}
void setIndexQuickly(VM& vm, uint32_t i, JSValue value)
{
ASSERT_WITH_SECURITY_IMPLICATION(isMappedArgument(i));
unsigned namedLength = m_table->length();
if (i < namedLength)
m_scope->variableAt(m_table->get(i)).set(vm, m_scope.get(), value);
else
overflowStorage()[i - namedLength].set(vm, this, value);
}
WriteBarrier<JSFunction>& callee()
{
return m_callee;
}
bool overrodeThings() const { return m_overrodeThings; }
void overrideThings(VM&);
void overrideThingsIfNecessary(VM&);
void unmapArgument(VM&, uint32_t index);
void initModifiedArgumentsDescriptorIfNecessary(VM& vm)
{
GenericArguments<ScopedArguments>::initModifiedArgumentsDescriptorIfNecessary(vm, m_table->length());
}
void setModifiedArgumentDescriptor(VM& vm, unsigned index)
{
GenericArguments<ScopedArguments>::setModifiedArgumentDescriptor(vm, index, m_table->length());
}
bool isModifiedArgumentDescriptor(unsigned index)
{
return GenericArguments<ScopedArguments>::isModifiedArgumentDescriptor(index, m_table->length());
}
void copyToArguments(ExecState*, VirtualRegister firstElementDest, unsigned offset, unsigned length);
DECLARE_INFO;
static Structure* createStructure(VM&, JSGlobalObject*, JSValue prototype);
static ptrdiff_t offsetOfOverrodeThings() { return OBJECT_OFFSETOF(ScopedArguments, m_overrodeThings); }
static ptrdiff_t offsetOfTotalLength() { return OBJECT_OFFSETOF(ScopedArguments, m_totalLength); }
static ptrdiff_t offsetOfTable() { return OBJECT_OFFSETOF(ScopedArguments, m_table); }
static ptrdiff_t offsetOfScope() { return OBJECT_OFFSETOF(ScopedArguments, m_scope); }
static size_t overflowStorageOffset()
{
return WTF::roundUpToMultipleOf<sizeof(WriteBarrier<Unknown>)>(sizeof(ScopedArguments));
}
static size_t allocationSize(Checked<size_t> overflowArgumentsLength)
{
return (overflowStorageOffset() + sizeof(WriteBarrier<Unknown>) * overflowArgumentsLength).unsafeGet();
}
private:
WriteBarrier<Unknown>* overflowStorage() const
{
return bitwise_cast<WriteBarrier<Unknown>*>(bitwise_cast<char*>(this) + overflowStorageOffset());
}
bool m_overrodeThings; unsigned m_totalLength; WriteBarrier<JSFunction> m_callee;
WriteBarrier<ScopedArgumentsTable> m_table;
WriteBarrier<JSLexicalEnvironment> m_scope;
};
}