source: webkit/trunk/JavaScriptCore/kjs/JSGlobalObject.cpp@ 35200

Last change on this file since 35200 was 35200, checked in by [email protected], 17 years ago

2008-07-16 Kevin McCullough <[email protected]>

Reviewed by Sam and Geoff.

<rdar://problem/5958840> Navigating to another page while profiler is
attached results in slow JavaScript for all time.

  • The UNLIKELY keeps this from being a sunspider performance regression.
  • kjs/JSGlobalObject.cpp: (KJS::JSGlobalObject::~JSGlobalObject): Stop the profiler associated with this exec state.
  • Property svn:eol-style set to native
File size: 19.0 KB
Line 
1/*
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Cameron Zwarich ([email protected])
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "config.h"
31#include "JSGlobalObject.h"
32
33#include "ArrayConstructor.h"
34#include "ArrayPrototype.h"
35#include "BooleanConstructor.h"
36#include "BooleanPrototype.h"
37#include "CodeBlock.h"
38#include "DateConstructor.h"
39#include "DatePrototype.h"
40#include "ErrorConstructor.h"
41#include "ErrorPrototype.h"
42#include "FunctionConstructor.h"
43#include "FunctionPrototype.h"
44#include "GlobalEvalFunction.h"
45#include "JSGlobalObjectFunctions.h"
46#include "JSLock.h"
47#include "Machine.h"
48#include "MathObject.h"
49#include "NativeErrorConstructor.h"
50#include "NativeErrorPrototype.h"
51#include "NumberConstructor.h"
52#include "NumberPrototype.h"
53#include "ObjectConstructor.h"
54#include "ObjectPrototype.h"
55#include "PrototypeFunction.h"
56#include "RegExpConstructor.h"
57#include "RegExpPrototype.h"
58#include "ScopeChainMark.h"
59#include "StringConstructor.h"
60#include "StringPrototype.h"
61#include "debugger.h"
62
63namespace KJS {
64
65// Default number of ticks before a timeout check should be done.
66static const int initialTickCountThreshold = 255;
67
68// Preferred number of milliseconds between each timeout check
69static const int preferredScriptCheckTimeInterval = 1000;
70
71static inline void markIfNeeded(JSValue* v)
72{
73 if (v && !v->marked())
74 v->mark();
75}
76
77JSGlobalObject::~JSGlobalObject()
78{
79 ASSERT(JSLock::currentThreadIsHoldingLock());
80
81 if (d()->debugger)
82 d()->debugger->detach(this);
83
84 Profiler** profiler = Profiler::enabledProfilerReference();
85 if (UNLIKELY(*profiler != 0)) {
86 (*profiler)->stopProfiling(globalExec(), UString());
87 (*profiler)->didFinishAllExecution(globalExec());
88 }
89
90 d()->next->d()->prev = d()->prev;
91 d()->prev->d()->next = d()->next;
92 JSGlobalObject*& headObject = head();
93 if (headObject == this)
94 headObject = d()->next;
95 if (headObject == this)
96 headObject = 0;
97
98 HashSet<ProgramCodeBlock*>::const_iterator end = codeBlocks().end();
99 for (HashSet<ProgramCodeBlock*>::const_iterator it = codeBlocks().begin(); it != end; ++it)
100 (*it)->globalObject = 0;
101
102 RegisterFile& registerFile = globalData()->machine->registerFile();
103 if (registerFile.globalObject() == this) {
104 registerFile.setGlobalObject(0);
105 registerFile.setNumGlobals(0);
106 }
107 delete d();
108}
109
110void JSGlobalObject::init(JSObject* thisValue)
111{
112 ASSERT(JSLock::currentThreadIsHoldingLock());
113
114 d()->globalData = Heap::heap(this)->globalData();
115
116 if (JSGlobalObject*& headObject = head()) {
117 d()->prev = headObject;
118 d()->next = headObject->d()->next;
119 headObject->d()->next->d()->prev = this;
120 headObject->d()->next = this;
121 } else
122 headObject = d()->next = d()->prev = this;
123
124 d()->recursion = 0;
125 d()->debugger = 0;
126 globalData()->machine->initTimeout();
127
128 d()->globalExec.set(new ExecState(this, thisValue, d()->globalScopeChain.node()));
129
130 d()->profileGroup = 0;
131
132 reset(prototype());
133}
134
135void JSGlobalObject::put(ExecState* exec, const Identifier& propertyName, JSValue* value)
136{
137 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
138
139 if (symbolTablePut(propertyName, value))
140 return;
141 return JSVariableObject::put(exec, propertyName, value);
142}
143
144void JSGlobalObject::putWithAttributes(ExecState* exec, const Identifier& propertyName, JSValue* value, unsigned attributes)
145{
146 if (symbolTablePutWithAttributes(propertyName, value, attributes))
147 return;
148
149 JSValue* valueBefore = getDirect(propertyName);
150 JSVariableObject::put(exec, propertyName, value);
151 if (!valueBefore) {
152 if (JSValue* valueAfter = getDirect(propertyName))
153 putDirect(propertyName, valueAfter, attributes);
154 }
155}
156
157void JSGlobalObject::defineGetter(ExecState* exec, const Identifier& propertyName, JSObject* getterFunc)
158{
159 PropertySlot slot;
160 if (!symbolTableGet(propertyName, slot))
161 JSVariableObject::defineGetter(exec, propertyName, getterFunc);
162}
163
164void JSGlobalObject::defineSetter(ExecState* exec, const Identifier& propertyName, JSObject* setterFunc)
165{
166 PropertySlot slot;
167 if (!symbolTableGet(propertyName, slot))
168 JSVariableObject::defineSetter(exec, propertyName, setterFunc);
169}
170
171static inline JSObject* lastInPrototypeChain(JSObject* object)
172{
173 JSObject* o = object;
174 while (o->prototype()->isObject())
175 o = static_cast<JSObject*>(o->prototype());
176 return o;
177}
178
179void JSGlobalObject::reset(JSValue* prototype)
180{
181 // Clear before inititalizing, to avoid calling mark() on stale pointers --
182 // which would be wasteful -- or uninitialized pointers -- which would be
183 // dangerous. (The allocations below may cause a GC.)
184
185 m_propertyMap.clear();
186 symbolTable().clear();
187 setRegisterArray(0, 0);
188
189 // Prototypes
190 d()->functionPrototype = 0;
191 d()->objectPrototype = 0;
192
193 d()->arrayPrototype = 0;
194 d()->stringPrototype = 0;
195 d()->booleanPrototype = 0;
196 d()->numberPrototype = 0;
197 d()->datePrototype = 0;
198 d()->regExpPrototype = 0;
199 d()->errorPrototype = 0;
200
201 d()->evalErrorPrototype = 0;
202 d()->rangeErrorPrototype = 0;
203 d()->referenceErrorPrototype = 0;
204 d()->syntaxErrorPrototype = 0;
205 d()->typeErrorPrototype = 0;
206 d()->URIErrorPrototype = 0;
207
208 // Constructors
209 d()->regExpConstructor = 0;
210 d()->errorConstructor = 0;
211
212 d()->evalErrorConstructor = 0;
213 d()->rangeErrorConstructor = 0;
214 d()->referenceErrorConstructor = 0;
215 d()->syntaxErrorConstructor = 0;
216 d()->typeErrorConstructor = 0;
217 d()->URIErrorConstructor = 0;
218
219 d()->evalFunction = 0;
220
221 ExecState* exec = d()->globalExec.get();
222
223 // Prototypes
224
225 d()->functionPrototype = new (exec) FunctionPrototype(exec);
226 d()->objectPrototype = new (exec) ObjectPrototype(exec, d()->functionPrototype);
227 d()->functionPrototype->setPrototype(d()->objectPrototype);
228
229 d()->arrayPrototype = new (exec) ArrayPrototype(exec, d()->objectPrototype);
230 d()->stringPrototype = new (exec) StringPrototype(exec, d()->objectPrototype);
231 d()->booleanPrototype = new (exec) BooleanPrototype(exec, d()->objectPrototype, d()->functionPrototype);
232 d()->numberPrototype = new (exec) NumberPrototype(exec, d()->objectPrototype, d()->functionPrototype);
233 d()->datePrototype = new (exec) DatePrototype(exec, d()->objectPrototype);
234 d()->regExpPrototype = new (exec) RegExpPrototype(exec, d()->objectPrototype, d()->functionPrototype);
235 d()->errorPrototype = new (exec) ErrorPrototype(exec, d()->objectPrototype, d()->functionPrototype);
236
237 d()->evalErrorPrototype = new (exec) NativeErrorPrototype(exec, d()->errorPrototype, "EvalError", "EvalError");
238 d()->rangeErrorPrototype = new (exec) NativeErrorPrototype(exec, d()->errorPrototype, "RangeError", "RangeError");
239 d()->referenceErrorPrototype = new (exec) NativeErrorPrototype(exec, d()->errorPrototype, "ReferenceError", "ReferenceError");
240 d()->syntaxErrorPrototype = new (exec) NativeErrorPrototype(exec, d()->errorPrototype, "SyntaxError", "SyntaxError");
241 d()->typeErrorPrototype = new (exec) NativeErrorPrototype(exec, d()->errorPrototype, "TypeError", "TypeError");
242 d()->URIErrorPrototype = new (exec) NativeErrorPrototype(exec, d()->errorPrototype, "URIError", "URIError");
243
244 // Constructors
245
246 JSValue* objectConstructor = new (exec) ObjectConstructor(exec, d()->objectPrototype, d()->functionPrototype);
247 JSValue* functionConstructor = new (exec) FunctionConstructor(exec, d()->functionPrototype);
248 JSValue* arrayConstructor = new (exec) ArrayConstructor(exec, d()->functionPrototype, d()->arrayPrototype);
249 JSValue* stringConstructor = new (exec) StringConstructor(exec, d()->functionPrototype, d()->stringPrototype);
250 JSValue* booleanConstructor = new (exec) BooleanConstructor(exec, d()->functionPrototype, d()->booleanPrototype);
251 JSValue* numberConstructor = new (exec) NumberConstructor(exec, d()->functionPrototype, d()->numberPrototype);
252 JSValue* dateConstructor = new (exec) DateConstructor(exec, d()->functionPrototype, d()->datePrototype);
253
254 d()->regExpConstructor = new (exec) RegExpConstructor(exec, d()->functionPrototype, d()->regExpPrototype);
255
256 d()->errorConstructor = new (exec) ErrorConstructor(exec, d()->functionPrototype, d()->errorPrototype);
257
258 d()->evalErrorConstructor = new (exec) NativeErrorConstructor(exec, d()->functionPrototype, d()->evalErrorPrototype);
259 d()->rangeErrorConstructor = new (exec) NativeErrorConstructor(exec, d()->functionPrototype, d()->rangeErrorPrototype);
260 d()->referenceErrorConstructor = new (exec) NativeErrorConstructor(exec, d()->functionPrototype, d()->referenceErrorPrototype);
261 d()->syntaxErrorConstructor = new (exec) NativeErrorConstructor(exec, d()->functionPrototype, d()->syntaxErrorPrototype);
262 d()->typeErrorConstructor = new (exec) NativeErrorConstructor(exec, d()->functionPrototype, d()->typeErrorPrototype);
263 d()->URIErrorConstructor = new (exec) NativeErrorConstructor(exec, d()->functionPrototype, d()->URIErrorPrototype);
264
265 d()->functionPrototype->putDirect(exec->propertyNames().constructor, functionConstructor, DontEnum);
266
267 d()->objectPrototype->putDirect(exec->propertyNames().constructor, objectConstructor, DontEnum);
268 d()->functionPrototype->putDirect(exec->propertyNames().constructor, functionConstructor, DontEnum);
269 d()->arrayPrototype->putDirect(exec->propertyNames().constructor, arrayConstructor, DontEnum);
270 d()->booleanPrototype->putDirect(exec->propertyNames().constructor, booleanConstructor, DontEnum);
271 d()->stringPrototype->putDirect(exec->propertyNames().constructor, stringConstructor, DontEnum);
272 d()->numberPrototype->putDirect(exec->propertyNames().constructor, numberConstructor, DontEnum);
273 d()->datePrototype->putDirect(exec->propertyNames().constructor, dateConstructor, DontEnum);
274 d()->regExpPrototype->putDirect(exec->propertyNames().constructor, d()->regExpConstructor, DontEnum);
275 d()->errorPrototype->putDirect(exec->propertyNames().constructor, d()->errorConstructor, DontEnum);
276 d()->evalErrorPrototype->putDirect(exec->propertyNames().constructor, d()->evalErrorConstructor, DontEnum);
277 d()->rangeErrorPrototype->putDirect(exec->propertyNames().constructor, d()->rangeErrorConstructor, DontEnum);
278 d()->referenceErrorPrototype->putDirect(exec->propertyNames().constructor, d()->referenceErrorConstructor, DontEnum);
279 d()->syntaxErrorPrototype->putDirect(exec->propertyNames().constructor, d()->syntaxErrorConstructor, DontEnum);
280 d()->typeErrorPrototype->putDirect(exec->propertyNames().constructor, d()->typeErrorConstructor, DontEnum);
281 d()->URIErrorPrototype->putDirect(exec->propertyNames().constructor, d()->URIErrorConstructor, DontEnum);
282
283 // Set global constructors
284
285 // FIXME: These properties could be handled by a static hash table.
286
287 putDirect(Identifier(exec, "Object"), objectConstructor, DontEnum);
288 putDirect(Identifier(exec, "Function"), functionConstructor, DontEnum);
289 putDirect(Identifier(exec, "Array"), arrayConstructor, DontEnum);
290 putDirect(Identifier(exec, "Boolean"), booleanConstructor, DontEnum);
291 putDirect(Identifier(exec, "String"), stringConstructor, DontEnum);
292 putDirect(Identifier(exec, "Number"), numberConstructor, DontEnum);
293 putDirect(Identifier(exec, "Date"), dateConstructor, DontEnum);
294 putDirect(Identifier(exec, "RegExp"), d()->regExpConstructor, DontEnum);
295 putDirect(Identifier(exec, "Error"), d()->errorConstructor, DontEnum);
296 putDirect(Identifier(exec, "EvalError"), d()->evalErrorConstructor);
297 putDirect(Identifier(exec, "RangeError"), d()->rangeErrorConstructor);
298 putDirect(Identifier(exec, "ReferenceError"), d()->referenceErrorConstructor);
299 putDirect(Identifier(exec, "SyntaxError"), d()->syntaxErrorConstructor);
300 putDirect(Identifier(exec, "TypeError"), d()->typeErrorConstructor);
301 putDirect(Identifier(exec, "URIError"), d()->URIErrorConstructor);
302
303 // Set global values.
304 GlobalPropertyInfo staticGlobals[] = {
305 GlobalPropertyInfo(Identifier(exec, "Math"), new (exec) MathObject(exec, d()->objectPrototype), DontEnum | DontDelete),
306 GlobalPropertyInfo(Identifier(exec, "NaN"), jsNaN(exec), DontEnum | DontDelete),
307 GlobalPropertyInfo(Identifier(exec, "Infinity"), jsNumber(exec, Inf), DontEnum | DontDelete),
308 GlobalPropertyInfo(Identifier(exec, "undefined"), jsUndefined(), DontEnum | DontDelete)
309 };
310
311 addStaticGlobals(staticGlobals, sizeof(staticGlobals) / sizeof(GlobalPropertyInfo));
312
313 // Set global functions.
314
315 d()->evalFunction = new (exec) GlobalEvalFunction(exec, d()->functionPrototype, 1, exec->propertyNames().eval, globalFuncEval, this);
316 putDirectFunction(d()->evalFunction, DontEnum);
317 putDirectFunction(new (exec) PrototypeFunction(exec, d()->functionPrototype, 2, Identifier(exec, "parseInt"), globalFuncParseInt), DontEnum);
318 putDirectFunction(new (exec) PrototypeFunction(exec, d()->functionPrototype, 1, Identifier(exec, "parseFloat"), globalFuncParseFloat), DontEnum);
319 putDirectFunction(new (exec) PrototypeFunction(exec, d()->functionPrototype, 1, Identifier(exec, "isNaN"), globalFuncIsNaN), DontEnum);
320 putDirectFunction(new (exec) PrototypeFunction(exec, d()->functionPrototype, 1, Identifier(exec, "isFinite"), globalFuncIsFinite), DontEnum);
321 putDirectFunction(new (exec) PrototypeFunction(exec, d()->functionPrototype, 1, Identifier(exec, "escape"), globalFuncEscape), DontEnum);
322 putDirectFunction(new (exec) PrototypeFunction(exec, d()->functionPrototype, 1, Identifier(exec, "unescape"), globalFuncUnescape), DontEnum);
323 putDirectFunction(new (exec) PrototypeFunction(exec, d()->functionPrototype, 1, Identifier(exec, "decodeURI"), globalFuncDecodeURI), DontEnum);
324 putDirectFunction(new (exec) PrototypeFunction(exec, d()->functionPrototype, 1, Identifier(exec, "decodeURIComponent"), globalFuncDecodeURIComponent), DontEnum);
325 putDirectFunction(new (exec) PrototypeFunction(exec, d()->functionPrototype, 1, Identifier(exec, "encodeURI"), globalFuncEncodeURI), DontEnum);
326 putDirectFunction(new (exec) PrototypeFunction(exec, d()->functionPrototype, 1, Identifier(exec, "encodeURIComponent"), globalFuncEncodeURIComponent), DontEnum);
327#ifndef NDEBUG
328 putDirectFunction(new (exec) PrototypeFunction(exec, d()->functionPrototype, 1, Identifier(exec, "kjsprint"), globalFuncKJSPrint), DontEnum);
329#endif
330
331 // Set prototype, and also insert the object prototype at the end of the chain.
332
333 setPrototype(prototype);
334 lastInPrototypeChain(this)->setPrototype(d()->objectPrototype);
335}
336
337void JSGlobalObject::setTimeoutTime(unsigned timeoutTime)
338{
339 globalData()->machine->setTimeoutTime(timeoutTime);
340}
341
342void JSGlobalObject::startTimeoutCheck()
343{
344 globalData()->machine->startTimeoutCheck();
345}
346
347void JSGlobalObject::stopTimeoutCheck()
348{
349 globalData()->machine->stopTimeoutCheck();
350}
351
352void JSGlobalObject::mark()
353{
354 JSVariableObject::mark();
355
356 HashSet<ProgramCodeBlock*>::const_iterator end = codeBlocks().end();
357 for (HashSet<ProgramCodeBlock*>::const_iterator it = codeBlocks().begin(); it != end; ++it)
358 (*it)->mark();
359
360 RegisterFile& registerFile = globalData()->machine->registerFile();
361 if (registerFile.globalObject() == this)
362 registerFile.markGlobals(globalData()->heap);
363
364 markIfNeeded(d()->globalExec->exception());
365
366 markIfNeeded(d()->regExpConstructor);
367 markIfNeeded(d()->errorConstructor);
368 markIfNeeded(d()->evalErrorConstructor);
369 markIfNeeded(d()->rangeErrorConstructor);
370 markIfNeeded(d()->referenceErrorConstructor);
371 markIfNeeded(d()->syntaxErrorConstructor);
372 markIfNeeded(d()->typeErrorConstructor);
373 markIfNeeded(d()->URIErrorConstructor);
374
375 markIfNeeded(d()->evalFunction);
376
377 markIfNeeded(d()->objectPrototype);
378 markIfNeeded(d()->functionPrototype);
379 markIfNeeded(d()->arrayPrototype);
380 markIfNeeded(d()->booleanPrototype);
381 markIfNeeded(d()->stringPrototype);
382 markIfNeeded(d()->numberPrototype);
383 markIfNeeded(d()->datePrototype);
384 markIfNeeded(d()->regExpPrototype);
385 markIfNeeded(d()->errorPrototype);
386 markIfNeeded(d()->evalErrorPrototype);
387 markIfNeeded(d()->rangeErrorPrototype);
388 markIfNeeded(d()->referenceErrorPrototype);
389 markIfNeeded(d()->syntaxErrorPrototype);
390 markIfNeeded(d()->typeErrorPrototype);
391 markIfNeeded(d()->URIErrorPrototype);
392}
393
394JSGlobalObject* JSGlobalObject::toGlobalObject(ExecState*) const
395{
396 return const_cast<JSGlobalObject*>(this);
397}
398
399ExecState* JSGlobalObject::globalExec()
400{
401 return d()->globalExec.get();
402}
403
404bool JSGlobalObject::isDynamicScope() const
405{
406 return true;
407}
408
409void JSGlobalObject::copyGlobalsFrom(RegisterFile& registerFile)
410{
411 ASSERT(!d()->registerArray);
412 ASSERT(!d()->registerArraySize);
413
414 int numGlobals = registerFile.numGlobals();
415 if (!numGlobals) {
416 d()->registers = 0;
417 return;
418 }
419 copyRegisterArray(registerFile.lastGlobal(), numGlobals);
420}
421
422void JSGlobalObject::copyGlobalsTo(RegisterFile& registerFile)
423{
424 JSGlobalObject* lastGlobalObject = registerFile.globalObject();
425 if (lastGlobalObject && lastGlobalObject != this)
426 lastGlobalObject->copyGlobalsFrom(registerFile);
427
428 registerFile.setGlobalObject(this);
429 registerFile.setNumGlobals(symbolTable().size());
430
431 if (d()->registerArray) {
432 memcpy(registerFile.base() - d()->registerArraySize, d()->registerArray.get(), d()->registerArraySize * sizeof(Register));
433 setRegisterArray(0, 0);
434 }
435
436 d()->registers = registerFile.base();
437}
438
439void* JSGlobalObject::operator new(size_t size, JSGlobalData* globalData)
440{
441#ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
442 return globalData->heap->inlineAllocate(size);
443#else
444 return globalData->heap->allocate(size);
445#endif
446}
447
448} // namespace KJS
Note: See TracBrowser for help on using the repository browser.