1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
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 | InternalFunctionImp(FunctionPrototype*);
|
---|
46 | InternalFunctionImp(FunctionPrototype*, const Identifier&);
|
---|
47 |
|
---|
48 | virtual bool implementsCall() const;
|
---|
49 | virtual JSValue* callAsFunction(ExecState*, JSObject* thisObjec, const List& args) = 0;
|
---|
50 | virtual bool implementsHasInstance() const;
|
---|
51 |
|
---|
52 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
53 | static const ClassInfo info;
|
---|
54 | const Identifier& functionName() const { return m_name; }
|
---|
55 |
|
---|
56 | private:
|
---|
57 | Identifier m_name;
|
---|
58 | };
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * @internal
|
---|
62 | *
|
---|
63 | * The initial value of Function.prototype (and thus all objects created
|
---|
64 | * with the Function constructor)
|
---|
65 | */
|
---|
66 | class FunctionPrototype : public InternalFunctionImp {
|
---|
67 | public:
|
---|
68 | FunctionPrototype(ExecState *exec);
|
---|
69 | virtual ~FunctionPrototype();
|
---|
70 |
|
---|
71 | virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
|
---|
72 | };
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * @short Implementation class for internal Functions.
|
---|
76 | */
|
---|
77 | class FunctionImp : public InternalFunctionImp {
|
---|
78 | friend class ActivationImp;
|
---|
79 | public:
|
---|
80 | FunctionImp(ExecState*, const Identifier& n, FunctionBodyNode* b);
|
---|
81 | virtual ~FunctionImp();
|
---|
82 |
|
---|
83 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
84 | virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
|
---|
85 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
86 |
|
---|
87 | virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List& args);
|
---|
88 |
|
---|
89 | // Note: unlike body->paramName, this returns Identifier::null for parameters
|
---|
90 | // that will never get set, due to later param having the same name
|
---|
91 | Identifier getParameterName(int index);
|
---|
92 | virtual CodeType codeType() const = 0;
|
---|
93 |
|
---|
94 | virtual Completion execute(ExecState*) = 0;
|
---|
95 |
|
---|
96 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
97 | static const ClassInfo info;
|
---|
98 |
|
---|
99 | RefPtr<FunctionBodyNode> body;
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Returns the scope of this object. This is used when execution declared
|
---|
103 | * functions - the execution context for the function is initialized with
|
---|
104 | * extra object in it's scope. An example of this is functions declared
|
---|
105 | * inside other functions:
|
---|
106 | *
|
---|
107 | * \code
|
---|
108 | * function f() {
|
---|
109 | *
|
---|
110 | * function b() {
|
---|
111 | * return prototype;
|
---|
112 | * }
|
---|
113 | *
|
---|
114 | * var x = 4;
|
---|
115 | * // do some stuff
|
---|
116 | * }
|
---|
117 | * f.prototype = new String();
|
---|
118 | * \endcode
|
---|
119 | *
|
---|
120 | * When the function f.b is executed, its scope will include properties of
|
---|
121 | * f. So in the example above the return value of f.b() would be the new
|
---|
122 | * String object that was assigned to f.prototype.
|
---|
123 | *
|
---|
124 | * @param exec The current execution state
|
---|
125 | * @return The function's scope
|
---|
126 | */
|
---|
127 | const ScopeChain& scope() const { return _scope; }
|
---|
128 | void setScope(const ScopeChain& s) { _scope = s; }
|
---|
129 |
|
---|
130 | virtual void mark();
|
---|
131 |
|
---|
132 | private:
|
---|
133 | ScopeChain _scope;
|
---|
134 |
|
---|
135 | static JSValue* argumentsGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
|
---|
136 | static JSValue* callerGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
|
---|
137 | static JSValue* lengthGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
|
---|
138 |
|
---|
139 | void passInParameters(ExecState*, const List&);
|
---|
140 | virtual void processVarDecls(ExecState*);
|
---|
141 | };
|
---|
142 |
|
---|
143 | class DeclaredFunctionImp : public FunctionImp {
|
---|
144 | public:
|
---|
145 | DeclaredFunctionImp(ExecState*, const Identifier& n,
|
---|
146 | FunctionBodyNode* b, const ScopeChain& sc);
|
---|
147 |
|
---|
148 | bool implementsConstruct() const;
|
---|
149 | JSObject* construct(ExecState*, const List& args);
|
---|
150 |
|
---|
151 | virtual Completion execute(ExecState*);
|
---|
152 | CodeType codeType() const { return FunctionCode; }
|
---|
153 |
|
---|
154 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
155 | static const ClassInfo info;
|
---|
156 |
|
---|
157 | private:
|
---|
158 | virtual void processVarDecls(ExecState*);
|
---|
159 | };
|
---|
160 |
|
---|
161 | class IndexToNameMap {
|
---|
162 | public:
|
---|
163 | IndexToNameMap(FunctionImp* func, const List& args);
|
---|
164 | ~IndexToNameMap();
|
---|
165 |
|
---|
166 | Identifier& operator[](int index);
|
---|
167 | Identifier& operator[](const Identifier &indexIdentifier);
|
---|
168 | bool isMapped(const Identifier& index) const;
|
---|
169 | void unMap(const Identifier& index);
|
---|
170 |
|
---|
171 | private:
|
---|
172 | IndexToNameMap(); // prevent construction w/o parameters
|
---|
173 | int size;
|
---|
174 | Identifier* _map;
|
---|
175 | };
|
---|
176 |
|
---|
177 | class Arguments : public JSObject {
|
---|
178 | public:
|
---|
179 | Arguments(ExecState*, FunctionImp* func, const List& args, ActivationImp* act);
|
---|
180 | virtual void mark();
|
---|
181 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
182 | virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
|
---|
183 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
184 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
185 | static const ClassInfo info;
|
---|
186 | private:
|
---|
187 | static JSValue* mappedIndexGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot);
|
---|
188 |
|
---|
189 | ActivationImp* _activationObject;
|
---|
190 | mutable IndexToNameMap indexToNameMap;
|
---|
191 | };
|
---|
192 |
|
---|
193 | class ActivationImp : public JSObject {
|
---|
194 | public:
|
---|
195 | ActivationImp(FunctionImp* function, const List& arguments);
|
---|
196 |
|
---|
197 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
198 | virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
|
---|
199 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
200 |
|
---|
201 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
202 | static const ClassInfo info;
|
---|
203 |
|
---|
204 | virtual void mark();
|
---|
205 |
|
---|
206 | bool isActivation() { return true; }
|
---|
207 |
|
---|
208 | void releaseArguments() { _arguments.reset(); }
|
---|
209 |
|
---|
210 | private:
|
---|
211 | static PropertySlot::GetValueFunc getArgumentsGetter();
|
---|
212 | static JSValue* argumentsGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot);
|
---|
213 | void createArgumentsObject(ExecState*);
|
---|
214 |
|
---|
215 | FunctionImp* _function;
|
---|
216 | List _arguments;
|
---|
217 | mutable Arguments* _argumentsObject;
|
---|
218 | };
|
---|
219 |
|
---|
220 | class GlobalFuncImp : public InternalFunctionImp {
|
---|
221 | public:
|
---|
222 | GlobalFuncImp(ExecState*, FunctionPrototype*, int i, int len, const Identifier&);
|
---|
223 | virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List& args);
|
---|
224 | virtual CodeType codeType() const;
|
---|
225 | enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, Escape, UnEscape,
|
---|
226 | DecodeURI, DecodeURIComponent, EncodeURI, EncodeURIComponent
|
---|
227 | #ifndef NDEBUG
|
---|
228 | , KJSPrint
|
---|
229 | #endif
|
---|
230 | };
|
---|
231 | private:
|
---|
232 | int id;
|
---|
233 | };
|
---|
234 |
|
---|
235 | UString escapeStringForPrettyPrinting(const UString& s);
|
---|
236 |
|
---|
237 | } // namespace
|
---|
238 |
|
---|
239 | #endif
|
---|