]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/JSFunction.cpp
JavaScriptCore-584.tar.gz
[apple/javascriptcore.git] / runtime / JSFunction.cpp
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
f9bf01c6 4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
9dae56ea
A
5 * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
6 * Copyright (C) 2007 Maks Orlovich
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 */
24
25#include "config.h"
26#include "JSFunction.h"
27
28#include "CodeBlock.h"
29#include "CommonIdentifiers.h"
30#include "CallFrame.h"
31#include "FunctionPrototype.h"
32#include "JSGlobalObject.h"
33#include "Interpreter.h"
34#include "ObjectPrototype.h"
35#include "Parser.h"
36#include "PropertyNameArray.h"
37#include "ScopeChainMark.h"
38
39using namespace WTF;
40using namespace Unicode;
41
42namespace JSC {
43
44ASSERT_CLASS_FITS_IN_CELL(JSFunction);
45
46const ClassInfo JSFunction::info = { "Function", &InternalFunction::info, 0, 0 };
47
f9bf01c6
A
48bool JSFunction::isHostFunctionNonInline() const
49{
50 return isHostFunction();
51}
52
53JSFunction::JSFunction(NonNullPassRefPtr<Structure> structure)
54 : Base(structure)
55 , m_executable(adoptRef(new VPtrHackExecutable()))
56{
57}
58
59JSFunction::JSFunction(ExecState* exec, NonNullPassRefPtr<Structure> structure, int length, const Identifier& name, NativeFunction func)
ba379fdc
A
60 : Base(&exec->globalData(), structure, name)
61#if ENABLE(JIT)
f9bf01c6 62 , m_executable(adoptRef(new NativeExecutable(exec)))
ba379fdc
A
63#endif
64{
65#if ENABLE(JIT)
66 setNativeFunction(func);
67 putDirect(exec->propertyNames().length, jsNumber(exec, length), DontDelete | ReadOnly | DontEnum);
68#else
69 UNUSED_PARAM(length);
70 UNUSED_PARAM(func);
71 ASSERT_NOT_REACHED();
72#endif
73}
74
f9bf01c6
A
75JSFunction::JSFunction(ExecState* exec, NonNullPassRefPtr<FunctionExecutable> executable, ScopeChainNode* scopeChainNode)
76 : Base(&exec->globalData(), exec->lexicalGlobalObject()->functionStructure(), executable->name())
77 , m_executable(executable)
9dae56ea 78{
ba379fdc 79 setScopeChain(scopeChainNode);
9dae56ea
A
80}
81
82JSFunction::~JSFunction()
83{
f9bf01c6
A
84 ASSERT(vptr() == JSGlobalData::jsFunctionVPtr);
85
9dae56ea
A
86 // JIT code for other functions may have had calls linked directly to the code for this function; these links
87 // are based on a check for the this pointer value for this JSFunction - which will no longer be valid once
88 // this memory is freed and may be reused (potentially for another, different JSFunction).
f9bf01c6 89 if (!isHostFunction()) {
ba379fdc 90#if ENABLE(JIT_OPTIMIZE_CALL)
f9bf01c6
A
91 ASSERT(m_executable);
92 if (jsExecutable()->isGenerated())
93 jsExecutable()->generatedBytecode().unlinkCallers();
9dae56ea 94#endif
ba379fdc 95 scopeChain().~ScopeChain(); // FIXME: Don't we need to do this in the interpreter too?
f9bf01c6 96 }
9dae56ea
A
97}
98
f9bf01c6 99void JSFunction::markChildren(MarkStack& markStack)
9dae56ea 100{
f9bf01c6
A
101 Base::markChildren(markStack);
102 if (!isHostFunction()) {
103 jsExecutable()->markAggregate(markStack);
104 scopeChain().markAggregate(markStack);
105 }
9dae56ea
A
106}
107
108CallType JSFunction::getCallData(CallData& callData)
109{
ba379fdc
A
110 if (isHostFunction()) {
111 callData.native.function = nativeFunction();
112 return CallTypeHost;
113 }
f9bf01c6 114 callData.js.functionExecutable = jsExecutable();
ba379fdc 115 callData.js.scopeChain = scopeChain().node();
9dae56ea
A
116 return CallTypeJS;
117}
118
ba379fdc 119JSValue JSFunction::call(ExecState* exec, JSValue thisValue, const ArgList& args)
9dae56ea 120{
ba379fdc 121 ASSERT(!isHostFunction());
f9bf01c6 122 return exec->interpreter()->execute(jsExecutable(), exec, this, thisValue.toThisObject(exec), args, scopeChain().node(), exec->exceptionSlot());
9dae56ea
A
123}
124
ba379fdc 125JSValue JSFunction::argumentsGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
9dae56ea
A
126{
127 JSFunction* thisObj = asFunction(slot.slotBase());
ba379fdc 128 ASSERT(!thisObj->isHostFunction());
9dae56ea
A
129 return exec->interpreter()->retrieveArguments(exec, thisObj);
130}
131
ba379fdc 132JSValue JSFunction::callerGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
9dae56ea
A
133{
134 JSFunction* thisObj = asFunction(slot.slotBase());
ba379fdc 135 ASSERT(!thisObj->isHostFunction());
9dae56ea
A
136 return exec->interpreter()->retrieveCaller(exec, thisObj);
137}
138
ba379fdc 139JSValue JSFunction::lengthGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
9dae56ea
A
140{
141 JSFunction* thisObj = asFunction(slot.slotBase());
ba379fdc 142 ASSERT(!thisObj->isHostFunction());
f9bf01c6 143 return jsNumber(exec, thisObj->jsExecutable()->parameterCount());
9dae56ea
A
144}
145
146bool JSFunction::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
147{
ba379fdc
A
148 if (isHostFunction())
149 return Base::getOwnPropertySlot(exec, propertyName, slot);
150
9dae56ea 151 if (propertyName == exec->propertyNames().prototype) {
ba379fdc 152 JSValue* location = getDirectLocation(propertyName);
9dae56ea
A
153
154 if (!location) {
ba379fdc 155 JSObject* prototype = new (exec) JSObject(scopeChain().globalObject()->emptyObjectStructure());
9dae56ea
A
156 prototype->putDirect(exec->propertyNames().constructor, this, DontEnum);
157 putDirect(exec->propertyNames().prototype, prototype, DontDelete);
158 location = getDirectLocation(propertyName);
159 }
160
161 slot.setValueSlot(this, location, offsetForLocation(location));
162 }
163
164 if (propertyName == exec->propertyNames().arguments) {
165 slot.setCustom(this, argumentsGetter);
166 return true;
167 }
168
169 if (propertyName == exec->propertyNames().length) {
170 slot.setCustom(this, lengthGetter);
171 return true;
172 }
173
174 if (propertyName == exec->propertyNames().caller) {
175 slot.setCustom(this, callerGetter);
176 return true;
177 }
178
179 return Base::getOwnPropertySlot(exec, propertyName, slot);
180}
181
f9bf01c6
A
182 bool JSFunction::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
183 {
184 if (isHostFunction())
185 return Base::getOwnPropertyDescriptor(exec, propertyName, descriptor);
186
187 if (propertyName == exec->propertyNames().prototype) {
188 PropertySlot slot;
189 getOwnPropertySlot(exec, propertyName, slot);
190 return Base::getOwnPropertyDescriptor(exec, propertyName, descriptor);
191 }
192
193 if (propertyName == exec->propertyNames().arguments) {
194 descriptor.setDescriptor(exec->interpreter()->retrieveArguments(exec, this), ReadOnly | DontEnum | DontDelete);
195 return true;
196 }
197
198 if (propertyName == exec->propertyNames().length) {
199 descriptor.setDescriptor(jsNumber(exec, jsExecutable()->parameterCount()), ReadOnly | DontEnum | DontDelete);
200 return true;
201 }
202
203 if (propertyName == exec->propertyNames().caller) {
204 descriptor.setDescriptor(exec->interpreter()->retrieveCaller(exec, this), ReadOnly | DontEnum | DontDelete);
205 return true;
206 }
207
208 return Base::getOwnPropertyDescriptor(exec, propertyName, descriptor);
209 }
210
211void JSFunction::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
212{
213 if (!isHostFunction() && (mode == IncludeDontEnumProperties)) {
214 propertyNames.add(exec->propertyNames().arguments);
215 propertyNames.add(exec->propertyNames().callee);
216 propertyNames.add(exec->propertyNames().caller);
217 propertyNames.add(exec->propertyNames().length);
218 }
219 Base::getOwnPropertyNames(exec, propertyNames, mode);
220}
221
ba379fdc 222void JSFunction::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
9dae56ea 223{
ba379fdc
A
224 if (isHostFunction()) {
225 Base::put(exec, propertyName, value, slot);
226 return;
227 }
9dae56ea
A
228 if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length)
229 return;
230 Base::put(exec, propertyName, value, slot);
231}
232
233bool JSFunction::deleteProperty(ExecState* exec, const Identifier& propertyName)
234{
ba379fdc
A
235 if (isHostFunction())
236 return Base::deleteProperty(exec, propertyName);
9dae56ea
A
237 if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length)
238 return false;
239 return Base::deleteProperty(exec, propertyName);
240}
241
242// ECMA 13.2.2 [[Construct]]
243ConstructType JSFunction::getConstructData(ConstructData& constructData)
244{
ba379fdc
A
245 if (isHostFunction())
246 return ConstructTypeNone;
f9bf01c6 247 constructData.js.functionExecutable = jsExecutable();
ba379fdc 248 constructData.js.scopeChain = scopeChain().node();
9dae56ea
A
249 return ConstructTypeJS;
250}
251
252JSObject* JSFunction::construct(ExecState* exec, const ArgList& args)
253{
ba379fdc 254 ASSERT(!isHostFunction());
9dae56ea 255 Structure* structure;
ba379fdc 256 JSValue prototype = get(exec, exec->propertyNames().prototype);
9dae56ea
A
257 if (prototype.isObject())
258 structure = asObject(prototype)->inheritorID();
259 else
260 structure = exec->lexicalGlobalObject()->emptyObjectStructure();
261 JSObject* thisObj = new (exec) JSObject(structure);
262
f9bf01c6 263 JSValue result = exec->interpreter()->execute(jsExecutable(), exec, this, thisObj, args, scopeChain().node(), exec->exceptionSlot());
9dae56ea
A
264 if (exec->hadException() || !result.isObject())
265 return thisObj;
266 return asObject(result);
267}
268
269} // namespace JSC