1 | /*
|
---|
2 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2003, 2006, 2007, 2008 Apple Inc. All rights reserved.
|
---|
4 | * Copyright (C) 2007 Cameron Zwarich ([email protected])
|
---|
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 "Machine.h"
|
---|
31 |
|
---|
32 | namespace JSC {
|
---|
33 |
|
---|
34 | struct ArgumentsData : Noncopyable {
|
---|
35 | JSActivation* activation;
|
---|
36 |
|
---|
37 | unsigned numParameters;
|
---|
38 | int firstParameterIndex;
|
---|
39 | unsigned numArguments;
|
---|
40 |
|
---|
41 | Register* registers;
|
---|
42 | OwnArrayPtr<Register> registerArray;
|
---|
43 |
|
---|
44 | Register* extraArguments;
|
---|
45 | OwnArrayPtr<bool> deletedArguments;
|
---|
46 | Register extraArgumentsFixedBuffer[4];
|
---|
47 |
|
---|
48 | JSFunction* callee;
|
---|
49 | bool overrodeLength : 1;
|
---|
50 | bool overrodeCallee : 1;
|
---|
51 | };
|
---|
52 |
|
---|
53 |
|
---|
54 | class Arguments : public JSObject {
|
---|
55 | public:
|
---|
56 | Arguments(ExecState*, Register* callFrame);
|
---|
57 | Arguments(ExecState*, JSActivation*);
|
---|
58 | virtual ~Arguments();
|
---|
59 |
|
---|
60 | static const ClassInfo info;
|
---|
61 |
|
---|
62 | virtual void mark();
|
---|
63 |
|
---|
64 | void fillArgList(ExecState*, ArgList&);
|
---|
65 |
|
---|
66 | void copyRegisters();
|
---|
67 | void setRegisters(Register* registers) { d->registers = registers; }
|
---|
68 |
|
---|
69 | private:
|
---|
70 | virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
71 | virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
72 | virtual void put(ExecState*, const Identifier& propertyName, JSValue*, PutPropertySlot&);
|
---|
73 | virtual void put(ExecState*, unsigned propertyName, JSValue*, PutPropertySlot&);
|
---|
74 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
75 | virtual bool deleteProperty(ExecState*, unsigned propertyName);
|
---|
76 |
|
---|
77 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
78 |
|
---|
79 | void init(ExecState*, Register* callFrame);
|
---|
80 |
|
---|
81 | OwnPtr<ArgumentsData> d;
|
---|
82 | };
|
---|
83 |
|
---|
84 | inline void Arguments::init(ExecState* exec, Register* callFrame)
|
---|
85 | {
|
---|
86 | JSFunction* callee;
|
---|
87 | int firstParameterIndex;
|
---|
88 | Register* argv;
|
---|
89 | int numArguments;
|
---|
90 | exec->machine()->getArgumentsData(callFrame, callee, firstParameterIndex, argv, numArguments);
|
---|
91 |
|
---|
92 | d->numParameters = callee->m_body->parameterCount();
|
---|
93 | d->firstParameterIndex = firstParameterIndex;
|
---|
94 | d->numArguments = numArguments;
|
---|
95 |
|
---|
96 | d->registers = callFrame;
|
---|
97 |
|
---|
98 | Register* extraArguments;
|
---|
99 | if (d->numArguments <= d->numParameters)
|
---|
100 | extraArguments = 0;
|
---|
101 | else {
|
---|
102 | unsigned numExtraArguments = d->numArguments - d->numParameters;
|
---|
103 | if (numExtraArguments > sizeof(d->extraArgumentsFixedBuffer) / sizeof(Register))
|
---|
104 | extraArguments = new Register[numExtraArguments];
|
---|
105 | else
|
---|
106 | extraArguments = d->extraArgumentsFixedBuffer;
|
---|
107 | for (unsigned i = 0; i < numExtraArguments; ++i)
|
---|
108 | extraArguments[i] = argv[d->numParameters + i];
|
---|
109 | }
|
---|
110 |
|
---|
111 | d->extraArguments = extraArguments;
|
---|
112 |
|
---|
113 | d->callee = callee;
|
---|
114 | d->overrodeLength = false;
|
---|
115 | d->overrodeCallee = false;
|
---|
116 | }
|
---|
117 |
|
---|
118 | inline Arguments::Arguments(ExecState* exec, Register* callFrame)
|
---|
119 | : JSObject(exec->lexicalGlobalObject()->argumentsStructure())
|
---|
120 | , d(new ArgumentsData)
|
---|
121 | {
|
---|
122 | d->activation = 0;
|
---|
123 | init(exec, callFrame);
|
---|
124 | }
|
---|
125 |
|
---|
126 | inline Arguments::Arguments(ExecState* exec, JSActivation* activation)
|
---|
127 | : JSObject(exec->lexicalGlobalObject()->argumentsStructure())
|
---|
128 | , d(new ArgumentsData)
|
---|
129 | {
|
---|
130 | ASSERT(activation);
|
---|
131 | d->activation = activation;
|
---|
132 | init(exec, &activation->registerAt(0));
|
---|
133 | }
|
---|
134 |
|
---|
135 | inline void Arguments::copyRegisters()
|
---|
136 | {
|
---|
137 | ASSERT(!d->activation);
|
---|
138 | ASSERT(!d->registerArray);
|
---|
139 |
|
---|
140 | if (!d->numParameters)
|
---|
141 | return;
|
---|
142 |
|
---|
143 | int registerOffset = d->numParameters + RegisterFile::CallFrameHeaderSize;
|
---|
144 | size_t registerArraySize = d->numParameters;
|
---|
145 |
|
---|
146 | Register* registerArray = new Register[registerArraySize];
|
---|
147 | memcpy(registerArray, d->registers - registerOffset, registerArraySize * sizeof(Register));
|
---|
148 | d->registerArray.set(registerArray);
|
---|
149 | d->registers = registerArray + registerOffset;
|
---|
150 | }
|
---|
151 |
|
---|
152 | // This JSActivation function is defined here so it can get at Arguments::setRegisters.
|
---|
153 | inline void JSActivation::copyRegisters(JSValue* arguments)
|
---|
154 | {
|
---|
155 | ASSERT(!d()->registerArray);
|
---|
156 |
|
---|
157 | size_t numParametersMinusThis = d()->functionBody->generatedByteCode().numParameters - 1;
|
---|
158 | size_t numVars = d()->functionBody->generatedByteCode().numVars;
|
---|
159 | size_t numLocals = numVars + numParametersMinusThis;
|
---|
160 |
|
---|
161 | if (!numLocals)
|
---|
162 | return;
|
---|
163 |
|
---|
164 | int registerOffset = numParametersMinusThis + RegisterFile::CallFrameHeaderSize;
|
---|
165 | size_t registerArraySize = numLocals + RegisterFile::CallFrameHeaderSize;
|
---|
166 |
|
---|
167 | Register* registerArray = copyRegisterArray(d()->registers - registerOffset, registerArraySize);
|
---|
168 | setRegisters(registerArray + registerOffset, registerArray);
|
---|
169 | if (arguments) {
|
---|
170 | ASSERT(arguments->isObject(&Arguments::info));
|
---|
171 | static_cast<Arguments*>(arguments)->setRegisters(registerArray + registerOffset);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | } // namespace JSC
|
---|
176 |
|
---|
177 | #endif // Arguments_h
|
---|