source: webkit/trunk/JavaScriptCore/runtime/Arguments.h@ 49323

Last change on this file since 49323 was 47780, checked in by [email protected], 16 years ago

[ES5] Implement getOwnPropertyDescriptor
https://bugs.webkit.org/show_bug.cgi?id=28724

Reviewed by Gavin Barraclough.

JavaScriptCore:
Implement the core runtime support for getOwnPropertyDescriptor.
This adds a virtual getOwnPropertyDescriptor method to every class
that implements getOwnPropertySlot that shadows the behaviour of
getOwnPropertySlot. The alternative would be to make getOwnPropertySlot
(or PropertySlots in general) provide property attribute information,
but quick testing showed this to be a regression.

WebCore:
Implement the WebCore side of getOwnPropertyDescriptor. This
requires a custom implementation of getOwnPropertyDescriptor
for every class with a custom implementation of getOwnPropertySlot.

The bindings generator has been updated to generate appropriate
versions of getOwnPropertyDescriptor for the general case where
a custom getOwnPropertyDescriptor is not needed. ES5 is vague
about how getOwnPropertyDescriptor should work in the context of
"host" functions with polymorphic GetOwnProperty, so it seems
okay that occasionally we "guess" what attributes -- eg. determining
whether a property is writable.

Test: fast/js/getOwnPropertyDescriptor.html

  • Property svn:eol-style set to native
File size: 8.6 KB
Line 
1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
3 * Copyright (C) 2003, 2006, 2007, 2008, 2009 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 "Interpreter.h"
31#include "ObjectConstructor.h"
32#include "PrototypeFunction.h"
33
34namespace 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 enum NoParametersType { NoParameters };
59
60 Arguments(CallFrame*);
61 Arguments(CallFrame*, NoParametersType);
62 virtual ~Arguments();
63
64 static const ClassInfo info;
65
66 virtual void markChildren(MarkStack&);
67
68 void fillArgList(ExecState*, MarkedArgumentBuffer&);
69
70 uint32_t numProvidedArguments(ExecState* exec) const
71 {
72 if (UNLIKELY(d->overrodeLength))
73 return get(exec, exec->propertyNames().length).toUInt32(exec);
74 return d->numArguments;
75 }
76
77 void copyToRegisters(ExecState* exec, Register* buffer, uint32_t maxSize);
78 void copyRegisters();
79 bool isTornOff() const { return d->registerArray; }
80 void setActivation(JSActivation* activation)
81 {
82 d->activation = activation;
83 d->registers = &activation->registerAt(0);
84 }
85
86 static PassRefPtr<Structure> createStructure(JSValue prototype)
87 {
88 return Structure::create(prototype, TypeInfo(ObjectType));
89 }
90
91 private:
92 void getArgumentsData(CallFrame*, JSFunction*&, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc);
93 virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
94 virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
95 virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
96 virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
97 virtual void put(ExecState*, unsigned propertyName, JSValue, PutPropertySlot&);
98 virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
99 virtual bool deleteProperty(ExecState*, unsigned propertyName);
100
101 virtual const ClassInfo* classInfo() const { return &info; }
102
103 void init(CallFrame*);
104
105 OwnPtr<ArgumentsData> d;
106 };
107
108 Arguments* asArguments(JSValue);
109
110 inline Arguments* asArguments(JSValue value)
111 {
112 ASSERT(asObject(value)->inherits(&Arguments::info));
113 return static_cast<Arguments*>(asObject(value));
114 }
115
116 ALWAYS_INLINE void Arguments::getArgumentsData(CallFrame* callFrame, JSFunction*& function, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc)
117 {
118 function = callFrame->callee();
119
120 int numParameters = function->jsExecutable()->parameterCount();
121 argc = callFrame->argumentCount();
122
123 if (argc <= numParameters)
124 argv = callFrame->registers() - RegisterFile::CallFrameHeaderSize - numParameters;
125 else
126 argv = callFrame->registers() - RegisterFile::CallFrameHeaderSize - numParameters - argc;
127
128 argc -= 1; // - 1 to skip "this"
129 firstParameterIndex = -RegisterFile::CallFrameHeaderSize - numParameters;
130 }
131
132 inline Arguments::Arguments(CallFrame* callFrame)
133 : JSObject(callFrame->lexicalGlobalObject()->argumentsStructure())
134 , d(new ArgumentsData)
135 {
136 JSFunction* callee;
137 ptrdiff_t firstParameterIndex;
138 Register* argv;
139 int numArguments;
140 getArgumentsData(callFrame, callee, firstParameterIndex, argv, numArguments);
141
142 d->numParameters = callee->jsExecutable()->parameterCount();
143 d->firstParameterIndex = firstParameterIndex;
144 d->numArguments = numArguments;
145
146 d->activation = 0;
147 d->registers = callFrame->registers();
148
149 Register* extraArguments;
150 if (d->numArguments <= d->numParameters)
151 extraArguments = 0;
152 else {
153 unsigned numExtraArguments = d->numArguments - d->numParameters;
154 if (numExtraArguments > sizeof(d->extraArgumentsFixedBuffer) / sizeof(Register))
155 extraArguments = new Register[numExtraArguments];
156 else
157 extraArguments = d->extraArgumentsFixedBuffer;
158 for (unsigned i = 0; i < numExtraArguments; ++i)
159 extraArguments[i] = argv[d->numParameters + i];
160 }
161
162 d->extraArguments = extraArguments;
163
164 d->callee = callee;
165 d->overrodeLength = false;
166 d->overrodeCallee = false;
167 }
168
169 inline Arguments::Arguments(CallFrame* callFrame, NoParametersType)
170 : JSObject(callFrame->lexicalGlobalObject()->argumentsStructure())
171 , d(new ArgumentsData)
172 {
173 ASSERT(!callFrame->callee()->jsExecutable()->parameterCount());
174
175 unsigned numArguments = callFrame->argumentCount() - 1;
176
177 d->numParameters = 0;
178 d->numArguments = numArguments;
179 d->activation = 0;
180
181 Register* extraArguments;
182 if (numArguments > sizeof(d->extraArgumentsFixedBuffer) / sizeof(Register))
183 extraArguments = new Register[numArguments];
184 else
185 extraArguments = d->extraArgumentsFixedBuffer;
186
187 Register* argv = callFrame->registers() - RegisterFile::CallFrameHeaderSize - numArguments - 1;
188 for (unsigned i = 0; i < numArguments; ++i)
189 extraArguments[i] = argv[i];
190
191 d->extraArguments = extraArguments;
192
193 d->callee = callFrame->callee();
194 d->overrodeLength = false;
195 d->overrodeCallee = false;
196 }
197
198 inline void Arguments::copyRegisters()
199 {
200 ASSERT(!isTornOff());
201
202 if (!d->numParameters)
203 return;
204
205 int registerOffset = d->numParameters + RegisterFile::CallFrameHeaderSize;
206 size_t registerArraySize = d->numParameters;
207
208 Register* registerArray = new Register[registerArraySize];
209 memcpy(registerArray, d->registers - registerOffset, registerArraySize * sizeof(Register));
210 d->registerArray.set(registerArray);
211 d->registers = registerArray + registerOffset;
212 }
213
214 // This JSActivation function is defined here so it can get at Arguments::setRegisters.
215 inline void JSActivation::copyRegisters(Arguments* arguments)
216 {
217 ASSERT(!d()->registerArray);
218
219 size_t numParametersMinusThis = d()->functionExecutable->generatedBytecode().m_numParameters - 1;
220 size_t numVars = d()->functionExecutable->generatedBytecode().m_numVars;
221 size_t numLocals = numVars + numParametersMinusThis;
222
223 if (!numLocals)
224 return;
225
226 int registerOffset = numParametersMinusThis + RegisterFile::CallFrameHeaderSize;
227 size_t registerArraySize = numLocals + RegisterFile::CallFrameHeaderSize;
228
229 Register* registerArray = copyRegisterArray(d()->registers - registerOffset, registerArraySize);
230 setRegisters(registerArray + registerOffset, registerArray);
231 if (arguments && !arguments->isTornOff())
232 static_cast<Arguments*>(arguments)->setActivation(this);
233 }
234
235 ALWAYS_INLINE Arguments* Register::arguments() const
236 {
237 if (jsValue() == JSValue())
238 return 0;
239 return asArguments(jsValue());
240 }
241
242
243} // namespace JSC
244
245#endif // Arguments_h
Note: See TracBrowser for help on using the repository browser.