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