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(JSGlobalObject* globalObject, Structure* structure)
41 : InternalFunction(globalObject, structure)
45 void FunctionPrototype::finishCreation(ExecState* exec, const Identifier& name)
47 Base::finishCreation(exec->globalData(), name);
48 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
51 void FunctionPrototype::addFunctionProperties(ExecState* exec, JSGlobalObject* globalObject, Structure* functionStructure, JSFunction** callFunction, JSFunction** applyFunction)
53 putDirectFunctionWithoutTransition(exec, JSFunction::create(exec, globalObject, functionStructure, 0, exec->propertyNames().toString, functionProtoFuncToString), DontEnum);
54 *applyFunction = JSFunction::create(exec, globalObject, functionStructure, 2, exec->propertyNames().apply, functionProtoFuncApply);
55 putDirectFunctionWithoutTransition(exec, *applyFunction, DontEnum);
56 *callFunction = JSFunction::create(exec, globalObject, functionStructure, 1, exec->propertyNames().call, functionProtoFuncCall);
57 putDirectFunctionWithoutTransition(exec, *callFunction, DontEnum);
60 static EncodedJSValue JSC_HOST_CALL callFunctionPrototype(ExecState*)
62 return JSValue::encode(jsUndefined());
66 CallType FunctionPrototype::getCallData(CallData& callData)
68 callData.native.function = callFunctionPrototype;
74 // Compatibility hack for the Optimost JavaScript library. (See <rdar://problem/6595040>.)
75 static inline void insertSemicolonIfNeeded(UString& functionBody)
77 ASSERT(functionBody[0] == '{');
78 ASSERT(functionBody[functionBody.length() - 1] == '}');
80 for (size_t i = functionBody.length() - 2; i > 0; --i) {
81 UChar ch = functionBody[i];
82 if (!Lexer::isWhiteSpace(ch) && !Lexer::isLineTerminator(ch)) {
83 if (ch != ';' && ch != '}')
84 functionBody = makeUString(functionBody.substringSharingImpl(0, i + 1), ";", functionBody.substringSharingImpl(i + 1, functionBody.length() - (i + 1)));
90 EncodedJSValue JSC_HOST_CALL functionProtoFuncToString(ExecState* exec)
92 JSValue thisValue = exec->hostThisValue();
93 if (thisValue.inherits(&JSFunction::s_info)) {
94 JSFunction* function = asFunction(thisValue);
95 if (function->isHostFunction())
96 return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "() {\n [native code]\n}"));
97 FunctionExecutable* executable = function->jsExecutable();
98 UString sourceString = executable->source().toString();
99 insertSemicolonIfNeeded(sourceString);
100 return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "(", executable->paramString(), ") ", sourceString));
103 if (thisValue.inherits(&InternalFunction::s_info)) {
104 InternalFunction* function = asInternalFunction(thisValue);
105 return JSValue::encode(jsMakeNontrivialString(exec, "function ", function->name(exec), "() {\n [native code]\n}"));
108 return throwVMTypeError(exec);
111 EncodedJSValue JSC_HOST_CALL functionProtoFuncApply(ExecState* exec)
113 JSValue thisValue = exec->hostThisValue();
115 CallType callType = getCallData(thisValue, callData);
116 if (callType == CallTypeNone)
117 return throwVMTypeError(exec);
119 JSValue array = exec->argument(1);
121 MarkedArgumentBuffer applyArgs;
122 if (!array.isUndefinedOrNull()) {
123 if (!array.isObject())
124 return throwVMTypeError(exec);
125 if (asObject(array)->classInfo() == &Arguments::s_info)
126 asArguments(array)->fillArgList(exec, applyArgs);
127 else if (isJSArray(&exec->globalData(), array))
128 asArray(array)->fillArgList(exec, applyArgs);
130 unsigned length = asObject(array)->get(exec, exec->propertyNames().length).toUInt32(exec);
131 for (unsigned i = 0; i < length; ++i)
132 applyArgs.append(asArray(array)->get(exec, i));
136 return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), applyArgs));
139 EncodedJSValue JSC_HOST_CALL functionProtoFuncCall(ExecState* exec)
141 JSValue thisValue = exec->hostThisValue();
143 CallType callType = getCallData(thisValue, callData);
144 if (callType == CallTypeNone)
145 return throwVMTypeError(exec);
149 args.getSlice(1, callArgs);
150 return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), callArgs));