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 "internal.h"
|
---|
28 | #include <wtf/OwnPtr.h>
|
---|
29 | #include <wtf/Vector.h>
|
---|
30 |
|
---|
31 | namespace KJS {
|
---|
32 |
|
---|
33 | class ActivationImp;
|
---|
34 | class FunctionBodyNode;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @short Implementation class for internal Functions.
|
---|
38 | */
|
---|
39 | class FunctionImp : public InternalFunctionImp {
|
---|
40 | friend class ActivationImp;
|
---|
41 | public:
|
---|
42 | FunctionImp(ExecState*, const Identifier& n, FunctionBodyNode* b);
|
---|
43 | virtual ~FunctionImp();
|
---|
44 |
|
---|
45 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
46 | virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
|
---|
47 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
48 |
|
---|
49 | virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List& args);
|
---|
50 |
|
---|
51 | // Note: unlike body->paramName, this returns Identifier::null for parameters
|
---|
52 | // that will never get set, due to later param having the same name
|
---|
53 | Identifier getParameterName(int index);
|
---|
54 | virtual CodeType codeType() const = 0;
|
---|
55 |
|
---|
56 | virtual Completion execute(ExecState*) = 0;
|
---|
57 |
|
---|
58 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
59 | static const ClassInfo info;
|
---|
60 |
|
---|
61 | RefPtr<FunctionBodyNode> body;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Returns the scope of this object. This is used when execution declared
|
---|
65 | * functions - the execution context for the function is initialized with
|
---|
66 | * extra object in it's scope. An example of this is functions declared
|
---|
67 | * inside other functions:
|
---|
68 | *
|
---|
69 | * \code
|
---|
70 | * function f() {
|
---|
71 | *
|
---|
72 | * function b() {
|
---|
73 | * return prototype;
|
---|
74 | * }
|
---|
75 | *
|
---|
76 | * var x = 4;
|
---|
77 | * // do some stuff
|
---|
78 | * }
|
---|
79 | * f.prototype = new String();
|
---|
80 | * \endcode
|
---|
81 | *
|
---|
82 | * When the function f.b is executed, its scope will include properties of
|
---|
83 | * f. So in the example above the return value of f.b() would be the new
|
---|
84 | * String object that was assigned to f.prototype.
|
---|
85 | *
|
---|
86 | * @param exec The current execution state
|
---|
87 | * @return The function's scope
|
---|
88 | */
|
---|
89 | const ScopeChain& scope() const { return _scope; }
|
---|
90 | void setScope(const ScopeChain& s) { _scope = s; }
|
---|
91 |
|
---|
92 | virtual void mark();
|
---|
93 |
|
---|
94 | private:
|
---|
95 | ScopeChain _scope;
|
---|
96 |
|
---|
97 | static JSValue* argumentsGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
|
---|
98 | static JSValue* callerGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
|
---|
99 | static JSValue* lengthGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
|
---|
100 |
|
---|
101 | void passInParameters(ExecState*, const List&);
|
---|
102 | virtual void processVarDecls(ExecState*);
|
---|
103 | };
|
---|
104 |
|
---|
105 | class DeclaredFunctionImp : public FunctionImp {
|
---|
106 | public:
|
---|
107 | DeclaredFunctionImp(ExecState*, const Identifier& n,
|
---|
108 | FunctionBodyNode* b, const ScopeChain& sc);
|
---|
109 |
|
---|
110 | bool implementsConstruct() const;
|
---|
111 | JSObject* construct(ExecState*, const List& args);
|
---|
112 |
|
---|
113 | virtual Completion execute(ExecState*);
|
---|
114 | CodeType codeType() const { return FunctionCode; }
|
---|
115 |
|
---|
116 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
117 | static const ClassInfo info;
|
---|
118 |
|
---|
119 | private:
|
---|
120 | virtual void processVarDecls(ExecState*);
|
---|
121 | };
|
---|
122 |
|
---|
123 | class IndexToNameMap {
|
---|
124 | public:
|
---|
125 | IndexToNameMap(FunctionImp* func, const List& args);
|
---|
126 | ~IndexToNameMap();
|
---|
127 |
|
---|
128 | Identifier& operator[](int index);
|
---|
129 | Identifier& operator[](const Identifier &indexIdentifier);
|
---|
130 | bool isMapped(const Identifier& index) const;
|
---|
131 | void unMap(const Identifier& index);
|
---|
132 |
|
---|
133 | private:
|
---|
134 | IndexToNameMap(); // prevent construction w/o parameters
|
---|
135 | int size;
|
---|
136 | Identifier* _map;
|
---|
137 | };
|
---|
138 |
|
---|
139 | class Arguments : public JSObject {
|
---|
140 | public:
|
---|
141 | Arguments(ExecState*, FunctionImp* func, const List& args, ActivationImp* act);
|
---|
142 | virtual void mark();
|
---|
143 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
144 | virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
|
---|
145 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
146 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
147 | static const ClassInfo info;
|
---|
148 | private:
|
---|
149 | static JSValue* mappedIndexGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot);
|
---|
150 |
|
---|
151 | ActivationImp* _activationObject;
|
---|
152 | mutable IndexToNameMap indexToNameMap;
|
---|
153 | };
|
---|
154 |
|
---|
155 | class ActivationImp : public JSObject {
|
---|
156 | public:
|
---|
157 | ActivationImp(FunctionImp* function, const List& arguments);
|
---|
158 |
|
---|
159 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
160 | virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
|
---|
161 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
162 |
|
---|
163 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
164 | static const ClassInfo info;
|
---|
165 |
|
---|
166 | virtual void mark();
|
---|
167 |
|
---|
168 | bool isActivation() { return true; }
|
---|
169 |
|
---|
170 | void releaseArguments() { _arguments.reset(); }
|
---|
171 |
|
---|
172 | private:
|
---|
173 | static PropertySlot::GetValueFunc getArgumentsGetter();
|
---|
174 | static JSValue* argumentsGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot);
|
---|
175 | void createArgumentsObject(ExecState*);
|
---|
176 |
|
---|
177 | FunctionImp* _function;
|
---|
178 | List _arguments;
|
---|
179 | mutable Arguments* _argumentsObject;
|
---|
180 | };
|
---|
181 |
|
---|
182 | class GlobalFuncImp : public InternalFunctionImp {
|
---|
183 | public:
|
---|
184 | GlobalFuncImp(ExecState*, FunctionPrototype*, int i, int len, const Identifier&);
|
---|
185 | virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List& args);
|
---|
186 | virtual CodeType codeType() const;
|
---|
187 | enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, Escape, UnEscape,
|
---|
188 | DecodeURI, DecodeURIComponent, EncodeURI, EncodeURIComponent
|
---|
189 | #ifndef NDEBUG
|
---|
190 | , KJSPrint
|
---|
191 | #endif
|
---|
192 | };
|
---|
193 | private:
|
---|
194 | int id;
|
---|
195 | };
|
---|
196 |
|
---|
197 | UString escapeStringForPrettyPrinting(const UString& s);
|
---|
198 |
|
---|
199 | } // namespace
|
---|
200 |
|
---|
201 | #endif
|
---|