2 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Cameron Zwarich (cwzwarich@uwaterloo.ca)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include "JSGlobalObject.h"
33 #include "JSCallbackConstructor.h"
34 #include "JSCallbackFunction.h"
35 #include "JSCallbackObject.h"
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"
52 #include "JSONObject.h"
53 #include "Interpreter.h"
54 #include "MathObject.h"
55 #include "NativeErrorConstructor.h"
56 #include "NativeErrorPrototype.h"
57 #include "NumberConstructor.h"
58 #include "NumberPrototype.h"
59 #include "ObjectConstructor.h"
60 #include "ObjectPrototype.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"
73 ASSERT_CLASS_FITS_IN_CELL(JSGlobalObject);
75 // Default number of ticks before a timeout check should be done.
76 static const int initialTickCountThreshold = 255;
78 // Preferred number of milliseconds between each timeout check
79 static const int preferredScriptCheckTimeInterval = 1000;
81 template <typename T> static inline void markIfNeeded(MarkStack& markStack, WriteBarrier<T>* v)
87 static inline void markIfNeeded(MarkStack& markStack, const RefPtr<Structure>& s)
89 if (s && s->storedPrototype())
90 markStack.append(s->storedPrototypeSlot());
93 JSGlobalObject::~JSGlobalObject()
95 ASSERT(JSLock::currentThreadIsHoldingLock());
98 m_debugger->detach(this);
100 Profiler** profiler = Profiler::enabledProfilerReference();
101 if (UNLIKELY(*profiler != 0)) {
102 (*profiler)->stopProfiling(globalExec(), UString());
106 void JSGlobalObject::init(JSObject* thisValue)
108 ASSERT(JSLock::currentThreadIsHoldingLock());
110 structure()->disableSpecificFunctionTracking();
112 m_globalData = Heap::heap(this)->globalData();
113 m_globalScopeChain.set(*m_globalData, this, new (m_globalData.get()) ScopeChainNode(0, this, m_globalData.get(), this, thisValue));
115 JSGlobalObject::globalExec()->init(0, 0, m_globalScopeChain.get(), CallFrame::noCaller(), 0, 0);
124 void JSGlobalObject::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
126 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
128 if (symbolTablePut(exec->globalData(), propertyName, value))
130 JSVariableObject::put(exec, propertyName, value, slot);
133 void JSGlobalObject::putWithAttributes(ExecState* exec, const Identifier& propertyName, JSValue value, unsigned attributes)
135 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
137 if (symbolTablePutWithAttributes(exec->globalData(), propertyName, value, attributes))
140 JSValue valueBefore = getDirect(propertyName);
141 PutPropertySlot slot;
142 JSVariableObject::put(exec, propertyName, value, slot);
144 JSValue valueAfter = getDirect(propertyName);
146 JSObject::putWithAttributes(exec, propertyName, valueAfter, attributes);
150 void JSGlobalObject::defineGetter(ExecState* exec, const Identifier& propertyName, JSObject* getterFunc, unsigned attributes)
153 if (!symbolTableGet(propertyName, slot))
154 JSVariableObject::defineGetter(exec, propertyName, getterFunc, attributes);
157 void JSGlobalObject::defineSetter(ExecState* exec, const Identifier& propertyName, JSObject* setterFunc, unsigned attributes)
160 if (!symbolTableGet(propertyName, slot))
161 JSVariableObject::defineSetter(exec, propertyName, setterFunc, attributes);
164 static inline JSObject* lastInPrototypeChain(JSObject* object)
166 JSObject* o = object;
167 while (o->prototype().isObject())
168 o = asObject(o->prototype());
172 void JSGlobalObject::reset(JSValue prototype)
174 ExecState* exec = JSGlobalObject::globalExec();
178 m_functionPrototype.set(exec->globalData(), this, new (exec) FunctionPrototype(exec, this, FunctionPrototype::createStructure(exec->globalData(), jsNull()))); // The real prototype will be set once ObjectPrototype is created.
179 m_functionStructure = JSFunction::createStructure(exec->globalData(), m_functionPrototype.get());
180 m_internalFunctionStructure = InternalFunction::createStructure(exec->globalData(), m_functionPrototype.get());
181 JSFunction* callFunction = 0;
182 JSFunction* applyFunction = 0;
183 m_functionPrototype->addFunctionProperties(exec, this, m_functionStructure.get(), &callFunction, &applyFunction);
184 m_callFunction.set(exec->globalData(), this, callFunction);
185 m_applyFunction.set(exec->globalData(), this, applyFunction);
186 m_objectPrototype.set(exec->globalData(), this, new (exec) ObjectPrototype(exec, this, ObjectPrototype::createStructure(exec->globalData(), jsNull()), m_functionStructure.get()));
187 m_functionPrototype->structure()->setPrototypeWithoutTransition(m_objectPrototype.get());
189 m_emptyObjectStructure = m_objectPrototype->inheritorID(exec->globalData());
191 m_callbackFunctionStructure = JSCallbackFunction::createStructure(exec->globalData(), m_functionPrototype.get());
192 m_argumentsStructure = Arguments::createStructure(exec->globalData(), m_objectPrototype.get());
193 m_callbackConstructorStructure = JSCallbackConstructor::createStructure(exec->globalData(), m_objectPrototype.get());
194 m_callbackObjectStructure = JSCallbackObject<JSObjectWithGlobalObject>::createStructure(exec->globalData(), m_objectPrototype.get());
196 m_arrayPrototype.set(exec->globalData(), this, new (exec) ArrayPrototype(this, ArrayPrototype::createStructure(exec->globalData(), m_objectPrototype.get())));
197 m_arrayStructure = JSArray::createStructure(exec->globalData(), m_arrayPrototype.get());
198 m_regExpMatchesArrayStructure = RegExpMatchesArray::createStructure(exec->globalData(), m_arrayPrototype.get());
200 m_stringPrototype.set(exec->globalData(), this, new (exec) StringPrototype(exec, this, StringPrototype::createStructure(exec->globalData(), m_objectPrototype.get())));
201 m_stringObjectStructure = StringObject::createStructure(exec->globalData(), m_stringPrototype.get());
203 m_booleanPrototype.set(exec->globalData(), this, new (exec) BooleanPrototype(exec, this, BooleanPrototype::createStructure(exec->globalData(), m_objectPrototype.get()), m_functionStructure.get()));
204 m_booleanObjectStructure = BooleanObject::createStructure(exec->globalData(), m_booleanPrototype.get());
206 m_numberPrototype.set(exec->globalData(), this, new (exec) NumberPrototype(exec, this, NumberPrototype::createStructure(exec->globalData(), m_objectPrototype.get()), m_functionStructure.get()));
207 m_numberObjectStructure = NumberObject::createStructure(exec->globalData(), m_numberPrototype.get());
209 m_datePrototype.set(exec->globalData(), this, new (exec) DatePrototype(exec, this, DatePrototype::createStructure(exec->globalData(), m_objectPrototype.get())));
210 m_dateStructure = DateInstance::createStructure(exec->globalData(), m_datePrototype.get());
212 m_regExpPrototype.set(exec->globalData(), this, new (exec) RegExpPrototype(exec, this, RegExpPrototype::createStructure(exec->globalData(), m_objectPrototype.get()), m_functionStructure.get()));
213 m_regExpStructure = RegExpObject::createStructure(exec->globalData(), m_regExpPrototype.get());
215 m_methodCallDummy.set(exec->globalData(), this, constructEmptyObject(exec));
217 ErrorPrototype* errorPrototype = new (exec) ErrorPrototype(exec, this, ErrorPrototype::createStructure(exec->globalData(), m_objectPrototype.get()), m_functionStructure.get());
218 m_errorStructure = ErrorInstance::createStructure(exec->globalData(), errorPrototype);
222 JSCell* objectConstructor = new (exec) ObjectConstructor(exec, this, ObjectConstructor::createStructure(exec->globalData(), m_functionPrototype.get()), m_objectPrototype.get());
223 JSCell* functionConstructor = new (exec) FunctionConstructor(exec, this, FunctionConstructor::createStructure(exec->globalData(), m_functionPrototype.get()), m_functionPrototype.get());
224 JSCell* arrayConstructor = new (exec) ArrayConstructor(exec, this, ArrayConstructor::createStructure(exec->globalData(), m_functionPrototype.get()), m_arrayPrototype.get(), m_functionStructure.get());
225 JSCell* stringConstructor = new (exec) StringConstructor(exec, this, StringConstructor::createStructure(exec->globalData(), m_functionPrototype.get()), m_functionStructure.get(), m_stringPrototype.get());
226 JSCell* booleanConstructor = new (exec) BooleanConstructor(exec, this, BooleanConstructor::createStructure(exec->globalData(), m_functionPrototype.get()), m_booleanPrototype.get());
227 JSCell* numberConstructor = new (exec) NumberConstructor(exec, this, NumberConstructor::createStructure(exec->globalData(), m_functionPrototype.get()), m_numberPrototype.get());
228 JSCell* dateConstructor = new (exec) DateConstructor(exec, this, DateConstructor::createStructure(exec->globalData(), m_functionPrototype.get()), m_functionStructure.get(), m_datePrototype.get());
230 m_regExpConstructor.set(exec->globalData(), this, new (exec) RegExpConstructor(exec, this, RegExpConstructor::createStructure(exec->globalData(), m_functionPrototype.get()), m_regExpPrototype.get()));
232 m_errorConstructor.set(exec->globalData(), this, new (exec) ErrorConstructor(exec, this, ErrorConstructor::createStructure(exec->globalData(), m_functionPrototype.get()), errorPrototype));
234 RefPtr<Structure> nativeErrorPrototypeStructure = NativeErrorPrototype::createStructure(exec->globalData(), errorPrototype);
235 RefPtr<Structure> nativeErrorStructure = NativeErrorConstructor::createStructure(exec->globalData(), m_functionPrototype.get());
236 m_evalErrorConstructor.set(exec->globalData(), this, new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "EvalError"));
237 m_rangeErrorConstructor.set(exec->globalData(), this, new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "RangeError"));
238 m_referenceErrorConstructor.set(exec->globalData(), this, new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "ReferenceError"));
239 m_syntaxErrorConstructor.set(exec->globalData(), this, new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "SyntaxError"));
240 m_typeErrorConstructor.set(exec->globalData(), this, new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "TypeError"));
241 m_URIErrorConstructor.set(exec->globalData(), this, new (exec) NativeErrorConstructor(exec, this, nativeErrorStructure, nativeErrorPrototypeStructure, "URIError"));
243 m_objectPrototype->putDirectFunctionWithoutTransition(exec->globalData(), exec->propertyNames().constructor, objectConstructor, DontEnum);
244 m_functionPrototype->putDirectFunctionWithoutTransition(exec->globalData(), exec->propertyNames().constructor, functionConstructor, DontEnum);
245 m_arrayPrototype->putDirectFunctionWithoutTransition(exec->globalData(), exec->propertyNames().constructor, arrayConstructor, DontEnum);
246 m_booleanPrototype->putDirectFunctionWithoutTransition(exec->globalData(), exec->propertyNames().constructor, booleanConstructor, DontEnum);
247 m_stringPrototype->putDirectFunctionWithoutTransition(exec->globalData(), exec->propertyNames().constructor, stringConstructor, DontEnum);
248 m_numberPrototype->putDirectFunctionWithoutTransition(exec->globalData(), exec->propertyNames().constructor, numberConstructor, DontEnum);
249 m_datePrototype->putDirectFunctionWithoutTransition(exec->globalData(), exec->propertyNames().constructor, dateConstructor, DontEnum);
250 m_regExpPrototype->putDirectFunctionWithoutTransition(exec->globalData(), exec->propertyNames().constructor, m_regExpConstructor.get(), DontEnum);
251 errorPrototype->putDirectFunctionWithoutTransition(exec->globalData(), exec->propertyNames().constructor, m_errorConstructor.get(), DontEnum);
253 // Set global constructors
255 // FIXME: These properties could be handled by a static hash table.
257 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "Object"), objectConstructor, DontEnum);
258 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "Function"), functionConstructor, DontEnum);
259 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "Array"), arrayConstructor, DontEnum);
260 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "Boolean"), booleanConstructor, DontEnum);
261 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "String"), stringConstructor, DontEnum);
262 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "Number"), numberConstructor, DontEnum);
263 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "Date"), dateConstructor, DontEnum);
264 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "RegExp"), m_regExpConstructor.get(), DontEnum);
265 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "Error"), m_errorConstructor.get(), DontEnum);
266 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "EvalError"), m_evalErrorConstructor.get(), DontEnum);
267 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "RangeError"), m_rangeErrorConstructor.get(), DontEnum);
268 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "ReferenceError"), m_referenceErrorConstructor.get(), DontEnum);
269 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "SyntaxError"), m_syntaxErrorConstructor.get(), DontEnum);
270 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "TypeError"), m_typeErrorConstructor.get(), DontEnum);
271 putDirectFunctionWithoutTransition(exec->globalData(), Identifier(exec, "URIError"), m_URIErrorConstructor.get(), DontEnum);
273 // Set global values.
274 GlobalPropertyInfo staticGlobals[] = {
275 GlobalPropertyInfo(Identifier(exec, "Math"), new (exec) MathObject(exec, this, MathObject::createStructure(exec->globalData(), m_objectPrototype.get())), DontEnum | DontDelete),
276 GlobalPropertyInfo(Identifier(exec, "NaN"), jsNaN(), DontEnum | DontDelete | ReadOnly),
277 GlobalPropertyInfo(Identifier(exec, "Infinity"), jsNumber(Inf), DontEnum | DontDelete | ReadOnly),
278 GlobalPropertyInfo(Identifier(exec, "undefined"), jsUndefined(), DontEnum | DontDelete | ReadOnly),
279 GlobalPropertyInfo(Identifier(exec, "JSON"), new (exec) JSONObject(this, JSONObject::createStructure(exec->globalData(), m_objectPrototype.get())), DontEnum | DontDelete)
282 addStaticGlobals(staticGlobals, WTF_ARRAY_LENGTH(staticGlobals));
284 // Set global functions.
286 m_evalFunction.set(exec->globalData(), this, new (exec) JSFunction(exec, this, m_functionStructure.get(), 1, exec->propertyNames().eval, globalFuncEval));
287 putDirectFunctionWithoutTransition(exec, m_evalFunction.get(), DontEnum);
288 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, this, m_functionStructure.get(), 2, Identifier(exec, "parseInt"), globalFuncParseInt), DontEnum);
289 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, this, m_functionStructure.get(), 1, Identifier(exec, "parseFloat"), globalFuncParseFloat), DontEnum);
290 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, this, m_functionStructure.get(), 1, Identifier(exec, "isNaN"), globalFuncIsNaN), DontEnum);
291 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, this, m_functionStructure.get(), 1, Identifier(exec, "isFinite"), globalFuncIsFinite), DontEnum);
292 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, this, m_functionStructure.get(), 1, Identifier(exec, "escape"), globalFuncEscape), DontEnum);
293 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, this, m_functionStructure.get(), 1, Identifier(exec, "unescape"), globalFuncUnescape), DontEnum);
294 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, this, m_functionStructure.get(), 1, Identifier(exec, "decodeURI"), globalFuncDecodeURI), DontEnum);
295 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, this, m_functionStructure.get(), 1, Identifier(exec, "decodeURIComponent"), globalFuncDecodeURIComponent), DontEnum);
296 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, this, m_functionStructure.get(), 1, Identifier(exec, "encodeURI"), globalFuncEncodeURI), DontEnum);
297 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, this, m_functionStructure.get(), 1, Identifier(exec, "encodeURIComponent"), globalFuncEncodeURIComponent), DontEnum);
299 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, this, m_functionStructure.get(), 1, Identifier(exec, "jscprint"), globalFuncJSCPrint), DontEnum);
302 resetPrototype(prototype);
305 // Set prototype, and also insert the object prototype at the end of the chain.
306 void JSGlobalObject::resetPrototype(JSValue prototype)
308 setPrototype(prototype);
310 JSObject* oldLastInPrototypeChain = lastInPrototypeChain(this);
311 JSObject* objectPrototype = m_objectPrototype.get();
312 if (oldLastInPrototypeChain != objectPrototype)
313 oldLastInPrototypeChain->setPrototype(objectPrototype);
316 void JSGlobalObject::markChildren(MarkStack& markStack)
318 JSVariableObject::markChildren(markStack);
320 markIfNeeded(markStack, &m_globalScopeChain);
321 markIfNeeded(markStack, &m_methodCallDummy);
323 markIfNeeded(markStack, &m_regExpConstructor);
324 markIfNeeded(markStack, &m_errorConstructor);
325 markIfNeeded(markStack, &m_evalErrorConstructor);
326 markIfNeeded(markStack, &m_rangeErrorConstructor);
327 markIfNeeded(markStack, &m_referenceErrorConstructor);
328 markIfNeeded(markStack, &m_syntaxErrorConstructor);
329 markIfNeeded(markStack, &m_typeErrorConstructor);
330 markIfNeeded(markStack, &m_URIErrorConstructor);
332 markIfNeeded(markStack, &m_evalFunction);
333 markIfNeeded(markStack, &m_callFunction);
334 markIfNeeded(markStack, &m_applyFunction);
336 markIfNeeded(markStack, &m_objectPrototype);
337 markIfNeeded(markStack, &m_functionPrototype);
338 markIfNeeded(markStack, &m_arrayPrototype);
339 markIfNeeded(markStack, &m_booleanPrototype);
340 markIfNeeded(markStack, &m_stringPrototype);
341 markIfNeeded(markStack, &m_numberPrototype);
342 markIfNeeded(markStack, &m_datePrototype);
343 markIfNeeded(markStack, &m_regExpPrototype);
345 markIfNeeded(markStack, m_argumentsStructure);
346 markIfNeeded(markStack, m_arrayStructure);
347 markIfNeeded(markStack, m_booleanObjectStructure);
348 markIfNeeded(markStack, m_callbackConstructorStructure);
349 markIfNeeded(markStack, m_callbackFunctionStructure);
350 markIfNeeded(markStack, m_callbackObjectStructure);
351 markIfNeeded(markStack, m_dateStructure);
352 markIfNeeded(markStack, m_emptyObjectStructure);
353 markIfNeeded(markStack, m_errorStructure);
354 markIfNeeded(markStack, m_functionStructure);
355 markIfNeeded(markStack, m_numberObjectStructure);
356 markIfNeeded(markStack, m_regExpMatchesArrayStructure);
357 markIfNeeded(markStack, m_regExpStructure);
358 markIfNeeded(markStack, m_stringObjectStructure);
359 markIfNeeded(markStack, m_internalFunctionStructure);
361 if (m_registerArray) {
362 // Outside the execution of global code, when our variables are torn off,
363 // we can mark the torn-off array.
364 markStack.appendValues(m_registerArray.get(), m_registerArraySize);
365 } else if (m_registers) {
366 // During execution of global code, when our variables are in the register file,
367 // the symbol table tells us how many variables there are, and registers
368 // points to where they end, and the registers used for execution begin.
369 markStack.appendValues(m_registers - symbolTable().size(), symbolTable().size());
373 ExecState* JSGlobalObject::globalExec()
375 return CallFrame::create(m_globalCallFrame + RegisterFile::CallFrameHeaderSize);
378 bool JSGlobalObject::isDynamicScope(bool&) const
383 void JSGlobalObject::copyGlobalsFrom(RegisterFile& registerFile)
385 ASSERT(!m_registerArray);
386 ASSERT(!m_registerArraySize);
388 int numGlobals = registerFile.numGlobals();
394 OwnArrayPtr<WriteBarrier<Unknown> > registerArray = copyRegisterArray(globalData(), reinterpret_cast<WriteBarrier<Unknown>*>(registerFile.lastGlobal()), numGlobals);
395 WriteBarrier<Unknown>* registers = registerArray.get() + numGlobals;
396 setRegisters(registers, registerArray.release(), numGlobals);
399 void JSGlobalObject::copyGlobalsTo(RegisterFile& registerFile)
401 JSGlobalObject* lastGlobalObject = registerFile.globalObject();
402 if (lastGlobalObject && lastGlobalObject != this)
403 lastGlobalObject->copyGlobalsFrom(registerFile);
405 registerFile.setGlobalObject(this);
406 registerFile.setNumGlobals(symbolTable().size());
408 if (m_registerArray) {
409 // The register file is always a gc root so no barrier is needed here
410 memcpy(registerFile.start() - m_registerArraySize, m_registerArray.get(), m_registerArraySize * sizeof(WriteBarrier<Unknown>));
411 setRegisters(reinterpret_cast<WriteBarrier<Unknown>*>(registerFile.start()), nullptr, 0);
415 void JSGlobalObject::resizeRegisters(int oldSize, int newSize)
417 ASSERT(oldSize <= newSize);
418 if (newSize == oldSize)
420 ASSERT(newSize && newSize > oldSize);
421 if (m_registerArray || !m_registers) {
422 ASSERT(static_cast<size_t>(oldSize) == m_registerArraySize);
423 OwnArrayPtr<WriteBarrier<Unknown> > registerArray = adoptArrayPtr(new WriteBarrier<Unknown>[newSize]);
424 for (int i = 0; i < oldSize; i++)
425 registerArray[newSize - oldSize + i].set(globalData(), this, m_registerArray[i].get());
426 WriteBarrier<Unknown>* registers = registerArray.get() + newSize;
427 setRegisters(registers, registerArray.release(), newSize);
429 ASSERT(static_cast<size_t>(newSize) < globalData().interpreter->registerFile().maxGlobals());
430 globalData().interpreter->registerFile().setNumGlobals(newSize);
433 for (int i = -newSize; i < -oldSize; ++i)
434 m_registers[i].setUndefined();
437 void* JSGlobalObject::operator new(size_t size, JSGlobalData* globalData)
439 return globalData->heap.allocate(size);
442 DynamicGlobalObjectScope::DynamicGlobalObjectScope(CallFrame* callFrame, JSGlobalObject* dynamicGlobalObject)
443 : m_dynamicGlobalObjectSlot(callFrame->globalData().dynamicGlobalObject)
444 , m_savedDynamicGlobalObject(m_dynamicGlobalObjectSlot)
446 if (!m_dynamicGlobalObjectSlot) {
447 #if ENABLE(ASSEMBLER)
448 if (ExecutableAllocator::underMemoryPressure())
449 callFrame->globalData().recompileAllJSFunctions();
452 m_dynamicGlobalObjectSlot = dynamicGlobalObject;
454 // Reset the date cache between JS invocations to force the VM
455 // to observe time zone changes.
456 callFrame->globalData().resetDateCache();