2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "JSGlobalData.h"
33 #include "Collector.h"
34 #include "CommonIdentifiers.h"
35 #include "FunctionConstructor.h"
36 #include "Interpreter.h"
37 #include "JSActivation.h"
38 #include "JSClassRef.h"
40 #include "JSNotAnObject.h"
41 #include "JSStaticScopeObject.h"
47 #if ENABLE(JSC_MULTIPLE_THREADS)
48 #include <wtf/Threading.h>
52 #include "ProfilerServer.h"
59 extern const HashTable arrayTable
;
60 extern const HashTable dateTable
;
61 extern const HashTable mathTable
;
62 extern const HashTable numberTable
;
63 extern const HashTable regExpTable
;
64 extern const HashTable regExpConstructorTable
;
65 extern const HashTable stringTable
;
67 JSGlobalData::JSGlobalData(bool isShared
)
68 : initializingLazyNumericCompareFunction(false)
69 , interpreter(new Interpreter
)
70 , exception(noValue())
71 , arrayTable(new HashTable(JSC::arrayTable
))
72 , dateTable(new HashTable(JSC::dateTable
))
73 , mathTable(new HashTable(JSC::mathTable
))
74 , numberTable(new HashTable(JSC::numberTable
))
75 , regExpTable(new HashTable(JSC::regExpTable
))
76 , regExpConstructorTable(new HashTable(JSC::regExpConstructorTable
))
77 , stringTable(new HashTable(JSC::stringTable
))
78 , activationStructure(JSActivation::createStructure(jsNull()))
79 , interruptedExecutionErrorStructure(JSObject::createStructure(jsNull()))
80 , staticScopeStructure(JSStaticScopeObject::createStructure(jsNull()))
81 , stringStructure(JSString::createStructure(jsNull()))
82 , notAnObjectErrorStubStructure(JSNotAnObjectErrorStub::createStructure(jsNull()))
83 , notAnObjectStructure(JSNotAnObject::createStructure(jsNull()))
84 #if !USE(ALTERNATE_JSIMMEDIATE)
85 , numberStructure(JSNumberCell::createStructure(jsNull()))
87 , identifierTable(createIdentifierTable())
88 , propertyNames(new CommonIdentifiers(this))
89 , emptyList(new ArgList
)
91 , parserObjectExtraRefCounts(0)
92 , lexer(new Lexer(this))
95 , dynamicGlobalObject(0)
96 , isSharedInstance(isShared
)
98 , scopeNodeBeingReparsed(0)
102 startProfilerServerIfNeeded();
104 interpreter
->initialize(this);
107 JSGlobalData::~JSGlobalData()
109 // By the time this is destroyed, heap.destroy() must already have been called.
113 // Zeroing out to make the behavior more predictable when someone attempts to use a deleted instance.
117 arrayTable
->deleteTable();
118 dateTable
->deleteTable();
119 mathTable
->deleteTable();
120 numberTable
->deleteTable();
121 regExpTable
->deleteTable();
122 regExpConstructorTable
->deleteTable();
123 stringTable
->deleteTable();
129 delete regExpConstructorTable
;
135 deleteAllValues(opaqueJSClassData
);
139 delete propertyNames
;
140 deleteIdentifierTable(identifierTable
);
142 delete newParserObjects
;
143 delete parserObjectExtraRefCounts
;
148 PassRefPtr
<JSGlobalData
> JSGlobalData::create()
150 return adoptRef(new JSGlobalData
);
153 PassRefPtr
<JSGlobalData
> JSGlobalData::createLeaked()
156 Structure::startIgnoringLeaks();
157 RefPtr
<JSGlobalData
> data
= create();
158 Structure::stopIgnoringLeaks();
159 return data
.release();
165 bool JSGlobalData::sharedInstanceExists()
167 return sharedInstanceInternal();
170 JSGlobalData
& JSGlobalData::sharedInstance()
172 JSGlobalData
*& instance
= sharedInstanceInternal();
174 instance
= new JSGlobalData(true);
175 #if ENABLE(JSC_MULTIPLE_THREADS)
176 instance
->makeUsableFromMultipleThreads();
182 JSGlobalData
*& JSGlobalData::sharedInstanceInternal()
184 ASSERT(JSLock::currentThreadIsHoldingLock());
185 static JSGlobalData
* sharedInstance
;
186 return sharedInstance
;
189 // FIXME: We can also detect forms like v1 < v2 ? -1 : 0, reverse comparison, etc.
190 const Vector
<Instruction
>& JSGlobalData::numericCompareFunction(ExecState
* exec
)
192 if (!lazyNumericCompareFunction
.size() && !initializingLazyNumericCompareFunction
) {
193 initializingLazyNumericCompareFunction
= true;
194 RefPtr
<ProgramNode
> programNode
= parser
->parse
<ProgramNode
>(exec
, 0, makeSource(UString("(function (v1, v2) { return v1 - v2; })")), 0, 0);
195 RefPtr
<FunctionBodyNode
> functionBody
= extractFunctionBody(programNode
.get());
196 lazyNumericCompareFunction
= functionBody
->bytecode(exec
->scopeChain()).instructions();
197 initializingLazyNumericCompareFunction
= false;
200 return lazyNumericCompareFunction
;
203 JSGlobalData::ClientData::~ClientData()