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