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
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.
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.
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.
26 #include "JSFunction.h"
28 #include "CodeBlock.h"
29 #include "CommonIdentifiers.h"
30 #include "CallFrame.h"
31 #include "ExceptionHelpers.h"
32 #include "FunctionPrototype.h"
34 #include "JSGlobalObject.h"
35 #include "JSNotAnObject.h"
36 #include "Interpreter.h"
37 #include "ObjectPrototype.h"
39 #include "PropertyNameArray.h"
40 #include "ScopeChainMark.h"
43 using namespace Unicode;
46 EncodedJSValue JSC_HOST_CALL callHostFunctionAsConstructor(ExecState* exec)
48 return throwVMError(exec, createNotAConstructorError(exec, exec->callee()));
51 ASSERT_CLASS_FITS_IN_CELL(JSFunction);
53 const ClassInfo JSFunction::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSFunction) };
55 bool JSFunction::isHostFunctionNonInline() const
57 return isHostFunction();
60 JSFunction* JSFunction::create(ExecState* exec, JSGlobalObject* globalObject, int length, const Identifier& name, NativeFunction nativeFunction, NativeFunction nativeConstructor)
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);
69 JSFunction* JSFunction::create(ExecState* exec, JSGlobalObject* globalObject, int length, const Identifier& name, NativeExecutable* nativeExecutable)
71 JSFunction* function = new (allocateCell<JSFunction>(*exec->heap())) JSFunction(exec, globalObject, globalObject->functionStructure());
72 function->finishCreation(exec, nativeExecutable, length, name);
76 JSFunction::JSFunction(VPtrStealingHackType)
77 : Base(VPtrStealingHack)
81 JSFunction::JSFunction(ExecState* exec, JSGlobalObject* globalObject, Structure* structure)
82 : Base(exec->globalData(), structure)
84 , m_scopeChain(exec->globalData(), this, globalObject->globalScopeChain())
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)
95 void JSFunction::finishCreation(ExecState* exec, NativeExecutable* executable, int length, const Identifier& name)
97 Base::finishCreation(exec->globalData());
98 ASSERT(inherits(&s_info));
99 m_executable.set(exec->globalData(), this, executable);
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);
105 void JSFunction::finishCreation(ExecState* exec, FunctionExecutable* executable, ScopeChainNode* scopeChainNode)
107 Base::finishCreation(exec->globalData());
108 ASSERT(inherits(&s_info));
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());
116 JSFunction::~JSFunction()
118 ASSERT(vptr() == JSGlobalData::jsFunctionVPtr);
121 void createDescriptorForThrowingProperty(ExecState* exec, PropertyDescriptor& descriptor, const char* message)
123 JSValue thrower = createTypeErrorFunction(exec, message);
124 descriptor.setAccessorDescriptor(thrower, thrower, DontEnum | DontDelete | Getter | Setter);
127 const UString& JSFunction::name(ExecState* exec)
129 return asString(getDirect(exec->globalData(), exec->globalData().propertyNames->name))->tryGetValue();
132 const UString JSFunction::displayName(ExecState* exec)
134 JSValue displayName = getDirect(exec->globalData(), exec->globalData().propertyNames->displayName);
136 if (displayName && isJSString(&exec->globalData(), displayName))
137 return asString(displayName)->tryGetValue();
142 const UString JSFunction::calculatedDisplayName(ExecState* exec)
144 const UString explicitName = displayName(exec);
146 if (!explicitName.isEmpty())
152 const SourceCode* JSFunction::sourceCode() const
154 if (isHostFunction())
156 return &jsExecutable()->source();
159 void JSFunction::visitChildren(JSCell* cell, SlotVisitor& visitor)
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);
167 visitor.append(&thisObject->m_scopeChain);
168 if (thisObject->m_executable)
169 visitor.append(&thisObject->m_executable);
172 CallType JSFunction::getCallData(JSCell* cell, CallData& callData)
174 JSFunction* thisObject = jsCast<JSFunction*>(cell);
175 if (thisObject->isHostFunction()) {
176 callData.native.function = thisObject->nativeFunction();
179 callData.js.functionExecutable = thisObject->jsExecutable();
180 callData.js.scopeChain = thisObject->scope();
184 JSValue JSFunction::argumentsGetter(ExecState* exec, JSValue slotBase, const Identifier&)
186 JSFunction* thisObj = asFunction(slotBase);
187 ASSERT(!thisObj->isHostFunction());
188 return exec->interpreter()->retrieveArguments(exec, thisObj);
191 JSValue JSFunction::callerGetter(ExecState* exec, JSValue slotBase, const Identifier&)
193 JSFunction* thisObj = asFunction(slotBase);
194 ASSERT(!thisObj->isHostFunction());
195 return exec->interpreter()->retrieveCaller(exec, thisObj);
198 JSValue JSFunction::lengthGetter(ExecState*, JSValue slotBase, const Identifier&)
200 JSFunction* thisObj = asFunction(slotBase);
201 ASSERT(!thisObj->isHostFunction());
202 return jsNumber(thisObj->jsExecutable()->parameterCount());
205 bool JSFunction::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
207 JSFunction* thisObject = jsCast<JSFunction*>(cell);
208 if (thisObject->isHostFunction())
209 return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
211 if (propertyName == exec->propertyNames().prototype) {
212 WriteBarrierBase<Unknown>* location = thisObject->getDirectLocation(exec->globalData(), propertyName);
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);
222 slot.setValue(thisObject, location->get(), thisObject->offsetForLocation(location));
225 if (propertyName == exec->propertyNames().arguments) {
226 if (thisObject->jsExecutable()->isStrictMode()) {
227 bool result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
229 thisObject->initializeGetterSetterProperty(exec, propertyName, thisObject->globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Getter | Setter);
230 result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
235 slot.setCacheableCustom(thisObject, argumentsGetter);
239 if (propertyName == exec->propertyNames().length) {
240 slot.setCacheableCustom(thisObject, lengthGetter);
244 if (propertyName == exec->propertyNames().caller) {
245 if (thisObject->jsExecutable()->isStrictMode()) {
246 bool result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
248 thisObject->initializeGetterSetterProperty(exec, propertyName, thisObject->globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Getter | Setter);
249 result = Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
254 slot.setCacheableCustom(thisObject, callerGetter);
258 return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);
261 bool JSFunction::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
263 JSFunction* thisObject = jsCast<JSFunction*>(object);
264 if (thisObject->isHostFunction())
265 return Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
267 if (propertyName == exec->propertyNames().prototype) {
269 thisObject->methodTable()->getOwnPropertySlot(thisObject, exec, propertyName, slot);
270 return Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
273 if (propertyName == exec->propertyNames().arguments) {
274 if (thisObject->jsExecutable()->isStrictMode()) {
275 bool result = Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
277 thisObject->initializeGetterSetterProperty(exec, propertyName, thisObject->globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Getter | Setter);
278 result = Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
283 descriptor.setDescriptor(exec->interpreter()->retrieveArguments(exec, thisObject), ReadOnly | DontEnum | DontDelete);
287 if (propertyName == exec->propertyNames().length) {
288 descriptor.setDescriptor(jsNumber(thisObject->jsExecutable()->parameterCount()), ReadOnly | DontEnum | DontDelete);
292 if (propertyName == exec->propertyNames().caller) {
293 if (thisObject->jsExecutable()->isStrictMode()) {
294 bool result = Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
296 thisObject->initializeGetterSetterProperty(exec, propertyName, thisObject->globalObject()->throwTypeErrorGetterSetter(exec), DontDelete | DontEnum | Getter | Setter);
297 result = Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
302 descriptor.setDescriptor(exec->interpreter()->retrieveCaller(exec, thisObject), ReadOnly | DontEnum | DontDelete);
306 return Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
309 void JSFunction::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
311 JSFunction* thisObject = jsCast<JSFunction*>(object);
312 if (!thisObject->isHostFunction() && (mode == IncludeDontEnumProperties)) {
313 // Make sure prototype has been reified.
315 thisObject->methodTable()->getOwnPropertySlot(thisObject, exec, exec->propertyNames().prototype, slot);
317 propertyNames.add(exec->propertyNames().arguments);
318 propertyNames.add(exec->propertyNames().caller);
319 propertyNames.add(exec->propertyNames().length);
321 Base::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
324 void JSFunction::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
326 JSFunction* thisObject = jsCast<JSFunction*>(cell);
327 if (thisObject->isHostFunction()) {
328 Base::put(thisObject, exec, propertyName, value, slot);
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.
335 thisObject->methodTable()->getOwnPropertySlot(thisObject, exec, propertyName, slot);
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);
344 if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length)
346 Base::put(thisObject, exec, propertyName, value, slot);
349 bool JSFunction::deleteProperty(JSCell* cell, ExecState* exec, const Identifier& propertyName)
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)
356 return Base::deleteProperty(thisObject, exec, propertyName);
359 // ECMA 13.2.2 [[Construct]]
360 ConstructType JSFunction::getConstructData(JSCell* cell, ConstructData& constructData)
362 JSFunction* thisObject = jsCast<JSFunction*>(cell);
363 if (thisObject->isHostFunction()) {
364 constructData.native.function = thisObject->nativeConstructor();
365 return ConstructTypeHost;
367 constructData.js.functionExecutable = thisObject->jsExecutable();
368 constructData.js.scopeChain = thisObject->scope();
369 return ConstructTypeJS;