]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSGlobalData.h
fee7818396599a19173d912bc1885ed9adeaf240
[apple/javascriptcore.git] / runtime / JSGlobalData.h
1 /*
2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef JSGlobalData_h
30 #define JSGlobalData_h
31
32 #include "CachedTranscendentalFunction.h"
33 #include "Heap.h"
34 #include "DateInstanceCache.h"
35 #include "ExecutableAllocator.h"
36 #include "Strong.h"
37 #include "JITStubs.h"
38 #include "JSValue.h"
39 #include "NumericStrings.h"
40 #include "SmallStrings.h"
41 #include "Terminator.h"
42 #include "TimeoutChecker.h"
43 #include "WeakRandom.h"
44 #include <wtf/BumpPointerAllocator.h>
45 #include <wtf/Forward.h>
46 #include <wtf/HashMap.h>
47 #include <wtf/RefCounted.h>
48 #include <wtf/ThreadSpecific.h>
49 #include <wtf/WTFThreadData.h>
50 #if ENABLE(REGEXP_TRACING)
51 #include <wtf/ListHashSet.h>
52 #endif
53
54 struct OpaqueJSClass;
55 struct OpaqueJSClassContextData;
56
57 namespace JSC {
58
59 class CodeBlock;
60 class CommonIdentifiers;
61 class HandleStack;
62 class IdentifierTable;
63 class Interpreter;
64 class JSGlobalObject;
65 class JSObject;
66 class Lexer;
67 class NativeExecutable;
68 class Parser;
69 class RegExpCache;
70 class Stringifier;
71 class Structure;
72 class UString;
73 #if ENABLE(REGEXP_TRACING)
74 class RegExp;
75 #endif
76
77 struct HashTable;
78 struct Instruction;
79
80 struct DSTOffsetCache {
81 DSTOffsetCache()
82 {
83 reset();
84 }
85
86 void reset()
87 {
88 offset = 0.0;
89 start = 0.0;
90 end = -1.0;
91 increment = 0.0;
92 }
93
94 double offset;
95 double start;
96 double end;
97 double increment;
98 };
99
100 enum ThreadStackType {
101 ThreadStackTypeLarge,
102 ThreadStackTypeSmall
103 };
104
105 class JSGlobalData : public RefCounted<JSGlobalData> {
106 public:
107 // WebCore has a one-to-one mapping of threads to JSGlobalDatas;
108 // either create() or createLeaked() should only be called once
109 // on a thread, this is the 'default' JSGlobalData (it uses the
110 // thread's default string uniquing table from wtfThreadData).
111 // API contexts created using the new context group aware interface
112 // create APIContextGroup objects which require less locking of JSC
113 // than the old singleton APIShared JSGlobalData created for use by
114 // the original API.
115 enum GlobalDataType { Default, APIContextGroup, APIShared };
116
117 struct ClientData {
118 virtual ~ClientData() = 0;
119 };
120
121 bool isSharedInstance() { return globalDataType == APIShared; }
122 bool usingAPI() { return globalDataType != Default; }
123 static bool sharedInstanceExists();
124 static JSGlobalData& sharedInstance();
125
126 static PassRefPtr<JSGlobalData> create(ThreadStackType);
127 static PassRefPtr<JSGlobalData> createLeaked(ThreadStackType);
128 static PassRefPtr<JSGlobalData> createContextGroup(ThreadStackType);
129 ~JSGlobalData();
130
131 #if ENABLE(JSC_MULTIPLE_THREADS)
132 // Will start tracking threads that use the heap, which is resource-heavy.
133 void makeUsableFromMultipleThreads() { heap.machineThreads().makeUsableFromMultipleThreads(); }
134 #endif
135
136 GlobalDataType globalDataType;
137 ClientData* clientData;
138
139 const HashTable* arrayConstructorTable;
140 const HashTable* arrayPrototypeTable;
141 const HashTable* booleanPrototypeTable;
142 const HashTable* dateTable;
143 const HashTable* dateConstructorTable;
144 const HashTable* errorPrototypeTable;
145 const HashTable* globalObjectTable;
146 const HashTable* jsonTable;
147 const HashTable* mathTable;
148 const HashTable* numberConstructorTable;
149 const HashTable* numberPrototypeTable;
150 const HashTable* objectConstructorTable;
151 const HashTable* objectPrototypeTable;
152 const HashTable* regExpTable;
153 const HashTable* regExpConstructorTable;
154 const HashTable* regExpPrototypeTable;
155 const HashTable* stringTable;
156 const HashTable* stringConstructorTable;
157
158 Strong<Structure> structureStructure;
159 Strong<Structure> debuggerActivationStructure;
160 Strong<Structure> activationStructure;
161 Strong<Structure> interruptedExecutionErrorStructure;
162 Strong<Structure> terminatedExecutionErrorStructure;
163 Strong<Structure> staticScopeStructure;
164 Strong<Structure> strictEvalActivationStructure;
165 Strong<Structure> stringStructure;
166 Strong<Structure> notAnObjectStructure;
167 Strong<Structure> propertyNameIteratorStructure;
168 Strong<Structure> getterSetterStructure;
169 Strong<Structure> apiWrapperStructure;
170 Strong<Structure> scopeChainNodeStructure;
171 Strong<Structure> executableStructure;
172 Strong<Structure> nativeExecutableStructure;
173 Strong<Structure> evalExecutableStructure;
174 Strong<Structure> programExecutableStructure;
175 Strong<Structure> functionExecutableStructure;
176 Strong<Structure> dummyMarkableCellStructure;
177 Strong<Structure> regExpStructure;
178 Strong<Structure> structureChainStructure;
179
180 #if ENABLE(JSC_ZOMBIES)
181 Strong<Structure> zombieStructure;
182 #endif
183
184 static void storeVPtrs();
185 static JS_EXPORTDATA void* jsArrayVPtr;
186 static JS_EXPORTDATA void* jsByteArrayVPtr;
187 static JS_EXPORTDATA void* jsStringVPtr;
188 static JS_EXPORTDATA void* jsFunctionVPtr;
189
190 IdentifierTable* identifierTable;
191 CommonIdentifiers* propertyNames;
192 const MarkedArgumentBuffer* emptyList; // Lists are supposed to be allocated on the stack to have their elements properly marked, which is not the case here - but this list has nothing to mark.
193 SmallStrings smallStrings;
194 NumericStrings numericStrings;
195 DateInstanceCache dateInstanceCache;
196
197 #if ENABLE(ASSEMBLER)
198 ExecutableAllocator executableAllocator;
199 ExecutableAllocator regexAllocator;
200 #endif
201
202 #if !ENABLE(JIT)
203 bool canUseJIT() { return false; } // interpreter only
204 #elif !ENABLE(INTERPRETER)
205 bool canUseJIT() { return true; } // jit only
206 #else
207 bool canUseJIT() { return m_canUseJIT; }
208 #endif
209
210 const StackBounds& stack()
211 {
212 return wtfThreadData().stack();
213 }
214
215 Lexer* lexer;
216 Parser* parser;
217 Interpreter* interpreter;
218 #if ENABLE(JIT)
219 OwnPtr<JITThunks> jitStubs;
220 MacroAssemblerCodePtr getCTIStub(ThunkGenerator generator)
221 {
222 return jitStubs->ctiStub(this, generator);
223 }
224 NativeExecutable* getHostFunction(NativeFunction, ThunkGenerator);
225 #endif
226 NativeExecutable* getHostFunction(NativeFunction);
227
228 TimeoutChecker timeoutChecker;
229 Terminator terminator;
230 Heap heap;
231
232 JSValue exception;
233 #if ENABLE(JIT)
234 ReturnAddressPtr exceptionLocation;
235 #endif
236
237 HashMap<OpaqueJSClass*, OpaqueJSClassContextData*> opaqueJSClassData;
238
239 unsigned globalObjectCount;
240 JSGlobalObject* dynamicGlobalObject;
241
242 HashSet<JSObject*> stringRecursionCheckVisitedObjects;
243
244 double cachedUTCOffset;
245 DSTOffsetCache dstOffsetCache;
246
247 UString cachedDateString;
248 double cachedDateStringValue;
249
250 int maxReentryDepth;
251
252 RegExpCache* m_regExpCache;
253 BumpPointerAllocator m_regExpAllocator;
254
255 #if ENABLE(REGEXP_TRACING)
256 typedef ListHashSet<RefPtr<RegExp> > RTTraceList;
257 RTTraceList* m_rtTraceList;
258 #endif
259
260 #ifndef NDEBUG
261 ThreadIdentifier exclusiveThread;
262 #endif
263
264 CachedTranscendentalFunction<sin> cachedSin;
265
266 void resetDateCache();
267
268 void startSampling();
269 void stopSampling();
270 void dumpSampleData(ExecState* exec);
271 void recompileAllJSFunctions();
272 RegExpCache* regExpCache() { return m_regExpCache; }
273 #if ENABLE(REGEXP_TRACING)
274 void addRegExpToTrace(PassRefPtr<RegExp> regExp);
275 #endif
276 void dumpRegExpTrace();
277 HandleSlot allocateGlobalHandle() { return heap.allocateGlobalHandle(); }
278 HandleSlot allocateLocalHandle() { return heap.allocateLocalHandle(); }
279 void clearBuiltinStructures();
280
281 bool isCollectorBusy() { return heap.isBusy(); }
282 void releaseExecutableMemory();
283
284 private:
285 JSGlobalData(GlobalDataType, ThreadStackType);
286 static JSGlobalData*& sharedInstanceInternal();
287 void createNativeThunk();
288 #if ENABLE(JIT) && ENABLE(INTERPRETER)
289 bool m_canUseJIT;
290 #endif
291 StackBounds m_stack;
292 };
293
294 inline HandleSlot allocateGlobalHandle(JSGlobalData& globalData)
295 {
296 return globalData.allocateGlobalHandle();
297 }
298
299 } // namespace JSC
300
301 #endif // JSGlobalData_h