2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 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"
24 #include "Arguments.h"
26 #include "JSBoundFunction.h"
27 #include "JSFunction.h"
29 #include "JSStringBuilder.h"
30 #include "Interpreter.h"
35 ASSERT_CLASS_FITS_IN_CELL(FunctionPrototype);
37 const ClassInfo FunctionPrototype::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(FunctionPrototype) };
39 static EncodedJSValue JSC_HOST_CALL functionProtoFuncToString(ExecState*);
40 static EncodedJSValue JSC_HOST_CALL functionProtoFuncApply(ExecState*);
41 static EncodedJSValue JSC_HOST_CALL functionProtoFuncCall(ExecState*);
42 static EncodedJSValue JSC_HOST_CALL functionProtoFuncBind(ExecState*);
44 FunctionPrototype::FunctionPrototype(JSGlobalObject* globalObject, Structure* structure)
45 : InternalFunction(globalObject, structure)
49 void FunctionPrototype::finishCreation(ExecState* exec, const Identifier& name)
51 Base::finishCreation(exec->globalData(), name);
52 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
55 void FunctionPrototype::addFunctionProperties(ExecState* exec, JSGlobalObject* globalObject, JSFunction** callFunction, JSFunction** applyFunction)
57 JSFunction* toStringFunction = JSFunction::create(exec, globalObject, 0, exec->propertyNames().toString, functionProtoFuncToString);
58 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().toString, toStringFunction, DontEnum);
60 *applyFunction = JSFunction::create(exec, globalObject, 2, exec->propertyNames().apply, functionProtoFuncApply);
61 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().apply, *applyFunction, DontEnum);
63 *callFunction = JSFunction::create(exec, globalObject, 1, exec->propertyNames().call, functionProtoFuncCall);
64 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().call, *callFunction, DontEnum);
66 JSFunction* bindFunction = JSFunction::create(exec, globalObject, 1, exec->propertyNames().bind, functionProtoFuncBind);
67 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().bind, bindFunction, DontEnum);
70 static EncodedJSValue JSC_HOST_CALL callFunctionPrototype(ExecState*)
72 return JSValue::encode(jsUndefined());
76 CallType FunctionPrototype::getCallData(JSCell*, CallData& callData)
78 callData.native.function = callFunctionPrototype;
84 // Compatibility hack for the Optimost JavaScript library. (See <rdar://problem/6595040>.)
85 static inline void insertSemicolonIfNeeded(UString& functionBody)
87 ASSERT(functionBody[0] == '{');
88 ASSERT(functionBody[functionBody.length() - 1] == '}');
90 for (size_t i = functionBody.length() - 2; i > 0; --i) {
91 UChar ch = functionBody[i];
92 if (!Lexer<UChar>::isWhiteSpace(ch) && !Lexer<UChar>::isLineTerminator(ch)) {
93 if (ch != ';' && ch != '}')
94 functionBody = makeUString(functionBody.substringSharingImpl(0, i + 1), ";", functionBody.substringSharingImpl(i + 1, functionBody.length() - (i + 1)));
100 EncodedJSValue JSC_HOST_CALL functionProtoFuncToString(ExecState* exec)
102 JSValue thisValue = exec->hostThisValue();
103 if (thisValue.inherits(&JSFunction::s_info)) {
104 JSFunction* function = asFunction(thisValue);
105 if (function->isHostFunction())
106 return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "() {\n [native code]\n}"));
107 FunctionExecutable* executable = function->jsExecutable();
108 UString sourceString = executable->source().toString();
109 insertSemicolonIfNeeded(sourceString);
110 return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "(", executable->paramString(), ") ", sourceString));
113 if (thisValue.inherits(&InternalFunction::s_info)) {
114 InternalFunction* function = asInternalFunction(thisValue);
115 return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "() {\n [native code]\n}"));
118 return throwVMTypeError(exec);
121 EncodedJSValue JSC_HOST_CALL functionProtoFuncApply(ExecState* exec)
123 JSValue thisValue = exec->hostThisValue();
125 CallType callType = getCallData(thisValue, callData);
126 if (callType == CallTypeNone)
127 return throwVMTypeError(exec);
129 JSValue array = exec->argument(1);
131 MarkedArgumentBuffer applyArgs;
132 if (!array.isUndefinedOrNull()) {
133 if (!array.isObject())
134 return throwVMTypeError(exec);
135 if (asObject(array)->classInfo() == &Arguments::s_info) {
136 if (asArguments(array)->length(exec) > Arguments::MaxArguments)
137 return JSValue::encode(throwStackOverflowError(exec));
138 asArguments(array)->fillArgList(exec, applyArgs);
139 } else if (isJSArray(&exec->globalData(), array)) {
140 if (asArray(array)->length() > Arguments::MaxArguments)
141 return JSValue::encode(throwStackOverflowError(exec));
142 asArray(array)->fillArgList(exec, applyArgs);
144 unsigned length = asObject(array)->get(exec, exec->propertyNames().length).toUInt32(exec);
145 if (length > Arguments::MaxArguments)
146 return JSValue::encode(throwStackOverflowError(exec));
148 for (unsigned i = 0; i < length; ++i)
149 applyArgs.append(asObject(array)->get(exec, i));
153 return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), applyArgs));
156 EncodedJSValue JSC_HOST_CALL functionProtoFuncCall(ExecState* exec)
158 JSValue thisValue = exec->hostThisValue();
160 CallType callType = getCallData(thisValue, callData);
161 if (callType == CallTypeNone)
162 return throwVMTypeError(exec);
166 args.getSlice(1, callArgs);
167 return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), callArgs));
170 // 15.3.4.5 Function.prototype.bind (thisArg [, arg1 [, arg2, ...]])
171 EncodedJSValue JSC_HOST_CALL functionProtoFuncBind(ExecState* exec)
173 JSGlobalObject* globalObject = exec->callee()->globalObject();
175 // Let Target be the this value.
176 JSValue target = exec->hostThisValue();
178 // If IsCallable(Target) is false, throw a TypeError exception.
180 CallType callType = getCallData(target, callData);
181 if (callType == CallTypeNone)
182 return throwVMTypeError(exec);
183 // Primitive values are not callable.
184 ASSERT(target.isObject());
185 JSObject* targetObject = asObject(target);
187 // Let A be a new (possibly empty) internal list of all of the argument values provided after thisArg (arg1, arg2 etc), in order.
188 size_t numBoundArgs = exec->argumentCount() > 1 ? exec->argumentCount() - 1 : 0;
189 JSArray* boundArgs = JSArray::create(exec->globalData(), globalObject->arrayStructure(), numBoundArgs, CreateCompact);
190 for (size_t i = 0; i < numBoundArgs; ++i)
191 boundArgs->uncheckedSetIndex(exec->globalData(), i, exec->argument(i + 1));
192 boundArgs->setLength(numBoundArgs);
194 // If the [[Class]] internal property of Target is "Function", then ...
195 // Else set the length own property of F to 0.
197 if (targetObject->inherits(&JSFunction::s_info)) {
198 ASSERT(target.get(exec, exec->propertyNames().length).isNumber());
199 // a. Let L be the length property of Target minus the length of A.
200 // b. Set the length own property of F to either 0 or L, whichever is larger.
201 unsigned targetLength = (unsigned)target.get(exec, exec->propertyNames().length).asNumber();
202 if (targetLength > numBoundArgs)
203 length = targetLength - numBoundArgs;
206 Identifier name(exec, target.get(exec, exec->propertyNames().name).toString(exec));
208 return JSValue::encode(JSBoundFunction::create(exec, globalObject, targetObject, exec->argument(0), boundArgs, length, name));