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 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 Steet, 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 "array_instance.h"
|
---|
29 |
|
---|
30 | namespace KJS {
|
---|
31 |
|
---|
32 | class Parameter;
|
---|
33 | class ActivationImp;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @short Implementation class for internal Functions.
|
---|
37 | */
|
---|
38 | class FunctionImp : public InternalFunctionImp {
|
---|
39 | friend class ActivationImp;
|
---|
40 | public:
|
---|
41 | FunctionImp(ExecState *exec, const Identifier &n = Identifier::null());
|
---|
42 | virtual ~FunctionImp();
|
---|
43 |
|
---|
44 | virtual bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot&);
|
---|
45 | virtual void put(ExecState *exec, const Identifier &propertyName, ValueImp *value, int attr = None);
|
---|
46 | virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
|
---|
47 |
|
---|
48 | virtual bool implementsCall() const;
|
---|
49 | virtual ValueImp *callAsFunction(ExecState *exec, ObjectImp *thisObj, const List &args);
|
---|
50 |
|
---|
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;
|
---|
56 |
|
---|
57 | virtual Completion execute(ExecState *exec) = 0;
|
---|
58 | Identifier name() const { return ident; }
|
---|
59 |
|
---|
60 | virtual const ClassInfo *classInfo() const { return &info; }
|
---|
61 | static const ClassInfo info;
|
---|
62 | protected:
|
---|
63 | Parameter *param;
|
---|
64 | Identifier ident;
|
---|
65 |
|
---|
66 | private:
|
---|
67 | static ValueImp *argumentsGetter(ExecState *, const Identifier &, const PropertySlot&);
|
---|
68 | static ValueImp *lengthGetter(ExecState *, const Identifier &, const PropertySlot&);
|
---|
69 |
|
---|
70 | void processParameters(ExecState *exec, const List &);
|
---|
71 | virtual void processVarDecls(ExecState *exec);
|
---|
72 | };
|
---|
73 |
|
---|
74 | class DeclaredFunctionImp : public FunctionImp {
|
---|
75 | public:
|
---|
76 | DeclaredFunctionImp(ExecState *exec, const Identifier &n,
|
---|
77 | FunctionBodyNode *b, const ScopeChain &sc);
|
---|
78 |
|
---|
79 | bool implementsConstruct() const;
|
---|
80 | ObjectImp *construct(ExecState *exec, const List &args);
|
---|
81 |
|
---|
82 | virtual Completion execute(ExecState *exec);
|
---|
83 | CodeType codeType() const { return FunctionCode; }
|
---|
84 | SharedPtr<FunctionBodyNode> body;
|
---|
85 |
|
---|
86 | virtual const ClassInfo *classInfo() const { return &info; }
|
---|
87 | static const ClassInfo info;
|
---|
88 | private:
|
---|
89 | virtual void processVarDecls(ExecState *exec);
|
---|
90 | };
|
---|
91 |
|
---|
92 | class IndexToNameMap {
|
---|
93 | public:
|
---|
94 | IndexToNameMap(FunctionImp *func, const List &args);
|
---|
95 | ~IndexToNameMap();
|
---|
96 |
|
---|
97 | Identifier& operator[](int index);
|
---|
98 | Identifier& operator[](const Identifier &indexIdentifier);
|
---|
99 | bool isMapped(const Identifier &index) const;
|
---|
100 | void IndexToNameMap::unMap(const Identifier &index);
|
---|
101 |
|
---|
102 | private:
|
---|
103 | IndexToNameMap(); // prevent construction w/o parameters
|
---|
104 | int size;
|
---|
105 | Identifier * _map;
|
---|
106 | };
|
---|
107 |
|
---|
108 | class ArgumentsImp : public ObjectImp {
|
---|
109 | public:
|
---|
110 | ArgumentsImp(ExecState *exec, FunctionImp *func, const List &args, ActivationImp *act);
|
---|
111 | virtual void mark();
|
---|
112 | virtual bool getOwnPropertySlot(ExecState *, const Identifier &, PropertySlot&);
|
---|
113 | virtual void put(ExecState *exec, const Identifier &propertyName, ValueImp *value, int attr = None);
|
---|
114 | virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
|
---|
115 | virtual const ClassInfo *classInfo() const { return &info; }
|
---|
116 | static const ClassInfo info;
|
---|
117 | private:
|
---|
118 | static ValueImp *mappedIndexGetter(ExecState *exec, const Identifier &, const PropertySlot& slot);
|
---|
119 |
|
---|
120 | ActivationImp *_activationObject;
|
---|
121 | mutable IndexToNameMap indexToNameMap;
|
---|
122 | };
|
---|
123 |
|
---|
124 | class ActivationImp : public ObjectImp {
|
---|
125 | public:
|
---|
126 | ActivationImp(FunctionImp *function, const List &arguments);
|
---|
127 |
|
---|
128 | virtual bool getOwnPropertySlot(ExecState *exec, const Identifier &, PropertySlot&);
|
---|
129 | virtual bool deleteProperty(ExecState *exec, const Identifier &propertyName);
|
---|
130 |
|
---|
131 | virtual const ClassInfo *classInfo() const { return &info; }
|
---|
132 | static const ClassInfo info;
|
---|
133 |
|
---|
134 | virtual void mark();
|
---|
135 |
|
---|
136 | bool isActivation() { return true; }
|
---|
137 | private:
|
---|
138 | static PropertySlot::GetValueFunc getArgumentsGetter();
|
---|
139 | static ValueImp *argumentsGetter(ExecState *exec, const Identifier &, const PropertySlot& slot);
|
---|
140 | void createArgumentsObject(ExecState *exec) const;
|
---|
141 |
|
---|
142 | FunctionImp *_function;
|
---|
143 | List _arguments;
|
---|
144 | mutable ArgumentsImp *_argumentsObject;
|
---|
145 | };
|
---|
146 |
|
---|
147 | class GlobalFuncImp : public InternalFunctionImp {
|
---|
148 | public:
|
---|
149 | GlobalFuncImp(ExecState *exec, FunctionPrototypeImp *funcProto, int i, int len);
|
---|
150 | virtual bool implementsCall() const;
|
---|
151 | virtual ValueImp *callAsFunction(ExecState *exec, ObjectImp *thisObj, const List &args);
|
---|
152 | virtual CodeType codeType() const;
|
---|
153 | enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, Escape, UnEscape,
|
---|
154 | DecodeURI, DecodeURIComponent, EncodeURI, EncodeURIComponent
|
---|
155 | #ifndef NDEBUG
|
---|
156 | , KJSPrint
|
---|
157 | #endif
|
---|
158 | };
|
---|
159 | private:
|
---|
160 | int id;
|
---|
161 | };
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 | } // namespace
|
---|
166 |
|
---|
167 | #endif
|
---|