9b67dbb734f50fa29012f76d7de4295355f62b6c
[WebKit-https.git] / Source / JavaScriptCore / runtime / JSGlobalObject.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2009 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 "JSCallbackConstructor.h"
34 #include "JSCallbackFunction.h"
35 #include "JSCallbackObject.h"
36
37 #include "Arguments.h"
38 #include "ArrayConstructor.h"
39 #include "ArrayPrototype.h"
40 #include "BooleanConstructor.h"
41 #include "BooleanPrototype.h"
42 #include "CodeBlock.h"
43 #include "DateConstructor.h"
44 #include "DatePrototype.h"
45 #include "ErrorConstructor.h"
46 #include "ErrorPrototype.h"
47 #include "FunctionConstructor.h"
48 #include "FunctionPrototype.h"
49 #include "GlobalEvalFunction.h"
50 #include "JSFunction.h"
51 #include "JSGlobalObjectFunctions.h"
52 #include "JSLock.h"
53 #include "JSONObject.h"
54 #include "Interpreter.h"
55 #include "MathObject.h"
56 #include "NativeErrorConstructor.h"
57 #include "NativeErrorPrototype.h"
58 #include "NumberConstructor.h"
59 #include "NumberPrototype.h"
60 #include "ObjectConstructor.h"
61 #include "ObjectPrototype.h"
62 #include "Profiler.h"
63 #include "PrototypeFunction.h"
64 #include "RegExpConstructor.h"
65 #include "RegExpMatchesArray.h"
66 #include "RegExpObject.h"
67 #include "RegExpPrototype.h"
68 #include "ScopeChainMark.h"
69 #include "StringConstructor.h"
70 #include "StringPrototype.h"
71 #include "Debugger.h"
72
73 namespace JSC {
74
75 ASSERT_CLASS_FITS_IN_CELL(JSGlobalObject);
76
77 // Default number of ticks before a timeout check should be done.
78 static const int initialTickCountThreshold = 255;
79
80 // Preferred number of milliseconds between each timeout check
81 static const int preferredScriptCheckTimeInterval = 1000;
82
83 static inline void markIfNeeded(MarkStack& markStack, JSValue v)
84 {
85     if (v)
86         markStack.append(v);
87 }
88
89 static inline void markIfNeeded(MarkStack& markStack, const RefPtr<Structure>& s)
90 {
91     if (s)
92         markIfNeeded(markStack, s->storedPrototype());
93 }
94
95 JSGlobalObject::~JSGlobalObject()
96 {
97     ASSERT(JSLock::currentThreadIsHoldingLock());
98
99     if (d()->debugger)
100         d()->debugger->detach(this);
101
102     Profiler** profiler = Profiler::enabledProfilerReference();
103     if (UNLIKELY(*profiler != 0)) {
104         (*profiler)->stopProfiling(globalExec(), UString());
105     }
106
107     d()->next->d()->prev = d()->prev;
108     d()->prev->d()->next = d()->next;
109     JSGlobalObject*& headObject = head();
110     if (headObject == this)
111         headObject = d()->next;
112     if (headObject == this)
113         headObject = 0;
114
115     HashSet<GlobalCodeBlock*>::const_iterator end = codeBlocks().end();
116     for (HashSet<GlobalCodeBlock*>::const_iterator it = codeBlocks().begin(); it != end; ++it)
117         (*it)->clearGlobalObject();
118         
119     RegisterFile& registerFile = globalData().interpreter->registerFile();
120     if (registerFile.clearGlobalObject(this))
121         registerFile.setNumGlobals(0);
122     d()->destructor(d());
123 }
124
125 void JSGlobalObject::init(JSObject* thisValue)
126 {
127     ASSERT(JSLock::currentThreadIsHoldingLock());
128
129     structure()->disableSpecificFunctionTracking();
130
131     d()->globalData = Heap::heap(this)->globalData();
132     d()->globalScopeChain = ScopeChain(this, d()->globalData.get(), this, thisValue);
133
134     JSGlobalObject::globalExec()->init(0, 0, d()->globalScopeChain.node(), CallFrame::noCaller(), 0, 0);
135
136     if (JSGlobalObject*& headObject = head()) {
137         d()->prev = headObject;
138         d()->next = headObject->d()->next;
139         headObject->d()->next->d()->prev = this;
140         headObject->d()->next = this;
141     } else
142         headObject = d()->next = d()->prev = this;
143
144     d()->debugger = 0;
145
146     d()->profileGroup = 0;
147
148     reset(prototype());
149 }
150
151 void JSGlobalObject::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
152 {
153     ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
154
155     if (symbolTablePut(propertyName, value))
156         return;
157     JSVariableObject::put(exec, propertyName, value, slot);
158 }
159
160 void JSGlobalObject::putWithAttributes(ExecState* exec, const Identifier& propertyName, JSValue value, unsigned attributes)
161 {
162     ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
163
164     if (symbolTablePutWithAttributes(propertyName, value, attributes))
165         return;
166
167     JSValue valueBefore = getDirect(propertyName);
168     PutPropertySlot slot;
169     JSVariableObject::put(exec, propertyName, value, slot);
170     if (!valueBefore) {
171         JSValue valueAfter = getDirect(propertyName);
172         if (valueAfter)
173             JSObject::putWithAttributes(exec, propertyName, valueAfter, attributes);
174     }
175 }
176
177 void JSGlobalObject::defineGetter(ExecState* exec, const Identifier& propertyName, JSObject* getterFunc, unsigned attributes)
178 {
179     PropertySlot slot;
180     if (!symbolTableGet(propertyName, slot))
181         JSVariableObject::defineGetter(exec, propertyName, getterFunc, attributes);
182 }
183
184 void JSGlobalObject::defineSetter(ExecState* exec, const Identifier& propertyName, JSObject* setterFunc, unsigned attributes)
185 {
186     PropertySlot slot;
187     if (!symbolTableGet(propertyName, slot))
188         JSVariableObject::defineSetter(exec, propertyName, setterFunc, attributes);
189 }
190
191 static inline JSObject* lastInPrototypeChain(JSObject* object)
192 {
193     JSObject* o = object;
194     while (o->prototype().isObject())
195         o = asObject(o->prototype());
196     return o;
197 }
198
199 void JSGlobalObject::reset(JSValue prototype)
200 {
201     ExecState* exec = JSGlobalObject::globalExec();
202
203     // Prototypes
204
205     d()->functionPrototype = new (exec) FunctionPrototype(exec, this, FunctionPrototype::createStructure(jsNull())); // The real prototype will be set once ObjectPrototype is created.
206     d()->prototypeFunctionStructure = PrototypeFunction::createStructure(d()->functionPrototype);
207     d()->internalFunctionStructure = InternalFunction::createStructure(d()->functionPrototype);
208     NativeFunctionWrapper* callFunction = 0;
209     NativeFunctionWrapper* applyFunction = 0;
210     d()->functionPrototype->addFunctionProperties(exec, this, d()->prototypeFunctionStructure.get(), &callFunction, &applyFunction);
211     d()->callFunction = callFunction;
212     d()->applyFunction = applyFunction;
213     d()->objectPrototype = new (exec) ObjectPrototype(exec, this, ObjectPrototype::createStructure(jsNull()), d()->prototypeFunctionStructure.get());
214     d()->functionPrototype->structure()->setPrototypeWithoutTransition(d()->objectPrototype);
215
216     d()->emptyObjectStructure = d()->objectPrototype->inheritorID();
217
218     d()->functionStructure = JSFunction::createStructure(d()->functionPrototype);
219     d()->callbackFunctionStructure = JSCallbackFunction::createStructure(d()->functionPrototype);
220     d()->argumentsStructure = Arguments::createStructure(d()->objectPrototype);
221     d()->callbackConstructorStructure = JSCallbackConstructor::createStructure(d()->objectPrototype);
222     d()->callbackObjectStructure = JSCallbackObject<JSObjectWithGlobalObject>::createStructure(d()->objectPrototype);
223
224     d()->arrayPrototype = new (exec) ArrayPrototype(this, ArrayPrototype::createStructure(d()->objectPrototype));
225     d()->arrayStructure = JSArray::createStructure(d()->arrayPrototype);
226     d()->regExpMatchesArrayStructure = RegExpMatchesArray::createStructure(d()->arrayPrototype);
227
228     d()->stringPrototype = new (exec) StringPrototype(exec, this, StringPrototype::createStructure(d()->objectPrototype));
229     d()->stringObjectStructure = StringObject::createStructure(d()->stringPrototype);
230
231     d()->booleanPrototype = new (exec) BooleanPrototype(exec, this, BooleanPrototype::createStructure(d()->objectPrototype), d()->prototypeFunctionStructure.get());
232     d()->booleanObjectStructure = BooleanObject::createStructure(d()->booleanPrototype);
233
234     d()->numberPrototype = new (exec) NumberPrototype(exec, this, NumberPrototype::createStructure(d()->objectPrototype), d()->prototypeFunctionStructure.get());
235     d()->numberObjectStructure = NumberObject::createStructure(d()->numberPrototype);
236
237     d()->datePrototype = new (exec) DatePrototype(exec, this, DatePrototype::createStructure(d()->objectPrototype));
238     d()->dateStructure = DateInstance::createStructure(d()->datePrototype);
239
240     d()->regExpPrototype = new (exec) RegExpPrototype(exec, this, RegExpPrototype::createStructure(d()->objectPrototype), d()->prototypeFunctionStructure.get());
241     d()->regExpStructure = RegExpObject::createStructure(d()->regExpPrototype);
242
243     d()->methodCallDummy = constructEmptyObject(exec);
244
245     ErrorPrototype* errorPrototype = new (exec) ErrorPrototype(exec, this, ErrorPrototype::createStructure(d()->objectPrototype), d()->prototypeFunctionStructure.get());
246     d()->errorStructure = ErrorInstance::createStructure(errorPrototype);
247
248     // Constructors
249
250     JSCell* objectConstructor = new (exec) ObjectConstructor(exec, this, ObjectConstructor::createStructure(d()->functionPrototype), d()->objectPrototype, d()->prototypeFunctionStructure.get());
251     JSCell* functionConstructor = new (exec) FunctionConstructor(exec, this, FunctionConstructor::createStructure(d()->functionPrototype), d()->functionPrototype);
252     JSCell* arrayConstructor = new (exec) ArrayConstructor(exec, this, ArrayConstructor::createStructure(d()->functionPrototype), d()->arrayPrototype, d()->prototypeFunctionStructure.get());
253     JSCell* stringConstructor = new (exec) StringConstructor(exec, this, StringConstructor::createStructure(d()->functionPrototype), d()->prototypeFunctionStructure.get(), d()->stringPrototype);
254     JSCell* booleanConstructor = new (exec) BooleanConstructor(exec, this, BooleanConstructor::createStructure(d()->functionPrototype), d()->booleanPrototype);
255     JSCell* numberConstructor = new (exec) NumberConstructor(exec, this, NumberConstructor::createStructure(d()->functionPrototype), d()->numberPrototype);
256     JSCell* dateConstructor = new (exec) DateConstructor(exec, this, DateConstructor::createStructure(d()->functionPrototype), d()->prototypeFunctionStructure.get(), d()->datePrototype);
257
258     d()->regExpConstructor = new (exec) RegExpConstructor(exec, this, RegExpConstructor::createStructure(d()->functionPrototype), d()->regExpPrototype);
259
260     d()->errorConstructor = new (exec) ErrorConstructor(exec, this, ErrorConstructor::createStructure(d()->functionPrototype), errorPrototype);
261
262     RefPtr<Structure> nativeErrorPrototypeStructure = NativeErrorPrototype::createStructure(errorPrototype);
263     RefPtr<Structure> nativeErrorStructure = NativeErrorConstructor::createStructure(d()->functionPrototype);
264     d()->evalErrorConstructor = new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "EvalError");
265     d()->rangeErrorConstructor = new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "RangeError");
266     d()->referenceErrorConstructor = new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "ReferenceError");
267     d()->syntaxErrorConstructor = new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "SyntaxError");
268     d()->typeErrorConstructor = new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "TypeError");
269     d()->URIErrorConstructor = new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "URIError");
270
271     d()->objectPrototype->putDirectFunctionWithoutTransition(exec->propertyNames().constructor, objectConstructor, DontEnum);
272     d()->functionPrototype->putDirectFunctionWithoutTransition(exec->propertyNames().constructor, functionConstructor, DontEnum);
273     d()->arrayPrototype->putDirectFunctionWithoutTransition(exec->propertyNames().constructor, arrayConstructor, DontEnum);
274     d()->booleanPrototype->putDirectFunctionWithoutTransition(exec->propertyNames().constructor, booleanConstructor, DontEnum);
275     d()->stringPrototype->putDirectFunctionWithoutTransition(exec->propertyNames().constructor, stringConstructor, DontEnum);
276     d()->numberPrototype->putDirectFunctionWithoutTransition(exec->propertyNames().constructor, numberConstructor, DontEnum);
277     d()->datePrototype->putDirectFunctionWithoutTransition(exec->propertyNames().constructor, dateConstructor, DontEnum);
278     d()->regExpPrototype->putDirectFunctionWithoutTransition(exec->propertyNames().constructor, d()->regExpConstructor, DontEnum);
279     errorPrototype->putDirectFunctionWithoutTransition(exec->propertyNames().constructor, d()->errorConstructor, DontEnum);
280
281     // Set global constructors
282
283     // FIXME: These properties could be handled by a static hash table.
284
285     putDirectFunctionWithoutTransition(Identifier(exec, "Object"), objectConstructor, DontEnum);
286     putDirectFunctionWithoutTransition(Identifier(exec, "Function"), functionConstructor, DontEnum);
287     putDirectFunctionWithoutTransition(Identifier(exec, "Array"), arrayConstructor, DontEnum);
288     putDirectFunctionWithoutTransition(Identifier(exec, "Boolean"), booleanConstructor, DontEnum);
289     putDirectFunctionWithoutTransition(Identifier(exec, "String"), stringConstructor, DontEnum);
290     putDirectFunctionWithoutTransition(Identifier(exec, "Number"), numberConstructor, DontEnum);
291     putDirectFunctionWithoutTransition(Identifier(exec, "Date"), dateConstructor, DontEnum);
292     putDirectFunctionWithoutTransition(Identifier(exec, "RegExp"), d()->regExpConstructor, DontEnum);
293     putDirectFunctionWithoutTransition(Identifier(exec, "Error"), d()->errorConstructor, DontEnum);
294     putDirectFunctionWithoutTransition(Identifier(exec, "EvalError"), d()->evalErrorConstructor, DontEnum);
295     putDirectFunctionWithoutTransition(Identifier(exec, "RangeError"), d()->rangeErrorConstructor, DontEnum);
296     putDirectFunctionWithoutTransition(Identifier(exec, "ReferenceError"), d()->referenceErrorConstructor, DontEnum);
297     putDirectFunctionWithoutTransition(Identifier(exec, "SyntaxError"), d()->syntaxErrorConstructor, DontEnum);
298     putDirectFunctionWithoutTransition(Identifier(exec, "TypeError"), d()->typeErrorConstructor, DontEnum);
299     putDirectFunctionWithoutTransition(Identifier(exec, "URIError"), d()->URIErrorConstructor, DontEnum);
300
301     // Set global values.
302     GlobalPropertyInfo staticGlobals[] = {
303         GlobalPropertyInfo(Identifier(exec, "Math"), new (exec) MathObject(exec, this, MathObject::createStructure(d()->objectPrototype)), DontEnum | DontDelete),
304         GlobalPropertyInfo(Identifier(exec, "NaN"), jsNaN(), DontEnum | DontDelete | ReadOnly),
305         GlobalPropertyInfo(Identifier(exec, "Infinity"), jsNumber(Inf), DontEnum | DontDelete | ReadOnly),
306         GlobalPropertyInfo(Identifier(exec, "undefined"), jsUndefined(), DontEnum | DontDelete | ReadOnly),
307         GlobalPropertyInfo(Identifier(exec, "JSON"), new (exec) JSONObject(this, JSONObject::createStructure(d()->objectPrototype)), DontEnum | DontDelete)
308     };
309
310     addStaticGlobals(staticGlobals, WTF_ARRAY_LENGTH(staticGlobals));
311
312     // Set global functions.
313
314     d()->evalFunction = new (exec) GlobalEvalFunction(exec, this, GlobalEvalFunction::createStructure(d()->functionPrototype), 1, exec->propertyNames().eval, globalFuncEval, this);
315     putDirectFunctionWithoutTransition(exec, d()->evalFunction, DontEnum);
316     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, this, d()->prototypeFunctionStructure.get(), 2, Identifier(exec, "parseInt"), globalFuncParseInt), DontEnum);
317     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, this, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "parseFloat"), globalFuncParseFloat), DontEnum);
318     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, this, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "isNaN"), globalFuncIsNaN), DontEnum);
319     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, this, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "isFinite"), globalFuncIsFinite), DontEnum);
320     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, this, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "escape"), globalFuncEscape), DontEnum);
321     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, this, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "unescape"), globalFuncUnescape), DontEnum);
322     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, this, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "decodeURI"), globalFuncDecodeURI), DontEnum);
323     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, this, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "decodeURIComponent"), globalFuncDecodeURIComponent), DontEnum);
324     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, this, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "encodeURI"), globalFuncEncodeURI), DontEnum);
325     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, this, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "encodeURIComponent"), globalFuncEncodeURIComponent), DontEnum);
326 #ifndef NDEBUG
327     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, this, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "jscprint"), globalFuncJSCPrint), DontEnum);
328 #endif
329
330     resetPrototype(prototype);
331 }
332
333 // Set prototype, and also insert the object prototype at the end of the chain.
334 void JSGlobalObject::resetPrototype(JSValue prototype)
335 {
336     setPrototype(prototype);
337
338     JSObject* oldLastInPrototypeChain = lastInPrototypeChain(this);
339     JSObject* objectPrototype = d()->objectPrototype;
340     if (oldLastInPrototypeChain != objectPrototype)
341         oldLastInPrototypeChain->setPrototype(objectPrototype);
342 }
343
344 void JSGlobalObject::markChildren(MarkStack& markStack)
345 {
346     JSVariableObject::markChildren(markStack);
347     
348     HashSet<GlobalCodeBlock*>::const_iterator end = codeBlocks().end();
349     for (HashSet<GlobalCodeBlock*>::const_iterator it = codeBlocks().begin(); it != end; ++it)
350         (*it)->markAggregate(markStack);
351
352     markIfNeeded(markStack, d()->regExpConstructor);
353     markIfNeeded(markStack, d()->errorConstructor);
354     markIfNeeded(markStack, d()->evalErrorConstructor);
355     markIfNeeded(markStack, d()->rangeErrorConstructor);
356     markIfNeeded(markStack, d()->referenceErrorConstructor);
357     markIfNeeded(markStack, d()->syntaxErrorConstructor);
358     markIfNeeded(markStack, d()->typeErrorConstructor);
359     markIfNeeded(markStack, d()->URIErrorConstructor);
360
361     markIfNeeded(markStack, d()->evalFunction);
362     markIfNeeded(markStack, d()->callFunction);
363     markIfNeeded(markStack, d()->applyFunction);
364
365     markIfNeeded(markStack, d()->objectPrototype);
366     markIfNeeded(markStack, d()->functionPrototype);
367     markIfNeeded(markStack, d()->arrayPrototype);
368     markIfNeeded(markStack, d()->booleanPrototype);
369     markIfNeeded(markStack, d()->stringPrototype);
370     markIfNeeded(markStack, d()->numberPrototype);
371     markIfNeeded(markStack, d()->datePrototype);
372     markIfNeeded(markStack, d()->regExpPrototype);
373
374     markIfNeeded(markStack, d()->methodCallDummy);
375
376     markIfNeeded(markStack, d()->errorStructure);
377     markIfNeeded(markStack, d()->argumentsStructure);
378     markIfNeeded(markStack, d()->arrayStructure);
379     markIfNeeded(markStack, d()->booleanObjectStructure);
380     markIfNeeded(markStack, d()->callbackConstructorStructure);
381     markIfNeeded(markStack, d()->callbackFunctionStructure);
382     markIfNeeded(markStack, d()->callbackObjectStructure);
383     markIfNeeded(markStack, d()->dateStructure);
384     markIfNeeded(markStack, d()->emptyObjectStructure);
385     markIfNeeded(markStack, d()->errorStructure);
386     markIfNeeded(markStack, d()->functionStructure);
387     markIfNeeded(markStack, d()->numberObjectStructure);
388     markIfNeeded(markStack, d()->prototypeFunctionStructure);
389     markIfNeeded(markStack, d()->regExpMatchesArrayStructure);
390     markIfNeeded(markStack, d()->regExpStructure);
391     markIfNeeded(markStack, d()->stringObjectStructure);
392
393     // No need to mark the other structures, because their prototypes are all
394     // guaranteed to be referenced elsewhere.
395
396     if (d()->registerArray) {
397         // Outside the execution of global code, when our variables are torn off,
398         // we can mark the torn-off array.
399         markStack.appendValues(d()->registerArray.get(), d()->registerArraySize);
400     } else if (d()->registers) {
401         // During execution of global code, when our variables are in the register file,
402         // the symbol table tells us how many variables there are, and registers
403         // points to where they end, and the registers used for execution begin.
404         markStack.appendValues(d()->registers - symbolTable().size(), symbolTable().size());
405     }
406 }
407
408 ExecState* JSGlobalObject::globalExec()
409 {
410     return CallFrame::create(d()->globalCallFrame + RegisterFile::CallFrameHeaderSize);
411 }
412
413 bool JSGlobalObject::isDynamicScope(bool&) const
414 {
415     return true;
416 }
417
418 void JSGlobalObject::copyGlobalsFrom(RegisterFile& registerFile)
419 {
420     ASSERT(!d()->registerArray);
421     ASSERT(!d()->registerArraySize);
422
423     int numGlobals = registerFile.numGlobals();
424     if (!numGlobals) {
425         d()->registers = 0;
426         return;
427     }
428     
429     Register* registerArray = copyRegisterArray(registerFile.lastGlobal(), numGlobals);
430     setRegisters(registerArray + numGlobals, registerArray, numGlobals);
431 }
432
433 void JSGlobalObject::copyGlobalsTo(RegisterFile& registerFile)
434 {
435     JSGlobalObject* lastGlobalObject = registerFile.globalObject();
436     if (lastGlobalObject && lastGlobalObject != this)
437         lastGlobalObject->copyGlobalsFrom(registerFile);
438
439     registerFile.setGlobalObject(this);
440     registerFile.setNumGlobals(symbolTable().size());
441
442     if (d()->registerArray) {
443         memcpy(registerFile.start() - d()->registerArraySize, d()->registerArray.get(), d()->registerArraySize * sizeof(Register));
444         setRegisters(registerFile.start(), 0, 0);
445     }
446 }
447
448 void* JSGlobalObject::operator new(size_t size, JSGlobalData* globalData)
449 {
450     return globalData->heap.allocate(size);
451 }
452
453 void JSGlobalObject::destroyJSGlobalObjectData(void* jsGlobalObjectData)
454 {
455     delete static_cast<JSGlobalObjectData*>(jsGlobalObjectData);
456 }
457
458 DynamicGlobalObjectScope::DynamicGlobalObjectScope(CallFrame* callFrame, JSGlobalObject* dynamicGlobalObject)
459     : m_dynamicGlobalObjectSlot(callFrame->globalData().dynamicGlobalObject)
460     , m_savedDynamicGlobalObject(m_dynamicGlobalObjectSlot)
461 {
462     if (!m_dynamicGlobalObjectSlot) {
463 #if ENABLE(ASSEMBLER)
464         if (ExecutableAllocator::underMemoryPressure())
465             callFrame->globalData().recompileAllJSFunctions();
466 #endif
467
468         m_dynamicGlobalObjectSlot = dynamicGlobalObject;
469
470         // Reset the date cache between JS invocations to force the VM
471         // to observe time zone changes.
472         callFrame->globalData().resetDateCache();
473     }
474 }
475
476 } // namespace JSC