#ifndef Arguments_h
#define Arguments_h
+#include "CodeOrigin.h"
#include "JSActivation.h"
+#include "JSDestructibleObject.h"
#include "JSFunction.h"
#include "JSGlobalObject.h"
#include "Interpreter.h"
#include "ObjectConstructor.h"
-#include "PrototypeFunction.h"
namespace JSC {
- struct ArgumentsData : Noncopyable {
- JSActivation* activation;
+class Arguments : public JSDestructibleObject {
+ friend class JIT;
+ friend class DFG::SpeculativeJIT;
+public:
+ typedef JSDestructibleObject Base;
- unsigned numParameters;
- ptrdiff_t firstParameterIndex;
- unsigned numArguments;
-
- Register* registers;
- OwnArrayPtr<Register> registerArray;
-
- Register* extraArguments;
- OwnArrayPtr<bool> deletedArguments;
- Register extraArgumentsFixedBuffer[4];
-
- JSFunction* callee;
- bool overrodeLength : 1;
- bool overrodeCallee : 1;
- };
-
-
- class Arguments : public JSObject {
- public:
- enum NoParametersType { NoParameters };
-
- Arguments(CallFrame*);
- Arguments(CallFrame*, NoParametersType);
- virtual ~Arguments();
-
- static const ClassInfo info;
-
- virtual void markChildren(MarkStack&);
-
- void fillArgList(ExecState*, MarkedArgumentBuffer&);
-
- uint32_t numProvidedArguments(ExecState* exec) const
- {
- if (UNLIKELY(d->overrodeLength))
- return get(exec, exec->propertyNames().length).toUInt32(exec);
- return d->numArguments;
- }
-
- void copyToRegisters(ExecState* exec, Register* buffer, uint32_t maxSize);
- void copyRegisters();
- bool isTornOff() const { return d->registerArray; }
- void setActivation(JSActivation* activation)
- {
- d->activation = activation;
- d->registers = &activation->registerAt(0);
- }
-
- static PassRefPtr<Structure> createStructure(JSValue prototype)
- {
- return Structure::create(prototype, TypeInfo(ObjectType, StructureFlags), AnonymousSlotCount);
- }
-
- protected:
- static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesMarkChildren | OverridesGetPropertyNames | JSObject::StructureFlags;
-
- private:
- void getArgumentsData(CallFrame*, JSFunction*&, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc);
- virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
- virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
- virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
- virtual void getOwnPropertyNames(ExecState*, PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
- virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
- virtual void put(ExecState*, unsigned propertyName, JSValue, PutPropertySlot&);
- virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
- virtual bool deleteProperty(ExecState*, unsigned propertyName);
-
- virtual const ClassInfo* classInfo() const { return &info; }
-
- void init(CallFrame*);
-
- OwnPtr<ArgumentsData> d;
- };
-
- Arguments* asArguments(JSValue);
-
- inline Arguments* asArguments(JSValue value)
+ static Arguments* create(VM& vm, CallFrame* callFrame)
{
- ASSERT(asObject(value)->inherits(&Arguments::info));
- return static_cast<Arguments*>(asObject(value));
- }
-
- ALWAYS_INLINE void Arguments::getArgumentsData(CallFrame* callFrame, JSFunction*& function, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc)
- {
- function = callFrame->callee();
-
- int numParameters = function->jsExecutable()->parameterCount();
- argc = callFrame->argumentCount();
-
- if (argc <= numParameters)
- argv = callFrame->registers() - RegisterFile::CallFrameHeaderSize - numParameters;
- else
- argv = callFrame->registers() - RegisterFile::CallFrameHeaderSize - numParameters - argc;
-
- argc -= 1; // - 1 to skip "this"
- firstParameterIndex = -RegisterFile::CallFrameHeaderSize - numParameters;
+ Arguments* arguments = new (NotNull, allocateCell<Arguments>(vm.heap)) Arguments(callFrame);
+ arguments->finishCreation(callFrame);
+ return arguments;
}
-
- inline Arguments::Arguments(CallFrame* callFrame)
- : JSObject(callFrame->lexicalGlobalObject()->argumentsStructure())
- , d(new ArgumentsData)
+
+ static Arguments* create(VM& vm, CallFrame* callFrame, InlineCallFrame* inlineCallFrame)
{
- JSFunction* callee;
- ptrdiff_t firstParameterIndex;
- Register* argv;
- int numArguments;
- getArgumentsData(callFrame, callee, firstParameterIndex, argv, numArguments);
-
- d->numParameters = callee->jsExecutable()->parameterCount();
- d->firstParameterIndex = firstParameterIndex;
- d->numArguments = numArguments;
-
- d->activation = 0;
- d->registers = callFrame->registers();
-
- Register* extraArguments;
- if (d->numArguments <= d->numParameters)
- extraArguments = 0;
- else {
- unsigned numExtraArguments = d->numArguments - d->numParameters;
- if (numExtraArguments > sizeof(d->extraArgumentsFixedBuffer) / sizeof(Register))
- extraArguments = new Register[numExtraArguments];
- else
- extraArguments = d->extraArgumentsFixedBuffer;
- for (unsigned i = 0; i < numExtraArguments; ++i)
- extraArguments[i] = argv[d->numParameters + i];
- }
-
- d->extraArguments = extraArguments;
-
- d->callee = callee;
- d->overrodeLength = false;
- d->overrodeCallee = false;
+ Arguments* arguments = new (NotNull, allocateCell<Arguments>(vm.heap)) Arguments(callFrame);
+ arguments->finishCreation(callFrame, inlineCallFrame);
+ return arguments;
}
- inline Arguments::Arguments(CallFrame* callFrame, NoParametersType)
- : JSObject(callFrame->lexicalGlobalObject()->argumentsStructure())
- , d(new ArgumentsData)
- {
- ASSERT(!callFrame->callee()->jsExecutable()->parameterCount());
-
- unsigned numArguments = callFrame->argumentCount() - 1;
-
- d->numParameters = 0;
- d->numArguments = numArguments;
- d->activation = 0;
+ enum { MaxArguments = 0x10000 };
- Register* extraArguments;
- if (numArguments > sizeof(d->extraArgumentsFixedBuffer) / sizeof(Register))
- extraArguments = new Register[numArguments];
- else
- extraArguments = d->extraArgumentsFixedBuffer;
+private:
+ enum NoParametersType { NoParameters };
+
+ Arguments(CallFrame*);
+ Arguments(CallFrame*, NoParametersType);
+
+ void tearOffForInlineCallFrame(VM& vm, Register*, InlineCallFrame*);
- Register* argv = callFrame->registers() - RegisterFile::CallFrameHeaderSize - numArguments - 1;
- for (unsigned i = 0; i < numArguments; ++i)
- extraArguments[i] = argv[i];
+public:
+ static const ClassInfo s_info;
- d->extraArguments = extraArguments;
+ static void visitChildren(JSCell*, SlotVisitor&);
- d->callee = callFrame->callee();
- d->overrodeLength = false;
- d->overrodeCallee = false;
- }
+ void fillArgList(ExecState*, MarkedArgumentBuffer&);
- inline void Arguments::copyRegisters()
+ uint32_t length(ExecState* exec) const
{
- ASSERT(!isTornOff());
-
- if (!d->numParameters)
- return;
-
- int registerOffset = d->numParameters + RegisterFile::CallFrameHeaderSize;
- size_t registerArraySize = d->numParameters;
-
- Register* registerArray = new Register[registerArraySize];
- memcpy(registerArray, d->registers - registerOffset, registerArraySize * sizeof(Register));
- d->registerArray.set(registerArray);
- d->registers = registerArray + registerOffset;
+ if (UNLIKELY(m_overrodeLength))
+ return get(exec, exec->propertyNames().length).toUInt32(exec);
+ return m_numArguments;
}
-
- // This JSActivation function is defined here so it can get at Arguments::setRegisters.
- inline void JSActivation::copyRegisters(Arguments* arguments)
- {
- ASSERT(!d()->registerArray);
-
- size_t numParametersMinusThis = d()->functionExecutable->generatedBytecode().m_numParameters - 1;
- size_t numVars = d()->functionExecutable->generatedBytecode().m_numVars;
- size_t numLocals = numVars + numParametersMinusThis;
-
- if (!numLocals)
- return;
-
- int registerOffset = numParametersMinusThis + RegisterFile::CallFrameHeaderSize;
- size_t registerArraySize = numLocals + RegisterFile::CallFrameHeaderSize;
-
- Register* registerArray = copyRegisterArray(d()->registers - registerOffset, registerArraySize);
- setRegisters(registerArray + registerOffset, registerArray);
- if (arguments && !arguments->isTornOff())
- static_cast<Arguments*>(arguments)->setActivation(this);
+
+ void copyToArguments(ExecState*, CallFrame*, uint32_t length);
+ void tearOff(CallFrame*);
+ void tearOff(CallFrame*, InlineCallFrame*);
+ bool isTornOff() const { return m_registerArray; }
+ void didTearOffActivation(ExecState*, JSActivation*);
+
+ static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
+ {
+ return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), &s_info);
}
-
- ALWAYS_INLINE Arguments* Register::arguments() const
- {
- if (jsValue() == JSValue())
- return 0;
- return asArguments(jsValue());
+
+protected:
+ static const unsigned StructureFlags = OverridesGetOwnPropertySlot | InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero | OverridesVisitChildren | OverridesGetPropertyNames | JSObject::StructureFlags;
+
+ void finishCreation(CallFrame*);
+ void finishCreation(CallFrame*, InlineCallFrame*);
+
+private:
+ static void destroy(JSCell*);
+ static bool getOwnPropertySlot(JSCell*, ExecState*, PropertyName, PropertySlot&);
+ static bool getOwnPropertySlotByIndex(JSCell*, ExecState*, unsigned propertyName, PropertySlot&);
+ static bool getOwnPropertyDescriptor(JSObject*, ExecState*, PropertyName, PropertyDescriptor&);
+ static void getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
+ static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
+ static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow);
+ static bool deleteProperty(JSCell*, ExecState*, PropertyName);
+ static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName);
+ static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, PropertyDescriptor&, bool shouldThrow);
+ void createStrictModeCallerIfNecessary(ExecState*);
+ void createStrictModeCalleeIfNecessary(ExecState*);
+
+ bool isArgument(size_t);
+ bool trySetArgument(VM&, size_t argument, JSValue);
+ JSValue tryGetArgument(size_t argument);
+ bool isDeletedArgument(size_t);
+ bool tryDeleteArgument(size_t);
+ WriteBarrierBase<Unknown>& argument(size_t);
+ void allocateSlowArguments();
+
+ void init(CallFrame*);
+
+ WriteBarrier<JSActivation> m_activation;
+
+ unsigned m_numArguments;
+
+ // We make these full byte booleans to make them easy to test from the JIT,
+ // and because even if they were single-bit booleans we still wouldn't save
+ // any space.
+ bool m_overrodeLength;
+ bool m_overrodeCallee;
+ bool m_overrodeCaller;
+ bool m_isStrictMode;
+
+ WriteBarrierBase<Unknown>* m_registers;
+ OwnArrayPtr<WriteBarrier<Unknown> > m_registerArray;
+
+ OwnArrayPtr<SlowArgument> m_slowArguments;
+
+ WriteBarrier<JSFunction> m_callee;
+};
+
+Arguments* asArguments(JSValue);
+
+inline Arguments* asArguments(JSValue value)
+{
+ ASSERT(asObject(value)->inherits(&Arguments::s_info));
+ return static_cast<Arguments*>(asObject(value));
+}
+
+inline Arguments::Arguments(CallFrame* callFrame)
+ : JSDestructibleObject(callFrame->vm(), callFrame->lexicalGlobalObject()->argumentsStructure())
+{
+}
+
+inline Arguments::Arguments(CallFrame* callFrame, NoParametersType)
+ : JSDestructibleObject(callFrame->vm(), callFrame->lexicalGlobalObject()->argumentsStructure())
+{
+}
+
+inline void Arguments::allocateSlowArguments()
+{
+ if (m_slowArguments)
+ return;
+ m_slowArguments = adoptArrayPtr(new SlowArgument[m_numArguments]);
+ for (size_t i = 0; i < m_numArguments; ++i) {
+ ASSERT(m_slowArguments[i].status == SlowArgument::Normal);
+ m_slowArguments[i].index = CallFrame::argumentOffset(i);
}
-
+}
+
+inline bool Arguments::tryDeleteArgument(size_t argument)
+{
+ if (!isArgument(argument))
+ return false;
+ allocateSlowArguments();
+ m_slowArguments[argument].status = SlowArgument::Deleted;
+ return true;
+}
+
+inline bool Arguments::trySetArgument(VM& vm, size_t argument, JSValue value)
+{
+ if (!isArgument(argument))
+ return false;
+ this->argument(argument).set(vm, this, value);
+ return true;
+}
+
+inline JSValue Arguments::tryGetArgument(size_t argument)
+{
+ if (!isArgument(argument))
+ return JSValue();
+ return this->argument(argument).get();
+}
+
+inline bool Arguments::isDeletedArgument(size_t argument)
+{
+ if (argument >= m_numArguments)
+ return false;
+ if (!m_slowArguments)
+ return false;
+ if (m_slowArguments[argument].status != SlowArgument::Deleted)
+ return false;
+ return true;
+}
+
+inline bool Arguments::isArgument(size_t argument)
+{
+ if (argument >= m_numArguments)
+ return false;
+ if (m_slowArguments && m_slowArguments[argument].status == SlowArgument::Deleted)
+ return false;
+ return true;
+}
+
+inline WriteBarrierBase<Unknown>& Arguments::argument(size_t argument)
+{
+ ASSERT(isArgument(argument));
+ if (!m_slowArguments)
+ return m_registers[CallFrame::argumentOffset(argument)];
+
+ int index = m_slowArguments[argument].index;
+ if (!m_activation || m_slowArguments[argument].status != SlowArgument::Captured)
+ return m_registers[index];
+
+ return m_activation->registerAt(index);
+}
+
+inline void Arguments::finishCreation(CallFrame* callFrame)
+{
+ Base::finishCreation(callFrame->vm());
+ ASSERT(inherits(&s_info));
+
+ JSFunction* callee = jsCast<JSFunction*>(callFrame->callee());
+ m_numArguments = callFrame->argumentCount();
+ m_registers = reinterpret_cast<WriteBarrierBase<Unknown>*>(callFrame->registers());
+ m_callee.set(callFrame->vm(), this, callee);
+ m_overrodeLength = false;
+ m_overrodeCallee = false;
+ m_overrodeCaller = false;
+ m_isStrictMode = callFrame->codeBlock()->isStrictMode();
+
+ SharedSymbolTable* symbolTable = callFrame->codeBlock()->symbolTable();
+ const SlowArgument* slowArguments = symbolTable->slowArguments();
+ if (slowArguments) {
+ allocateSlowArguments();
+ size_t count = std::min<unsigned>(m_numArguments, symbolTable->parameterCount());
+ for (size_t i = 0; i < count; ++i)
+ m_slowArguments[i] = slowArguments[i];
+ }
+
+ // The bytecode generator omits op_tear_off_activation in cases of no
+ // declared parameters, so we need to tear off immediately.
+ if (m_isStrictMode || !callee->jsExecutable()->parameterCount())
+ tearOff(callFrame);
+}
+
+inline void Arguments::finishCreation(CallFrame* callFrame, InlineCallFrame* inlineCallFrame)
+{
+ Base::finishCreation(callFrame->vm());
+ ASSERT(inherits(&s_info));
+
+ JSFunction* callee = inlineCallFrame->calleeForCallFrame(callFrame);
+ m_numArguments = inlineCallFrame->arguments.size() - 1;
+ m_registers = reinterpret_cast<WriteBarrierBase<Unknown>*>(callFrame->registers()) + inlineCallFrame->stackOffset;
+ m_callee.set(callFrame->vm(), this, callee);
+ m_overrodeLength = false;
+ m_overrodeCallee = false;
+ m_overrodeCaller = false;
+ m_isStrictMode = jsCast<FunctionExecutable*>(inlineCallFrame->executable.get())->isStrictMode();
+ ASSERT(!jsCast<FunctionExecutable*>(inlineCallFrame->executable.get())->symbolTable(inlineCallFrame->isCall ? CodeForCall : CodeForConstruct)->slowArguments());
+
+ // The bytecode generator omits op_tear_off_activation in cases of no
+ // declared parameters, so we need to tear off immediately.
+ if (m_isStrictMode || !callee->jsExecutable()->parameterCount())
+ tearOff(callFrame, inlineCallFrame);
+}
} // namespace JSC