2008-03-19 Mark Rowe <mrowe@apple.com>
[WebKit-https.git] / JavaScriptCore / kjs / JSGlobalObject.cpp
1 /*
2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Cameron Zwarich (cwzwarich@uwaterloo.ca)
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 "Activation.h"
34 #include "SavedBuiltins.h"
35 #include "array_object.h"
36 #include "bool_object.h"
37 #include "date_object.h"
38 #include "debugger.h"
39 #include "error_object.h"
40 #include "function_object.h"
41 #include "math_object.h"
42 #include "number_object.h"
43 #include "object_object.h"
44 #include "regexp_object.h"
45 #include "scope_chain_mark.h"
46 #include "string_object.h"
47
48 #if HAVE(SYS_TIME_H)
49 #include <sys/time.h>
50 #endif
51
52 #if PLATFORM(WIN_OS)
53 #include <windows.h>
54 #endif
55
56 #if PLATFORM(QT)
57 #include <QDateTime>
58 #endif
59
60 namespace KJS {
61
62 // Default number of ticks before a timeout check should be done.
63 static const int initialTickCountThreshold = 255;
64
65 // Preferred number of milliseconds between each timeout check
66 static const int preferredScriptCheckTimeInterval = 1000;
67
68 static inline void markIfNeeded(JSValue* v)
69 {
70     if (v && !v->marked())
71         v->mark();
72 }
73     
74 // Returns the current time in milliseconds
75 // It doesn't matter what "current time" is here, just as long as
76 // it's possible to measure the time difference correctly.
77 static inline unsigned getCurrentTime()
78 {
79 #if HAVE(SYS_TIME_H)
80     struct timeval tv;
81     gettimeofday(&tv, 0);
82     return tv.tv_sec * 1000 + tv.tv_usec / 1000;
83 #elif PLATFORM(QT)
84     QDateTime t = QDateTime::currentDateTime();
85     return t.toTime_t() * 1000 + t.time().msec();
86 #elif PLATFORM(WIN_OS)
87     return timeGetTime();
88 #else
89 #error Platform does not have getCurrentTime function
90 #endif
91 }
92
93 JSGlobalObject* JSGlobalObject::s_head = 0;
94
95 void JSGlobalObject::deleteActivationStack()
96 {
97     ActivationStackNode* prevNode = 0;
98     for (ActivationStackNode* currentNode = d()->activations; currentNode; currentNode = prevNode) {
99         prevNode = currentNode->prev;
100         delete currentNode;
101     }
102 }
103
104 JSGlobalObject::~JSGlobalObject()
105 {
106     ASSERT(JSLock::currentThreadIsHoldingLock());
107
108     if (d()->debugger)
109         d()->debugger->detach(this);
110
111     d()->next->d()->prev = d()->prev;
112     d()->prev->d()->next = d()->next;
113     s_head = d()->next;
114     if (s_head == this)
115         s_head = 0;
116     
117     deleteActivationStack();
118     
119     delete d();
120 }
121
122 void JSGlobalObject::init()
123 {
124     ASSERT(JSLock::currentThreadIsHoldingLock());
125
126     if (s_head) {
127         d()->prev = s_head;
128         d()->next = s_head->d()->next;
129         s_head->d()->next->d()->prev = this;
130         s_head->d()->next = this;
131     } else
132         s_head = d()->next = d()->prev = this;
133
134     resetTimeoutCheck();
135     d()->timeoutTime = 0;
136     d()->timeoutCheckCount = 0;
137
138     d()->recursion = 0;
139     d()->debugger = 0;
140     
141     ActivationStackNode* newStackNode = new ActivationStackNode;
142     newStackNode->prev = 0;    
143     d()->activations = newStackNode;
144     d()->activationCount = 0;
145
146     reset(prototype());
147 }
148
149 bool JSGlobalObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
150 {
151     if (symbolTableGet(propertyName, slot))
152         return true;
153     return JSVariableObject::getOwnPropertySlot(exec, propertyName, slot);
154 }
155
156 void JSGlobalObject::put(ExecState* exec, const Identifier& propertyName, JSValue* value)
157 {
158     if (symbolTablePut(propertyName, value))
159         return;
160     return JSVariableObject::put(exec, propertyName, value);
161 }
162
163 void JSGlobalObject::initializeVariable(ExecState* exec, const Identifier& propertyName, JSValue* value, unsigned attributes)
164 {
165     if (symbolTableInitializeVariable(propertyName, value, attributes))
166         return;
167
168     JSValue* valueBefore = getDirect(propertyName);
169     JSVariableObject::put(exec, propertyName, value);
170     if (!valueBefore) {
171         if (JSValue* valueAfter = getDirect(propertyName))
172             putDirect(propertyName, valueAfter, attributes);
173     }
174 }
175
176 static inline JSObject* lastInPrototypeChain(JSObject* object)
177 {
178     JSObject* o = object;
179     while (o->prototype()->isObject())
180         o = static_cast<JSObject*>(o->prototype());
181     return o;
182 }
183
184 void JSGlobalObject::reset(JSValue* prototype)
185 {
186     // Clear before inititalizing, to avoid calling mark() on stale pointers --
187     // which would be wasteful -- or uninitialized pointers -- which would be
188     // dangerous. (The allocations below may cause a GC.)
189
190     _prop.clear();
191     localStorage().clear();
192     symbolTable().clear();
193
194     // Prototypes
195     d()->functionPrototype = 0;
196     d()->objectPrototype = 0;
197
198     d()->arrayPrototype = 0;
199     d()->stringPrototype = 0;
200     d()->booleanPrototype = 0;
201     d()->numberPrototype = 0;
202     d()->datePrototype = 0;
203     d()->regExpPrototype = 0;
204     d()->errorPrototype = 0;
205     
206     d()->evalErrorPrototype = 0;
207     d()->rangeErrorPrototype = 0;
208     d()->referenceErrorPrototype = 0;
209     d()->syntaxErrorPrototype = 0;
210     d()->typeErrorPrototype = 0;
211     d()->URIErrorPrototype = 0;
212
213     // Constructors
214     d()->objectConstructor = 0;
215     d()->functionConstructor = 0;
216     d()->arrayConstructor = 0;
217     d()->stringConstructor = 0;
218     d()->booleanConstructor = 0;
219     d()->numberConstructor = 0;
220     d()->dateConstructor = 0;
221     d()->regExpConstructor = 0;
222     d()->errorConstructor = 0;
223     
224     d()->evalErrorConstructor = 0;
225     d()->rangeErrorConstructor = 0;
226     d()->referenceErrorConstructor = 0;
227     d()->syntaxErrorConstructor = 0;
228     d()->typeErrorConstructor = 0;
229     d()->URIErrorConstructor = 0;
230
231     d()->evalFunction = 0;
232
233     ExecState* exec = &d()->globalExec;
234
235     // Prototypes
236     d()->functionPrototype = new FunctionPrototype(exec);
237     d()->objectPrototype = new ObjectPrototype(exec, d()->functionPrototype);
238     d()->functionPrototype->setPrototype(d()->objectPrototype);
239
240     d()->arrayPrototype = new ArrayPrototype(exec, d()->objectPrototype);
241     d()->stringPrototype = new StringPrototype(exec, d()->objectPrototype);
242     d()->booleanPrototype = new BooleanPrototype(exec, d()->objectPrototype, d()->functionPrototype);
243     d()->numberPrototype = new NumberPrototype(exec, d()->objectPrototype, d()->functionPrototype);
244     d()->datePrototype = new DatePrototype(exec, d()->objectPrototype);
245     d()->regExpPrototype = new RegExpPrototype(exec, d()->objectPrototype, d()->functionPrototype);
246     d()->errorPrototype = new ErrorPrototype(exec, d()->objectPrototype, d()->functionPrototype);
247     
248     d()->evalErrorPrototype = new NativeErrorPrototype(exec, d()->errorPrototype, "EvalError", "EvalError");
249     d()->rangeErrorPrototype = new NativeErrorPrototype(exec, d()->errorPrototype, "RangeError", "RangeError");
250     d()->referenceErrorPrototype = new NativeErrorPrototype(exec, d()->errorPrototype, "ReferenceError", "ReferenceError");
251     d()->syntaxErrorPrototype = new NativeErrorPrototype(exec, d()->errorPrototype, "SyntaxError", "SyntaxError");
252     d()->typeErrorPrototype = new NativeErrorPrototype(exec, d()->errorPrototype, "TypeError", "TypeError");
253     d()->URIErrorPrototype = new NativeErrorPrototype(exec, d()->errorPrototype, "URIError", "URIError");
254
255     // Constructors
256     d()->objectConstructor = new ObjectObjectImp(exec, d()->objectPrototype, d()->functionPrototype);
257     d()->functionConstructor = new FunctionObjectImp(exec, d()->functionPrototype);
258     d()->arrayConstructor = new ArrayObjectImp(exec, d()->functionPrototype, d()->arrayPrototype);
259     d()->stringConstructor = new StringObjectImp(exec, d()->functionPrototype, d()->stringPrototype);
260     d()->booleanConstructor = new BooleanObjectImp(exec, d()->functionPrototype, d()->booleanPrototype);
261     d()->numberConstructor = new NumberObjectImp(exec, d()->functionPrototype, d()->numberPrototype);
262     d()->dateConstructor = new DateObjectImp(exec, d()->functionPrototype, d()->datePrototype);
263     d()->regExpConstructor = new RegExpObjectImp(exec, d()->functionPrototype, d()->regExpPrototype);
264     d()->errorConstructor = new ErrorObjectImp(exec, d()->functionPrototype, d()->errorPrototype);
265     
266     d()->evalErrorConstructor = new NativeErrorImp(exec, d()->functionPrototype, d()->evalErrorPrototype);
267     d()->rangeErrorConstructor = new NativeErrorImp(exec, d()->functionPrototype, d()->rangeErrorPrototype);
268     d()->referenceErrorConstructor = new NativeErrorImp(exec, d()->functionPrototype, d()->referenceErrorPrototype);
269     d()->syntaxErrorConstructor = new NativeErrorImp(exec, d()->functionPrototype, d()->syntaxErrorPrototype);
270     d()->typeErrorConstructor = new NativeErrorImp(exec, d()->functionPrototype, d()->typeErrorPrototype);
271     d()->URIErrorConstructor = new NativeErrorImp(exec, d()->functionPrototype, d()->URIErrorPrototype);
272     
273     d()->functionPrototype->putDirect(exec->propertyNames().constructor, d()->functionConstructor, DontEnum);
274
275     d()->objectPrototype->putDirect(exec->propertyNames().constructor, d()->objectConstructor, DontEnum);
276     d()->functionPrototype->putDirect(exec->propertyNames().constructor, d()->functionConstructor, DontEnum);
277     d()->arrayPrototype->putDirect(exec->propertyNames().constructor, d()->arrayConstructor, DontEnum);
278     d()->booleanPrototype->putDirect(exec->propertyNames().constructor, d()->booleanConstructor, DontEnum);
279     d()->stringPrototype->putDirect(exec->propertyNames().constructor, d()->stringConstructor, DontEnum);
280     d()->numberPrototype->putDirect(exec->propertyNames().constructor, d()->numberConstructor, DontEnum);
281     d()->datePrototype->putDirect(exec->propertyNames().constructor, d()->dateConstructor, DontEnum);
282     d()->regExpPrototype->putDirect(exec->propertyNames().constructor, d()->regExpConstructor, DontEnum);
283     d()->errorPrototype->putDirect(exec->propertyNames().constructor, d()->errorConstructor, DontEnum);
284     d()->evalErrorPrototype->putDirect(exec->propertyNames().constructor, d()->evalErrorConstructor, DontEnum);
285     d()->rangeErrorPrototype->putDirect(exec->propertyNames().constructor, d()->rangeErrorConstructor, DontEnum);
286     d()->referenceErrorPrototype->putDirect(exec->propertyNames().constructor, d()->referenceErrorConstructor, DontEnum);
287     d()->syntaxErrorPrototype->putDirect(exec->propertyNames().constructor, d()->syntaxErrorConstructor, DontEnum);
288     d()->typeErrorPrototype->putDirect(exec->propertyNames().constructor, d()->typeErrorConstructor, DontEnum);
289     d()->URIErrorPrototype->putDirect(exec->propertyNames().constructor, d()->URIErrorConstructor, DontEnum);
290
291     // Set global constructors
292
293     // FIXME: These properties could be handled by a static hash table.
294
295     putDirect("Object", d()->objectConstructor, DontEnum);
296     putDirect("Function", d()->functionConstructor, DontEnum);
297     putDirect("Array", d()->arrayConstructor, DontEnum);
298     putDirect("Boolean", d()->booleanConstructor, DontEnum);
299     putDirect("String", d()->stringConstructor, DontEnum);
300     putDirect("Number", d()->numberConstructor, DontEnum);
301     putDirect("Date", d()->dateConstructor, DontEnum);
302     putDirect("RegExp", d()->regExpConstructor, DontEnum);
303     putDirect("Error", d()->errorConstructor, DontEnum);
304     putDirect("EvalError", d()->evalErrorConstructor);
305     putDirect("RangeError", d()->rangeErrorConstructor);
306     putDirect("ReferenceError", d()->referenceErrorConstructor);
307     putDirect("SyntaxError", d()->syntaxErrorConstructor);
308     putDirect("TypeError", d()->typeErrorConstructor);
309     putDirect("URIError", d()->URIErrorConstructor);
310
311     // Set global values.
312
313     putDirect("Math", new MathObjectImp(exec, d()->objectPrototype), DontEnum);
314
315     putDirect("NaN", jsNaN(), DontEnum | DontDelete);
316     putDirect("Infinity", jsNumber(Inf), DontEnum | DontDelete);
317     putDirect("undefined", jsUndefined(), DontEnum | DontDelete);
318
319     // Set global functions.
320
321     d()->evalFunction = new PrototypeReflexiveFunction(exec, d()->functionPrototype, 1, exec->propertyNames().eval, globalFuncEval);
322     putDirectFunction(d()->evalFunction, DontEnum);
323     putDirectFunction(new PrototypeFunction(exec, d()->functionPrototype, 2, "parseInt", globalFuncParseInt), DontEnum);
324     putDirectFunction(new PrototypeFunction(exec, d()->functionPrototype, 1, "parseFloat", globalFuncParseFloat), DontEnum);
325     putDirectFunction(new PrototypeFunction(exec, d()->functionPrototype, 1, "isNaN", globalFuncIsNaN), DontEnum);
326     putDirectFunction(new PrototypeFunction(exec, d()->functionPrototype, 1, "isFinite", globalFuncIsFinite), DontEnum);
327     putDirectFunction(new PrototypeFunction(exec, d()->functionPrototype, 1, "escape", globalFuncEscape), DontEnum);
328     putDirectFunction(new PrototypeFunction(exec, d()->functionPrototype, 1, "unescape", globalFuncUnescape), DontEnum);
329     putDirectFunction(new PrototypeFunction(exec, d()->functionPrototype, 1, "decodeURI", globalFuncDecodeURI), DontEnum);
330     putDirectFunction(new PrototypeFunction(exec, d()->functionPrototype, 1, "decodeURIComponent", globalFuncDecodeURIComponent), DontEnum);
331     putDirectFunction(new PrototypeFunction(exec, d()->functionPrototype, 1, "encodeURI", globalFuncEncodeURI), DontEnum);
332     putDirectFunction(new PrototypeFunction(exec, d()->functionPrototype, 1, "encodeURIComponent", globalFuncEncodeURIComponent), DontEnum);
333 #ifndef NDEBUG
334     putDirectFunction(new PrototypeFunction(exec, d()->functionPrototype, 1, "kjsprint", globalFuncKJSPrint), DontEnum);
335 #endif
336
337     // Set prototype, and also insert the object prototype at the end of the chain.
338
339     setPrototype(prototype);
340     lastInPrototypeChain(this)->setPrototype(d()->objectPrototype);
341 }
342
343 void JSGlobalObject::startTimeoutCheck()
344 {
345     if (!d()->timeoutCheckCount)
346         resetTimeoutCheck();
347     
348     ++d()->timeoutCheckCount;
349 }
350
351 void JSGlobalObject::stopTimeoutCheck()
352 {
353     --d()->timeoutCheckCount;
354 }
355
356 void JSGlobalObject::resetTimeoutCheck()
357 {
358     d()->tickCount = 0;
359     d()->ticksUntilNextTimeoutCheck = initialTickCountThreshold;
360     d()->timeAtLastCheckTimeout = 0;
361     d()->timeExecuting = 0;
362 }
363
364 bool JSGlobalObject::checkTimeout()
365 {    
366     d()->tickCount = 0;
367     
368     unsigned currentTime = getCurrentTime();
369
370     if (!d()->timeAtLastCheckTimeout) {
371         // Suspicious amount of looping in a script -- start timing it
372         d()->timeAtLastCheckTimeout = currentTime;
373         return false;
374     }
375
376     unsigned timeDiff = currentTime - d()->timeAtLastCheckTimeout;
377
378     if (timeDiff == 0)
379         timeDiff = 1;
380     
381     d()->timeExecuting += timeDiff;
382     d()->timeAtLastCheckTimeout = currentTime;
383     
384     // Adjust the tick threshold so we get the next checkTimeout call in the interval specified in 
385     // preferredScriptCheckTimeInterval
386     d()->ticksUntilNextTimeoutCheck = (unsigned)((float)preferredScriptCheckTimeInterval / timeDiff) * d()->ticksUntilNextTimeoutCheck;
387
388     // If the new threshold is 0 reset it to the default threshold. This can happen if the timeDiff is higher than the
389     // preferred script check time interval.
390     if (d()->ticksUntilNextTimeoutCheck == 0)
391         d()->ticksUntilNextTimeoutCheck = initialTickCountThreshold;
392
393     if (d()->timeoutTime && d()->timeExecuting > d()->timeoutTime) {
394         if (shouldInterruptScript())
395             return true;
396         
397         resetTimeoutCheck();
398     }
399     
400     return false;
401 }
402
403 void JSGlobalObject::saveBuiltins(SavedBuiltins& builtins) const
404 {
405     if (!builtins._internal)
406         builtins._internal = new SavedBuiltinsInternal;
407
408     builtins._internal->objectConstructor = d()->objectConstructor;
409     builtins._internal->functionConstructor = d()->functionConstructor;
410     builtins._internal->arrayConstructor = d()->arrayConstructor;
411     builtins._internal->booleanConstructor = d()->booleanConstructor;
412     builtins._internal->stringConstructor = d()->stringConstructor;
413     builtins._internal->numberConstructor = d()->numberConstructor;
414     builtins._internal->dateConstructor = d()->dateConstructor;
415     builtins._internal->regExpConstructor = d()->regExpConstructor;
416     builtins._internal->errorConstructor = d()->errorConstructor;
417     builtins._internal->evalErrorConstructor = d()->evalErrorConstructor;
418     builtins._internal->rangeErrorConstructor = d()->rangeErrorConstructor;
419     builtins._internal->referenceErrorConstructor = d()->referenceErrorConstructor;
420     builtins._internal->syntaxErrorConstructor = d()->syntaxErrorConstructor;
421     builtins._internal->typeErrorConstructor = d()->typeErrorConstructor;
422     builtins._internal->URIErrorConstructor = d()->URIErrorConstructor;
423     
424     builtins._internal->evalFunction = d()->evalFunction;
425     
426     builtins._internal->objectPrototype = d()->objectPrototype;
427     builtins._internal->functionPrototype = d()->functionPrototype;
428     builtins._internal->arrayPrototype = d()->arrayPrototype;
429     builtins._internal->booleanPrototype = d()->booleanPrototype;
430     builtins._internal->stringPrototype = d()->stringPrototype;
431     builtins._internal->numberPrototype = d()->numberPrototype;
432     builtins._internal->datePrototype = d()->datePrototype;
433     builtins._internal->regExpPrototype = d()->regExpPrototype;
434     builtins._internal->errorPrototype = d()->errorPrototype;
435     builtins._internal->evalErrorPrototype = d()->evalErrorPrototype;
436     builtins._internal->rangeErrorPrototype = d()->rangeErrorPrototype;
437     builtins._internal->referenceErrorPrototype = d()->referenceErrorPrototype;
438     builtins._internal->syntaxErrorPrototype = d()->syntaxErrorPrototype;
439     builtins._internal->typeErrorPrototype = d()->typeErrorPrototype;
440     builtins._internal->URIErrorPrototype = d()->URIErrorPrototype;
441 }
442
443 void JSGlobalObject::restoreBuiltins(const SavedBuiltins& builtins)
444 {
445     if (!builtins._internal)
446         return;
447
448     d()->objectConstructor = builtins._internal->objectConstructor;
449     d()->functionConstructor = builtins._internal->functionConstructor;
450     d()->arrayConstructor = builtins._internal->arrayConstructor;
451     d()->booleanConstructor = builtins._internal->booleanConstructor;
452     d()->stringConstructor = builtins._internal->stringConstructor;
453     d()->numberConstructor = builtins._internal->numberConstructor;
454     d()->dateConstructor = builtins._internal->dateConstructor;
455     d()->regExpConstructor = builtins._internal->regExpConstructor;
456     d()->errorConstructor = builtins._internal->errorConstructor;
457     d()->evalErrorConstructor = builtins._internal->evalErrorConstructor;
458     d()->rangeErrorConstructor = builtins._internal->rangeErrorConstructor;
459     d()->referenceErrorConstructor = builtins._internal->referenceErrorConstructor;
460     d()->syntaxErrorConstructor = builtins._internal->syntaxErrorConstructor;
461     d()->typeErrorConstructor = builtins._internal->typeErrorConstructor;
462     d()->URIErrorConstructor = builtins._internal->URIErrorConstructor;
463     
464     d()->evalFunction = builtins._internal->evalFunction;
465
466     d()->objectPrototype = builtins._internal->objectPrototype;
467     d()->functionPrototype = builtins._internal->functionPrototype;
468     d()->arrayPrototype = builtins._internal->arrayPrototype;
469     d()->booleanPrototype = builtins._internal->booleanPrototype;
470     d()->stringPrototype = builtins._internal->stringPrototype;
471     d()->numberPrototype = builtins._internal->numberPrototype;
472     d()->datePrototype = builtins._internal->datePrototype;
473     d()->regExpPrototype = builtins._internal->regExpPrototype;
474     d()->errorPrototype = builtins._internal->errorPrototype;
475     d()->evalErrorPrototype = builtins._internal->evalErrorPrototype;
476     d()->rangeErrorPrototype = builtins._internal->rangeErrorPrototype;
477     d()->referenceErrorPrototype = builtins._internal->referenceErrorPrototype;
478     d()->syntaxErrorPrototype = builtins._internal->syntaxErrorPrototype;
479     d()->typeErrorPrototype = builtins._internal->typeErrorPrototype;
480     d()->URIErrorPrototype = builtins._internal->URIErrorPrototype;
481 }
482
483 void JSGlobalObject::mark()
484 {
485     JSVariableObject::mark();
486
487     ExecStateStack::const_iterator end = d()->activeExecStates.end();
488     for (ExecStateStack::const_iterator it = d()->activeExecStates.begin(); it != end; ++it)
489         (*it)->m_scopeChain.mark();
490
491     markIfNeeded(d()->globalExec.exception());
492
493     markIfNeeded(d()->objectConstructor);
494     markIfNeeded(d()->functionConstructor);
495     markIfNeeded(d()->arrayConstructor);
496     markIfNeeded(d()->booleanConstructor);
497     markIfNeeded(d()->stringConstructor);
498     markIfNeeded(d()->numberConstructor);
499     markIfNeeded(d()->dateConstructor);
500     markIfNeeded(d()->regExpConstructor);
501     markIfNeeded(d()->errorConstructor);
502     markIfNeeded(d()->evalErrorConstructor);
503     markIfNeeded(d()->rangeErrorConstructor);
504     markIfNeeded(d()->referenceErrorConstructor);
505     markIfNeeded(d()->syntaxErrorConstructor);
506     markIfNeeded(d()->typeErrorConstructor);
507     markIfNeeded(d()->URIErrorConstructor);
508     
509     markIfNeeded(d()->evalFunction);
510     
511     markIfNeeded(d()->objectPrototype);
512     markIfNeeded(d()->functionPrototype);
513     markIfNeeded(d()->arrayPrototype);
514     markIfNeeded(d()->booleanPrototype);
515     markIfNeeded(d()->stringPrototype);
516     markIfNeeded(d()->numberPrototype);
517     markIfNeeded(d()->datePrototype);
518     markIfNeeded(d()->regExpPrototype);
519     markIfNeeded(d()->errorPrototype);
520     markIfNeeded(d()->evalErrorPrototype);
521     markIfNeeded(d()->rangeErrorPrototype);
522     markIfNeeded(d()->referenceErrorPrototype);
523     markIfNeeded(d()->syntaxErrorPrototype);
524     markIfNeeded(d()->typeErrorPrototype);
525     markIfNeeded(d()->URIErrorPrototype);
526 }
527
528 ExecState* JSGlobalObject::globalExec()
529 {
530     return &d()->globalExec;
531 }
532
533 ActivationImp* JSGlobalObject::pushActivation(ExecState* exec)
534 {
535     if (d()->activationCount == activationStackNodeSize) {
536         ActivationStackNode* newNode = new ActivationStackNode;
537         newNode->prev = d()->activations;
538         d()->activations = newNode;
539         d()->activationCount = 0;
540     }
541     
542     StackActivation* stackEntry = &d()->activations->data[d()->activationCount++];
543     stackEntry->activationStorage.init(exec);
544     return &stackEntry->activationStorage;
545 }
546
547 inline void JSGlobalObject::checkActivationCount()
548 {
549     if (!d()->activationCount) {
550         ActivationStackNode* prev = d()->activations->prev;
551         ASSERT(prev);
552         delete d()->activations;
553         d()->activations = prev;
554         d()->activationCount = activationStackNodeSize;
555     }
556 }
557
558 void JSGlobalObject::popActivation()
559 {
560     checkActivationCount();
561     d()->activations->data[--d()->activationCount].activationDataStorage.localStorage.shrink(0);    
562 }
563
564 void JSGlobalObject::tearOffActivation(ExecState* exec, bool leaveRelic)
565 {
566     ActivationImp* oldActivation = exec->activationObject();
567     if (!oldActivation || !oldActivation->isOnStack())
568         return;
569
570     ASSERT(exec->codeType() == FunctionCode);
571     ActivationImp* newActivation = new ActivationImp(*oldActivation->d(), leaveRelic);
572     
573     if (!leaveRelic) {
574         checkActivationCount();
575         d()->activationCount--;
576     }
577     
578     oldActivation->d()->localStorage.shrink(0);
579     
580     exec->setActivationObject(newActivation);
581     exec->setVariableObject(newActivation);
582     exec->setLocalStorage(&newActivation->localStorage());
583     exec->replaceScopeChainTop(newActivation);
584 }
585
586 bool JSGlobalObject::isDynamicScope() const
587 {
588     return true;
589 }
590
591 } // namespace KJS