1 | /*
|
---|
2 | * Copyright (C) 2007 Eric Seidel <[email protected]>
|
---|
3 | * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Library General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Library General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Library General Public License
|
---|
16 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
18 | * Boston, MA 02110-1301, USA.
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef JSGlobalObject_h
|
---|
23 | #define JSGlobalObject_h
|
---|
24 |
|
---|
25 | #include "JSGlobalData.h"
|
---|
26 | #include "JSVariableObject.h"
|
---|
27 | #include "NumberPrototype.h"
|
---|
28 | #include "StringPrototype.h"
|
---|
29 | #include <wtf/HashSet.h>
|
---|
30 | #include <wtf/OwnPtr.h>
|
---|
31 |
|
---|
32 | namespace JSC {
|
---|
33 |
|
---|
34 | class ArrayPrototype;
|
---|
35 | class BooleanPrototype;
|
---|
36 | class DatePrototype;
|
---|
37 | class Debugger;
|
---|
38 | class ErrorConstructor;
|
---|
39 | class FunctionPrototype;
|
---|
40 | class GlobalEvalFunction;
|
---|
41 | class NativeErrorConstructor;
|
---|
42 | class ProgramCodeBlock;
|
---|
43 | class RegExpConstructor;
|
---|
44 | class RegExpPrototype;
|
---|
45 | class RegisterFile;
|
---|
46 |
|
---|
47 | struct ActivationStackNode;
|
---|
48 | struct HashTable;
|
---|
49 |
|
---|
50 | typedef Vector<ExecState*, 16> ExecStateStack;
|
---|
51 |
|
---|
52 | class JSGlobalObject : public JSVariableObject {
|
---|
53 | protected:
|
---|
54 | using JSVariableObject::JSVariableObjectData;
|
---|
55 |
|
---|
56 | struct JSGlobalObjectData : public JSVariableObjectData {
|
---|
57 | JSGlobalObjectData(JSGlobalObject* globalObject, JSObject* thisValue)
|
---|
58 | : JSVariableObjectData(&symbolTable, 0)
|
---|
59 | , globalScopeChain(globalObject, thisValue)
|
---|
60 | , regExpConstructor(0)
|
---|
61 | , errorConstructor(0)
|
---|
62 | , evalErrorConstructor(0)
|
---|
63 | , rangeErrorConstructor(0)
|
---|
64 | , referenceErrorConstructor(0)
|
---|
65 | , syntaxErrorConstructor(0)
|
---|
66 | , typeErrorConstructor(0)
|
---|
67 | , URIErrorConstructor(0)
|
---|
68 | , evalFunction(0)
|
---|
69 | , objectPrototype(0)
|
---|
70 | , functionPrototype(0)
|
---|
71 | , arrayPrototype(0)
|
---|
72 | , booleanPrototype(0)
|
---|
73 | , stringPrototype(0)
|
---|
74 | , numberPrototype(0)
|
---|
75 | , datePrototype(0)
|
---|
76 | , regExpPrototype(0)
|
---|
77 | {
|
---|
78 | }
|
---|
79 |
|
---|
80 | virtual ~JSGlobalObjectData()
|
---|
81 | {
|
---|
82 | }
|
---|
83 |
|
---|
84 | JSGlobalObject* next;
|
---|
85 | JSGlobalObject* prev;
|
---|
86 |
|
---|
87 | Debugger* debugger;
|
---|
88 |
|
---|
89 | ScopeChain globalScopeChain;
|
---|
90 | OwnPtr<ExecState> globalExec;
|
---|
91 |
|
---|
92 | int recursion;
|
---|
93 |
|
---|
94 | RegExpConstructor* regExpConstructor;
|
---|
95 | ErrorConstructor* errorConstructor;
|
---|
96 | NativeErrorConstructor* evalErrorConstructor;
|
---|
97 | NativeErrorConstructor* rangeErrorConstructor;
|
---|
98 | NativeErrorConstructor* referenceErrorConstructor;
|
---|
99 | NativeErrorConstructor* syntaxErrorConstructor;
|
---|
100 | NativeErrorConstructor* typeErrorConstructor;
|
---|
101 | NativeErrorConstructor* URIErrorConstructor;
|
---|
102 |
|
---|
103 | GlobalEvalFunction* evalFunction;
|
---|
104 |
|
---|
105 | ObjectPrototype* objectPrototype;
|
---|
106 | FunctionPrototype* functionPrototype;
|
---|
107 | ArrayPrototype* arrayPrototype;
|
---|
108 | BooleanPrototype* booleanPrototype;
|
---|
109 | StringPrototype* stringPrototype;
|
---|
110 | NumberPrototype* numberPrototype;
|
---|
111 | DatePrototype* datePrototype;
|
---|
112 | RegExpPrototype* regExpPrototype;
|
---|
113 |
|
---|
114 | RefPtr<StructureID> argumentsStructure;
|
---|
115 | RefPtr<StructureID> arrayStructure;
|
---|
116 | RefPtr<StructureID> booleanObjectStructure;
|
---|
117 | RefPtr<StructureID> callbackConstructorStructure;
|
---|
118 | RefPtr<StructureID> callbackFunctionStructure;
|
---|
119 | RefPtr<StructureID> callbackObjectStructure;
|
---|
120 | RefPtr<StructureID> dateStructure;
|
---|
121 | RefPtr<StructureID> emptyObjectStructure;
|
---|
122 | RefPtr<StructureID> errorStructure;
|
---|
123 | RefPtr<StructureID> functionStructure;
|
---|
124 | RefPtr<StructureID> numberObjectStructure;
|
---|
125 | RefPtr<StructureID> prototypeFunctionStructure;
|
---|
126 | RefPtr<StructureID> regExpMatchesArrayStructure;
|
---|
127 | RefPtr<StructureID> regExpStructure;
|
---|
128 | RefPtr<StructureID> stringObjectStructure;
|
---|
129 |
|
---|
130 | SymbolTable symbolTable;
|
---|
131 | unsigned profileGroup;
|
---|
132 |
|
---|
133 | RefPtr<JSGlobalData> globalData;
|
---|
134 |
|
---|
135 | HashSet<ProgramCodeBlock*> codeBlocks;
|
---|
136 |
|
---|
137 | OwnPtr<HashSet<JSObject*> > arrayVisitedElements; // Global data shared by array prototype functions.
|
---|
138 | };
|
---|
139 |
|
---|
140 | public:
|
---|
141 | void* operator new(size_t, JSGlobalData*);
|
---|
142 |
|
---|
143 | JSGlobalObject(JSGlobalData* globalData)
|
---|
144 | : JSVariableObject(globalData->nullProtoStructureID, new JSGlobalObjectData(this, this))
|
---|
145 | {
|
---|
146 | init(this);
|
---|
147 | }
|
---|
148 |
|
---|
149 | protected:
|
---|
150 | JSGlobalObject(PassRefPtr<StructureID> structure, JSGlobalObjectData* data, JSObject* globalThisValue)
|
---|
151 | : JSVariableObject(structure, data)
|
---|
152 | {
|
---|
153 | init(globalThisValue);
|
---|
154 | }
|
---|
155 |
|
---|
156 | public:
|
---|
157 | virtual ~JSGlobalObject();
|
---|
158 |
|
---|
159 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
160 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&, bool& slotIsWriteable);
|
---|
161 | virtual void put(ExecState*, const Identifier&, JSValue*, PutPropertySlot&);
|
---|
162 | virtual void putWithAttributes(ExecState*, const Identifier& propertyName, JSValue* value, unsigned attributes);
|
---|
163 |
|
---|
164 | virtual void defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunc);
|
---|
165 | virtual void defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunc);
|
---|
166 |
|
---|
167 | // Linked list of all global objects that use the same JSGlobalData.
|
---|
168 | JSGlobalObject*& head() { return d()->globalData->head; }
|
---|
169 | JSGlobalObject* next() { return d()->next; }
|
---|
170 |
|
---|
171 | // The following accessors return pristine values, even if a script
|
---|
172 | // replaces the global object's associated property.
|
---|
173 |
|
---|
174 | RegExpConstructor* regExpConstructor() const { return d()->regExpConstructor; }
|
---|
175 |
|
---|
176 | ErrorConstructor* errorConstructor() const { return d()->errorConstructor; }
|
---|
177 | NativeErrorConstructor* evalErrorConstructor() const { return d()->evalErrorConstructor; }
|
---|
178 | NativeErrorConstructor* rangeErrorConstructor() const { return d()->rangeErrorConstructor; }
|
---|
179 | NativeErrorConstructor* referenceErrorConstructor() const { return d()->referenceErrorConstructor; }
|
---|
180 | NativeErrorConstructor* syntaxErrorConstructor() const { return d()->syntaxErrorConstructor; }
|
---|
181 | NativeErrorConstructor* typeErrorConstructor() const { return d()->typeErrorConstructor; }
|
---|
182 | NativeErrorConstructor* URIErrorConstructor() const { return d()->URIErrorConstructor; }
|
---|
183 |
|
---|
184 | GlobalEvalFunction* evalFunction() const { return d()->evalFunction; }
|
---|
185 |
|
---|
186 | ObjectPrototype* objectPrototype() const { return d()->objectPrototype; }
|
---|
187 | FunctionPrototype* functionPrototype() const { return d()->functionPrototype; }
|
---|
188 | ArrayPrototype* arrayPrototype() const { return d()->arrayPrototype; }
|
---|
189 | BooleanPrototype* booleanPrototype() const { return d()->booleanPrototype; }
|
---|
190 | StringPrototype* stringPrototype() const { return d()->stringPrototype; }
|
---|
191 | NumberPrototype* numberPrototype() const { return d()->numberPrototype; }
|
---|
192 | DatePrototype* datePrototype() const { return d()->datePrototype; }
|
---|
193 | RegExpPrototype* regExpPrototype() const { return d()->regExpPrototype; }
|
---|
194 |
|
---|
195 | StructureID* argumentsStructure() const { return d()->argumentsStructure.get(); }
|
---|
196 | StructureID* arrayStructure() const { return d()->arrayStructure.get(); }
|
---|
197 | StructureID* booleanObjectStructure() const { return d()->booleanObjectStructure.get(); }
|
---|
198 | StructureID* callbackConstructorStructure() const { return d()->callbackConstructorStructure.get(); }
|
---|
199 | StructureID* callbackFunctionStructure() const { return d()->callbackFunctionStructure.get(); }
|
---|
200 | StructureID* callbackObjectStructure() const { return d()->callbackObjectStructure.get(); }
|
---|
201 | StructureID* dateStructure() const { return d()->dateStructure.get(); }
|
---|
202 | StructureID* emptyObjectStructure() const { return d()->emptyObjectStructure.get(); }
|
---|
203 | StructureID* errorStructure() const { return d()->errorStructure.get(); }
|
---|
204 | StructureID* functionStructure() const { return d()->functionStructure.get(); }
|
---|
205 | StructureID* numberObjectStructure() const { return d()->numberObjectStructure.get(); }
|
---|
206 | StructureID* prototypeFunctionStructure() const { return d()->prototypeFunctionStructure.get(); }
|
---|
207 | StructureID* regExpMatchesArrayStructure() const { return d()->regExpMatchesArrayStructure.get(); }
|
---|
208 | StructureID* regExpStructure() const { return d()->regExpStructure.get(); }
|
---|
209 | StructureID* stringObjectStructure() const { return d()->stringObjectStructure.get(); }
|
---|
210 |
|
---|
211 | void setProfileGroup(unsigned value) { d()->profileGroup = value; }
|
---|
212 | unsigned profileGroup() const { return d()->profileGroup; }
|
---|
213 |
|
---|
214 | void setTimeoutTime(unsigned timeoutTime);
|
---|
215 | void startTimeoutCheck();
|
---|
216 | void stopTimeoutCheck();
|
---|
217 |
|
---|
218 | Debugger* debugger() const { return d()->debugger; }
|
---|
219 | void setDebugger(Debugger* debugger) { d()->debugger = debugger; }
|
---|
220 |
|
---|
221 | int recursion() { return d()->recursion; }
|
---|
222 | void incRecursion() { ++d()->recursion; }
|
---|
223 | void decRecursion() { --d()->recursion; }
|
---|
224 |
|
---|
225 | ScopeChain& globalScopeChain() { return d()->globalScopeChain; }
|
---|
226 |
|
---|
227 | virtual void mark();
|
---|
228 |
|
---|
229 | virtual bool isGlobalObject() const { return true; }
|
---|
230 | virtual JSGlobalObject* toGlobalObject(ExecState*) const;
|
---|
231 |
|
---|
232 | virtual ExecState* globalExec();
|
---|
233 |
|
---|
234 | virtual bool shouldInterruptScript() const { return true; }
|
---|
235 |
|
---|
236 | virtual bool allowsAccessFrom(const JSGlobalObject*) const { return true; }
|
---|
237 |
|
---|
238 | virtual bool isDynamicScope() const;
|
---|
239 |
|
---|
240 | HashSet<JSObject*>& arrayVisitedElements() { if (!d()->arrayVisitedElements) d()->arrayVisitedElements.set(new HashSet<JSObject*>); return *d()->arrayVisitedElements; }
|
---|
241 |
|
---|
242 | HashSet<ProgramCodeBlock*>& codeBlocks() { return d()->codeBlocks; }
|
---|
243 |
|
---|
244 | void copyGlobalsFrom(RegisterFile&);
|
---|
245 | void copyGlobalsTo(RegisterFile&);
|
---|
246 |
|
---|
247 | void resetPrototype(JSValue* prototype);
|
---|
248 |
|
---|
249 | JSGlobalData* globalData() { return d()->globalData.get(); }
|
---|
250 | JSGlobalObjectData* d() const { return static_cast<JSGlobalObjectData*>(JSVariableObject::d); }
|
---|
251 |
|
---|
252 | protected:
|
---|
253 | struct GlobalPropertyInfo {
|
---|
254 | GlobalPropertyInfo(const Identifier& i, JSValue* v, unsigned a)
|
---|
255 | : identifier(i)
|
---|
256 | , value(v)
|
---|
257 | , attributes(a)
|
---|
258 | {
|
---|
259 | }
|
---|
260 |
|
---|
261 | const Identifier identifier;
|
---|
262 | JSValue* value;
|
---|
263 | unsigned attributes;
|
---|
264 | };
|
---|
265 | void addStaticGlobals(GlobalPropertyInfo*, int count);
|
---|
266 |
|
---|
267 | private:
|
---|
268 | // FIXME: Fold these functions into the constructor.
|
---|
269 | void init(JSObject* thisValue);
|
---|
270 | void reset(JSValue* prototype);
|
---|
271 |
|
---|
272 | void* operator new(size_t); // can only be allocated with JSGlobalData
|
---|
273 | };
|
---|
274 |
|
---|
275 | inline void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
|
---|
276 | {
|
---|
277 | size_t registerArraySize = d()->registerArraySize;
|
---|
278 | Register* registerArray = new Register[registerArraySize + count];
|
---|
279 | if (d()->registerArray)
|
---|
280 | memcpy(registerArray + count, d()->registerArray.get(), registerArraySize * sizeof(Register));
|
---|
281 | setRegisterArray(registerArray, registerArraySize + count);
|
---|
282 |
|
---|
283 | for (int i = 0, index = -static_cast<int>(registerArraySize) - 1; i < count; ++i, --index) {
|
---|
284 | GlobalPropertyInfo& global = globals[i];
|
---|
285 | ASSERT(global.attributes & DontDelete);
|
---|
286 | SymbolTableEntry newEntry(index, global.attributes);
|
---|
287 | symbolTable().add(global.identifier.ustring().rep(), newEntry);
|
---|
288 | registerAt(index) = global.value;
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | inline bool JSGlobalObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
|
---|
293 | {
|
---|
294 | if (JSVariableObject::getOwnPropertySlot(exec, propertyName, slot))
|
---|
295 | return true;
|
---|
296 | return symbolTableGet(propertyName, slot);
|
---|
297 | }
|
---|
298 |
|
---|
299 | inline bool JSGlobalObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot, bool& slotIsWriteable)
|
---|
300 | {
|
---|
301 | if (JSVariableObject::getOwnPropertySlotForWrite(exec, propertyName, slot, slotIsWriteable))
|
---|
302 | return true;
|
---|
303 | return symbolTableGet(propertyName, slot, slotIsWriteable);
|
---|
304 | }
|
---|
305 |
|
---|
306 | inline JSGlobalObject* ScopeChainNode::globalObject() const
|
---|
307 | {
|
---|
308 | JSGlobalObject* globalObject = static_cast<JSGlobalObject*>(bottom());
|
---|
309 | ASSERT(globalObject->isGlobalObject());
|
---|
310 | return globalObject;
|
---|
311 | }
|
---|
312 |
|
---|
313 | inline JSValue* StructureID::prototypeForLookup(ExecState* exec)
|
---|
314 | {
|
---|
315 | if (m_type == ObjectType)
|
---|
316 | return m_prototype;
|
---|
317 |
|
---|
318 | if (m_type == StringType)
|
---|
319 | return exec->lexicalGlobalObject()->stringPrototype();
|
---|
320 |
|
---|
321 | ASSERT(m_type == NumberType);
|
---|
322 | return exec->lexicalGlobalObject()->numberPrototype();
|
---|
323 | }
|
---|
324 |
|
---|
325 | } // namespace JSC
|
---|
326 |
|
---|
327 | #endif // JSGlobalObject_h
|
---|