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