tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / 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, 2009 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 "ExceptionHelpers.h"
32 #include "FunctionPrototype.h"
33 #include "JSArray.h"
34 #include "JSGlobalObject.h"
35 #include "JSNotAnObject.h"
36 #include "Interpreter.h"
37 #include "ObjectPrototype.h"
38 #include "Parser.h"
39 #include "PropertyNameArray.h"
40 #include "ScopeChainMark.h"
41
42 using namespace WTF;
43 using namespace Unicode;
44
45 namespace JSC {
46 EncodedJSValue JSC_HOST_CALL callHostFunctionAsConstructor(ExecState* exec)
47 {
48     return throwVMError(exec, createNotAConstructorError(exec, exec->callee()));
49 }
50
51 ASSERT_CLASS_FITS_IN_CELL(JSFunction);
52
53 const ClassInfo JSFunction::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSFunction) };
54
55 bool JSFunction::isHostFunctionNonInline() const
56 {
57     return isHostFunction();
58 }
59
60 JSFunction* JSFunction::create(ExecState* exec, JSGlobalObject* globalObject, int length, const Identifier& name, NativeFunction nativeFunction, NativeFunction nativeConstructor)
61 {
62     NativeExecutable* executable = exec->globalData().getHostFunction(nativeFunction, nativeConstructor);
63     JSFunction* function = new (allocateCell<JSFunction>(*exec->heap())) JSFunction(exec, globalObject, globalObject->functionStructure());
64     // Can't do this during initialization because getHostFunction might do a GC allocation.
65     function->finishCreation(exec, executable, length, name);
66     return function;
67 }
68
69 JSFunction* JSFunction::create(ExecState* exec, JSGlobalObject* globalObject, int length, const Identifier& name, NativeExecutable* nativeExecutable)
70 {
71     JSFunction* function = new (allocateCell<JSFunction>(*exec->heap())) JSFunction(exec, globalObject, globalObject->functionStructure());
72     function->finishCreation(exec, nativeExecutable, length, name);
73     return function;
74 }
75
76 JSFunction::JSFunction(VPtrStealingHackType)
77     : Base(VPtrStealingHack)
78 {
79 }
80
81 JSFunction::JSFunction(ExecState* exec, JSGlobalObject* globalObject, Structure* structure)
82     : Base(exec->globalData(), structure)
83     , m_executable()
84     , m_scopeChain(exec->globalData(), this, globalObject->globalScopeChain())
85 {
86 }
87
88 JSFunction::JSFunction(ExecState* exec, FunctionExecutable* executable, ScopeChainNode* scopeChainNode)
89     : Base(exec->globalData(), scopeChainNode->globalObject->functionStructure())
90     , m_executable(exec->globalData(), this, executable)
91     , m_scopeChain(exec->globalData(), this, scopeChainNode)
92 {
93 }
94
95 void JSFunction::finishCreation(ExecState* exec, NativeExecutable* executable, int length, const Identifier& name)
96 {
97     Base::finishCreation(exec->globalData());
98     ASSERT(inherits(&s_info));
99     m_executable.set(exec->globalData(), this, executable);
100     if (!name.isNull())
101         putDirect(exec->globalData(), exec->globalData().propertyNames->name, jsString(exec, name.ustring()), DontDelete | ReadOnly | DontEnum);
102     putDirect(exec->globalData(), exec->propertyNames().length, jsNumber(length), DontDelete | ReadOnly | DontEnum);
103 }
104
105 void JSFunction::finishCreation(ExecState* exec, FunctionExecutable* executable, ScopeChainNode* scopeChainNode)
106 {
107     Base::finishCreation(exec->globalData());
108     ASSERT(inherits(&s_info));
109
110     // Switching the structure here is only safe if we currently have the function structure!
111     ASSERT(structure() == scopeChainNode->globalObject->functionStructure());
112     setStructure(exec->globalData(), scopeChainNode->globalObject->namedFunctionStructure());
113     putDirectOffset(exec->globalData(), scopeChainNode->globalObject->functionNameOffset(), executable->nameValue());
114 }
115
116 JSFunction::~JSFunction()
117 {
118     ASSERT(vptr() == JSGlobalData::jsFunctionVPtr);
119 }
120
121 void createDescriptorForThrowingProperty(ExecState* exec, PropertyDescriptor& descriptor, const char* message)
122 {
123     JSValue thrower = createTypeErrorFunction(exec, message);
124     descriptor.setAccessorDescriptor(thrower, thrower, DontEnum | DontDelete | Getter | Setter);
125 }
126
127 const UString& JSFunction::name(ExecState* exec)
128 {
129     return asString(getDirect(exec->globalData(), exec->globalData().propertyNames->name))->tryGetValue();
130 }
131
132 const UString JSFunction::displayName(ExecState* exec)
133 {
134     JSValue displayName = getDirect(exec->globalData(), exec->globalData().propertyNames->displayName);
135     
136     if (displayName && isJSString(&exec->globalData(), displayName))
137         return asString(displayName)->tryGetValue();
138     
139     return UString();
140 }
141
142 const UString JSFunction::calculatedDisplayName(ExecState* exec)
143 {
144     const UString explicitName = displayName(exec);
145     
146     if (!explicitName.isEmpty())
147         return explicitName;
148     
149     return name(exec);
150 }
151
152 const SourceCode* JSFunction::sourceCode() const
153 {
154     if (isHostFunction())
155         return 0;
156     return &jsExecutable()->source();
157 }
158
159 void JSFunction::visitChildren(JSCell* cell, SlotVisitor& visitor)
160 {
161     JSFunction* thisObject = jsCast<JSFunction*>(cell);
162     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
163     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
164     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
165     Base::visitChildren(thisObject, visitor);
166
167     visitor.append(&thisObject->m_scopeChain);
168     if (thisObject->m_executable)
169         visitor.append(&thisObject->m_executable);
170 }
171
172 CallType JSFunction::getCallData(JSCell* cell, CallData& callData)
173 {
174     JSFunction* thisObject = jsCast<JSFunction*>(cell);
175     if (thisObject->isHostFunction()) {
176         callData.native.function = thisObject->nativeFunction();
177         return CallTypeHost;
178     }
179     callData.js.functionExecutable = thisObject->jsExecutable();
180     callData.js.scopeChain = thisObject->scope();
181     return CallTypeJS;
182 }
183
184 JSValue JSFunction::argumentsGetter(ExecState* exec, JSValue slotBase, const Identifier&)
185 {
186     JSFunction* thisObj = asFunction(slotBase);
187     ASSERT(!thisObj->isHostFunction());
188     return exec->interpreter()->retrieveArguments(exec, thisObj);
189 }
190
191 JSValue JSFunction::callerGetter(ExecState* exec, JSValue slotBase, const Identifier&)
192 {
193     JSFunction* thisObj = asFunction(slotBase);
194     ASSERT(!thisObj->isHostFunction());
195     return exec->interpreter()->retrieveCaller(exec, thisObj);
196 }
197
198 JSValue JSFunction::lengthGetter(ExecState*, JSValue slotBase, const Identifier&)
199 {
200     JSFunction* thisObj = asFunction(slotBase);
201     ASSERT(!thisObj->isHostFunction());
202     return jsNumber(thisObj->jsExecutable()->parameterCount());
203 }
204
205 bool JSFunction::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
206 {
207     JSFunction* thisObject = jsCast<JSFunction*>(cell);
208     if (thisObject->isHostFunction())
209         return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
210
211     if (propertyName == exec->propertyNames().prototype) {
212         WriteBarrierBase<Unknown>* location = thisObject->getDirectLocation(exec->globalData(), propertyName);
213
214         if (!location) {
215             JSObject* prototype = constructEmptyObject(exec, thisObject->globalObject()->emptyObjectStructure());
216             prototype->putDirect(exec->globalData(), exec->propertyNames().constructor, thisObject, DontEnum);
217             PutPropertySlot slot;
218             thisObject->putDirect(exec->globalData(), exec->propertyNames().prototype, prototype, DontDelete | DontEnum, false, slot);
219             location = thisObject->getDirectLocation(exec->globalData(), exec->propertyNames().prototype);
220         }
221
222         slot.setValue(thisObject, location->get(), thisObject->offsetForLocation(location));
223     }
224
225     if (propertyName == exec->propertyNames().arguments) {
226         if (thisObject->jsExecutable()->isStrictMode()) {
227             bool result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
228             if (!result) {
229                 thisObject->initializeGetterSetterProperty(exec, propertyName, thisObject->globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Getter | Setter);
230                 result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
231                 ASSERT(result);
232             }
233             return result;
234         }
235         slot.setCacheableCustom(thisObject, argumentsGetter);
236         return true;
237     }
238
239     if (propertyName == exec->propertyNames().length) {
240         slot.setCacheableCustom(thisObject, lengthGetter);
241         return true;
242     }
243
244     if (propertyName == exec->propertyNames().caller) {
245         if (thisObject->jsExecutable()->isStrictMode()) {
246             bool result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
247             if (!result) {
248                 thisObject->initializeGetterSetterProperty(exec, propertyName, thisObject->globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Getter | Setter);
249                 result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
250                 ASSERT(result);
251             }
252             return result;
253         }
254         slot.setCacheableCustom(thisObject, callerGetter);
255         return true;
256     }
257
258     return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
259 }
260
261 bool JSFunction::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
262 {
263     JSFunction* thisObject = jsCast<JSFunction*>(object);
264     if (thisObject->isHostFunction())
265         return Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
266     
267     if (propertyName == exec->propertyNames().prototype) {
268         PropertySlot slot;
269         thisObject->methodTable()->getOwnPropertySlot(thisObject, exec, propertyName, slot);
270         return Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
271     }
272     
273     if (propertyName == exec->propertyNames().arguments) {
274         if (thisObject->jsExecutable()->isStrictMode()) {
275             bool result = Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
276             if (!result) {
277                 thisObject->initializeGetterSetterProperty(exec, propertyName, thisObject->globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Getter | Setter);
278                 result = Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
279                 ASSERT(result);
280             }
281             return result;
282         }
283         descriptor.setDescriptor(exec->interpreter()->retrieveArguments(exec, thisObject), ReadOnly | DontEnum | DontDelete);
284         return true;
285     }
286     
287     if (propertyName == exec->propertyNames().length) {
288         descriptor.setDescriptor(jsNumber(thisObject->jsExecutable()->parameterCount()), ReadOnly | DontEnum | DontDelete);
289         return true;
290     }
291     
292     if (propertyName == exec->propertyNames().caller) {
293         if (thisObject->jsExecutable()->isStrictMode()) {
294             bool result = Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
295             if (!result) {
296                 thisObject->initializeGetterSetterProperty(exec, propertyName, thisObject->globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Getter | Setter);
297                 result = Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
298                 ASSERT(result);
299             }
300             return result;
301         }
302         descriptor.setDescriptor(exec->interpreter()->retrieveCaller(exec, thisObject), ReadOnly | DontEnum | DontDelete);
303         return true;
304     }
305     
306     return Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
307 }
308
309 void JSFunction::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
310 {
311     JSFunction* thisObject = jsCast<JSFunction*>(object);
312     if (!thisObject->isHostFunction() && (mode == IncludeDontEnumProperties)) {
313         // Make sure prototype has been reified.
314         PropertySlot slot;
315         thisObject->methodTable()->getOwnPropertySlot(thisObject, exec, exec->propertyNames().prototype, slot);
316
317         propertyNames.add(exec->propertyNames().arguments);
318         propertyNames.add(exec->propertyNames().caller);
319         propertyNames.add(exec->propertyNames().length);
320     }
321     Base::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
322 }
323
324 void JSFunction::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
325 {
326     JSFunction* thisObject = jsCast<JSFunction*>(cell);
327     if (thisObject->isHostFunction()) {
328         Base::put(thisObject, exec, propertyName, value, slot);
329         return;
330     }
331     if (propertyName == exec->propertyNames().prototype) {
332         // Make sure prototype has been reified, such that it can only be overwritten
333         // following the rules set out in ECMA-262 8.12.9.
334         PropertySlot slot;
335         thisObject->methodTable()->getOwnPropertySlot(thisObject, exec, propertyName, slot);
336     }
337     if (thisObject->jsExecutable()->isStrictMode() && (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().caller)) {
338         // This will trigger the property to be reified, if this is not already the case!
339         bool okay = thisObject->hasProperty(exec, propertyName);
340         ASSERT_UNUSED(okay, okay);
341         Base::put(thisObject, exec, propertyName, value, slot);
342         return;
343     }
344     if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length)
345         return;
346     Base::put(thisObject, exec, propertyName, value, slot);
347 }
348
349 bool JSFunction::deleteProperty(JSCell* cell, ExecState* exec, const Identifier& propertyName)
350 {
351     JSFunction* thisObject = jsCast<JSFunction*>(cell);
352     if (thisObject->isHostFunction())
353         return Base::deleteProperty(thisObject, exec, propertyName);
354     if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length)
355         return false;
356     return Base::deleteProperty(thisObject, exec, propertyName);
357 }
358
359 // ECMA 13.2.2 [[Construct]]
360 ConstructType JSFunction::getConstructData(JSCell* cell, ConstructData& constructData)
361 {
362     JSFunction* thisObject = jsCast<JSFunction*>(cell);
363     if (thisObject->isHostFunction()) {
364         constructData.native.function = thisObject->nativeConstructor();
365         return ConstructTypeHost;
366     }
367     constructData.js.functionExecutable = thisObject->jsExecutable();
368     constructData.js.scopeChain = thisObject->scope();
369     return ConstructTypeJS;
370 }
371
372 } // namespace JSC