]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSFunction.cpp
7fedad7cd22a428b1aa10489372357f0b2b5474e
[apple/javascriptcore.git] / runtime / JSFunction.cpp
1 /*
2 * Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
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
39 using namespace WTF;
40 using namespace Unicode;
41
42 namespace JSC {
43
44 ASSERT_CLASS_FITS_IN_CELL(JSFunction);
45
46 const ClassInfo JSFunction::info = { "Function", &InternalFunction::info, 0, 0 };
47
48 JSFunction::JSFunction(ExecState* exec, PassRefPtr<Structure> structure, int length, const Identifier& name, NativeFunction func)
49 : Base(&exec->globalData(), structure, name)
50 #if ENABLE(JIT)
51 , m_body(FunctionBodyNode::createNativeThunk(&exec->globalData()))
52 #else
53 , m_body(0)
54 #endif
55 {
56 #if ENABLE(JIT)
57 setNativeFunction(func);
58 putDirect(exec->propertyNames().length, jsNumber(exec, length), DontDelete | ReadOnly | DontEnum);
59 #else
60 UNUSED_PARAM(length);
61 UNUSED_PARAM(func);
62 ASSERT_NOT_REACHED();
63 #endif
64 }
65
66 JSFunction::JSFunction(ExecState* exec, const Identifier& name, FunctionBodyNode* body, ScopeChainNode* scopeChainNode)
67 : Base(&exec->globalData(), exec->lexicalGlobalObject()->functionStructure(), name)
68 , m_body(body)
69 {
70 setScopeChain(scopeChainNode);
71 }
72
73 JSFunction::~JSFunction()
74 {
75 // JIT code for other functions may have had calls linked directly to the code for this function; these links
76 // are based on a check for the this pointer value for this JSFunction - which will no longer be valid once
77 // this memory is freed and may be reused (potentially for another, different JSFunction).
78 #if ENABLE(JIT_OPTIMIZE_CALL)
79 if (m_body && m_body->isGenerated())
80 m_body->generatedBytecode().unlinkCallers();
81 #endif
82 if (!isHostFunction())
83 scopeChain().~ScopeChain(); // FIXME: Don't we need to do this in the interpreter too?
84 }
85
86 void JSFunction::mark()
87 {
88 Base::mark();
89 m_body->mark();
90 if (!isHostFunction())
91 scopeChain().mark();
92 }
93
94 CallType JSFunction::getCallData(CallData& callData)
95 {
96 if (isHostFunction()) {
97 callData.native.function = nativeFunction();
98 return CallTypeHost;
99 }
100 callData.js.functionBody = m_body.get();
101 callData.js.scopeChain = scopeChain().node();
102 return CallTypeJS;
103 }
104
105 JSValue JSFunction::call(ExecState* exec, JSValue thisValue, const ArgList& args)
106 {
107 ASSERT(!isHostFunction());
108 return exec->interpreter()->execute(m_body.get(), exec, this, thisValue.toThisObject(exec), args, scopeChain().node(), exec->exceptionSlot());
109 }
110
111 JSValue JSFunction::argumentsGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
112 {
113 JSFunction* thisObj = asFunction(slot.slotBase());
114 ASSERT(!thisObj->isHostFunction());
115 return exec->interpreter()->retrieveArguments(exec, thisObj);
116 }
117
118 JSValue JSFunction::callerGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
119 {
120 JSFunction* thisObj = asFunction(slot.slotBase());
121 ASSERT(!thisObj->isHostFunction());
122 return exec->interpreter()->retrieveCaller(exec, thisObj);
123 }
124
125 JSValue JSFunction::lengthGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
126 {
127 JSFunction* thisObj = asFunction(slot.slotBase());
128 ASSERT(!thisObj->isHostFunction());
129 return jsNumber(exec, thisObj->m_body->parameterCount());
130 }
131
132 bool JSFunction::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
133 {
134 if (isHostFunction())
135 return Base::getOwnPropertySlot(exec, propertyName, slot);
136
137 if (propertyName == exec->propertyNames().prototype) {
138 JSValue* location = getDirectLocation(propertyName);
139
140 if (!location) {
141 JSObject* prototype = new (exec) JSObject(scopeChain().globalObject()->emptyObjectStructure());
142 prototype->putDirect(exec->propertyNames().constructor, this, DontEnum);
143 putDirect(exec->propertyNames().prototype, prototype, DontDelete);
144 location = getDirectLocation(propertyName);
145 }
146
147 slot.setValueSlot(this, location, offsetForLocation(location));
148 }
149
150 if (propertyName == exec->propertyNames().arguments) {
151 slot.setCustom(this, argumentsGetter);
152 return true;
153 }
154
155 if (propertyName == exec->propertyNames().length) {
156 slot.setCustom(this, lengthGetter);
157 return true;
158 }
159
160 if (propertyName == exec->propertyNames().caller) {
161 slot.setCustom(this, callerGetter);
162 return true;
163 }
164
165 return Base::getOwnPropertySlot(exec, propertyName, slot);
166 }
167
168 void JSFunction::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
169 {
170 if (isHostFunction()) {
171 Base::put(exec, propertyName, value, slot);
172 return;
173 }
174 if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length)
175 return;
176 Base::put(exec, propertyName, value, slot);
177 }
178
179 bool JSFunction::deleteProperty(ExecState* exec, const Identifier& propertyName)
180 {
181 if (isHostFunction())
182 return Base::deleteProperty(exec, propertyName);
183 if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length)
184 return false;
185 return Base::deleteProperty(exec, propertyName);
186 }
187
188 // ECMA 13.2.2 [[Construct]]
189 ConstructType JSFunction::getConstructData(ConstructData& constructData)
190 {
191 if (isHostFunction())
192 return ConstructTypeNone;
193 constructData.js.functionBody = m_body.get();
194 constructData.js.scopeChain = scopeChain().node();
195 return ConstructTypeJS;
196 }
197
198 JSObject* JSFunction::construct(ExecState* exec, const ArgList& args)
199 {
200 ASSERT(!isHostFunction());
201 Structure* structure;
202 JSValue prototype = get(exec, exec->propertyNames().prototype);
203 if (prototype.isObject())
204 structure = asObject(prototype)->inheritorID();
205 else
206 structure = exec->lexicalGlobalObject()->emptyObjectStructure();
207 JSObject* thisObj = new (exec) JSObject(structure);
208
209 JSValue result = exec->interpreter()->execute(m_body.get(), exec, this, thisObj, args, scopeChain().node(), exec->exceptionSlot());
210 if (exec->hadException() || !result.isObject())
211 return thisObj;
212 return asObject(result);
213 }
214
215 } // namespace JSC