JavaScriptCore/runtime/Arguments.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
       
     3  *  Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
       
     4  *  Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
       
     5  *  Copyright (C) 2007 Maks Orlovich
       
     6  *
       
     7  *  This library is free software; you can redistribute it and/or
       
     8  *  modify it under the terms of the GNU Library General Public
       
     9  *  License as published by the Free Software Foundation; either
       
    10  *  version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  *  This library is distributed in the hope that it will be useful,
       
    13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    15  *  Library General Public License for more details.
       
    16  *
       
    17  *  You should have received a copy of the GNU Library General Public License
       
    18  *  along with this library; see the file COPYING.LIB.  If not, write to
       
    19  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    20  *  Boston, MA 02110-1301, USA.
       
    21  *
       
    22  */
       
    23 
       
    24 #ifndef Arguments_h
       
    25 #define Arguments_h
       
    26 
       
    27 #include "JSActivation.h"
       
    28 #include "JSFunction.h"
       
    29 #include "JSGlobalObject.h"
       
    30 #include "Interpreter.h"
       
    31 #include "ObjectConstructor.h"
       
    32 #include "PrototypeFunction.h"
       
    33 
       
    34 namespace JSC {
       
    35 
       
    36     struct ArgumentsData : Noncopyable {
       
    37         JSActivation* activation;
       
    38 
       
    39         unsigned numParameters;
       
    40         ptrdiff_t firstParameterIndex;
       
    41         unsigned numArguments;
       
    42 
       
    43         Register* registers;
       
    44         OwnArrayPtr<Register> registerArray;
       
    45 
       
    46         Register* extraArguments;
       
    47         OwnArrayPtr<bool> deletedArguments;
       
    48         Register extraArgumentsFixedBuffer[4];
       
    49 
       
    50         JSFunction* callee;
       
    51         bool overrodeLength : 1;
       
    52         bool overrodeCallee : 1;
       
    53     };
       
    54 
       
    55 
       
    56     class Arguments : public JSObject {
       
    57     public:
       
    58         // Use an enum because otherwise gcc insists on doing a memory
       
    59         // read.
       
    60         enum { MaxArguments = 0x10000 };
       
    61 
       
    62         enum NoParametersType { NoParameters };
       
    63 
       
    64         Arguments(CallFrame*);
       
    65         Arguments(CallFrame*, NoParametersType);
       
    66         virtual ~Arguments();
       
    67 
       
    68         static const ClassInfo info;
       
    69 
       
    70         virtual void markChildren(MarkStack&);
       
    71 
       
    72         void fillArgList(ExecState*, MarkedArgumentBuffer&);
       
    73 
       
    74         uint32_t numProvidedArguments(ExecState* exec) const 
       
    75         {
       
    76             if (UNLIKELY(d->overrodeLength))
       
    77                 return get(exec, exec->propertyNames().length).toUInt32(exec);
       
    78             return d->numArguments; 
       
    79         }
       
    80         
       
    81         void copyToRegisters(ExecState* exec, Register* buffer, uint32_t maxSize);
       
    82         void copyRegisters();
       
    83         bool isTornOff() const { return d->registerArray; }
       
    84         void setActivation(JSActivation* activation)
       
    85         {
       
    86             d->activation = activation;
       
    87             d->registers = &activation->registerAt(0);
       
    88         }
       
    89 
       
    90         static PassRefPtr<Structure> createStructure(JSValue prototype) 
       
    91         { 
       
    92             return Structure::create(prototype, TypeInfo(ObjectType, StructureFlags), AnonymousSlotCount); 
       
    93         }
       
    94 
       
    95     protected:
       
    96         static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesMarkChildren | OverridesGetPropertyNames | JSObject::StructureFlags;
       
    97 
       
    98     private:
       
    99         void getArgumentsData(CallFrame*, JSFunction*&, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc);
       
   100         virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
       
   101         virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
       
   102         virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
       
   103         virtual void getOwnPropertyNames(ExecState*, PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
       
   104         virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
       
   105         virtual void put(ExecState*, unsigned propertyName, JSValue, PutPropertySlot&);
       
   106         virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
       
   107         virtual bool deleteProperty(ExecState*, unsigned propertyName);
       
   108 
       
   109         virtual const ClassInfo* classInfo() const { return &info; }
       
   110 
       
   111         void init(CallFrame*);
       
   112 
       
   113         OwnPtr<ArgumentsData> d;
       
   114     };
       
   115 
       
   116     Arguments* asArguments(JSValue);
       
   117 
       
   118     inline Arguments* asArguments(JSValue value)
       
   119     {
       
   120         ASSERT(asObject(value)->inherits(&Arguments::info));
       
   121         return static_cast<Arguments*>(asObject(value));
       
   122     }
       
   123 
       
   124     ALWAYS_INLINE void Arguments::getArgumentsData(CallFrame* callFrame, JSFunction*& function, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc)
       
   125     {
       
   126         function = asFunction(callFrame->callee());
       
   127 
       
   128         int numParameters = function->jsExecutable()->parameterCount();
       
   129         argc = callFrame->argumentCountIncludingThis();
       
   130 
       
   131         if (argc <= numParameters)
       
   132             argv = callFrame->registers() - RegisterFile::CallFrameHeaderSize - numParameters;
       
   133         else
       
   134             argv = callFrame->registers() - RegisterFile::CallFrameHeaderSize - numParameters - argc;
       
   135 
       
   136         argc -= 1; // - 1 to skip "this"
       
   137         firstParameterIndex = -RegisterFile::CallFrameHeaderSize - numParameters;
       
   138     }
       
   139 
       
   140     inline Arguments::Arguments(CallFrame* callFrame)
       
   141         : JSObject(callFrame->lexicalGlobalObject()->argumentsStructure())
       
   142         , d(adoptPtr(new ArgumentsData))
       
   143     {
       
   144         JSFunction* callee;
       
   145         ptrdiff_t firstParameterIndex;
       
   146         Register* argv;
       
   147         int numArguments;
       
   148         getArgumentsData(callFrame, callee, firstParameterIndex, argv, numArguments);
       
   149 
       
   150         d->numParameters = callee->jsExecutable()->parameterCount();
       
   151         d->firstParameterIndex = firstParameterIndex;
       
   152         d->numArguments = numArguments;
       
   153 
       
   154         d->activation = 0;
       
   155         d->registers = callFrame->registers();
       
   156 
       
   157         Register* extraArguments;
       
   158         if (d->numArguments <= d->numParameters)
       
   159             extraArguments = 0;
       
   160         else {
       
   161             unsigned numExtraArguments = d->numArguments - d->numParameters;
       
   162             if (numExtraArguments > sizeof(d->extraArgumentsFixedBuffer) / sizeof(Register))
       
   163                 extraArguments = new Register[numExtraArguments];
       
   164             else
       
   165                 extraArguments = d->extraArgumentsFixedBuffer;
       
   166             for (unsigned i = 0; i < numExtraArguments; ++i)
       
   167                 extraArguments[i] = argv[d->numParameters + i];
       
   168         }
       
   169 
       
   170         d->extraArguments = extraArguments;
       
   171 
       
   172         d->callee = callee;
       
   173         d->overrodeLength = false;
       
   174         d->overrodeCallee = false;
       
   175     }
       
   176 
       
   177     inline Arguments::Arguments(CallFrame* callFrame, NoParametersType)
       
   178         : JSObject(callFrame->lexicalGlobalObject()->argumentsStructure())
       
   179         , d(adoptPtr(new ArgumentsData))
       
   180     {
       
   181         ASSERT(!asFunction(callFrame->callee())->jsExecutable()->parameterCount());
       
   182 
       
   183         unsigned numArguments = callFrame->argumentCount();
       
   184 
       
   185         d->numParameters = 0;
       
   186         d->numArguments = numArguments;
       
   187         d->activation = 0;
       
   188 
       
   189         Register* extraArguments;
       
   190         if (numArguments > sizeof(d->extraArgumentsFixedBuffer) / sizeof(Register))
       
   191             extraArguments = new Register[numArguments];
       
   192         else
       
   193             extraArguments = d->extraArgumentsFixedBuffer;
       
   194 
       
   195         Register* argv = callFrame->registers() - RegisterFile::CallFrameHeaderSize - numArguments - 1;
       
   196         for (unsigned i = 0; i < numArguments; ++i)
       
   197             extraArguments[i] = argv[i];
       
   198 
       
   199         d->extraArguments = extraArguments;
       
   200 
       
   201         d->callee = asFunction(callFrame->callee());
       
   202         d->overrodeLength = false;
       
   203         d->overrodeCallee = false;
       
   204     }
       
   205 
       
   206     inline void Arguments::copyRegisters()
       
   207     {
       
   208         ASSERT(!isTornOff());
       
   209 
       
   210         if (!d->numParameters)
       
   211             return;
       
   212 
       
   213         int registerOffset = d->numParameters + RegisterFile::CallFrameHeaderSize;
       
   214         size_t registerArraySize = d->numParameters;
       
   215 
       
   216         Register* registerArray = new Register[registerArraySize];
       
   217         memcpy(registerArray, d->registers - registerOffset, registerArraySize * sizeof(Register));
       
   218         d->registerArray.set(registerArray);
       
   219         d->registers = registerArray + registerOffset;
       
   220     }
       
   221 
       
   222     // This JSActivation function is defined here so it can get at Arguments::setRegisters.
       
   223     inline void JSActivation::copyRegisters()
       
   224     {
       
   225         ASSERT(!d()->registerArray);
       
   226 
       
   227         size_t numParametersMinusThis = d()->functionExecutable->parameterCount();
       
   228         size_t numVars = d()->functionExecutable->variableCount();
       
   229         size_t numLocals = numVars + numParametersMinusThis;
       
   230 
       
   231         if (!numLocals)
       
   232             return;
       
   233 
       
   234         int registerOffset = numParametersMinusThis + RegisterFile::CallFrameHeaderSize;
       
   235         size_t registerArraySize = numLocals + RegisterFile::CallFrameHeaderSize;
       
   236 
       
   237         Register* registerArray = copyRegisterArray(d()->registers - registerOffset, registerArraySize);
       
   238         setRegisters(registerArray + registerOffset, registerArray);
       
   239     }
       
   240 
       
   241 } // namespace JSC
       
   242 
       
   243 #endif // Arguments_h