Add custom vtable struct to ClassInfo struct
[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 "JSBoundFunction.h"
50 #include "JSFunction.h"
51 #include "JSGlobalObjectFunctions.h"
52 #include "JSLock.h"
53 #include "JSONObject.h"
54 #include "Interpreter.h"
55 #include "Lookup.h"
56 #include "MathObject.h"
57 #include "NativeErrorConstructor.h"
58 #include "NativeErrorPrototype.h"
59 #include "NumberConstructor.h"
60 #include "NumberPrototype.h"
61 #include "ObjectConstructor.h"
62 #include "ObjectPrototype.h"
63 #include "Profiler.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 #include "JSGlobalObject.lut.h"
74
75 namespace JSC {
76
77 const ClassInfo JSGlobalObject::s_info = { "GlobalObject", &JSVariableObject::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(JSGlobalObject) };
78
79 /* Source for JSGlobalObject.lut.h
80 @begin globalObjectTable
81   parseInt              globalFuncParseInt              DontEnum|Function 2
82   parseFloat            globalFuncParseFloat            DontEnum|Function 1
83   isNaN                 globalFuncIsNaN                 DontEnum|Function 1
84   isFinite              globalFuncIsFinite              DontEnum|Function 1
85   escape                globalFuncEscape                DontEnum|Function 1
86   unescape              globalFuncUnescape              DontEnum|Function 1
87   decodeURI             globalFuncDecodeURI             DontEnum|Function 1
88   decodeURIComponent    globalFuncDecodeURIComponent    DontEnum|Function 1
89   encodeURI             globalFuncEncodeURI             DontEnum|Function 1
90   encodeURIComponent    globalFuncEncodeURIComponent    DontEnum|Function 1
91 @end
92 */
93
94 ASSERT_CLASS_FITS_IN_CELL(JSGlobalObject);
95
96 // Default number of ticks before a timeout check should be done.
97 static const int initialTickCountThreshold = 255;
98
99 // Preferred number of milliseconds between each timeout check
100 static const int preferredScriptCheckTimeInterval = 1000;
101
102 template <typename T> static inline void visitIfNeeded(SlotVisitor& visitor, WriteBarrier<T>* v)
103 {
104     if (*v)
105         visitor.append(v);
106 }
107
108 JSGlobalObject::~JSGlobalObject()
109 {
110     ASSERT(JSLock::currentThreadIsHoldingLock());
111
112     if (m_debugger)
113         m_debugger->detach(this);
114
115     Profiler** profiler = Profiler::enabledProfilerReference();
116     if (UNLIKELY(*profiler != 0)) {
117         (*profiler)->stopProfiling(this);
118     }
119 }
120
121 void JSGlobalObject::init(JSObject* thisValue)
122 {
123     ASSERT(JSLock::currentThreadIsHoldingLock());
124     
125     structure()->disableSpecificFunctionTracking();
126
127     m_globalData = Heap::heap(this)->globalData();
128     m_globalScopeChain.set(*m_globalData, this, ScopeChainNode::create(0, this, m_globalData.get(), this, thisValue));
129
130     JSGlobalObject::globalExec()->init(0, 0, m_globalScopeChain.get(), CallFrame::noCaller(), 0, 0);
131
132     m_debugger = 0;
133
134     reset(prototype());
135 }
136
137 void JSGlobalObject::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
138 {
139     ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
140
141     if (symbolTablePut(exec->globalData(), propertyName, value))
142         return;
143     JSVariableObject::put(exec, propertyName, value, slot);
144 }
145
146 void JSGlobalObject::putWithAttributes(ExecState* exec, const Identifier& propertyName, JSValue value, unsigned attributes)
147 {
148     ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
149
150     if (symbolTablePutWithAttributes(exec->globalData(), propertyName, value, attributes))
151         return;
152
153     JSValue valueBefore = getDirect(exec->globalData(), propertyName);
154     PutPropertySlot slot;
155     JSVariableObject::put(exec, propertyName, value, slot);
156     if (!valueBefore) {
157         JSValue valueAfter = getDirect(exec->globalData(), propertyName);
158         if (valueAfter)
159             JSObject::putWithAttributes(exec, propertyName, valueAfter, attributes);
160     }
161 }
162
163 void JSGlobalObject::defineGetter(ExecState* exec, const Identifier& propertyName, JSObject* getterFunc, unsigned attributes)
164 {
165     PropertySlot slot;
166     if (!symbolTableGet(propertyName, slot))
167         JSVariableObject::defineGetter(exec, propertyName, getterFunc, attributes);
168 }
169
170 void JSGlobalObject::defineSetter(ExecState* exec, const Identifier& propertyName, JSObject* setterFunc, unsigned attributes)
171 {
172     PropertySlot slot;
173     if (!symbolTableGet(propertyName, slot))
174         JSVariableObject::defineSetter(exec, propertyName, setterFunc, attributes);
175 }
176
177 static inline JSObject* lastInPrototypeChain(JSObject* object)
178 {
179     JSObject* o = object;
180     while (o->prototype().isObject())
181         o = asObject(o->prototype());
182     return o;
183 }
184
185 void JSGlobalObject::reset(JSValue prototype)
186 {
187     ExecState* exec = JSGlobalObject::globalExec();
188
189     m_functionPrototype.set(exec->globalData(), this, FunctionPrototype::create(exec, this, FunctionPrototype::createStructure(exec->globalData(), this, jsNull()))); // The real prototype will be set once ObjectPrototype is created.
190     m_functionStructure.set(exec->globalData(), this, JSFunction::createStructure(exec->globalData(), this, m_functionPrototype.get()));
191     m_boundFunctionStructure.set(exec->globalData(), this, JSBoundFunction::createStructure(exec->globalData(), this, m_functionPrototype.get()));
192     m_namedFunctionStructure.set(exec->globalData(), this, Structure::addPropertyTransition(exec->globalData(), m_functionStructure.get(), exec->globalData().propertyNames->name, DontDelete | ReadOnly | DontEnum, 0, m_functionNameOffset));
193     m_internalFunctionStructure.set(exec->globalData(), this, InternalFunction::createStructure(exec->globalData(), this, m_functionPrototype.get()));
194     JSFunction* callFunction = 0;
195     JSFunction* applyFunction = 0;
196     m_functionPrototype->addFunctionProperties(exec, this, &callFunction, &applyFunction);
197     m_callFunction.set(exec->globalData(), this, callFunction);
198     m_applyFunction.set(exec->globalData(), this, applyFunction);
199     m_objectPrototype.set(exec->globalData(), this, ObjectPrototype::create(exec, this, ObjectPrototype::createStructure(exec->globalData(), this, jsNull())));
200     m_functionPrototype->structure()->setPrototypeWithoutTransition(exec->globalData(), m_objectPrototype.get());
201
202     m_emptyObjectStructure.set(exec->globalData(), this, m_objectPrototype->inheritorID(exec->globalData()));
203     m_nullPrototypeObjectStructure.set(exec->globalData(), this, createEmptyObjectStructure(exec->globalData(), this, jsNull()));
204
205     m_callbackFunctionStructure.set(exec->globalData(), this, JSCallbackFunction::createStructure(exec->globalData(), this, m_functionPrototype.get()));
206     m_argumentsStructure.set(exec->globalData(), this, Arguments::createStructure(exec->globalData(), this, m_objectPrototype.get()));
207     m_callbackConstructorStructure.set(exec->globalData(), this, JSCallbackConstructor::createStructure(exec->globalData(), this, m_objectPrototype.get()));
208     m_callbackObjectStructure.set(exec->globalData(), this, JSCallbackObject<JSNonFinalObject>::createStructure(exec->globalData(), this, m_objectPrototype.get()));
209
210     m_arrayPrototype.set(exec->globalData(), this, ArrayPrototype::create(exec, this, ArrayPrototype::createStructure(exec->globalData(), this, m_objectPrototype.get())));
211     m_arrayStructure.set(exec->globalData(), this, JSArray::createStructure(exec->globalData(), this, m_arrayPrototype.get()));
212     m_regExpMatchesArrayStructure.set(exec->globalData(), this, RegExpMatchesArray::createStructure(exec->globalData(), this, m_arrayPrototype.get()));
213
214     m_stringPrototype.set(exec->globalData(), this, StringPrototype::create(exec, this, StringPrototype::createStructure(exec->globalData(), this, m_objectPrototype.get())));
215     m_stringObjectStructure.set(exec->globalData(), this, StringObject::createStructure(exec->globalData(), this, m_stringPrototype.get()));
216
217     m_booleanPrototype.set(exec->globalData(), this, BooleanPrototype::create(exec, this, BooleanPrototype::createStructure(exec->globalData(), this, m_objectPrototype.get())));
218     m_booleanObjectStructure.set(exec->globalData(), this, BooleanObject::createStructure(exec->globalData(), this, m_booleanPrototype.get()));
219
220     m_numberPrototype.set(exec->globalData(), this, NumberPrototype::create(exec, this, NumberPrototype::createStructure(exec->globalData(), this, m_objectPrototype.get())));
221     m_numberObjectStructure.set(exec->globalData(), this, NumberObject::createStructure(exec->globalData(), this, m_numberPrototype.get()));
222
223     m_datePrototype.set(exec->globalData(), this, DatePrototype::create(exec, this, DatePrototype::createStructure(exec->globalData(), this, m_objectPrototype.get())));
224     m_dateStructure.set(exec->globalData(), this, DateInstance::createStructure(exec->globalData(), this, m_datePrototype.get()));
225
226     RegExp* emptyRegex = RegExp::create(exec->globalData(), "", NoFlags);
227     
228     m_regExpPrototype.set(exec->globalData(), this, RegExpPrototype::create(exec, this, RegExpPrototype::createStructure(exec->globalData(), this, m_objectPrototype.get()), emptyRegex));
229     m_regExpStructure.set(exec->globalData(), this, RegExpObject::createStructure(exec->globalData(), this, m_regExpPrototype.get()));
230
231     m_methodCallDummy.set(exec->globalData(), this, constructEmptyObject(exec));
232
233     ErrorPrototype* errorPrototype = ErrorPrototype::create(exec, this, ErrorPrototype::createStructure(exec->globalData(), this, m_objectPrototype.get()));
234     m_errorStructure.set(exec->globalData(), this, ErrorInstance::createStructure(exec->globalData(), this, errorPrototype));
235
236     // Constructors
237
238     JSCell* objectConstructor = ObjectConstructor::create(exec, this, ObjectConstructor::createStructure(exec->globalData(), this, m_functionPrototype.get()), m_objectPrototype.get());
239     JSCell* functionConstructor = FunctionConstructor::create(exec, this, FunctionConstructor::createStructure(exec->globalData(), this, m_functionPrototype.get()), m_functionPrototype.get());
240     JSCell* arrayConstructor = ArrayConstructor::create(exec, this, ArrayConstructor::createStructure(exec->globalData(), this, m_functionPrototype.get()), m_arrayPrototype.get());
241     JSCell* stringConstructor = StringConstructor::create(exec, this, StringConstructor::createStructure(exec->globalData(), this, m_functionPrototype.get()), m_stringPrototype.get());
242     JSCell* booleanConstructor = BooleanConstructor::create(exec, this, BooleanConstructor::createStructure(exec->globalData(), this, m_functionPrototype.get()), m_booleanPrototype.get());
243     JSCell* numberConstructor = NumberConstructor::create(exec, this, NumberConstructor::createStructure(exec->globalData(), this, m_functionPrototype.get()), m_numberPrototype.get());
244     JSCell* dateConstructor = DateConstructor::create(exec, this, DateConstructor::createStructure(exec->globalData(), this, m_functionPrototype.get()), m_datePrototype.get());
245
246     m_regExpConstructor.set(exec->globalData(), this, RegExpConstructor::create(exec, this, RegExpConstructor::createStructure(exec->globalData(), this, m_functionPrototype.get()), m_regExpPrototype.get()));
247
248     m_errorConstructor.set(exec->globalData(), this, ErrorConstructor::create(exec, this, ErrorConstructor::createStructure(exec->globalData(), this, m_functionPrototype.get()), errorPrototype));
249
250     Structure* nativeErrorPrototypeStructure = NativeErrorPrototype::createStructure(exec->globalData(), this, errorPrototype);
251     Structure* nativeErrorStructure = NativeErrorConstructor::createStructure(exec->globalData(), this, m_functionPrototype.get());
252     m_evalErrorConstructor.set(exec->globalData(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "EvalError"));
253     m_rangeErrorConstructor.set(exec->globalData(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "RangeError"));
254     m_referenceErrorConstructor.set(exec->globalData(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "ReferenceError"));
255     m_syntaxErrorConstructor.set(exec->globalData(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "SyntaxError"));
256     m_typeErrorConstructor.set(exec->globalData(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "TypeError"));
257     m_URIErrorConstructor.set(exec->globalData(), this, NativeErrorConstructor::create(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "URIError"));
258
259     m_objectPrototype->putDirectWithoutTransition(exec->globalData(), exec->propertyNames().constructor, objectConstructor, DontEnum);
260     m_functionPrototype->putDirectWithoutTransition(exec->globalData(), exec->propertyNames().constructor, functionConstructor, DontEnum);
261     m_arrayPrototype->putDirectWithoutTransition(exec->globalData(), exec->propertyNames().constructor, arrayConstructor, DontEnum);
262     m_booleanPrototype->putDirectWithoutTransition(exec->globalData(), exec->propertyNames().constructor, booleanConstructor, DontEnum);
263     m_stringPrototype->putDirectWithoutTransition(exec->globalData(), exec->propertyNames().constructor, stringConstructor, DontEnum);
264     m_numberPrototype->putDirectWithoutTransition(exec->globalData(), exec->propertyNames().constructor, numberConstructor, DontEnum);
265     m_datePrototype->putDirectWithoutTransition(exec->globalData(), exec->propertyNames().constructor, dateConstructor, DontEnum);
266     m_regExpPrototype->putDirectWithoutTransition(exec->globalData(), exec->propertyNames().constructor, m_regExpConstructor.get(), DontEnum);
267     errorPrototype->putDirectWithoutTransition(exec->globalData(), exec->propertyNames().constructor, m_errorConstructor.get(), DontEnum);
268
269     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "Object"), objectConstructor, DontEnum);
270     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "Function"), functionConstructor, DontEnum);
271     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "Array"), arrayConstructor, DontEnum);
272     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "Boolean"), booleanConstructor, DontEnum);
273     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "String"), stringConstructor, DontEnum);
274     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "Number"), numberConstructor, DontEnum);
275     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "Date"), dateConstructor, DontEnum);
276     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "RegExp"), m_regExpConstructor.get(), DontEnum);
277     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "Error"), m_errorConstructor.get(), DontEnum);
278     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "EvalError"), m_evalErrorConstructor.get(), DontEnum);
279     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "RangeError"), m_rangeErrorConstructor.get(), DontEnum);
280     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "ReferenceError"), m_referenceErrorConstructor.get(), DontEnum);
281     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "SyntaxError"), m_syntaxErrorConstructor.get(), DontEnum);
282     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "TypeError"), m_typeErrorConstructor.get(), DontEnum);
283     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "URIError"), m_URIErrorConstructor.get(), DontEnum);
284
285     m_evalFunction.set(exec->globalData(), this, JSFunction::create(exec, this, 1, exec->propertyNames().eval, globalFuncEval));
286     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().eval, m_evalFunction.get(), DontEnum);
287
288     GlobalPropertyInfo staticGlobals[] = {
289         GlobalPropertyInfo(Identifier(exec, "Math"), MathObject::create(exec, this, MathObject::createStructure(exec->globalData(), this, m_objectPrototype.get())), DontEnum | DontDelete),
290         GlobalPropertyInfo(Identifier(exec, "NaN"), jsNaN(), DontEnum | DontDelete | ReadOnly),
291         GlobalPropertyInfo(Identifier(exec, "Infinity"), jsNumber(std::numeric_limits<double>::infinity()), DontEnum | DontDelete | ReadOnly),
292         GlobalPropertyInfo(Identifier(exec, "undefined"), jsUndefined(), DontEnum | DontDelete | ReadOnly),
293         GlobalPropertyInfo(Identifier(exec, "JSON"), JSONObject::create(exec, this, JSONObject::createStructure(exec->globalData(), this, m_objectPrototype.get())), DontEnum | DontDelete)
294     };
295     addStaticGlobals(staticGlobals, WTF_ARRAY_LENGTH(staticGlobals));
296
297     resetPrototype(exec->globalData(), prototype);
298 }
299
300 // Set prototype, and also insert the object prototype at the end of the chain.
301 void JSGlobalObject::resetPrototype(JSGlobalData& globalData, JSValue prototype)
302 {
303     setPrototype(globalData, prototype);
304
305     JSObject* oldLastInPrototypeChain = lastInPrototypeChain(this);
306     JSObject* objectPrototype = m_objectPrototype.get();
307     if (oldLastInPrototypeChain != objectPrototype)
308         oldLastInPrototypeChain->setPrototype(globalData, objectPrototype);
309 }
310
311 void JSGlobalObject::visitChildrenVirtual(SlotVisitor& visitor)
312 {
313     visitChildren(this, visitor);
314 }
315
316 void JSGlobalObject::visitChildren(JSCell* cell, SlotVisitor& visitor)
317
318     JSGlobalObject* thisObject = static_cast<JSGlobalObject*>(cell);
319     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
320     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
321     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
322     JSVariableObject::visitChildren(thisObject, visitor);
323
324     visitIfNeeded(visitor, &thisObject->m_globalScopeChain);
325     visitIfNeeded(visitor, &thisObject->m_methodCallDummy);
326
327     visitIfNeeded(visitor, &thisObject->m_regExpConstructor);
328     visitIfNeeded(visitor, &thisObject->m_errorConstructor);
329     visitIfNeeded(visitor, &thisObject->m_evalErrorConstructor);
330     visitIfNeeded(visitor, &thisObject->m_rangeErrorConstructor);
331     visitIfNeeded(visitor, &thisObject->m_referenceErrorConstructor);
332     visitIfNeeded(visitor, &thisObject->m_syntaxErrorConstructor);
333     visitIfNeeded(visitor, &thisObject->m_typeErrorConstructor);
334     visitIfNeeded(visitor, &thisObject->m_URIErrorConstructor);
335
336     visitIfNeeded(visitor, &thisObject->m_evalFunction);
337     visitIfNeeded(visitor, &thisObject->m_callFunction);
338     visitIfNeeded(visitor, &thisObject->m_applyFunction);
339
340     visitIfNeeded(visitor, &thisObject->m_objectPrototype);
341     visitIfNeeded(visitor, &thisObject->m_functionPrototype);
342     visitIfNeeded(visitor, &thisObject->m_arrayPrototype);
343     visitIfNeeded(visitor, &thisObject->m_booleanPrototype);
344     visitIfNeeded(visitor, &thisObject->m_stringPrototype);
345     visitIfNeeded(visitor, &thisObject->m_numberPrototype);
346     visitIfNeeded(visitor, &thisObject->m_datePrototype);
347     visitIfNeeded(visitor, &thisObject->m_regExpPrototype);
348
349     visitIfNeeded(visitor, &thisObject->m_argumentsStructure);
350     visitIfNeeded(visitor, &thisObject->m_arrayStructure);
351     visitIfNeeded(visitor, &thisObject->m_booleanObjectStructure);
352     visitIfNeeded(visitor, &thisObject->m_callbackConstructorStructure);
353     visitIfNeeded(visitor, &thisObject->m_callbackFunctionStructure);
354     visitIfNeeded(visitor, &thisObject->m_callbackObjectStructure);
355     visitIfNeeded(visitor, &thisObject->m_dateStructure);
356     visitIfNeeded(visitor, &thisObject->m_emptyObjectStructure);
357     visitIfNeeded(visitor, &thisObject->m_nullPrototypeObjectStructure);
358     visitIfNeeded(visitor, &thisObject->m_errorStructure);
359     visitIfNeeded(visitor, &thisObject->m_functionStructure);
360     visitIfNeeded(visitor, &thisObject->m_boundFunctionStructure);
361     visitIfNeeded(visitor, &thisObject->m_namedFunctionStructure);
362     visitIfNeeded(visitor, &thisObject->m_numberObjectStructure);
363     visitIfNeeded(visitor, &thisObject->m_regExpMatchesArrayStructure);
364     visitIfNeeded(visitor, &thisObject->m_regExpStructure);
365     visitIfNeeded(visitor, &thisObject->m_stringObjectStructure);
366     visitIfNeeded(visitor, &thisObject->m_internalFunctionStructure);
367
368     if (thisObject->m_registerArray) {
369         // Outside the execution of global code, when our variables are torn off,
370         // we can mark the torn-off array.
371         visitor.appendValues(thisObject->m_registerArray.get(), thisObject->m_registerArraySize);
372     } else if (thisObject->m_registers) {
373         // During execution of global code, when our variables are in the register file,
374         // the symbol table tells us how many variables there are, and registers
375         // points to where they end, and the registers used for execution begin.
376         visitor.appendValues(thisObject->m_registers - thisObject->symbolTable().size(), thisObject->symbolTable().size());
377     }
378 }
379
380 ExecState* JSGlobalObject::globalExec()
381 {
382     return CallFrame::create(m_globalCallFrame + RegisterFile::CallFrameHeaderSize);
383 }
384
385 bool JSGlobalObject::isDynamicScope(bool&) const
386 {
387     return true;
388 }
389
390 void JSGlobalObject::resizeRegisters(size_t newSize)
391 {
392     // Previous duplicate symbols may have created spare capacity in m_registerArray.
393     if (newSize <= m_registerArraySize)
394         return;
395
396     size_t oldSize = m_registerArraySize;
397     OwnArrayPtr<WriteBarrier<Unknown> > registerArray = adoptArrayPtr(new WriteBarrier<Unknown>[newSize]);
398     for (size_t i = 0; i < oldSize; ++i)
399         registerArray[i].set(globalData(), this, m_registerArray[i].get());
400     for (size_t i = oldSize; i < newSize; ++i)
401         registerArray[i].setUndefined();
402
403     WriteBarrier<Unknown>* registers = registerArray.get();
404     setRegisters(registers, registerArray.release(), newSize);
405 }
406
407 void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
408 {
409     resizeRegisters(symbolTable().size() + count);
410
411     for (int i = 0; i < count; ++i) {
412         GlobalPropertyInfo& global = globals[i];
413         ASSERT(global.attributes & DontDelete);
414         
415         int index = symbolTable().size();
416         SymbolTableEntry newEntry(index, global.attributes);
417         symbolTable().add(global.identifier.impl(), newEntry);
418         registerAt(index).set(globalData(), this, global.value);
419     }
420 }
421
422 bool JSGlobalObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
423 {
424     if (getStaticFunctionSlot<JSVariableObject>(exec, ExecState::globalObjectTable(exec), this, propertyName, slot))
425         return true;
426     return symbolTableGet(propertyName, slot);
427 }
428
429 bool JSGlobalObject::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
430 {
431     if (getStaticFunctionDescriptor<JSVariableObject>(exec, ExecState::globalObjectTable(exec), this, propertyName, descriptor))
432         return true;
433     return symbolTableGet(propertyName, descriptor);
434 }
435
436 void JSGlobalObject::WeakMapsFinalizer::finalize(Handle<Unknown> handle, void*)
437 {
438     JSGlobalObject* globalObject = asGlobalObject(handle.get());
439     globalObject->m_rareData.clear();
440 }
441
442 JSGlobalObject::WeakMapsFinalizer* JSGlobalObject::weakMapsFinalizer()
443 {
444     static WeakMapsFinalizer* finalizer = new WeakMapsFinalizer();
445     return finalizer;
446 }
447
448 DynamicGlobalObjectScope::DynamicGlobalObjectScope(JSGlobalData& globalData, JSGlobalObject* dynamicGlobalObject)
449     : m_dynamicGlobalObjectSlot(globalData.dynamicGlobalObject)
450     , m_savedDynamicGlobalObject(m_dynamicGlobalObjectSlot)
451 {
452     if (!m_dynamicGlobalObjectSlot) {
453 #if ENABLE(ASSEMBLER)
454         if (ExecutableAllocator::underMemoryPressure())
455             globalData.recompileAllJSFunctions();
456 #endif
457
458         m_dynamicGlobalObjectSlot = dynamicGlobalObject;
459
460         // Reset the date cache between JS invocations to force the VM
461         // to observe time zone changes.
462         globalData.resetDateCache();
463     }
464 }
465
466 void slowValidateCell(JSGlobalObject* globalObject)
467 {
468     if (!globalObject->isGlobalObject())
469         CRASH();
470     ASSERT_GC_OBJECT_INHERITS(globalObject, &JSGlobalObject::s_info);
471 }
472
473 } // namespace JSC