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 |
|
---|
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 |
|
---|
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 | , functionCodeBlockBeingReparsed(0)
|
---|
148 | , firstStringifierToMark(0)
|
---|
149 | , markStack(vptrSet.jsArrayVPtr)
|
---|
150 | #ifndef NDEBUG
|
---|
151 | , mainThreadOnly(false)
|
---|
152 | #endif
|
---|
153 | {
|
---|
154 | #if PLATFORM(MAC)
|
---|
155 | startProfilerServerIfNeeded();
|
---|
156 | #endif
|
---|
157 | }
|
---|
158 |
|
---|
159 | JSGlobalData::~JSGlobalData()
|
---|
160 | {
|
---|
161 | // By the time this is destroyed, heap.destroy() must already have been called.
|
---|
162 |
|
---|
163 | delete interpreter;
|
---|
164 | #ifndef NDEBUG
|
---|
165 | // Zeroing out to make the behavior more predictable when someone attempts to use a deleted instance.
|
---|
166 | interpreter = 0;
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | arrayTable->deleteTable();
|
---|
170 | dateTable->deleteTable();
|
---|
171 | jsonTable->deleteTable();
|
---|
172 | mathTable->deleteTable();
|
---|
173 | numberTable->deleteTable();
|
---|
174 | regExpTable->deleteTable();
|
---|
175 | regExpConstructorTable->deleteTable();
|
---|
176 | stringTable->deleteTable();
|
---|
177 |
|
---|
178 | fastDelete(const_cast<HashTable*>(arrayTable));
|
---|
179 | fastDelete(const_cast<HashTable*>(dateTable));
|
---|
180 | fastDelete(const_cast<HashTable*>(jsonTable));
|
---|
181 | fastDelete(const_cast<HashTable*>(mathTable));
|
---|
182 | fastDelete(const_cast<HashTable*>(numberTable));
|
---|
183 | fastDelete(const_cast<HashTable*>(regExpTable));
|
---|
184 | fastDelete(const_cast<HashTable*>(regExpConstructorTable));
|
---|
185 | fastDelete(const_cast<HashTable*>(stringTable));
|
---|
186 |
|
---|
187 | delete parser;
|
---|
188 | delete lexer;
|
---|
189 |
|
---|
190 | deleteAllValues(opaqueJSClassData);
|
---|
191 |
|
---|
192 | delete emptyList;
|
---|
193 |
|
---|
194 | delete propertyNames;
|
---|
195 | deleteIdentifierTable(identifierTable);
|
---|
196 |
|
---|
197 | delete clientData;
|
---|
198 | }
|
---|
199 |
|
---|
200 | PassRefPtr<JSGlobalData> JSGlobalData::create(bool isShared)
|
---|
201 | {
|
---|
202 | return adoptRef(new JSGlobalData(isShared, VPtrSet()));
|
---|
203 | }
|
---|
204 |
|
---|
205 | PassRefPtr<JSGlobalData> JSGlobalData::createLeaked()
|
---|
206 | {
|
---|
207 | Structure::startIgnoringLeaks();
|
---|
208 | RefPtr<JSGlobalData> data = create();
|
---|
209 | Structure::stopIgnoringLeaks();
|
---|
210 | return data.release();
|
---|
211 | }
|
---|
212 |
|
---|
213 | bool JSGlobalData::sharedInstanceExists()
|
---|
214 | {
|
---|
215 | return sharedInstanceInternal();
|
---|
216 | }
|
---|
217 |
|
---|
218 | JSGlobalData& JSGlobalData::sharedInstance()
|
---|
219 | {
|
---|
220 | JSGlobalData*& instance = sharedInstanceInternal();
|
---|
221 | if (!instance) {
|
---|
222 | instance = create(true).releaseRef();
|
---|
223 | #if ENABLE(JSC_MULTIPLE_THREADS)
|
---|
224 | instance->makeUsableFromMultipleThreads();
|
---|
225 | #endif
|
---|
226 | }
|
---|
227 | return *instance;
|
---|
228 | }
|
---|
229 |
|
---|
230 | JSGlobalData*& JSGlobalData::sharedInstanceInternal()
|
---|
231 | {
|
---|
232 | ASSERT(JSLock::currentThreadIsHoldingLock());
|
---|
233 | static JSGlobalData* sharedInstance;
|
---|
234 | return sharedInstance;
|
---|
235 | }
|
---|
236 |
|
---|
237 | // FIXME: We can also detect forms like v1 < v2 ? -1 : 0, reverse comparison, etc.
|
---|
238 | const Vector<Instruction>& JSGlobalData::numericCompareFunction(ExecState* exec)
|
---|
239 | {
|
---|
240 | if (!lazyNumericCompareFunction.size() && !initializingLazyNumericCompareFunction) {
|
---|
241 | initializingLazyNumericCompareFunction = true;
|
---|
242 | RefPtr<FunctionExecutable> function = FunctionExecutable::fromGlobalCode(Identifier(exec, "numericCompare"), exec, 0, makeSource(UString("(function (v1, v2) { return v1 - v2; })")), 0, 0);
|
---|
243 | lazyNumericCompareFunction = function->bytecode(exec, exec->scopeChain()).instructions();
|
---|
244 | initializingLazyNumericCompareFunction = false;
|
---|
245 | }
|
---|
246 |
|
---|
247 | return lazyNumericCompareFunction;
|
---|
248 | }
|
---|
249 |
|
---|
250 | JSGlobalData::ClientData::~ClientData()
|
---|
251 | {
|
---|
252 | }
|
---|
253 |
|
---|
254 | void JSGlobalData::startSampling()
|
---|
255 | {
|
---|
256 | interpreter->startSampling();
|
---|
257 | }
|
---|
258 |
|
---|
259 | void JSGlobalData::stopSampling()
|
---|
260 | {
|
---|
261 | interpreter->stopSampling();
|
---|
262 | }
|
---|
263 |
|
---|
264 | void JSGlobalData::dumpSampleData(ExecState* exec)
|
---|
265 | {
|
---|
266 | interpreter->dumpSampleData(exec);
|
---|
267 | }
|
---|
268 |
|
---|
269 | } // namespace JSC
|
---|