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>
33 class FunctionBodyNode;
37 * @short Implementation class for internal Functions.
39 class FunctionImp : public InternalFunctionImp {
40 friend class ActivationImp;
42 FunctionImp(ExecState* exec, const Identifier& n, FunctionBodyNode* b);
43 virtual ~FunctionImp();
45 virtual bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot&);
46 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
47 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
49 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
51 void addParameter(const Identifier &n);
52 Identifier getParameterName(int index);
53 // parameters in string representation, e.g. (a, b, c)
54 UString parameterString() const;
55 virtual CodeType codeType() const = 0;
57 virtual Completion execute(ExecState *exec) = 0;
59 virtual const ClassInfo *classInfo() const { return &info; }
60 static const ClassInfo info;
62 RefPtr<FunctionBodyNode> body;
65 OwnPtr<Parameter> param;
68 static JSValue *argumentsGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot&);
69 static JSValue *lengthGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot&);
71 void processParameters(ExecState *exec, const List &);
72 virtual void processVarDecls(ExecState *exec);
75 class DeclaredFunctionImp : public FunctionImp {
77 DeclaredFunctionImp(ExecState *exec, const Identifier &n,
78 FunctionBodyNode *b, const ScopeChain &sc);
80 bool implementsConstruct() const;
81 JSObject *construct(ExecState *exec, const List &args);
83 virtual Completion execute(ExecState *exec);
84 CodeType codeType() const { return FunctionCode; }
86 virtual const ClassInfo *classInfo() const { return &info; }
87 static const ClassInfo info;
90 virtual void processVarDecls(ExecState *exec);
93 class IndexToNameMap {
95 IndexToNameMap(FunctionImp *func, const List &args);
98 Identifier& operator[](int index);
99 Identifier& operator[](const Identifier &indexIdentifier);
100 bool isMapped(const Identifier &index) const;
101 void unMap(const Identifier &index);
104 IndexToNameMap(); // prevent construction w/o parameters
109 class Arguments : public JSObject {
111 Arguments(ExecState *exec, FunctionImp *func, const List &args, ActivationImp *act);
113 virtual bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot&);
114 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
115 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
116 virtual const ClassInfo *classInfo() const { return &info; }
117 static const ClassInfo info;
119 static JSValue *mappedIndexGetter(ExecState *exec, JSObject *, const Identifier &, const PropertySlot& slot);
121 ActivationImp *_activationObject;
122 mutable IndexToNameMap indexToNameMap;
125 class ActivationImp : public JSObject {
127 ActivationImp(FunctionImp *function, const List &arguments);
129 virtual bool getOwnPropertySlot(ExecState *exec, const Identifier &, PropertySlot&);
130 virtual void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None);
131 virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
133 virtual const ClassInfo *classInfo() const { return &info; }
134 static const ClassInfo info;
138 bool isActivation() { return true; }
140 static PropertySlot::GetValueFunc getArgumentsGetter();
141 static JSValue *argumentsGetter(ExecState *exec, JSObject *, const Identifier &, const PropertySlot& slot);
142 void createArgumentsObject(ExecState *exec) const;
144 FunctionImp *_function;
146 mutable Arguments *_argumentsObject;
149 class GlobalFuncImp : public InternalFunctionImp {
151 GlobalFuncImp(ExecState*, FunctionPrototype*, int i, int len, const Identifier&);
152 virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
153 virtual CodeType codeType() const;
154 enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, Escape, UnEscape,
155 DecodeURI, DecodeURIComponent, EncodeURI, EncodeURIComponent
164 UString escapeStringForPrettyPrinting(const UString& s);