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 "Collector.h"
33#include "DateInstanceCache.h"
34#include "ExecutableAllocator.h"
35#include "JITStubs.h"
36#include "JSValue.h"
37#include "MarkStack.h"
38#include "NumericStrings.h"
39#include "SmallStrings.h"
40#include "TimeoutChecker.h"
41#include "WeakRandom.h"
42#include <wtf/Forward.h>
43#include <wtf/HashMap.h>
44#include <wtf/RefCounted.h>
45
46struct OpaqueJSClass;
47struct OpaqueJSClassContextData;
48
49namespace JSC {
50
51    class CodeBlock;
52    class CommonIdentifiers;
53    class IdentifierTable;
54    class Interpreter;
55    class JSGlobalObject;
56    class JSObject;
57    class Lexer;
58    class Parser;
59    class Stringifier;
60    class Structure;
61    class UString;
62
63    struct HashTable;
64    struct Instruction;
65
66    struct DSTOffsetCache {
67        DSTOffsetCache()
68        {
69            reset();
70        }
71
72        void reset()
73        {
74            offset = 0.0;
75            start = 0.0;
76            end = -1.0;
77            increment = 0.0;
78        }
79
80        double offset;
81        double start;
82        double end;
83        double increment;
84    };
85
86    class JSGlobalData : public RefCounted<JSGlobalData> {
87    public:
88        struct ClientData {
89            virtual ~ClientData() = 0;
90        };
91
92        static bool sharedInstanceExists();
93        static JSGlobalData& sharedInstance();
94
95        static PassRefPtr<JSGlobalData> create();
96        static PassRefPtr<JSGlobalData> createLeaked();
97        static PassRefPtr<JSGlobalData> createNonDefault();
98        ~JSGlobalData();
99
100#if ENABLE(JSC_MULTIPLE_THREADS)
101        // Will start tracking threads that use the heap, which is resource-heavy.
102        void makeUsableFromMultipleThreads() { heap.makeUsableFromMultipleThreads(); }
103#endif
104
105        bool isSharedInstance;
106        ClientData* clientData;
107
108        const HashTable* arrayTable;
109        const HashTable* dateTable;
110        const HashTable* jsonTable;
111        const HashTable* mathTable;
112        const HashTable* numberTable;
113        const HashTable* regExpTable;
114        const HashTable* regExpConstructorTable;
115        const HashTable* stringTable;
116
117        RefPtr<Structure> activationStructure;
118        RefPtr<Structure> interruptedExecutionErrorStructure;
119        RefPtr<Structure> staticScopeStructure;
120        RefPtr<Structure> stringStructure;
121        RefPtr<Structure> notAnObjectErrorStubStructure;
122        RefPtr<Structure> notAnObjectStructure;
123        RefPtr<Structure> propertyNameIteratorStructure;
124        RefPtr<Structure> getterSetterStructure;
125        RefPtr<Structure> apiWrapperStructure;
126        RefPtr<Structure> dummyMarkableCellStructure;
127
128#if USE(JSVALUE32)
129        RefPtr<Structure> numberStructure;
130#endif
131
132        static void storeVPtrs();
133        static JS_EXPORTDATA void* jsArrayVPtr;
134        static JS_EXPORTDATA void* jsByteArrayVPtr;
135        static JS_EXPORTDATA void* jsStringVPtr;
136        static JS_EXPORTDATA void* jsFunctionVPtr;
137
138        IdentifierTable* identifierTable;
139        CommonIdentifiers* propertyNames;
140        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.
141        SmallStrings smallStrings;
142        NumericStrings numericStrings;
143        DateInstanceCache dateInstanceCache;
144
145#if ENABLE(ASSEMBLER)
146        ExecutableAllocator executableAllocator;
147#endif
148
149        Lexer* lexer;
150        Parser* parser;
151        Interpreter* interpreter;
152#if ENABLE(JIT)
153        JITThunks jitStubs;
154#endif
155        TimeoutChecker timeoutChecker;
156        Heap heap;
157
158        JSValue exception;
159#if ENABLE(JIT)
160        ReturnAddressPtr exceptionLocation;
161#endif
162
163        const Vector<Instruction>& numericCompareFunction(ExecState*);
164        Vector<Instruction> lazyNumericCompareFunction;
165        bool initializingLazyNumericCompareFunction;
166
167        HashMap<OpaqueJSClass*, OpaqueJSClassContextData*> opaqueJSClassData;
168
169        JSGlobalObject* head;
170        JSGlobalObject* dynamicGlobalObject;
171
172        HashSet<JSObject*> arrayVisitedElements;
173
174        CodeBlock* functionCodeBlockBeingReparsed;
175        Stringifier* firstStringifierToMark;
176
177        MarkStack markStack;
178
179        double cachedUTCOffset;
180        DSTOffsetCache dstOffsetCache;
181
182        UString cachedDateString;
183        double cachedDateStringValue;
184
185        WeakRandom weakRandom;
186
187#ifndef NDEBUG
188        bool mainThreadOnly;
189#endif
190
191        void resetDateCache();
192
193        void startSampling();
194        void stopSampling();
195        void dumpSampleData(ExecState* exec);
196    private:
197        JSGlobalData(bool isShared);
198        static JSGlobalData*& sharedInstanceInternal();
199        void createNativeThunk();
200    };
201
202} // namespace JSC
203
204#endif // JSGlobalData_h
205