JavaScriptCore:
[WebKit-https.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 ArrayObjectImp;
31     class ArrayPrototype;
32     class BooleanObjectImp;
33     class BooleanPrototype;
34     class DateObjectImp;
35     class DatePrototype;
36     class Debugger;
37     class ErrorObjectImp;
38     class ErrorPrototype;
39     class EvalError;
40     class EvalErrorPrototype;
41     class FunctionObjectImp;
42     class FunctionPrototype;
43     class JSGlobalObject;
44     class NativeErrorImp;
45     class NativeErrorPrototype;
46     class NumberObjectImp;
47     class NumberPrototype;
48     class ObjectObjectImp;
49     class ObjectPrototype;
50     class RangeError;
51     class RangeErrorPrototype;
52     class ReferenceError;
53     class ReferenceError;
54     class ReferenceErrorPrototype;
55     class RegExpObjectImp;
56     class RegExpPrototype;
57     class RuntimeMethod;
58     class SavedBuiltins;
59     class ScopeChain;
60     class StringObjectImp;
61     class StringPrototype;
62     class SyntaxErrorPrototype;
63     class TypeError;
64     class TypeErrorPrototype;
65     class UriError;
66     class UriErrorPrototype;
67
68     enum CompatMode { NativeMode, IECompat, NetscapeCompat };
69
70     class JSGlobalObject : public JSVariableObject {
71     protected:
72         using JSVariableObject::JSVariableObjectData;
73
74         struct JSGlobalObjectData : public JSVariableObjectData {
75             JSGlobalObjectData(JSGlobalObject* globalObject)
76                 : JSVariableObjectData(&inlineSymbolTable)
77                 , globalExec(globalObject, globalObject, 0)
78             {
79             }
80
81             JSGlobalObject* next;
82             JSGlobalObject* prev;
83
84             Debugger* debugger;
85             CompatMode compatMode;
86             
87             ExecState globalExec;
88             ExecState* currentExec;
89             int recursion;
90
91             unsigned timeoutTime;
92             unsigned timeAtLastCheckTimeout;
93             unsigned timeExecuting;
94             unsigned timeoutCheckCount;
95             unsigned tickCount;
96             unsigned ticksUntilNextTimeoutCheck;
97
98             ObjectObjectImp* objectConstructor;
99             FunctionObjectImp* functionConstructor;
100             ArrayObjectImp* arrayConstructor;
101             BooleanObjectImp* booleanConstructor;
102             StringObjectImp* stringConstructor;
103             NumberObjectImp* numberConstructor;
104             DateObjectImp* dateConstructor;
105             RegExpObjectImp* regExpConstructor;
106             ErrorObjectImp* errorConstructor;
107             NativeErrorImp* evalErrorConstructor;
108             NativeErrorImp* rangeErrorConstructor;
109             NativeErrorImp* referenceErrorConstructor;
110             NativeErrorImp* syntaxErrorConstructor;
111             NativeErrorImp* typeErrorConstructor;
112             NativeErrorImp* URIErrorConstructor;
113
114             ObjectPrototype* objectPrototype;
115             FunctionPrototype* functionPrototype;
116             ArrayPrototype* arrayPrototype;
117             BooleanPrototype* booleanPrototype;
118             StringPrototype* stringPrototype;
119             NumberPrototype* numberPrototype;
120             DatePrototype* datePrototype;
121             RegExpPrototype* regExpPrototype;
122             ErrorPrototype* errorPrototype;
123             NativeErrorPrototype* evalErrorPrototype;
124             NativeErrorPrototype* rangeErrorPrototype;
125             NativeErrorPrototype* referenceErrorPrototype;
126             NativeErrorPrototype* syntaxErrorPrototype;
127             NativeErrorPrototype* typeErrorPrototype;
128             NativeErrorPrototype* URIErrorPrototype;
129
130             SymbolTable inlineSymbolTable;
131         };
132
133     public:
134         JSGlobalObject()
135             : JSVariableObject(new JSGlobalObjectData(this))
136         {
137             init();
138         }
139
140     protected:
141         JSGlobalObject(JSValue* proto)
142             : JSVariableObject(proto, new JSGlobalObjectData(this))
143         {
144             init();
145         }
146
147     public:
148         virtual ~JSGlobalObject();
149
150         // Linked list of all global objects.
151         static JSGlobalObject* head() { return s_head; }
152         JSGlobalObject* next() { return d()->next; }
153
154         // Resets the global object to contain only built-in properties, sets
155         // the global object's prototype to "prototype," then adds the 
156         // default object prototype to the tail of the global object's 
157         // prototype chain.
158         void reset(JSValue* prototype);
159
160         // The following accessors return pristine values, even if a script 
161         // replaces the global object's associated property.
162
163         ObjectObjectImp* objectConstructor() const { return d()->objectConstructor; }
164         FunctionObjectImp* functionConstructor() const { return d()->functionConstructor; }
165         ArrayObjectImp* arrayConstructor() const { return d()->arrayConstructor; }
166         BooleanObjectImp* booleanConstructor() const { return d()->booleanConstructor; }
167         StringObjectImp* stringConstructor() const{ return d()->stringConstructor; }
168         NumberObjectImp* numberConstructor() const{ return d()->numberConstructor; }
169         DateObjectImp* dateConstructor() const{ return d()->dateConstructor; }
170         RegExpObjectImp* regExpConstructor() const { return d()->regExpConstructor; }
171         ErrorObjectImp* errorConstructor() const { return d()->errorConstructor; }
172         NativeErrorImp* evalErrorConstructor() const { return d()->evalErrorConstructor; }
173         NativeErrorImp* rangeErrorConstructor() const { return d()->rangeErrorConstructor; }
174         NativeErrorImp* referenceErrorConstructor() const { return d()->referenceErrorConstructor; }
175         NativeErrorImp* syntaxErrorConstructor() const { return d()->syntaxErrorConstructor; }
176         NativeErrorImp* typeErrorConstructor() const { return d()->typeErrorConstructor; }
177         NativeErrorImp* URIErrorConstructor() const { return d()->URIErrorConstructor; }
178
179         ObjectPrototype* objectPrototype() const { return d()->objectPrototype; }
180         FunctionPrototype* functionPrototype() const { return d()->functionPrototype; }
181         ArrayPrototype* arrayPrototype() const { return d()->arrayPrototype; }
182         BooleanPrototype* booleanPrototype() const { return d()->booleanPrototype; }
183         StringPrototype* stringPrototype() const { return d()->stringPrototype; }
184         NumberPrototype* numberPrototype() const { return d()->numberPrototype; }
185         DatePrototype* datePrototype() const { return d()->datePrototype; }
186         RegExpPrototype* regExpPrototype() const { return d()->regExpPrototype; }
187         ErrorPrototype* errorPrototype() const { return d()->errorPrototype; }
188         NativeErrorPrototype* evalErrorPrototype() const { return d()->evalErrorPrototype; }
189         NativeErrorPrototype* rangeErrorPrototype() const { return d()->rangeErrorPrototype; }
190         NativeErrorPrototype* referenceErrorPrototype() const { return d()->referenceErrorPrototype; }
191         NativeErrorPrototype* syntaxErrorPrototype() const { return d()->syntaxErrorPrototype; }
192         NativeErrorPrototype* typeErrorPrototype() const { return d()->typeErrorPrototype; }
193         NativeErrorPrototype* URIErrorPrototype() const { return d()->URIErrorPrototype; }
194
195         void saveBuiltins(SavedBuiltins&) const;
196         void restoreBuiltins(const SavedBuiltins&);
197
198         void setTimeoutTime(unsigned timeoutTime) { d()->timeoutTime = timeoutTime; }
199         void startTimeoutCheck();
200         void stopTimeoutCheck();
201         bool timedOut();
202
203         Debugger* debugger() const { return d()->debugger; }
204         void setDebugger(Debugger* debugger) { d()->debugger = debugger; }
205
206         void setCurrentExec(ExecState* exec) { d()->currentExec = exec; }
207         ExecState* currentExec() const { return d()->currentExec; }
208
209         // FIXME: Let's just pick one compatible behavior and go with it.
210         void setCompatMode(CompatMode mode) { d()->compatMode = mode; }
211         CompatMode compatMode() const { return d()->compatMode; }
212         
213         int recursion() { return d()->recursion; }
214         void incRecursion() { ++d()->recursion; }
215         void decRecursion() { --d()->recursion; }
216
217         virtual void mark();
218
219         virtual bool isGlobalObject() const { return true; }
220
221         virtual ExecState* globalExec();
222
223         virtual bool shouldInterruptScript() const { return true; }
224
225         virtual bool isSafeScript(const JSGlobalObject*) const { return true; }
226
227     private:
228         void init();
229         
230         JSGlobalObjectData* d() const { return static_cast<JSGlobalObjectData*>(JSVariableObject::d); }
231
232         bool checkTimeout();
233         void resetTimeoutCheck();
234
235         static JSGlobalObject* s_head;
236     };
237
238     inline bool JSGlobalObject::timedOut()
239     {
240         d()->tickCount++;
241
242         if (d()->tickCount != d()->ticksUntilNextTimeoutCheck)
243             return false;
244
245         return checkTimeout();
246     }
247
248 } // namespace KJS
249
250 #endif // KJS_GlobalObject_h