1 /*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 *
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.
9 *
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.
14 *
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
18 *
19 */
20
21 #include "config.h"
22 #include "FunctionPrototype.h"
23
24 #include "Arguments.h"
25 #include "JSArray.h"
26 #include "JSFunction.h"
27 #include "JSString.h"
28 #include "Interpreter.h"
29 #include "Lexer.h"
30 #include "PrototypeFunction.h"
31
32 namespace JSC {
33
34 ASSERT_CLASS_FITS_IN_CELL(FunctionPrototype);
35
36 static JSValue JSC_HOST_CALL functionProtoFuncToString(ExecState*, JSObject*, JSValue, const ArgList&);
37 static JSValue JSC_HOST_CALL functionProtoFuncApply(ExecState*, JSObject*, JSValue, const ArgList&);
38 static JSValue JSC_HOST_CALL functionProtoFuncCall(ExecState*, JSObject*, JSValue, const ArgList&);
39
FunctionPrototype(ExecState * exec,PassRefPtr<Structure> structure)40 FunctionPrototype::FunctionPrototype(ExecState* exec, PassRefPtr<Structure> structure)
41 : InternalFunction(&exec->globalData(), structure, exec->propertyNames().nullIdentifier)
42 {
43 putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 0), DontDelete | ReadOnly | DontEnum);
44 }
45
addFunctionProperties(ExecState * exec,Structure * prototypeFunctionStructure,NativeFunctionWrapper ** callFunction,NativeFunctionWrapper ** applyFunction)46 void FunctionPrototype::addFunctionProperties(ExecState* exec, Structure* prototypeFunctionStructure, NativeFunctionWrapper** callFunction, NativeFunctionWrapper** applyFunction)
47 {
48 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 0, exec->propertyNames().toString, functionProtoFuncToString), DontEnum);
49 *applyFunction = new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 2, exec->propertyNames().apply, functionProtoFuncApply);
50 putDirectFunctionWithoutTransition(exec, *applyFunction, DontEnum);
51 *callFunction = new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().call, functionProtoFuncCall);
52 putDirectFunctionWithoutTransition(exec, *callFunction, DontEnum);
53 }
54
callFunctionPrototype(ExecState *,JSObject *,JSValue,const ArgList &)55 static JSValue JSC_HOST_CALL callFunctionPrototype(ExecState*, JSObject*, JSValue, const ArgList&)
56 {
57 return jsUndefined();
58 }
59
60 // ECMA 15.3.4
getCallData(CallData & callData)61 CallType FunctionPrototype::getCallData(CallData& callData)
62 {
63 callData.native.function = callFunctionPrototype;
64 return CallTypeHost;
65 }
66
67 // Functions
68
69 // Compatibility hack for the Optimost JavaScript library. (See <rdar://problem/6595040>.)
insertSemicolonIfNeeded(UString & functionBody)70 static inline void insertSemicolonIfNeeded(UString& functionBody)
71 {
72 ASSERT(functionBody[0] == '{');
73 ASSERT(functionBody[functionBody.size() - 1] == '}');
74
75 for (size_t i = functionBody.size() - 2; i > 0; --i) {
76 UChar ch = functionBody[i];
77 if (!Lexer::isWhiteSpace(ch) && !Lexer::isLineTerminator(ch)) {
78 if (ch != ';' && ch != '}')
79 functionBody = functionBody.substr(0, i + 1) + ";" + functionBody.substr(i + 1, functionBody.size() - (i + 1));
80 return;
81 }
82 }
83 }
84
functionProtoFuncToString(ExecState * exec,JSObject *,JSValue thisValue,const ArgList &)85 JSValue JSC_HOST_CALL functionProtoFuncToString(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
86 {
87 if (thisValue.isObject(&JSFunction::info)) {
88 JSFunction* function = asFunction(thisValue);
89 if (!function->isHostFunction()) {
90 UString functionBody = function->body()->toSourceString();
91 insertSemicolonIfNeeded(functionBody);
92 return jsString(exec, "function " + function->name(&exec->globalData()) + "(" + function->body()->paramString() + ") " + functionBody);
93 }
94 }
95
96 if (thisValue.isObject(&InternalFunction::info)) {
97 InternalFunction* function = asInternalFunction(thisValue);
98 return jsString(exec, "function " + function->name(&exec->globalData()) + "() {\n [native code]\n}");
99 }
100
101 return throwError(exec, TypeError);
102 }
103
functionProtoFuncApply(ExecState * exec,JSObject *,JSValue thisValue,const ArgList & args)104 JSValue JSC_HOST_CALL functionProtoFuncApply(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
105 {
106 CallData callData;
107 CallType callType = thisValue.getCallData(callData);
108 if (callType == CallTypeNone)
109 return throwError(exec, TypeError);
110
111 JSValue array = args.at(1);
112
113 MarkedArgumentBuffer applyArgs;
114 if (!array.isUndefinedOrNull()) {
115 if (!array.isObject())
116 return throwError(exec, TypeError);
117 if (asObject(array)->classInfo() == &Arguments::info)
118 asArguments(array)->fillArgList(exec, applyArgs);
119 else if (isJSArray(&exec->globalData(), array))
120 asArray(array)->fillArgList(exec, applyArgs);
121 else if (asObject(array)->inherits(&JSArray::info)) {
122 unsigned length = asArray(array)->get(exec, exec->propertyNames().length).toUInt32(exec);
123 for (unsigned i = 0; i < length; ++i)
124 applyArgs.append(asArray(array)->get(exec, i));
125 } else
126 return throwError(exec, TypeError);
127 }
128
129 return call(exec, thisValue, callType, callData, args.at(0), applyArgs);
130 }
131
functionProtoFuncCall(ExecState * exec,JSObject *,JSValue thisValue,const ArgList & args)132 JSValue JSC_HOST_CALL functionProtoFuncCall(ExecState* exec, JSObject*, JSValue thisValue, const ArgList& args)
133 {
134 CallData callData;
135 CallType callType = thisValue.getCallData(callData);
136 if (callType == CallTypeNone)
137 return throwError(exec, TypeError);
138
139 ArgList callArgs;
140 args.getSlice(1, callArgs);
141 return call(exec, thisValue, callType, callData, args.at(0), callArgs);
142 }
143
144 } // namespace JSC
145