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