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 "JSFunction.h"
28 #include "JSStringBuilder.h"
29 #include "Interpreter.h"
34 ASSERT_CLASS_FITS_IN_CELL(FunctionPrototype);
36 static EncodedJSValue JSC_HOST_CALL functionProtoFuncToString(ExecState*);
37 static EncodedJSValue JSC_HOST_CALL functionProtoFuncApply(ExecState*);
38 static EncodedJSValue JSC_HOST_CALL functionProtoFuncCall(ExecState*);
40 FunctionPrototype::FunctionPrototype(ExecState* exec, JSGlobalObject* globalObject, Structure* structure)
41 : InternalFunction(globalObject, structure)
43 finishCreation(exec, exec->propertyNames().nullIdentifier);
46 void FunctionPrototype::finishCreation(ExecState* exec, const Identifier& name)
48 Base::finishCreation(exec->globalData(), name);
49 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
52 void FunctionPrototype::addFunctionProperties(ExecState* exec, JSGlobalObject* globalObject, Structure* functionStructure, JSFunction** callFunction, JSFunction** applyFunction)
54 putDirectFunctionWithoutTransition(exec, JSFunction::create(exec, globalObject, functionStructure, 0, exec->propertyNames().toString, functionProtoFuncToString), DontEnum);
55 *applyFunction = JSFunction::create(exec, globalObject, functionStructure, 2, exec->propertyNames().apply, functionProtoFuncApply);
56 putDirectFunctionWithoutTransition(exec, *applyFunction, DontEnum);
57 *callFunction = JSFunction::create(exec, globalObject, functionStructure, 1, exec->propertyNames().call, functionProtoFuncCall);
58 putDirectFunctionWithoutTransition(exec, *callFunction, DontEnum);
61 static EncodedJSValue JSC_HOST_CALL callFunctionPrototype(ExecState*)
63 return JSValue::encode(jsUndefined());
67 CallType FunctionPrototype::getCallData(CallData& callData)
69 callData.native.function = callFunctionPrototype;
75 // Compatibility hack for the Optimost JavaScript library. (See <rdar://problem/6595040>.)
76 static inline void insertSemicolonIfNeeded(UString& functionBody)
78 ASSERT(functionBody[0] == '{');
79 ASSERT(functionBody[functionBody.length() - 1] == '}');
81 for (size_t i = functionBody.length() - 2; i > 0; --i) {
82 UChar ch = functionBody[i];
83 if (!Lexer::isWhiteSpace(ch) && !Lexer::isLineTerminator(ch)) {
84 if (ch != ';' && ch != '}')
85 functionBody = makeUString(functionBody.substringSharingImpl(0, i + 1), ";", functionBody.substringSharingImpl(i + 1, functionBody.length() - (i + 1)));
91 EncodedJSValue JSC_HOST_CALL functionProtoFuncToString(ExecState* exec)
93 JSValue thisValue = exec->hostThisValue();
94 if (thisValue.inherits(&JSFunction::s_info)) {
95 JSFunction* function = asFunction(thisValue);
96 if (function->isHostFunction())
97 return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "() {\n [native code]\n}"));
98 FunctionExecutable* executable = function->jsExecutable();
99 UString sourceString = executable->source().toString();
100 insertSemicolonIfNeeded(sourceString);
101 return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "(", executable->paramString(), ") ", sourceString));
104 if (thisValue.inherits(&InternalFunction::s_info)) {
105 InternalFunction* function = asInternalFunction(thisValue);
106 return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "() {\n [native code]\n}"));
109 return throwVMTypeError(exec);
112 EncodedJSValue JSC_HOST_CALL functionProtoFuncApply(ExecState* exec)
114 JSValue thisValue = exec->hostThisValue();
116 CallType callType = getCallData(thisValue, callData);
117 if (callType == CallTypeNone)
118 return throwVMTypeError(exec);
120 JSValue array = exec->argument(1);
122 MarkedArgumentBuffer applyArgs;
123 if (!array.isUndefinedOrNull()) {
124 if (!array.isObject())
125 return throwVMTypeError(exec);
126 if (asObject(array)->classInfo() == &Arguments::s_info)
127 asArguments(array)->fillArgList(exec, applyArgs);
128 else if (isJSArray(&exec->globalData(), array))
129 asArray(array)->fillArgList(exec, applyArgs);
131 unsigned length = asObject(array)->get(exec, exec->propertyNames().length).toUInt32(exec);
132 for (unsigned i = 0; i < length; ++i)
133 applyArgs.append(asArray(array)->get(exec, i));
137 return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), applyArgs));
140 EncodedJSValue JSC_HOST_CALL functionProtoFuncCall(ExecState* exec)
142 JSValue thisValue = exec->hostThisValue();
144 CallType callType = getCallData(thisValue, callData);
145 if (callType == CallTypeNone)
146 return throwVMTypeError(exec);
150 args.getSlice(1, callArgs);
151 return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), callArgs));