1 // -*- c-basic-offset: 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.
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.
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.
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.
24 #ifndef KJS_FUNCTION_H
25 #define KJS_FUNCTION_H
28 #include <wtf/OwnPtr.h>
29 #include <wtf/Vector.h>
34 class FunctionBodyNode;
38 * @short Implementation class for internal Functions.
40 class FunctionImp : public InternalFunctionImp {
41 friend class ActivationImp;
43 FunctionImp(ExecState*, const Identifier& n, FunctionBodyNode* b);
44 virtual ~FunctionImp();
46 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
47 virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
48 virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
50 virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List& args);
52 void addParameter(const Identifier& n);
53 Identifier getParameterName(int index);
54 // parameters in string representation, e.g. (a, b, c)
55 UString parameterString() const;
56 virtual CodeType codeType() const = 0;
58 virtual Completion execute(ExecState*) = 0;
60 virtual const ClassInfo* classInfo() const { return &info; }
61 static const ClassInfo info;
63 RefPtr<FunctionBodyNode> body;
66 * Returns the scope of this object. This is used when execution declared
67 * functions - the execution context for the function is initialized with
68 * extra object in it's scope. An example of this is functions declared
69 * inside other functions:
81 * f.prototype = new String();
84 * When the function f.b is executed, its scope will include properties of
85 * f. So in the example above the return value of f.b() would be the new
86 * String object that was assigned to f.prototype.
88 * @param exec The current execution state
89 * @return The function's scope
91 const ScopeChain& scope() const { return _scope; }
92 void setScope(const ScopeChain& s) { _scope = s; }
96 OwnPtr<Vector<Parameter> > parameters;
101 static JSValue* argumentsGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
102 static JSValue* callerGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
103 static JSValue* lengthGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
105 void processParameters(ExecState*, const List&);
106 virtual void processVarDecls(ExecState*);
109 class DeclaredFunctionImp : public FunctionImp {
111 DeclaredFunctionImp(ExecState*, const Identifier& n,
112 FunctionBodyNode* b, const ScopeChain& sc);
114 bool implementsConstruct() const;
115 JSObject* construct(ExecState*, const List& args);
117 virtual Completion execute(ExecState*);
118 CodeType codeType() const { return FunctionCode; }
120 virtual const ClassInfo* classInfo() const { return &info; }
121 static const ClassInfo info;
124 virtual void processVarDecls(ExecState*);
127 class IndexToNameMap {
129 IndexToNameMap(FunctionImp* func, const List& args);
132 Identifier& operator[](int index);
133 Identifier& operator[](const Identifier &indexIdentifier);
134 bool isMapped(const Identifier& index) const;
135 void unMap(const Identifier& index);
138 IndexToNameMap(); // prevent construction w/o parameters
143 class Arguments : public JSObject {
145 Arguments(ExecState*, FunctionImp* func, const List& args, ActivationImp* act);
147 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
148 virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
149 virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
150 virtual const ClassInfo* classInfo() const { return &info; }
151 static const ClassInfo info;
153 static JSValue* mappedIndexGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot);
155 ActivationImp* _activationObject;
156 mutable IndexToNameMap indexToNameMap;
159 class ActivationImp : public JSObject {
161 ActivationImp(FunctionImp* function, const List& arguments);
163 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
164 virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
165 virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
167 virtual const ClassInfo* classInfo() const { return &info; }
168 static const ClassInfo info;
172 bool isActivation() { return true; }
174 static PropertySlot::GetValueFunc getArgumentsGetter();
175 static JSValue* argumentsGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot);
176 void createArgumentsObject(ExecState*) const;
178 FunctionImp* _function;
180 mutable Arguments* _argumentsObject;
183 class GlobalFuncImp : public InternalFunctionImp {
185 GlobalFuncImp(ExecState*, FunctionPrototype*, int i, int len, const Identifier&);
186 virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List& args);
187 virtual CodeType codeType() const;
188 enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, Escape, UnEscape,
189 DecodeURI, DecodeURIComponent, EncodeURI, EncodeURIComponent
198 UString escapeStringForPrettyPrinting(const UString& s);