2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "FunctionPrototype.h"
25 #include "JSFunction.h"
30 static JSValue* functionProtoFuncToString(ExecState*, JSObject*, JSValue*, const ArgList&);
31 static JSValue* functionProtoFuncApply(ExecState*, JSObject*, JSValue*, const ArgList&);
32 static JSValue* functionProtoFuncCall(ExecState*, JSObject*, JSValue*, const ArgList&);
34 FunctionPrototype::FunctionPrototype(ExecState* exec)
36 putDirect(exec->propertyNames().length, jsNumber(exec, 0), DontDelete | ReadOnly | DontEnum);
38 putDirectFunction(new (exec) PrototypeFunction(exec, this, 0, exec->propertyNames().toString, functionProtoFuncToString), DontEnum);
39 putDirectFunction(new (exec) PrototypeFunction(exec, this, 2, exec->propertyNames().apply, functionProtoFuncApply), DontEnum);
40 putDirectFunction(new (exec) PrototypeFunction(exec, this, 1, exec->propertyNames().call, functionProtoFuncCall), DontEnum);
43 static JSValue* callFunctionPrototype(ExecState*, JSObject*, JSValue*, const ArgList&)
49 CallType FunctionPrototype::getCallData(CallData& callData)
51 callData.native.function = callFunctionPrototype;
52 return CallTypeNative;
57 JSValue* functionProtoFuncToString(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList&)
59 if (!thisValue->isObject(&InternalFunction::info))
60 return throwError(exec, TypeError);
62 InternalFunction* function = static_cast<InternalFunction*>(thisValue);
64 if (function->inherits(&JSFunction::info)) {
65 JSFunction* fi = static_cast<JSFunction*>(thisValue);
66 return jsString(exec, "function " + fi->functionName().ustring() + "(" + fi->body->paramString() + ") " + fi->body->toSourceString());
69 return jsString(exec, "function " + function->functionName().ustring() + "() {\n [native code]\n}");
72 JSValue* functionProtoFuncApply(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)
75 CallType callType = thisValue->getCallData(callData);
76 if (callType == CallTypeNone)
77 return throwError(exec, TypeError);
79 JSValue* thisArg = args[0];
80 JSValue* argArray = args[1];
83 if (thisArg->isUndefinedOrNull())
84 applyThis = exec->globalThisValue();
86 applyThis = thisArg->toObject(exec);
89 if (!argArray->isUndefinedOrNull()) {
90 if (argArray->isObject() &&
91 (static_cast<JSObject*>(argArray)->inherits(&JSArray::info) ||
92 static_cast<JSObject*>(argArray)->inherits(&Arguments::info))) {
94 JSObject* argArrayObj = static_cast<JSObject*>(argArray);
95 unsigned int length = argArrayObj->get(exec, exec->propertyNames().length)->toUInt32(exec);
96 for (unsigned int i = 0; i < length; i++)
97 applyArgs.append(argArrayObj->get(exec, i));
99 return throwError(exec, TypeError);
102 return call(exec, thisValue, callType, callData, applyThis, applyArgs);
105 JSValue* functionProtoFuncCall(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)
108 CallType callType = thisValue->getCallData(callData);
109 if (callType == CallTypeNone)
110 return throwError(exec, TypeError);
112 JSValue* thisArg = args[0];
115 if (thisArg->isUndefinedOrNull())
116 callThis = exec->globalThisValue();
118 callThis = thisArg->toObject(exec);
121 args.getSlice(1, argsTail);
122 return call(exec, thisValue, callType, callData, callThis, argsTail);