JavaScriptCore:
[WebKit.git] / JavaScriptCore / kjs / JSGlobalObject.h
1 // -*- c-basic-offset: 4 -*-
2 /*
3  *  Copyright (C) 2007 Eric Seidel <eric@webkit.org>
4  *  Copyright (C) 2007 Apple Inc. All rights reserved.
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public License
17  *  along with this library; see the file COPYING.LIB.  If not, write to
18  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301, USA.
20  *
21  */
22
23 #ifndef KJS_GlobalObject_h
24 #define KJS_GlobalObject_h
25
26 #include "JSVariableObject.h"
27
28 namespace KJS {
29
30     class ActivationImp;
31     class ActivationStackNode;
32     class ArrayObjectImp;
33     class ArrayPrototype;
34     class BooleanObjectImp;
35     class BooleanPrototype;
36     class DateObjectImp;
37     class DatePrototype;
38     class Debugger;
39     class ErrorObjectImp;
40     class ErrorPrototype;
41     class EvalError;
42     class EvalErrorPrototype;
43     class FunctionObjectImp;
44     class FunctionPrototype;
45     class JSGlobalObject;
46     class NativeErrorImp;
47     class NativeErrorPrototype;
48     class NumberObjectImp;
49     class NumberPrototype;
50     class ObjectObjectImp;
51     class ObjectPrototype;
52     class RangeError;
53     class RangeErrorPrototype;
54     class ReferenceError;
55     class ReferenceError;
56     class ReferenceErrorPrototype;
57     class RegExpObjectImp;
58     class RegExpPrototype;
59     class RuntimeMethod;
60     class SavedBuiltins;
61     class ScopeChain;
62     class StringObjectImp;
63     class StringPrototype;
64     class SyntaxErrorPrototype;
65     class TypeError;
66     class TypeErrorPrototype;
67     class UriError;
68     class UriErrorPrototype;
69
70     enum CompatMode { NativeMode, IECompat, NetscapeCompat };
71
72     class JSGlobalObject : public JSVariableObject {
73     protected:
74         using JSVariableObject::JSVariableObjectData;
75
76         struct JSGlobalObjectData : public JSVariableObjectData {
77             JSGlobalObjectData(JSGlobalObject* globalObject)
78                 : JSVariableObjectData(&inlineSymbolTable)
79                 , globalExec(globalObject, globalObject, 0)
80             {
81             }
82
83             JSGlobalObject* next;
84             JSGlobalObject* prev;
85
86             Debugger* debugger;
87             CompatMode compatMode;
88             
89             ExecState globalExec;
90             ExecState* currentExec;
91             int recursion;
92
93             unsigned timeoutTime;
94             unsigned timeAtLastCheckTimeout;
95             unsigned timeExecuting;
96             unsigned timeoutCheckCount;
97             unsigned tickCount;
98             unsigned ticksUntilNextTimeoutCheck;
99
100             ObjectObjectImp* objectConstructor;
101             FunctionObjectImp* functionConstructor;
102             ArrayObjectImp* arrayConstructor;
103             BooleanObjectImp* booleanConstructor;
104             StringObjectImp* stringConstructor;
105             NumberObjectImp* numberConstructor;
106             DateObjectImp* dateConstructor;
107             RegExpObjectImp* regExpConstructor;
108             ErrorObjectImp* errorConstructor;
109             NativeErrorImp* evalErrorConstructor;
110             NativeErrorImp* rangeErrorConstructor;
111             NativeErrorImp* referenceErrorConstructor;
112             NativeErrorImp* syntaxErrorConstructor;
113             NativeErrorImp* typeErrorConstructor;
114             NativeErrorImp* URIErrorConstructor;
115
116             ObjectPrototype* objectPrototype;
117             FunctionPrototype* functionPrototype;
118             ArrayPrototype* arrayPrototype;
119             BooleanPrototype* booleanPrototype;
120             StringPrototype* stringPrototype;
121             NumberPrototype* numberPrototype;
122             DatePrototype* datePrototype;
123             RegExpPrototype* regExpPrototype;
124             ErrorPrototype* errorPrototype;
125             NativeErrorPrototype* evalErrorPrototype;
126             NativeErrorPrototype* rangeErrorPrototype;
127             NativeErrorPrototype* referenceErrorPrototype;
128             NativeErrorPrototype* syntaxErrorPrototype;
129             NativeErrorPrototype* typeErrorPrototype;
130             NativeErrorPrototype* URIErrorPrototype;
131
132             SymbolTable inlineSymbolTable;
133
134             ActivationStackNode* activations;
135             size_t activationCount;
136         };
137
138     public:
139         JSGlobalObject()
140             : JSVariableObject(new JSGlobalObjectData(this))
141         {
142             init();
143         }
144
145     protected:
146         JSGlobalObject(JSValue* proto)
147             : JSVariableObject(proto, new JSGlobalObjectData(this))
148         {
149             init();
150         }
151
152     public:
153         virtual ~JSGlobalObject();
154
155         virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
156         virtual void put(ExecState*, const Identifier&, JSValue*, int attr = None);
157
158         // Linked list of all global objects.
159         static JSGlobalObject* head() { return s_head; }
160         JSGlobalObject* next() { return d()->next; }
161
162         // Resets the global object to contain only built-in properties, sets
163         // the global object's prototype to "prototype," then adds the 
164         // default object prototype to the tail of the global object's 
165         // prototype chain.
166         void reset(JSValue* prototype);
167
168         // The following accessors return pristine values, even if a script 
169         // replaces the global object's associated property.
170
171         ObjectObjectImp* objectConstructor() const { return d()->objectConstructor; }
172         FunctionObjectImp* functionConstructor() const { return d()->functionConstructor; }
173         ArrayObjectImp* arrayConstructor() const { return d()->arrayConstructor; }
174         BooleanObjectImp* booleanConstructor() const { return d()->booleanConstructor; }
175         StringObjectImp* stringConstructor() const{ return d()->stringConstructor; }
176         NumberObjectImp* numberConstructor() const{ return d()->numberConstructor; }
177         DateObjectImp* dateConstructor() const{ return d()->dateConstructor; }
178         RegExpObjectImp* regExpConstructor() const { return d()->regExpConstructor; }
179         ErrorObjectImp* errorConstructor() const { return d()->errorConstructor; }
180         NativeErrorImp* evalErrorConstructor() const { return d()->evalErrorConstructor; }
181         NativeErrorImp* rangeErrorConstructor() const { return d()->rangeErrorConstructor; }
182         NativeErrorImp* referenceErrorConstructor() const { return d()->referenceErrorConstructor; }
183         NativeErrorImp* syntaxErrorConstructor() const { return d()->syntaxErrorConstructor; }
184         NativeErrorImp* typeErrorConstructor() const { return d()->typeErrorConstructor; }
185         NativeErrorImp* URIErrorConstructor() const { return d()->URIErrorConstructor; }
186
187         ObjectPrototype* objectPrototype() const { return d()->objectPrototype; }
188         FunctionPrototype* functionPrototype() const { return d()->functionPrototype; }
189         ArrayPrototype* arrayPrototype() const { return d()->arrayPrototype; }
190         BooleanPrototype* booleanPrototype() const { return d()->booleanPrototype; }
191         StringPrototype* stringPrototype() const { return d()->stringPrototype; }
192         NumberPrototype* numberPrototype() const { return d()->numberPrototype; }
193         DatePrototype* datePrototype() const { return d()->datePrototype; }
194         RegExpPrototype* regExpPrototype() const { return d()->regExpPrototype; }
195         ErrorPrototype* errorPrototype() const { return d()->errorPrototype; }
196         NativeErrorPrototype* evalErrorPrototype() const { return d()->evalErrorPrototype; }
197         NativeErrorPrototype* rangeErrorPrototype() const { return d()->rangeErrorPrototype; }
198         NativeErrorPrototype* referenceErrorPrototype() const { return d()->referenceErrorPrototype; }
199         NativeErrorPrototype* syntaxErrorPrototype() const { return d()->syntaxErrorPrototype; }
200         NativeErrorPrototype* typeErrorPrototype() const { return d()->typeErrorPrototype; }
201         NativeErrorPrototype* URIErrorPrototype() const { return d()->URIErrorPrototype; }
202
203         void saveBuiltins(SavedBuiltins&) const;
204         void restoreBuiltins(const SavedBuiltins&);
205
206         void setTimeoutTime(unsigned timeoutTime) { d()->timeoutTime = timeoutTime; }
207         void startTimeoutCheck();
208         void stopTimeoutCheck();
209         bool timedOut();
210
211         Debugger* debugger() const { return d()->debugger; }
212         void setDebugger(Debugger* debugger) { d()->debugger = debugger; }
213
214         void setCurrentExec(ExecState* exec) { d()->currentExec = exec; }
215         ExecState* currentExec() const { return d()->currentExec; }
216
217         // FIXME: Let's just pick one compatible behavior and go with it.
218         void setCompatMode(CompatMode mode) { d()->compatMode = mode; }
219         CompatMode compatMode() const { return d()->compatMode; }
220         
221         int recursion() { return d()->recursion; }
222         void incRecursion() { ++d()->recursion; }
223         void decRecursion() { --d()->recursion; }
224
225         virtual void mark();
226
227         virtual bool isGlobalObject() const { return true; }
228
229         virtual ExecState* globalExec();
230
231         virtual bool shouldInterruptScript() const { return true; }
232
233         virtual bool allowsAccessFrom(const JSGlobalObject*) const { return true; }
234
235         ActivationImp* pushActivation(ExecState*);
236         void popActivation();
237         void tearOffActivation(ExecState*, bool markAsRelic = false);
238
239     private:
240         void init();
241         
242         JSGlobalObjectData* d() const { return static_cast<JSGlobalObjectData*>(JSVariableObject::d); }
243
244         bool checkTimeout();
245         void resetTimeoutCheck();
246
247         void checkActivationCount();
248
249         static JSGlobalObject* s_head;
250     };
251
252     inline bool JSGlobalObject::timedOut()
253     {
254         d()->tickCount++;
255
256         if (d()->tickCount != d()->ticksUntilNextTimeoutCheck)
257             return false;
258
259         return checkTimeout();
260     }
261
262 } // namespace KJS
263
264 #endif // KJS_GlobalObject_h