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