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