initial import
[vuplus_webkit] / 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 "JSFunction.h"
27 #include "JSString.h"
28 #include "JSStringBuilder.h"
29 #include "Interpreter.h"
30 #include "Lexer.h"
31
32 namespace JSC {
33
34 ASSERT_CLASS_FITS_IN_CELL(FunctionPrototype);
35
36 static EncodedJSValue JSC_HOST_CALL functionProtoFuncToString(ExecState*);
37 static EncodedJSValue JSC_HOST_CALL functionProtoFuncApply(ExecState*);
38 static EncodedJSValue JSC_HOST_CALL functionProtoFuncCall(ExecState*);
39
40 FunctionPrototype::FunctionPrototype(JSGlobalObject* globalObject, Structure* structure)
41     : InternalFunction(globalObject, structure)
42 {
43 }
44
45 void FunctionPrototype::finishCreation(ExecState* exec, const Identifier& name)
46 {
47     Base::finishCreation(exec->globalData(), name);
48     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(0), DontDelete | ReadOnly | DontEnum);
49 }
50
51 void FunctionPrototype::addFunctionProperties(ExecState* exec, JSGlobalObject* globalObject, Structure* functionStructure, JSFunction** callFunction, JSFunction** applyFunction)
52 {
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);
58 }
59
60 static EncodedJSValue JSC_HOST_CALL callFunctionPrototype(ExecState*)
61 {
62     return JSValue::encode(jsUndefined());
63 }
64
65 // ECMA 15.3.4
66 CallType FunctionPrototype::getCallData(CallData& callData)
67 {
68     callData.native.function = callFunctionPrototype;
69     return CallTypeHost;
70 }
71
72 // Functions
73
74 // Compatibility hack for the Optimost JavaScript library. (See <rdar://problem/6595040>.)
75 static inline void insertSemicolonIfNeeded(UString& functionBody)
76 {
77     ASSERT(functionBody[0] == '{');
78     ASSERT(functionBody[functionBody.length() - 1] == '}');
79
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)));
85             return;
86         }
87     }
88 }
89
90 EncodedJSValue JSC_HOST_CALL functionProtoFuncToString(ExecState* exec)
91 {
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));
101     }
102
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}"));
106     }
107
108     return throwVMTypeError(exec);
109 }
110
111 EncodedJSValue JSC_HOST_CALL functionProtoFuncApply(ExecState* exec)
112 {
113     JSValue thisValue = exec->hostThisValue();
114     CallData callData;
115     CallType callType = getCallData(thisValue, callData);
116     if (callType == CallTypeNone)
117         return throwVMTypeError(exec);
118
119     JSValue array = exec->argument(1);
120
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);
129         else {
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));
133         }
134     }
135
136     return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), applyArgs));
137 }
138
139 EncodedJSValue JSC_HOST_CALL functionProtoFuncCall(ExecState* exec)
140 {
141     JSValue thisValue = exec->hostThisValue();
142     CallData callData;
143     CallType callType = getCallData(thisValue, callData);
144     if (callType == CallTypeNone)
145         return throwVMTypeError(exec);
146
147     ArgList args(exec);
148     ArgList callArgs;
149     args.getSlice(1, callArgs);
150     return JSValue::encode(call(exec, thisValue, callType, callData, exec->argument(0), callArgs));
151 }
152
153 } // namespace JSC