• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 #include "config.h"
30 #include "JSGlobalData.h"
31 
32 #include "ArgList.h"
33 #include "Collector.h"
34 #include "CommonIdentifiers.h"
35 #include "FunctionConstructor.h"
36 #include "GetterSetter.h"
37 #include "Interpreter.h"
38 #include "JSActivation.h"
39 #include "JSAPIValueWrapper.h"
40 #include "JSArray.h"
41 #include "JSByteArray.h"
42 #include "JSClassRef.h"
43 #include "JSFunction.h"
44 #include "JSLock.h"
45 #include "JSNotAnObject.h"
46 #include "JSPropertyNameIterator.h"
47 #include "JSStaticScopeObject.h"
48 #include "Parser.h"
49 #include "Lexer.h"
50 #include "Lookup.h"
51 #include "Nodes.h"
52 
53 #if ENABLE(JSC_MULTIPLE_THREADS)
54 #include <wtf/Threading.h>
55 #endif
56 
57 #if PLATFORM(MAC)
58 #include "ProfilerServer.h"
59 #endif
60 
61 using namespace WTF;
62 
63 namespace JSC {
64 
65 extern JSC_CONST_HASHTABLE HashTable arrayTable;
66 extern JSC_CONST_HASHTABLE HashTable jsonTable;
67 extern JSC_CONST_HASHTABLE HashTable dateTable;
68 extern JSC_CONST_HASHTABLE HashTable mathTable;
69 extern JSC_CONST_HASHTABLE HashTable numberTable;
70 extern JSC_CONST_HASHTABLE HashTable regExpTable;
71 extern JSC_CONST_HASHTABLE HashTable regExpConstructorTable;
72 extern JSC_CONST_HASHTABLE HashTable stringTable;
73 
74 struct VPtrSet {
75     VPtrSet();
76 
77     void* jsArrayVPtr;
78     void* jsByteArrayVPtr;
79     void* jsStringVPtr;
80     void* jsFunctionVPtr;
81 };
82 
VPtrSet()83 VPtrSet::VPtrSet()
84 {
85     // Bizarrely, calling fastMalloc here is faster than allocating space on the stack.
86     void* storage = fastMalloc(sizeof(CollectorBlock));
87 
88     JSCell* jsArray = new (storage) JSArray(JSArray::createStructure(jsNull()));
89     jsArrayVPtr = jsArray->vptr();
90     jsArray->~JSCell();
91 
92     JSCell* jsByteArray = new (storage) JSByteArray(JSByteArray::VPtrStealingHack);
93     jsByteArrayVPtr = jsByteArray->vptr();
94     jsByteArray->~JSCell();
95 
96     JSCell* jsString = new (storage) JSString(JSString::VPtrStealingHack);
97     jsStringVPtr = jsString->vptr();
98     jsString->~JSCell();
99 
100     JSCell* jsFunction = new (storage) JSFunction(JSFunction::createStructure(jsNull()));
101     jsFunctionVPtr = jsFunction->vptr();
102     jsFunction->~JSCell();
103 
104     fastFree(storage);
105 }
106 
JSGlobalData(bool isShared,const VPtrSet & vptrSet)107 JSGlobalData::JSGlobalData(bool isShared, const VPtrSet& vptrSet)
108     : isSharedInstance(isShared)
109     , clientData(0)
110     , arrayTable(fastNew<HashTable>(JSC::arrayTable))
111     , dateTable(fastNew<HashTable>(JSC::dateTable))
112     , jsonTable(fastNew<HashTable>(JSC::jsonTable))
113     , mathTable(fastNew<HashTable>(JSC::mathTable))
114     , numberTable(fastNew<HashTable>(JSC::numberTable))
115     , regExpTable(fastNew<HashTable>(JSC::regExpTable))
116     , regExpConstructorTable(fastNew<HashTable>(JSC::regExpConstructorTable))
117     , stringTable(fastNew<HashTable>(JSC::stringTable))
118     , activationStructure(JSActivation::createStructure(jsNull()))
119     , interruptedExecutionErrorStructure(JSObject::createStructure(jsNull()))
120     , staticScopeStructure(JSStaticScopeObject::createStructure(jsNull()))
121     , stringStructure(JSString::createStructure(jsNull()))
122     , notAnObjectErrorStubStructure(JSNotAnObjectErrorStub::createStructure(jsNull()))
123     , notAnObjectStructure(JSNotAnObject::createStructure(jsNull()))
124     , propertyNameIteratorStructure(JSPropertyNameIterator::createStructure(jsNull()))
125     , getterSetterStructure(GetterSetter::createStructure(jsNull()))
126     , apiWrapperStructure(JSAPIValueWrapper::createStructure(jsNull()))
127 #if USE(JSVALUE32)
128     , numberStructure(JSNumberCell::createStructure(jsNull()))
129 #endif
130     , jsArrayVPtr(vptrSet.jsArrayVPtr)
131     , jsByteArrayVPtr(vptrSet.jsByteArrayVPtr)
132     , jsStringVPtr(vptrSet.jsStringVPtr)
133     , jsFunctionVPtr(vptrSet.jsFunctionVPtr)
134     , identifierTable(createIdentifierTable())
135     , propertyNames(new CommonIdentifiers(this))
136     , emptyList(new MarkedArgumentBuffer)
137     , lexer(new Lexer(this))
138     , parser(new Parser)
139     , interpreter(new Interpreter)
140 #if ENABLE(JIT)
141     , jitStubs(this)
142 #endif
143     , heap(this)
144     , initializingLazyNumericCompareFunction(false)
145     , head(0)
146     , dynamicGlobalObject(0)
147     , scopeNodeBeingReparsed(0)
148     , firstStringifierToMark(0)
149 {
150 #if PLATFORM(MAC)
151     startProfilerServerIfNeeded();
152 #endif
153 }
154 
~JSGlobalData()155 JSGlobalData::~JSGlobalData()
156 {
157     // By the time this is destroyed, heap.destroy() must already have been called.
158 
159     delete interpreter;
160 #ifndef NDEBUG
161     // Zeroing out to make the behavior more predictable when someone attempts to use a deleted instance.
162     interpreter = 0;
163 #endif
164 
165     arrayTable->deleteTable();
166     dateTable->deleteTable();
167     jsonTable->deleteTable();
168     mathTable->deleteTable();
169     numberTable->deleteTable();
170     regExpTable->deleteTable();
171     regExpConstructorTable->deleteTable();
172     stringTable->deleteTable();
173 
174     fastDelete(const_cast<HashTable*>(arrayTable));
175     fastDelete(const_cast<HashTable*>(dateTable));
176     fastDelete(const_cast<HashTable*>(jsonTable));
177     fastDelete(const_cast<HashTable*>(mathTable));
178     fastDelete(const_cast<HashTable*>(numberTable));
179     fastDelete(const_cast<HashTable*>(regExpTable));
180     fastDelete(const_cast<HashTable*>(regExpConstructorTable));
181     fastDelete(const_cast<HashTable*>(stringTable));
182 
183     delete parser;
184     delete lexer;
185 
186     deleteAllValues(opaqueJSClassData);
187 
188     delete emptyList;
189 
190     delete propertyNames;
191     deleteIdentifierTable(identifierTable);
192 
193     delete clientData;
194 }
195 
create(bool isShared)196 PassRefPtr<JSGlobalData> JSGlobalData::create(bool isShared)
197 {
198     return adoptRef(new JSGlobalData(isShared, VPtrSet()));
199 }
200 
createLeaked()201 PassRefPtr<JSGlobalData> JSGlobalData::createLeaked()
202 {
203     Structure::startIgnoringLeaks();
204     RefPtr<JSGlobalData> data = create();
205     Structure::stopIgnoringLeaks();
206     return data.release();
207 }
208 
sharedInstanceExists()209 bool JSGlobalData::sharedInstanceExists()
210 {
211     return sharedInstanceInternal();
212 }
213 
sharedInstance()214 JSGlobalData& JSGlobalData::sharedInstance()
215 {
216     JSGlobalData*& instance = sharedInstanceInternal();
217     if (!instance) {
218         instance = create(true).releaseRef();
219 #if ENABLE(JSC_MULTIPLE_THREADS)
220         instance->makeUsableFromMultipleThreads();
221 #endif
222     }
223     return *instance;
224 }
225 
sharedInstanceInternal()226 JSGlobalData*& JSGlobalData::sharedInstanceInternal()
227 {
228     ASSERT(JSLock::currentThreadIsHoldingLock());
229     static JSGlobalData* sharedInstance;
230     return sharedInstance;
231 }
232 
233 // FIXME: We can also detect forms like v1 < v2 ? -1 : 0, reverse comparison, etc.
numericCompareFunction(ExecState * exec)234 const Vector<Instruction>& JSGlobalData::numericCompareFunction(ExecState* exec)
235 {
236     if (!lazyNumericCompareFunction.size() && !initializingLazyNumericCompareFunction) {
237         initializingLazyNumericCompareFunction = true;
238         RefPtr<ProgramNode> programNode = parser->parse<ProgramNode>(exec, 0, makeSource(UString("(function (v1, v2) { return v1 - v2; })")), 0, 0);
239         RefPtr<FunctionBodyNode> functionBody = extractFunctionBody(programNode.get());
240         lazyNumericCompareFunction = functionBody->bytecode(exec->scopeChain()).instructions();
241         initializingLazyNumericCompareFunction = false;
242     }
243 
244     return lazyNumericCompareFunction;
245 }
246 
~ClientData()247 JSGlobalData::ClientData::~ClientData()
248 {
249 }
250 
251 } // namespace JSC
252