|
1 // -*- c-basic-offset: 2 -*- |
|
2 /* |
|
3 * This file is part of the KDE libraries |
|
4 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
|
5 * Copyright (C) 2003, 2006 Apple Computer, Inc. |
|
6 * |
|
7 * This library is free software; you can redistribute it and/or |
|
8 * modify it under the terms of the GNU Library General Public |
|
9 * License as published by the Free Software Foundation; either |
|
10 * version 2 of the License, or (at your option) any later version. |
|
11 * |
|
12 * This library is distributed in the hope that it will be useful, |
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 * Library General Public License for more details. |
|
16 * |
|
17 * You should have received a copy of the GNU Library General Public License |
|
18 * along with this library; see the file COPYING.LIB. If not, write to |
|
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
20 * Boston, MA 02110-1301, USA. |
|
21 * |
|
22 */ |
|
23 |
|
24 #ifndef KJS_FUNCTION_H |
|
25 #define KJS_FUNCTION_H |
|
26 |
|
27 #include "object.h" |
|
28 #include <wtf/OwnPtr.h> |
|
29 #include <wtf/Vector.h> |
|
30 |
|
31 namespace KJS { |
|
32 |
|
33 class ActivationImp; |
|
34 class FunctionBodyNode; |
|
35 class FunctionPrototype; |
|
36 |
|
37 enum CodeType { GlobalCode, |
|
38 EvalCode, |
|
39 FunctionCode, |
|
40 AnonymousCode }; |
|
41 |
|
42 class InternalFunctionImp : public JSObject { |
|
43 public: |
|
44 InternalFunctionImp(); |
|
45 IMPORT InternalFunctionImp(FunctionPrototype*, const Identifier&); |
|
46 |
|
47 IMPORT virtual bool implementsCall() const; |
|
48 virtual JSValue* callAsFunction(ExecState*, JSObject* thisObjec, const List& args) = 0; |
|
49 IMPORT virtual bool implementsHasInstance() const; |
|
50 |
|
51 #if PLATFORM(SYMBIAN) |
|
52 // compiler issue |
|
53 IMPORT virtual const ClassInfo* classInfo() const; |
|
54 #else |
|
55 virtual const ClassInfo* classInfo() const { return &info; } |
|
56 #endif |
|
57 static const ClassInfo info; |
|
58 const Identifier& functionName() const { return m_name; } |
|
59 |
|
60 private: |
|
61 Identifier m_name; |
|
62 }; |
|
63 |
|
64 /** |
|
65 * @internal |
|
66 * |
|
67 * The initial value of Function.prototype (and thus all objects created |
|
68 * with the Function constructor) |
|
69 */ |
|
70 class FunctionPrototype : public InternalFunctionImp { |
|
71 public: |
|
72 FunctionPrototype(ExecState *exec); |
|
73 virtual ~FunctionPrototype(); |
|
74 |
|
75 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args); |
|
76 }; |
|
77 |
|
78 /** |
|
79 * @short Implementation class for internal Functions. |
|
80 */ |
|
81 class FunctionImp : public InternalFunctionImp { |
|
82 friend class ActivationImp; |
|
83 public: |
|
84 FunctionImp(ExecState*, const Identifier& n, FunctionBodyNode* b); |
|
85 virtual ~FunctionImp(); |
|
86 |
|
87 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&); |
|
88 virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None); |
|
89 virtual bool deleteProperty(ExecState*, const Identifier& propertyName); |
|
90 |
|
91 virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List& args); |
|
92 |
|
93 // Note: unlike body->paramName, this returns Identifier::null for parameters |
|
94 // that will never get set, due to later param having the same name |
|
95 Identifier getParameterName(int index); |
|
96 virtual CodeType codeType() const = 0; |
|
97 |
|
98 virtual Completion execute(ExecState*) = 0; |
|
99 |
|
100 virtual const ClassInfo* classInfo() const { return &info; } |
|
101 static const ClassInfo info; |
|
102 |
|
103 RefPtr<FunctionBodyNode> body; |
|
104 |
|
105 /** |
|
106 * Returns the scope of this object. This is used when execution declared |
|
107 * functions - the execution context for the function is initialized with |
|
108 * extra object in it's scope. An example of this is functions declared |
|
109 * inside other functions: |
|
110 * |
|
111 * \code |
|
112 * function f() { |
|
113 * |
|
114 * function b() { |
|
115 * return prototype; |
|
116 * } |
|
117 * |
|
118 * var x = 4; |
|
119 * // do some stuff |
|
120 * } |
|
121 * f.prototype = new String(); |
|
122 * \endcode |
|
123 * |
|
124 * When the function f.b is executed, its scope will include properties of |
|
125 * f. So in the example above the return value of f.b() would be the new |
|
126 * String object that was assigned to f.prototype. |
|
127 * |
|
128 * @param exec The current execution state |
|
129 * @return The function's scope |
|
130 */ |
|
131 const ScopeChain& scope() const { return _scope; } |
|
132 void setScope(const ScopeChain& s) { _scope = s; } |
|
133 |
|
134 virtual void mark(); |
|
135 |
|
136 private: |
|
137 ScopeChain _scope; |
|
138 |
|
139 static JSValue* argumentsGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&); |
|
140 static JSValue* callerGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&); |
|
141 static JSValue* lengthGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&); |
|
142 |
|
143 void passInParameters(ExecState*, const List&); |
|
144 virtual void processVarDecls(ExecState*); |
|
145 }; |
|
146 |
|
147 class DeclaredFunctionImp : public FunctionImp { |
|
148 public: |
|
149 DeclaredFunctionImp(ExecState*, const Identifier& n, |
|
150 FunctionBodyNode* b, const ScopeChain& sc); |
|
151 |
|
152 bool implementsConstruct() const; |
|
153 JSObject* construct(ExecState*, const List& args); |
|
154 |
|
155 virtual Completion execute(ExecState*); |
|
156 CodeType codeType() const { return FunctionCode; } |
|
157 |
|
158 virtual const ClassInfo* classInfo() const { return &info; } |
|
159 static const ClassInfo info; |
|
160 |
|
161 private: |
|
162 virtual void processVarDecls(ExecState*); |
|
163 }; |
|
164 |
|
165 class IndexToNameMap { |
|
166 public: |
|
167 IndexToNameMap(FunctionImp* func, const List& args); |
|
168 ~IndexToNameMap(); |
|
169 |
|
170 Identifier& operator[](int index); |
|
171 Identifier& operator[](const Identifier &indexIdentifier); |
|
172 bool isMapped(const Identifier& index) const; |
|
173 void unMap(const Identifier& index); |
|
174 |
|
175 private: |
|
176 IndexToNameMap(); // prevent construction w/o parameters |
|
177 int size; |
|
178 Identifier* _map; |
|
179 }; |
|
180 |
|
181 class Arguments : public JSObject { |
|
182 public: |
|
183 Arguments(ExecState*, FunctionImp* func, const List& args, ActivationImp* act); |
|
184 virtual void mark(); |
|
185 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&); |
|
186 virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None); |
|
187 virtual bool deleteProperty(ExecState*, const Identifier& propertyName); |
|
188 virtual const ClassInfo* classInfo() const { return &info; } |
|
189 static const ClassInfo info; |
|
190 private: |
|
191 static JSValue* mappedIndexGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot); |
|
192 |
|
193 ActivationImp* _activationObject; |
|
194 mutable IndexToNameMap indexToNameMap; |
|
195 }; |
|
196 |
|
197 class ActivationImp : public JSObject { |
|
198 public: |
|
199 ActivationImp(FunctionImp* function, const List& arguments); |
|
200 |
|
201 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&); |
|
202 virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None); |
|
203 virtual bool deleteProperty(ExecState*, const Identifier& propertyName); |
|
204 |
|
205 virtual const ClassInfo* classInfo() const { return &info; } |
|
206 static const ClassInfo info; |
|
207 |
|
208 virtual void mark(); |
|
209 |
|
210 bool isActivation() { return true; } |
|
211 |
|
212 void releaseArguments() { _arguments.reset(); } |
|
213 |
|
214 private: |
|
215 static PropertySlot::GetValueFunc getArgumentsGetter(); |
|
216 static JSValue* argumentsGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot); |
|
217 void createArgumentsObject(ExecState*); |
|
218 |
|
219 FunctionImp* _function; |
|
220 List _arguments; |
|
221 mutable Arguments* _argumentsObject; |
|
222 }; |
|
223 |
|
224 class GlobalFuncImp : public InternalFunctionImp { |
|
225 public: |
|
226 GlobalFuncImp(ExecState*, FunctionPrototype*, int i, int len, const Identifier&); |
|
227 virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List& args); |
|
228 virtual CodeType codeType() const; |
|
229 enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, Escape, UnEscape, |
|
230 DecodeURI, DecodeURIComponent, EncodeURI, EncodeURIComponent |
|
231 #ifndef NDEBUG |
|
232 , KJSPrint |
|
233 #endif |
|
234 }; |
|
235 private: |
|
236 int id; |
|
237 }; |
|
238 |
|
239 static const double mantissaOverflowLowerBound = 9007199254740992.0; |
|
240 double parseIntOverflow(const char* s, int length, int radix); |
|
241 |
|
242 UString escapeStringForPrettyPrinting(const UString& s); |
|
243 |
|
244 } // namespace |
|
245 |
|
246 #endif |