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 ErrorPrototype;
|
---|
40 | class EvalError;
|
---|
41 | class EvalErrorPrototype;
|
---|
42 | class FunctionPrototype;
|
---|
43 | class GlobalEvalFunction;
|
---|
44 | class JSGlobalObject;
|
---|
45 | class NativeErrorConstructor;
|
---|
46 | class NativeErrorPrototype;
|
---|
47 | class NumberPrototype;
|
---|
48 | class ObjectPrototype;
|
---|
49 | class ProgramCodeBlock;
|
---|
50 | class RangeError;
|
---|
51 | class RangeErrorPrototype;
|
---|
52 | class ReferenceError;
|
---|
53 | class ReferenceError;
|
---|
54 | class ReferenceErrorPrototype;
|
---|
55 | class RegExpConstructor;
|
---|
56 | class RegExpPrototype;
|
---|
57 | class RegisterFile;
|
---|
58 | class RuntimeMethod;
|
---|
59 | class ScopeChain;
|
---|
60 | class StringPrototype;
|
---|
61 | class SyntaxErrorPrototype;
|
---|
62 | class TypeError;
|
---|
63 | class TypeErrorPrototype;
|
---|
64 | class UriError;
|
---|
65 | class UriErrorPrototype;
|
---|
66 |
|
---|
67 | struct ActivationStackNode;
|
---|
68 | struct HashTable;
|
---|
69 |
|
---|
70 | typedef Vector<ExecState*, 16> ExecStateStack;
|
---|
71 |
|
---|
72 | class JSGlobalObject : public JSVariableObject {
|
---|
73 | protected:
|
---|
74 | using JSVariableObject::JSVariableObjectData;
|
---|
75 |
|
---|
76 | struct JSGlobalObjectData : public JSVariableObjectData {
|
---|
77 | JSGlobalObjectData(JSGlobalObject* globalObject, JSObject* thisValue)
|
---|
78 | : JSVariableObjectData(&symbolTable, 0)
|
---|
79 | , globalScopeChain(globalObject, thisValue)
|
---|
80 | {
|
---|
81 | }
|
---|
82 |
|
---|
83 | virtual ~JSGlobalObjectData()
|
---|
84 | {
|
---|
85 | }
|
---|
86 |
|
---|
87 | JSGlobalObject* next;
|
---|
88 | JSGlobalObject* prev;
|
---|
89 |
|
---|
90 | Debugger* debugger;
|
---|
91 |
|
---|
92 | ScopeChain globalScopeChain;
|
---|
93 | OwnPtr<ExecState> globalExec;
|
---|
94 |
|
---|
95 | int recursion;
|
---|
96 |
|
---|
97 | RegExpConstructor* regExpConstructor;
|
---|
98 | ErrorConstructor* errorConstructor;
|
---|
99 | NativeErrorConstructor* evalErrorConstructor;
|
---|
100 | NativeErrorConstructor* rangeErrorConstructor;
|
---|
101 | NativeErrorConstructor* referenceErrorConstructor;
|
---|
102 | NativeErrorConstructor* syntaxErrorConstructor;
|
---|
103 | NativeErrorConstructor* typeErrorConstructor;
|
---|
104 | NativeErrorConstructor* URIErrorConstructor;
|
---|
105 |
|
---|
106 | GlobalEvalFunction* evalFunction;
|
---|
107 |
|
---|
108 | ObjectPrototype* objectPrototype;
|
---|
109 | FunctionPrototype* functionPrototype;
|
---|
110 | ArrayPrototype* arrayPrototype;
|
---|
111 | BooleanPrototype* booleanPrototype;
|
---|
112 | StringPrototype* stringPrototype;
|
---|
113 | NumberPrototype* numberPrototype;
|
---|
114 | DatePrototype* datePrototype;
|
---|
115 | RegExpPrototype* regExpPrototype;
|
---|
116 | ErrorPrototype* errorPrototype;
|
---|
117 | NativeErrorPrototype* evalErrorPrototype;
|
---|
118 | NativeErrorPrototype* rangeErrorPrototype;
|
---|
119 | NativeErrorPrototype* referenceErrorPrototype;
|
---|
120 | NativeErrorPrototype* syntaxErrorPrototype;
|
---|
121 | NativeErrorPrototype* typeErrorPrototype;
|
---|
122 | NativeErrorPrototype* URIErrorPrototype;
|
---|
123 |
|
---|
124 | SymbolTable symbolTable;
|
---|
125 | unsigned profileGroup;
|
---|
126 |
|
---|
127 | RefPtr<JSGlobalData> globalData;
|
---|
128 |
|
---|
129 | HashSet<ProgramCodeBlock*> codeBlocks;
|
---|
130 |
|
---|
131 | OwnPtr<HashSet<JSObject*> > arrayVisitedElements; // Global data shared by array prototype functions.
|
---|
132 | };
|
---|
133 |
|
---|
134 | public:
|
---|
135 | void* operator new(size_t, JSGlobalData*);
|
---|
136 |
|
---|
137 | JSGlobalObject(JSGlobalData* globalData)
|
---|
138 | : JSVariableObject(globalData->nullProtoStructureID, new JSGlobalObjectData(this, this))
|
---|
139 | {
|
---|
140 | init(this);
|
---|
141 | }
|
---|
142 |
|
---|
143 | protected:
|
---|
144 | JSGlobalObject(PassRefPtr<StructureID> structure, JSGlobalObjectData* data, JSObject* globalThisValue)
|
---|
145 | : JSVariableObject(structure, data)
|
---|
146 | {
|
---|
147 | init(globalThisValue);
|
---|
148 | }
|
---|
149 |
|
---|
150 | public:
|
---|
151 | virtual ~JSGlobalObject();
|
---|
152 |
|
---|
153 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
154 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&, bool& slotIsWriteable);
|
---|
155 | virtual void put(ExecState*, const Identifier&, JSValue*, PutPropertySlot&);
|
---|
156 | virtual void putWithAttributes(ExecState*, const Identifier& propertyName, JSValue* value, unsigned attributes);
|
---|
157 |
|
---|
158 | virtual void defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunc);
|
---|
159 | virtual void defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunc);
|
---|
160 |
|
---|
161 | // Linked list of all global objects that use the same JSGlobalData.
|
---|
162 | JSGlobalObject*& head() { return d()->globalData->head; }
|
---|
163 | JSGlobalObject* next() { return d()->next; }
|
---|
164 |
|
---|
165 | // The following accessors return pristine values, even if a script
|
---|
166 | // replaces the global object's associated property.
|
---|
167 |
|
---|
168 | RegExpConstructor* regExpConstructor() const { return d()->regExpConstructor; }
|
---|
169 |
|
---|
170 | ErrorConstructor* errorConstructor() const { return d()->errorConstructor; }
|
---|
171 | NativeErrorConstructor* evalErrorConstructor() const { return d()->evalErrorConstructor; }
|
---|
172 | NativeErrorConstructor* rangeErrorConstructor() const { return d()->rangeErrorConstructor; }
|
---|
173 | NativeErrorConstructor* referenceErrorConstructor() const { return d()->referenceErrorConstructor; }
|
---|
174 | NativeErrorConstructor* syntaxErrorConstructor() const { return d()->syntaxErrorConstructor; }
|
---|
175 | NativeErrorConstructor* typeErrorConstructor() const { return d()->typeErrorConstructor; }
|
---|
176 | NativeErrorConstructor* URIErrorConstructor() const { return d()->URIErrorConstructor; }
|
---|
177 |
|
---|
178 | GlobalEvalFunction* evalFunction() const { return d()->evalFunction; }
|
---|
179 |
|
---|
180 | ObjectPrototype* objectPrototype() const { return d()->objectPrototype; }
|
---|
181 | FunctionPrototype* functionPrototype() const { return d()->functionPrototype; }
|
---|
182 | ArrayPrototype* arrayPrototype() const { return d()->arrayPrototype; }
|
---|
183 | BooleanPrototype* booleanPrototype() const { return d()->booleanPrototype; }
|
---|
184 | StringPrototype* stringPrototype() const { return d()->stringPrototype; }
|
---|
185 | NumberPrototype* numberPrototype() const { return d()->numberPrototype; }
|
---|
186 | DatePrototype* datePrototype() const { return d()->datePrototype; }
|
---|
187 | RegExpPrototype* regExpPrototype() const { return d()->regExpPrototype; }
|
---|
188 | ErrorPrototype* errorPrototype() const { return d()->errorPrototype; }
|
---|
189 | NativeErrorPrototype* evalErrorPrototype() const { return d()->evalErrorPrototype; }
|
---|
190 | NativeErrorPrototype* rangeErrorPrototype() const { return d()->rangeErrorPrototype; }
|
---|
191 | NativeErrorPrototype* referenceErrorPrototype() const { return d()->referenceErrorPrototype; }
|
---|
192 | NativeErrorPrototype* syntaxErrorPrototype() const { return d()->syntaxErrorPrototype; }
|
---|
193 | NativeErrorPrototype* typeErrorPrototype() const { return d()->typeErrorPrototype; }
|
---|
194 | NativeErrorPrototype* URIErrorPrototype() const { return d()->URIErrorPrototype; }
|
---|
195 |
|
---|
196 | void setProfileGroup(unsigned value) { d()->profileGroup = value; }
|
---|
197 | unsigned profileGroup() const { return d()->profileGroup; }
|
---|
198 |
|
---|
199 | void setTimeoutTime(unsigned timeoutTime);
|
---|
200 | void startTimeoutCheck();
|
---|
201 | void stopTimeoutCheck();
|
---|
202 |
|
---|
203 | Debugger* debugger() const { return d()->debugger; }
|
---|
204 | void setDebugger(Debugger* debugger) { d()->debugger = debugger; }
|
---|
205 |
|
---|
206 | int recursion() { return d()->recursion; }
|
---|
207 | void incRecursion() { ++d()->recursion; }
|
---|
208 | void decRecursion() { --d()->recursion; }
|
---|
209 |
|
---|
210 | ScopeChain& globalScopeChain() { return d()->globalScopeChain; }
|
---|
211 |
|
---|
212 | virtual void mark();
|
---|
213 |
|
---|
214 | virtual bool isGlobalObject() const { return true; }
|
---|
215 | virtual JSGlobalObject* toGlobalObject(ExecState*) const;
|
---|
216 |
|
---|
217 | virtual ExecState* globalExec();
|
---|
218 |
|
---|
219 | virtual bool shouldInterruptScript() const { return true; }
|
---|
220 |
|
---|
221 | virtual bool allowsAccessFrom(const JSGlobalObject*) const { return true; }
|
---|
222 |
|
---|
223 | virtual bool isDynamicScope() const;
|
---|
224 |
|
---|
225 | HashSet<JSObject*>& arrayVisitedElements() { if (!d()->arrayVisitedElements) d()->arrayVisitedElements.set(new HashSet<JSObject*>); return *d()->arrayVisitedElements; }
|
---|
226 |
|
---|
227 | HashSet<ProgramCodeBlock*>& codeBlocks() { return d()->codeBlocks; }
|
---|
228 |
|
---|
229 | void copyGlobalsFrom(RegisterFile&);
|
---|
230 | void copyGlobalsTo(RegisterFile&);
|
---|
231 |
|
---|
232 | void resetPrototype(JSValue* prototype);
|
---|
233 |
|
---|
234 | JSGlobalData* globalData() { return d()->globalData.get(); }
|
---|
235 | JSGlobalObjectData* d() const { return static_cast<JSGlobalObjectData*>(JSVariableObject::d); }
|
---|
236 |
|
---|
237 | protected:
|
---|
238 | struct GlobalPropertyInfo {
|
---|
239 | GlobalPropertyInfo(const Identifier& i, JSValue* v, unsigned a)
|
---|
240 | : identifier(i)
|
---|
241 | , value(v)
|
---|
242 | , attributes(a)
|
---|
243 | {
|
---|
244 | }
|
---|
245 |
|
---|
246 | const Identifier identifier;
|
---|
247 | JSValue* value;
|
---|
248 | unsigned attributes;
|
---|
249 | };
|
---|
250 | void addStaticGlobals(GlobalPropertyInfo*, int count);
|
---|
251 |
|
---|
252 | private:
|
---|
253 | // FIXME: Fold these functions into the constructor.
|
---|
254 | void init(JSObject* thisValue);
|
---|
255 | void reset(JSValue* prototype);
|
---|
256 |
|
---|
257 | void* operator new(size_t); // can only be allocated with JSGlobalData
|
---|
258 | };
|
---|
259 |
|
---|
260 | inline void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
|
---|
261 | {
|
---|
262 | size_t registerArraySize = d()->registerArraySize;
|
---|
263 | Register* registerArray = new Register[registerArraySize + count];
|
---|
264 | if (d()->registerArray)
|
---|
265 | memcpy(registerArray + count, d()->registerArray.get(), registerArraySize * sizeof(Register));
|
---|
266 | setRegisterArray(registerArray, registerArraySize + count);
|
---|
267 |
|
---|
268 | for (int i = 0, index = -static_cast<int>(registerArraySize) - 1; i < count; ++i, --index) {
|
---|
269 | GlobalPropertyInfo& global = globals[i];
|
---|
270 | ASSERT(global.attributes & DontDelete);
|
---|
271 | SymbolTableEntry newEntry(index, global.attributes);
|
---|
272 | symbolTable().add(global.identifier.ustring().rep(), newEntry);
|
---|
273 | registerAt(index) = global.value;
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | inline bool JSGlobalObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
|
---|
278 | {
|
---|
279 | if (JSVariableObject::getOwnPropertySlot(exec, propertyName, slot))
|
---|
280 | return true;
|
---|
281 | return symbolTableGet(propertyName, slot);
|
---|
282 | }
|
---|
283 |
|
---|
284 | inline bool JSGlobalObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot, bool& slotIsWriteable)
|
---|
285 | {
|
---|
286 | if (JSVariableObject::getOwnPropertySlotForWrite(exec, propertyName, slot, slotIsWriteable))
|
---|
287 | return true;
|
---|
288 | return symbolTableGet(propertyName, slot, slotIsWriteable);
|
---|
289 | }
|
---|
290 |
|
---|
291 | inline JSGlobalObject* ScopeChainNode::globalObject() const
|
---|
292 | {
|
---|
293 | JSGlobalObject* globalObject = static_cast<JSGlobalObject*>(bottom());
|
---|
294 | ASSERT(globalObject->isGlobalObject());
|
---|
295 | return globalObject;
|
---|
296 | }
|
---|
297 |
|
---|
298 | inline JSValue* StructureID::prototypeForLookup(ExecState* exec)
|
---|
299 | {
|
---|
300 | if (m_type == ObjectType)
|
---|
301 | return m_prototype;
|
---|
302 |
|
---|
303 | if (m_type == StringType)
|
---|
304 | return exec->lexicalGlobalObject()->stringPrototype();
|
---|
305 |
|
---|
306 | ASSERT(m_type == NumberType);
|
---|
307 | return exec->lexicalGlobalObject()->numberPrototype();
|
---|
308 | }
|
---|
309 |
|
---|
310 | } // namespace JSC
|
---|
311 |
|
---|
312 | #endif // JSGlobalObject_h
|
---|