1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
4 | * Copyright (C) 2003, 2006, 2007, 2008 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 JSFunction_h
|
---|
26 | #define JSFunction_h
|
---|
27 |
|
---|
28 | #include "JSVariableObject.h"
|
---|
29 | #include "SymbolTable.h"
|
---|
30 | #include "nodes.h"
|
---|
31 | #include "JSObject.h"
|
---|
32 |
|
---|
33 | namespace KJS {
|
---|
34 |
|
---|
35 | class FunctionBodyNode;
|
---|
36 | class FunctionPrototype;
|
---|
37 | class JSActivation;
|
---|
38 | class JSGlobalObject;
|
---|
39 |
|
---|
40 | class InternalFunction : public JSObject {
|
---|
41 | public:
|
---|
42 | InternalFunction();
|
---|
43 | InternalFunction(FunctionPrototype*, const Identifier&);
|
---|
44 |
|
---|
45 | virtual CallType getCallData(CallData&);
|
---|
46 |
|
---|
47 | virtual JSValue* callAsFunction(ExecState*, JSObject* thisObjec, const ArgList& 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 | class JSFunction : public InternalFunction {
|
---|
59 | public:
|
---|
60 | JSFunction(ExecState*, const Identifier&, FunctionBodyNode*, ScopeChainNode*);
|
---|
61 |
|
---|
62 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
63 | virtual void put(ExecState*, const Identifier& propertyName, JSValue*);
|
---|
64 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
65 |
|
---|
66 | virtual ConstructType getConstructData(ConstructData&);
|
---|
67 | virtual JSObject* construct(ExecState*, const ArgList& args);
|
---|
68 |
|
---|
69 | virtual CallType getCallData(CallData&);
|
---|
70 | virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const ArgList& args);
|
---|
71 |
|
---|
72 | // Note: unlike body->paramName, this returns Identifier::null for parameters
|
---|
73 | // that will never get set, due to later param having the same name
|
---|
74 | Identifier getParameterName(int index);
|
---|
75 |
|
---|
76 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
77 | static const ClassInfo info;
|
---|
78 |
|
---|
79 | RefPtr<FunctionBodyNode> body;
|
---|
80 |
|
---|
81 | void setScope(const ScopeChain& s) { _scope = s; }
|
---|
82 | ScopeChain& scope() { return _scope; }
|
---|
83 |
|
---|
84 | virtual void mark();
|
---|
85 |
|
---|
86 | private:
|
---|
87 | ScopeChain _scope;
|
---|
88 |
|
---|
89 | static JSValue* argumentsGetter(ExecState*, const Identifier&, const PropertySlot&);
|
---|
90 | static JSValue* callerGetter(ExecState*, const Identifier&, const PropertySlot&);
|
---|
91 | static JSValue* lengthGetter(ExecState*, const Identifier&, const PropertySlot&);
|
---|
92 | };
|
---|
93 |
|
---|
94 | class IndexToNameMap {
|
---|
95 | public:
|
---|
96 | IndexToNameMap(JSFunction*, const ArgList& args);
|
---|
97 | ~IndexToNameMap();
|
---|
98 |
|
---|
99 | Identifier& operator[](const Identifier& index);
|
---|
100 | bool isMapped(const Identifier& index) const;
|
---|
101 | void unMap(const Identifier& index);
|
---|
102 |
|
---|
103 | private:
|
---|
104 | unsigned size;
|
---|
105 | Identifier* _map;
|
---|
106 | };
|
---|
107 |
|
---|
108 | class Arguments : public JSObject {
|
---|
109 | public:
|
---|
110 | Arguments(ExecState*, JSFunction* func, const ArgList& args, JSActivation* act);
|
---|
111 | virtual void mark();
|
---|
112 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
113 | virtual void put(ExecState*, const Identifier& propertyName, JSValue*);
|
---|
114 | virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
|
---|
115 | virtual const ClassInfo* classInfo() const { return &info; }
|
---|
116 | static const ClassInfo info;
|
---|
117 | private:
|
---|
118 | static JSValue* mappedIndexGetter(ExecState*, const Identifier&, const PropertySlot& slot);
|
---|
119 |
|
---|
120 | JSActivation* _activationObject;
|
---|
121 | mutable IndexToNameMap indexToNameMap;
|
---|
122 | };
|
---|
123 |
|
---|
124 | class PrototypeFunction : public InternalFunction {
|
---|
125 | public:
|
---|
126 | typedef JSValue* (*JSMemberFunction)(ExecState*, JSObject* thisObj, const ArgList&);
|
---|
127 |
|
---|
128 | PrototypeFunction(ExecState*, int len, const Identifier&, JSMemberFunction);
|
---|
129 | PrototypeFunction(ExecState*, FunctionPrototype*, int len, const Identifier&, JSMemberFunction);
|
---|
130 |
|
---|
131 | virtual JSValue* callAsFunction(ExecState* exec, JSObject* thisObj, const ArgList&);
|
---|
132 |
|
---|
133 | private:
|
---|
134 | const JSMemberFunction m_function;
|
---|
135 | };
|
---|
136 |
|
---|
137 |
|
---|
138 | // Just like PrototypeFunction, but callbacks also get passed the JS function object.
|
---|
139 | class PrototypeReflexiveFunction : public InternalFunction {
|
---|
140 | public:
|
---|
141 | typedef JSValue* (*JSMemberFunction)(ExecState*, PrototypeReflexiveFunction*, JSObject* thisObj, const ArgList&);
|
---|
142 |
|
---|
143 | PrototypeReflexiveFunction(ExecState*, FunctionPrototype*, int len, const Identifier&, JSMemberFunction, JSGlobalObject* expectedThisObject);
|
---|
144 |
|
---|
145 | virtual void mark();
|
---|
146 | virtual JSValue* callAsFunction(ExecState* exec, JSObject* thisObj, const ArgList&);
|
---|
147 |
|
---|
148 | JSGlobalObject* cachedGlobalObject() const { return m_cachedGlobalObject; }
|
---|
149 |
|
---|
150 | private:
|
---|
151 | const JSMemberFunction m_function;
|
---|
152 | JSGlobalObject* m_cachedGlobalObject;
|
---|
153 | };
|
---|
154 |
|
---|
155 | // Global Functions
|
---|
156 | JSValue* globalFuncEval(ExecState*, PrototypeReflexiveFunction*, JSObject*, const ArgList&);
|
---|
157 | JSValue* globalFuncParseInt(ExecState*, JSObject*, const ArgList&);
|
---|
158 | JSValue* globalFuncParseFloat(ExecState*, JSObject*, const ArgList&);
|
---|
159 | JSValue* globalFuncIsNaN(ExecState*, JSObject*, const ArgList&);
|
---|
160 | JSValue* globalFuncIsFinite(ExecState*, JSObject*, const ArgList&);
|
---|
161 | JSValue* globalFuncDecodeURI(ExecState*, JSObject*, const ArgList&);
|
---|
162 | JSValue* globalFuncDecodeURIComponent(ExecState*, JSObject*, const ArgList&);
|
---|
163 | JSValue* globalFuncEncodeURI(ExecState*, JSObject*, const ArgList&);
|
---|
164 | JSValue* globalFuncEncodeURIComponent(ExecState*, JSObject*, const ArgList&);
|
---|
165 | JSValue* globalFuncEscape(ExecState*, JSObject*, const ArgList&);
|
---|
166 | JSValue* globalFuncUnescape(ExecState*, JSObject*, const ArgList&);
|
---|
167 | #ifndef NDEBUG
|
---|
168 | JSValue* globalFuncKJSPrint(ExecState*, JSObject*, const ArgList&);
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | static const double mantissaOverflowLowerBound = 9007199254740992.0;
|
---|
172 | double parseIntOverflow(const char*, int length, int radix);
|
---|
173 |
|
---|
174 | } // namespace
|
---|
175 |
|
---|
176 | #endif
|
---|