1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
4 | * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
|
---|
5 | * Copyright (C) 2007 Cameron Zwarich ([email protected])
|
---|
6 | * Copyright (C) 2007 Maks Orlovich
|
---|
7 | *
|
---|
8 | * This library is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU Library General Public
|
---|
10 | * License as published by the Free Software Foundation; either
|
---|
11 | * version 2 of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This library is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * Library General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Library General Public License
|
---|
19 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
21 | * Boston, MA 02110-1301, USA.
|
---|
22 | *
|
---|
23 | */
|
---|
24 |
|
---|
25 | #ifndef KJS_FUNCTION_H
|
---|
26 | #define KJS_FUNCTION_H
|
---|
27 |
|
---|
28 | #include "JSVariableObject.h"
|
---|
29 | #include "LocalStorage.h"
|
---|
30 | #include "SymbolTable.h"
|
---|
31 | #include "nodes.h"
|
---|
32 | #include "object.h"
|
---|
33 |
|
---|
34 | namespace KJS {
|
---|
35 |
|
---|
36 | class ActivationImp;
|
---|
37 | class FunctionBodyNode;
|
---|
38 | class FunctionPrototype;
|
---|
39 |
|
---|
40 | class InternalFunctionImp : public JSObject {
|
---|
41 | public:
|
---|
42 | InternalFunctionImp();
|
---|
43 | InternalFunctionImp(FunctionPrototype*);
|
---|
44 | InternalFunctionImp(FunctionPrototype*, const Identifier&);
|
---|
45 |
|
---|
46 | virtual bool implementsCall() const;
|
---|
47 | virtual JSValue* callAsFunction(ExecState*, JSObject* thisObjec, const List& args) = 0;
|
---|
48 | virtual bool implementsHasInstance() const;
|
---|
49 |
|
---|
50 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
51 | static const ClassInfo info;
|
---|
52 | const Identifier& functionName() const { return m_name; }
|
---|
53 |
|
---|
54 | private:
|
---|
55 | Identifier m_name;
|
---|
56 | };
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * @internal
|
---|
60 | *
|
---|
61 | * The initial value of Function.prototype (and thus all objects created
|
---|
62 | * with the Function constructor)
|
---|
63 | */
|
---|
64 | class FunctionPrototype : public InternalFunctionImp {
|
---|
65 | public:
|
---|
66 | FunctionPrototype(ExecState *exec);
|
---|
67 | virtual ~FunctionPrototype();
|
---|
68 |
|
---|
69 | virtual JSValue *callAsFunction(ExecState *exec, JSObject *thisObj, const List &args);
|
---|
70 | };
|
---|
71 |
|
---|
72 | class FunctionImp : public InternalFunctionImp {
|
---|
73 | friend class ActivationImp;
|
---|
74 | public:
|
---|
75 | FunctionImp(ExecState*, const Identifier& name, FunctionBodyNode*, const ScopeChain&);
|
---|
76 |
|
---|
77 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
78 | virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
|
---|
79 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
80 |
|
---|
81 | virtual bool implementsConstruct() const { return true; }
|
---|
82 | virtual JSObject* construct(ExecState*, const List& args);
|
---|
83 |
|
---|
84 | virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List& args);
|
---|
85 | Completion execute(ExecState*);
|
---|
86 |
|
---|
87 | // Note: unlike body->paramName, this returns Identifier::null for parameters
|
---|
88 | // that will never get set, due to later param having the same name
|
---|
89 | Identifier getParameterName(int index);
|
---|
90 |
|
---|
91 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
92 | static const ClassInfo info;
|
---|
93 |
|
---|
94 | RefPtr<FunctionBodyNode> body;
|
---|
95 |
|
---|
96 | void setScope(const ScopeChain& s) { _scope = s; }
|
---|
97 | const ScopeChain& scope() const { return _scope; }
|
---|
98 |
|
---|
99 | virtual void mark();
|
---|
100 |
|
---|
101 | private:
|
---|
102 | ScopeChain _scope;
|
---|
103 |
|
---|
104 | static JSValue* argumentsGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
|
---|
105 | static JSValue* callerGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
|
---|
106 | static JSValue* lengthGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot&);
|
---|
107 | };
|
---|
108 |
|
---|
109 | class IndexToNameMap {
|
---|
110 | public:
|
---|
111 | IndexToNameMap(FunctionImp* func, const List& args);
|
---|
112 | ~IndexToNameMap();
|
---|
113 |
|
---|
114 | Identifier& operator[](int index);
|
---|
115 | Identifier& operator[](const Identifier &indexIdentifier);
|
---|
116 | bool isMapped(const Identifier& index) const;
|
---|
117 | void unMap(const Identifier& index);
|
---|
118 |
|
---|
119 | private:
|
---|
120 | IndexToNameMap(); // prevent construction w/o parameters
|
---|
121 | int size;
|
---|
122 | Identifier* _map;
|
---|
123 | };
|
---|
124 |
|
---|
125 | class Arguments : public JSObject {
|
---|
126 | public:
|
---|
127 | Arguments(ExecState*, FunctionImp* func, const List& args, ActivationImp* act);
|
---|
128 | virtual void mark();
|
---|
129 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
130 | virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
|
---|
131 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
132 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
133 | static const ClassInfo info;
|
---|
134 | private:
|
---|
135 | static JSValue* mappedIndexGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot);
|
---|
136 |
|
---|
137 | ActivationImp* _activationObject;
|
---|
138 | mutable IndexToNameMap indexToNameMap;
|
---|
139 | };
|
---|
140 |
|
---|
141 | class ActivationImp : public JSVariableObject {
|
---|
142 | private:
|
---|
143 | using JSVariableObject::JSVariableObjectData;
|
---|
144 |
|
---|
145 | struct ActivationImpData : public JSVariableObjectData {
|
---|
146 | ActivationImpData(ExecState* e)
|
---|
147 | : JSVariableObjectData(&e->function()->body->symbolTable())
|
---|
148 | , exec(e)
|
---|
149 | , function(e->function()) // Store this pointer for marking, to keep our symbol table / scope alive after exec has gone out of scope.
|
---|
150 | , argumentsObject(0)
|
---|
151 | {
|
---|
152 | }
|
---|
153 |
|
---|
154 | ExecState* exec;
|
---|
155 | FunctionImp* function;
|
---|
156 | Arguments* argumentsObject;
|
---|
157 | };
|
---|
158 |
|
---|
159 | public:
|
---|
160 | ActivationImp(ExecState* exec)
|
---|
161 | : JSVariableObject(new ActivationImpData(exec))
|
---|
162 | {
|
---|
163 | }
|
---|
164 |
|
---|
165 | virtual ~ActivationImp()
|
---|
166 | {
|
---|
167 | delete d();
|
---|
168 | }
|
---|
169 |
|
---|
170 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
171 | virtual void put(ExecState*, const Identifier& propertyName, JSValue* value, int attr = None);
|
---|
172 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
173 |
|
---|
174 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
175 | static const ClassInfo info;
|
---|
176 |
|
---|
177 | virtual void mark();
|
---|
178 |
|
---|
179 | virtual bool isActivationObject() { return true; }
|
---|
180 |
|
---|
181 | private:
|
---|
182 | static PropertySlot::GetValueFunc getArgumentsGetter();
|
---|
183 | static JSValue* argumentsGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot);
|
---|
184 | void createArgumentsObject(ExecState*);
|
---|
185 | ActivationImpData* d() { return static_cast<ActivationImpData*>(JSVariableObject::d); }
|
---|
186 | };
|
---|
187 |
|
---|
188 | class GlobalFuncImp : public InternalFunctionImp {
|
---|
189 | public:
|
---|
190 | GlobalFuncImp(ExecState*, FunctionPrototype*, int i, int len, const Identifier&);
|
---|
191 | virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List& args);
|
---|
192 | enum { Eval, ParseInt, ParseFloat, IsNaN, IsFinite, Escape, UnEscape,
|
---|
193 | DecodeURI, DecodeURIComponent, EncodeURI, EncodeURIComponent
|
---|
194 | #ifndef NDEBUG
|
---|
195 | , KJSPrint
|
---|
196 | #endif
|
---|
197 | };
|
---|
198 | private:
|
---|
199 | int id;
|
---|
200 | };
|
---|
201 |
|
---|
202 | static const double mantissaOverflowLowerBound = 9007199254740992.0;
|
---|
203 | double parseIntOverflow(const char* s, int length, int radix);
|
---|
204 |
|
---|
205 | } // namespace
|
---|
206 |
|
---|
207 | #endif
|
---|