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 "GetterSetter.h"
37
#include "Interpreter.h"
38
#include "JSActivation.h"
39
#include "JSAPIValueWrapper.h"
41
#include "JSByteArray.h"
42
#include "JSClassRef.h"
43
#include "JSFunction.h"
45
#include "JSNotAnObject.h"
46
#include "JSPropertyNameIterator.h"
47
#include "JSStaticScopeObject.h"
53
#if ENABLE(JSC_MULTIPLE_THREADS)
54
#include <wtf/Threading.h>
58
#include "ProfilerServer.h"
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;
78
void* jsByteArrayVPtr;
85
// Bizarrely, calling fastMalloc here is faster than allocating space on the stack.
86
void* storage = fastMalloc(sizeof(CollectorBlock));
88
JSCell* jsArray = new (storage) JSArray(JSArray::createStructure(jsNull()));
89
jsArrayVPtr = jsArray->vptr();
92
JSCell* jsByteArray = new (storage) JSByteArray(JSByteArray::VPtrStealingHack);
93
jsByteArrayVPtr = jsByteArray->vptr();
94
jsByteArray->~JSCell();
96
JSCell* jsString = new (storage) JSString(JSString::VPtrStealingHack);
97
jsStringVPtr = jsString->vptr();
100
JSCell* jsFunction = new (storage) JSFunction(JSFunction::createStructure(jsNull()));
101
jsFunctionVPtr = jsFunction->vptr();
102
jsFunction->~JSCell();
107
JSGlobalData::JSGlobalData(bool isShared, const VPtrSet& vptrSet)
108
: isSharedInstance(isShared)
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()))
128
, numberStructure(JSNumberCell::createStructure(jsNull()))
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))
139
, interpreter(new Interpreter)
143
, timeoutChecker(new TimeoutChecker)
145
, initializingLazyNumericCompareFunction(false)
147
, dynamicGlobalObject(0)
148
, functionCodeBlockBeingReparsed(0)
149
, firstStringifierToMark(0)
150
, markStack(vptrSet.jsArrayVPtr)
152
, mainThreadOnly(false)
156
startProfilerServerIfNeeded();
160
JSGlobalData::~JSGlobalData()
162
// By the time this is destroyed, heap.destroy() must already have been called.
166
// Zeroing out to make the behavior more predictable when someone attempts to use a deleted instance.
170
arrayTable->deleteTable();
171
dateTable->deleteTable();
172
jsonTable->deleteTable();
173
mathTable->deleteTable();
174
numberTable->deleteTable();
175
regExpTable->deleteTable();
176
regExpConstructorTable->deleteTable();
177
stringTable->deleteTable();
179
fastDelete(const_cast<HashTable*>(arrayTable));
180
fastDelete(const_cast<HashTable*>(dateTable));
181
fastDelete(const_cast<HashTable*>(jsonTable));
182
fastDelete(const_cast<HashTable*>(mathTable));
183
fastDelete(const_cast<HashTable*>(numberTable));
184
fastDelete(const_cast<HashTable*>(regExpTable));
185
fastDelete(const_cast<HashTable*>(regExpConstructorTable));
186
fastDelete(const_cast<HashTable*>(stringTable));
190
delete timeoutChecker;
192
deleteAllValues(opaqueJSClassData);
196
delete propertyNames;
197
deleteIdentifierTable(identifierTable);
202
PassRefPtr<JSGlobalData> JSGlobalData::create(bool isShared)
204
return adoptRef(new JSGlobalData(isShared, VPtrSet()));
207
PassRefPtr<JSGlobalData> JSGlobalData::createLeaked()
209
Structure::startIgnoringLeaks();
210
RefPtr<JSGlobalData> data = create();
211
Structure::stopIgnoringLeaks();
212
return data.release();
215
bool JSGlobalData::sharedInstanceExists()
217
return sharedInstanceInternal();
220
JSGlobalData& JSGlobalData::sharedInstance()
222
JSGlobalData*& instance = sharedInstanceInternal();
224
instance = create(true).releaseRef();
225
#if ENABLE(JSC_MULTIPLE_THREADS)
226
instance->makeUsableFromMultipleThreads();
232
JSGlobalData*& JSGlobalData::sharedInstanceInternal()
234
ASSERT(JSLock::currentThreadIsHoldingLock());
235
static JSGlobalData* sharedInstance;
236
return sharedInstance;
239
// FIXME: We can also detect forms like v1 < v2 ? -1 : 0, reverse comparison, etc.
240
const Vector<Instruction>& JSGlobalData::numericCompareFunction(ExecState* exec)
242
if (!lazyNumericCompareFunction.size() && !initializingLazyNumericCompareFunction) {
243
initializingLazyNumericCompareFunction = true;
244
RefPtr<FunctionExecutable> function = FunctionExecutable::fromGlobalCode(Identifier(exec, "numericCompare"), exec, 0, makeSource(UString("(function (v1, v2) { return v1 - v2; })")), 0, 0);
245
lazyNumericCompareFunction = function->bytecode(exec, exec->scopeChain()).instructions();
246
initializingLazyNumericCompareFunction = false;
249
return lazyNumericCompareFunction;
252
JSGlobalData::ClientData::~ClientData()