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* exec, const Identifier& n, FunctionBodyNode* b);
44 virtual ~FunctionImp();
46 virtual bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot&);
47 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
48 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
50 virtual JSValue *callAsFunction(ExecState *exec, 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 *exec) = 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 *lengthGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot&);
104 void processParameters(ExecState *exec, const List &);
105 virtual void processVarDecls(ExecState *exec);
108 class DeclaredFunctionImp : public FunctionImp {
110 DeclaredFunctionImp(ExecState *exec, const Identifier &n,
111 FunctionBodyNode *b, const ScopeChain &sc);
113 bool implementsConstruct() const;
114 JSObject *construct(ExecState *exec, const List &args);
116 virtual Completion execute(ExecState *exec);
117 CodeType codeType() const { return FunctionCode; }
119 virtual const ClassInfo *classInfo() const { return &info; }
120 static const ClassInfo info;
123 virtual void processVarDecls(ExecState *exec);
126 class IndexToNameMap {
128 IndexToNameMap(FunctionImp *func, const List &args);
131 Identifier& operator[](int index);
132 Identifier& operator[](const Identifier &indexIdentifier);
133 bool isMapped(const Identifier &index) const;
134 void unMap(const Identifier &index);
137 IndexToNameMap(); // prevent construction w/o parameters
142 class Arguments : public JSObject {
144 Arguments(ExecState *exec, FunctionImp *func, const List &args, ActivationImp *act);
146 virtual bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot&);
147 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
148 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
149 virtual const ClassInfo *classInfo() const { return &info; }
150 static const ClassInfo info;
152 static JSValue *mappedIndexGetter(ExecState *exec, JSObject *, const Identifier &, const PropertySlot& slot);
154 ActivationImp *_activationObject;
155 mutable IndexToNameMap indexToNameMap;
158 class ActivationImp : public JSObject {
160 ActivationImp(FunctionImp *function, const List &arguments);
162 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier &, PropertySlot&);
163 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
164 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
166 virtual const ClassInfo *classInfo() const { return &info; }
167 static const ClassInfo info;
171 bool isActivation() { return true; }
173 static PropertySlot::GetValueFunc getArgumentsGetter();
174 static JSValue *argumentsGetter(ExecState *exec, JSObject *, const Identifier &, const PropertySlot& slot);
175 void createArgumentsObject(ExecState *exec) const;
177 FunctionImp *_function;
179 mutable Arguments *_argumentsObject;
182 class GlobalFuncImp : public InternalFunctionImp {
184 GlobalFuncImp(ExecState*, FunctionPrototype*, int i, int len, const Identifier&);
185 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
186 virtual CodeType codeType() const;
187 enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, Escape, UnEscape,
188 DecodeURI, DecodeURIComponent, EncodeURI, EncodeURIComponent
197 UString escapeStringForPrettyPrinting(const UString& s);