tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / runtime / FunctionPrototype.cpp
1 /*
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.
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 "JSBoundFunction.h"
27 #include "JSFunction.h"
28 #include "JSString.h"
29 #include "JSStringBuilder.h"
30 #include "Interpreter.h"
31 #include "Lexer.h"
32
33 namespace JSC {
34
35 ASSERT_CLASS_FITS_IN_CELL(FunctionPrototype);
36
37 const ClassInfo FunctionPrototype::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(FunctionPrototype) };
38
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*);
43
44 FunctionPrototype::FunctionPrototype(JSGlobalObject* globalObject, Structure* structure)
45     : InternalFunction(globalObject, structure)
46 {
47 }
48
49 void FunctionPrototype::finishCreation(ExecState* exec, const Identifier& name)
50 {
51     Base::finishCreation(exec->globalData(), name);
52     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
53 }
54
55 void FunctionPrototype::addFunctionProperties(ExecState* exec, JSGlobalObject* globalObject, JSFunction** callFunction, JSFunction** applyFunction)
56 {
57     JSFunction* toStringFunction = JSFunction::create(exec, globalObject, 0, exec->propertyNames().toString, functionProtoFuncToString);
58     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().toString, toStringFunction, DontEnum);
59
60     *applyFunction = JSFunction::create(exec, globalObject, 2, exec->propertyNames().apply, functionProtoFuncApply);
61     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().apply, *applyFunction, DontEnum);
62
63     *callFunction = JSFunction::create(exec, globalObject, 1, exec->propertyNames().call, functionProtoFuncCall);
64     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().call, *callFunction, DontEnum);
65
66     JSFunction* bindFunction = JSFunction::create(exec, globalObject, 1, exec->propertyNames().bind, functionProtoFuncBind);
67     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().bind, bindFunction, DontEnum);
68 }
69
70 static EncodedJSValue JSC_HOST_CALL callFunctionPrototype(ExecState*)
71 {
72     return JSValue::encode(jsUndefined());
73 }
74
75 // ECMA 15.3.4
76 CallType FunctionPrototype::getCallData(JSCell*, CallData& callData)
77 {
78     callData.native.function = callFunctionPrototype;
79     return CallTypeHost;
80 }
81
82 // Functions
83
84 // Compatibility hack for the Optimost JavaScript library. (See <rdar://problem/6595040>.)
85 static inline void insertSemicolonIfNeeded(UString& functionBody)
86 {
87     ASSERT(functionBody[0] == '{');
88     ASSERT(functionBody[functionBody.length() - 1] == '}');
89
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)));
95             return;
96         }
97     }
98 }
99
100 EncodedJSValue JSC_HOST_CALL functionProtoFuncToString(ExecState* exec)
101 {
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));
111     }
112
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}"));
116     }
117
118     return throwVMTypeError(exec);
119 }
120
121 EncodedJSValue JSC_HOST_CALL functionProtoFuncApply(ExecState* exec)
122 {
123     JSValue thisValue = exec->hostThisValue();
124     CallData callData;
125     CallType callType = getCallData(thisValue, callData);
126     if (callType == CallTypeNone)
127         return throwVMTypeError(exec);
128
129     JSValue array = exec->argument(1);
130
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);
143         } else {
144             unsigned length = asObject(array)->get(exec, exec->propertyNames().length).toUInt32(exec);
145             if (length > Arguments::MaxArguments)
146                 return JSValue::encode(throwStackOverflowError(exec));
147
148             for (unsigned i = 0; i < length; ++i)
149                 applyArgs.append(asObject(array)->get(exec, i));
150         }
151     }
152     
153     return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), applyArgs));
154 }
155
156 EncodedJSValue JSC_HOST_CALL functionProtoFuncCall(ExecState* exec)
157 {
158     JSValue thisValue = exec->hostThisValue();
159     CallData callData;
160     CallType callType = getCallData(thisValue, callData);
161     if (callType == CallTypeNone)
162         return throwVMTypeError(exec);
163
164     ArgList args(exec);
165     ArgList callArgs;
166     args.getSlice(1, callArgs);
167     return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), callArgs));
168 }
169
170 // 15.3.4.5 Function.prototype.bind (thisArg [, arg1 [, arg2, ...]])
171 EncodedJSValue JSC_HOST_CALL functionProtoFuncBind(ExecState* exec)
172 {
173     JSGlobalObject* globalObject = exec->callee()->globalObject();
174
175     // Let Target be the this value.
176     JSValue target = exec->hostThisValue();
177
178     // If IsCallable(Target) is false, throw a TypeError exception.
179     CallData callData;
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);
186
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);
193
194     // If the [[Class]] internal property of Target is "Function", then ...
195     // Else set the length own property of F to 0.
196     unsigned length = 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;
204     }
205
206     Identifier name(exec, target.get(exec, exec->propertyNames().name).toString(exec));
207
208     return JSValue::encode(JSBoundFunction::create(exec, globalObject, targetObject, exec->argument(0), boundArgs, length, name));
209 }
210
211 } // namespace JSC