2 * Copyright (C) 2007, 2008 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 "GlobalEvalFunction.h"
50 #include "JSGlobalObjectFunctions.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"
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"
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 static inline void markIfNeeded(JSValue* v)
83 if (v && !v->marked())
87 static inline void markIfNeeded(const RefPtr<StructureID>& s)
93 JSGlobalObject::~JSGlobalObject()
95 ASSERT(JSLock::currentThreadIsHoldingLock());
98 d()->debugger->detach(this);
100 Profiler** profiler = Profiler::enabledProfilerReference();
101 if (UNLIKELY(*profiler != 0)) {
102 (*profiler)->stopProfiling(globalExec(), UString());
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)
113 HashSet<ProgramCodeBlock*>::const_iterator end = codeBlocks().end();
114 for (HashSet<ProgramCodeBlock*>::const_iterator it = codeBlocks().begin(); it != end; ++it)
115 (*it)->globalObject = 0;
117 RegisterFile& registerFile = globalData()->machine->registerFile();
118 if (registerFile.globalObject() == this) {
119 registerFile.setGlobalObject(0);
120 registerFile.setNumGlobals(0);
125 void JSGlobalObject::init(JSObject* thisValue)
127 ASSERT(JSLock::currentThreadIsHoldingLock());
129 d()->globalData = Heap::heap(this)->globalData();
131 if (JSGlobalObject*& headObject = head()) {
132 d()->prev = headObject;
133 d()->next = headObject->d()->next;
134 headObject->d()->next->d()->prev = this;
135 headObject->d()->next = this;
137 headObject = d()->next = d()->prev = this;
142 d()->globalExec.set(new ExecState(this, thisValue, d()->globalScopeChain.node()));
144 d()->profileGroup = 0;
149 void JSGlobalObject::put(ExecState* exec, const Identifier& propertyName, JSValue* value, PutPropertySlot& slot)
151 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
153 if (symbolTablePut(propertyName, value))
155 JSVariableObject::put(exec, propertyName, value, slot);
158 void JSGlobalObject::putWithAttributes(ExecState* exec, const Identifier& propertyName, JSValue* value, unsigned attributes)
160 ASSERT(!Heap::heap(value) || Heap::heap(value) == Heap::heap(this));
162 if (symbolTablePutWithAttributes(propertyName, value, attributes))
165 JSValue* valueBefore = getDirect(propertyName);
166 PutPropertySlot slot;
167 JSVariableObject::put(exec, propertyName, value, slot);
169 if (JSValue* valueAfter = getDirect(propertyName))
170 putDirect(propertyName, valueAfter, attributes);
174 void JSGlobalObject::defineGetter(ExecState* exec, const Identifier& propertyName, JSObject* getterFunc)
177 if (!symbolTableGet(propertyName, slot))
178 JSVariableObject::defineGetter(exec, propertyName, getterFunc);
181 void JSGlobalObject::defineSetter(ExecState* exec, const Identifier& propertyName, JSObject* setterFunc)
184 if (!symbolTableGet(propertyName, slot))
185 JSVariableObject::defineSetter(exec, propertyName, setterFunc);
188 static inline JSObject* lastInPrototypeChain(JSObject* object)
190 JSObject* o = object;
191 while (o->prototype()->isObject())
192 o = static_cast<JSObject*>(o->prototype());
196 void JSGlobalObject::reset(JSValue* prototype)
198 // Clear before inititalizing, to avoid calling mark() on stale pointers --
199 // which would be wasteful -- or uninitialized pointers -- which would be
200 // dangerous. (The allocations below may cause a GC.)
202 ASSERT(!hasCustomProperties());
203 symbolTable().clear();
204 setRegisters(0, 0, 0);
206 ExecState* exec = d()->globalExec.get();
210 d()->functionPrototype = new (exec) FunctionPrototype(exec);
211 d()->functionStructure = JSFunction::createStructureID(d()->functionPrototype);
212 d()->callbackFunctionStructure = JSCallbackFunction::createStructureID(d()->functionPrototype);
213 d()->prototypeFunctionStructure = PrototypeFunction::createStructureID(d()->functionPrototype);
214 d()->functionPrototype->addFunctionProperties(exec, d()->prototypeFunctionStructure.get());
215 d()->objectPrototype = new (exec) ObjectPrototype(exec, d()->prototypeFunctionStructure.get());
216 d()->emptyObjectStructure = d()->objectPrototype->inheritorID();
217 d()->functionPrototype->setPrototype(d()->objectPrototype);
218 d()->argumentsStructure = Arguments::createStructureID(d()->objectPrototype);
219 d()->callbackConstructorStructure = JSCallbackConstructor::createStructureID(d()->objectPrototype);
220 d()->callbackObjectStructure = JSCallbackObject<JSObject>::createStructureID(d()->objectPrototype);
221 d()->arrayPrototype = new (exec) ArrayPrototype(ArrayPrototype::createStructureID(d()->objectPrototype));
222 d()->arrayStructure = JSArray::createStructureID(d()->arrayPrototype);
223 d()->regExpMatchesArrayStructure = RegExpMatchesArray::createStructureID(d()->arrayPrototype);
224 d()->stringPrototype = new (exec) StringPrototype(exec, StringPrototype::createStructureID(d()->objectPrototype));
225 d()->stringObjectStructure = StringObject::createStructureID(d()->stringPrototype);
226 d()->booleanPrototype = new (exec) BooleanPrototype(exec, BooleanPrototype::createStructureID(d()->objectPrototype), d()->prototypeFunctionStructure.get());
227 d()->booleanObjectStructure = BooleanObject::createStructureID(d()->booleanPrototype);
228 d()->numberPrototype = new (exec) NumberPrototype(exec, NumberPrototype::createStructureID(d()->objectPrototype), d()->prototypeFunctionStructure.get());
229 d()->numberObjectStructure = NumberObject::createStructureID(d()->numberPrototype);
230 d()->datePrototype = new (exec) DatePrototype(exec, DatePrototype::createStructureID(d()->objectPrototype));
231 d()->dateStructure = DateInstance::createStructureID(d()->datePrototype);
232 d()->regExpPrototype = new (exec) RegExpPrototype(exec, RegExpPrototype::createStructureID(d()->objectPrototype), d()->prototypeFunctionStructure.get());
233 d()->regExpStructure = RegExpObject::createStructureID(d()->regExpPrototype);
234 ErrorPrototype* errorPrototype = new (exec) ErrorPrototype(exec, ErrorPrototype::createStructureID(d()->objectPrototype), d()->prototypeFunctionStructure.get());
235 d()->errorStructure = ErrorInstance::createStructureID(errorPrototype);
237 RefPtr<StructureID> nativeErrorPrototypeStructure = NativeErrorPrototype::createStructureID(errorPrototype);
239 NativeErrorPrototype* evalErrorPrototype = new (exec) NativeErrorPrototype(exec, nativeErrorPrototypeStructure, "EvalError", "EvalError");
240 NativeErrorPrototype* rangeErrorPrototype = new (exec) NativeErrorPrototype(exec, nativeErrorPrototypeStructure, "RangeError", "RangeError");
241 NativeErrorPrototype* referenceErrorPrototype = new (exec) NativeErrorPrototype(exec, nativeErrorPrototypeStructure, "ReferenceError", "ReferenceError");
242 NativeErrorPrototype* syntaxErrorPrototype = new (exec) NativeErrorPrototype(exec, nativeErrorPrototypeStructure, "SyntaxError", "SyntaxError");
243 NativeErrorPrototype* typeErrorPrototype = new (exec) NativeErrorPrototype(exec, nativeErrorPrototypeStructure, "TypeError", "TypeError");
244 NativeErrorPrototype* URIErrorPrototype = new (exec) NativeErrorPrototype(exec, nativeErrorPrototypeStructure, "URIError", "URIError");
248 JSValue* objectConstructor = new (exec) ObjectConstructor(exec, ObjectConstructor::createStructureID(d()->functionPrototype), d()->objectPrototype);
249 JSValue* functionConstructor = new (exec) FunctionConstructor(exec, FunctionConstructor::createStructureID(d()->functionPrototype), d()->functionPrototype);
250 JSValue* arrayConstructor = new (exec) ArrayConstructor(exec, ArrayConstructor::createStructureID(d()->functionPrototype), d()->arrayPrototype);
251 JSValue* stringConstructor = new (exec) StringConstructor(exec, StringConstructor::createStructureID(d()->functionPrototype), d()->prototypeFunctionStructure.get(), d()->stringPrototype);
252 JSValue* booleanConstructor = new (exec) BooleanConstructor(exec, BooleanConstructor::createStructureID(d()->functionPrototype), d()->booleanPrototype);
253 JSValue* numberConstructor = new (exec) NumberConstructor(exec, NumberConstructor::createStructureID(d()->functionPrototype), d()->numberPrototype);
254 JSValue* dateConstructor = new (exec) DateConstructor(exec, DateConstructor::createStructureID(d()->functionPrototype), d()->prototypeFunctionStructure.get(), d()->datePrototype);
256 d()->regExpConstructor = new (exec) RegExpConstructor(exec, RegExpConstructor::createStructureID(d()->functionPrototype), d()->regExpPrototype);
258 d()->errorConstructor = new (exec) ErrorConstructor(exec, ErrorConstructor::createStructureID(d()->functionPrototype), errorPrototype);
260 RefPtr<StructureID> nativeErrorStructure = NativeErrorConstructor::createStructureID(d()->functionPrototype);
262 d()->evalErrorConstructor = new (exec) NativeErrorConstructor(exec, nativeErrorStructure, evalErrorPrototype);
263 d()->rangeErrorConstructor = new (exec) NativeErrorConstructor(exec, nativeErrorStructure, rangeErrorPrototype);
264 d()->referenceErrorConstructor = new (exec) NativeErrorConstructor(exec, nativeErrorStructure, referenceErrorPrototype);
265 d()->syntaxErrorConstructor = new (exec) NativeErrorConstructor(exec, nativeErrorStructure, syntaxErrorPrototype);
266 d()->typeErrorConstructor = new (exec) NativeErrorConstructor(exec, nativeErrorStructure, typeErrorPrototype);
267 d()->URIErrorConstructor = new (exec) NativeErrorConstructor(exec, nativeErrorStructure, URIErrorPrototype);
269 d()->functionPrototype->putDirect(exec->propertyNames().constructor, functionConstructor, DontEnum);
271 d()->objectPrototype->putDirect(exec->propertyNames().constructor, objectConstructor, DontEnum);
272 d()->functionPrototype->putDirect(exec->propertyNames().constructor, functionConstructor, DontEnum);
273 d()->arrayPrototype->putDirect(exec->propertyNames().constructor, arrayConstructor, DontEnum);
274 d()->booleanPrototype->putDirect(exec->propertyNames().constructor, booleanConstructor, DontEnum);
275 d()->stringPrototype->putDirect(exec->propertyNames().constructor, stringConstructor, DontEnum);
276 d()->numberPrototype->putDirect(exec->propertyNames().constructor, numberConstructor, DontEnum);
277 d()->datePrototype->putDirect(exec->propertyNames().constructor, dateConstructor, DontEnum);
278 d()->regExpPrototype->putDirect(exec->propertyNames().constructor, d()->regExpConstructor, DontEnum);
279 errorPrototype->putDirect(exec->propertyNames().constructor, d()->errorConstructor, DontEnum);
280 evalErrorPrototype->putDirect(exec->propertyNames().constructor, d()->evalErrorConstructor, DontEnum);
281 rangeErrorPrototype->putDirect(exec->propertyNames().constructor, d()->rangeErrorConstructor, DontEnum);
282 referenceErrorPrototype->putDirect(exec->propertyNames().constructor, d()->referenceErrorConstructor, DontEnum);
283 syntaxErrorPrototype->putDirect(exec->propertyNames().constructor, d()->syntaxErrorConstructor, DontEnum);
284 typeErrorPrototype->putDirect(exec->propertyNames().constructor, d()->typeErrorConstructor, DontEnum);
285 URIErrorPrototype->putDirect(exec->propertyNames().constructor, d()->URIErrorConstructor, DontEnum);
287 // Set global constructors
289 // FIXME: These properties could be handled by a static hash table.
291 putDirect(Identifier(exec, "Object"), objectConstructor, DontEnum);
292 putDirect(Identifier(exec, "Function"), functionConstructor, DontEnum);
293 putDirect(Identifier(exec, "Array"), arrayConstructor, DontEnum);
294 putDirect(Identifier(exec, "Boolean"), booleanConstructor, DontEnum);
295 putDirect(Identifier(exec, "String"), stringConstructor, DontEnum);
296 putDirect(Identifier(exec, "Number"), numberConstructor, DontEnum);
297 putDirect(Identifier(exec, "Date"), dateConstructor, DontEnum);
298 putDirect(Identifier(exec, "RegExp"), d()->regExpConstructor, DontEnum);
299 putDirect(Identifier(exec, "Error"), d()->errorConstructor, DontEnum);
300 putDirect(Identifier(exec, "EvalError"), d()->evalErrorConstructor);
301 putDirect(Identifier(exec, "RangeError"), d()->rangeErrorConstructor);
302 putDirect(Identifier(exec, "ReferenceError"), d()->referenceErrorConstructor);
303 putDirect(Identifier(exec, "SyntaxError"), d()->syntaxErrorConstructor);
304 putDirect(Identifier(exec, "TypeError"), d()->typeErrorConstructor);
305 putDirect(Identifier(exec, "URIError"), d()->URIErrorConstructor);
307 // Set global values.
308 GlobalPropertyInfo staticGlobals[] = {
309 GlobalPropertyInfo(Identifier(exec, "Math"), new (exec) MathObject(exec, MathObject::createStructureID(d()->objectPrototype)), DontEnum | DontDelete),
310 GlobalPropertyInfo(Identifier(exec, "NaN"), jsNaN(exec), DontEnum | DontDelete),
311 GlobalPropertyInfo(Identifier(exec, "Infinity"), jsNumber(exec, Inf), DontEnum | DontDelete),
312 GlobalPropertyInfo(Identifier(exec, "undefined"), jsUndefined(), DontEnum | DontDelete)
315 addStaticGlobals(staticGlobals, sizeof(staticGlobals) / sizeof(GlobalPropertyInfo));
317 // Set global functions.
319 d()->evalFunction = new (exec) GlobalEvalFunction(exec, GlobalEvalFunction::createStructureID(d()->functionPrototype), 1, exec->propertyNames().eval, globalFuncEval, this);
320 putDirectFunction(exec, d()->evalFunction, DontEnum);
321 putDirectFunction(exec, new (exec) PrototypeFunction(exec, d()->prototypeFunctionStructure.get(), 2, Identifier(exec, "parseInt"), globalFuncParseInt), DontEnum);
322 putDirectFunction(exec, new (exec) PrototypeFunction(exec, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "parseFloat"), globalFuncParseFloat), DontEnum);
323 putDirectFunction(exec, new (exec) PrototypeFunction(exec, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "isNaN"), globalFuncIsNaN), DontEnum);
324 putDirectFunction(exec, new (exec) PrototypeFunction(exec, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "isFinite"), globalFuncIsFinite), DontEnum);
325 putDirectFunction(exec, new (exec) PrototypeFunction(exec, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "escape"), globalFuncEscape), DontEnum);
326 putDirectFunction(exec, new (exec) PrototypeFunction(exec, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "unescape"), globalFuncUnescape), DontEnum);
327 putDirectFunction(exec, new (exec) PrototypeFunction(exec, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "decodeURI"), globalFuncDecodeURI), DontEnum);
328 putDirectFunction(exec, new (exec) PrototypeFunction(exec, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "decodeURIComponent"), globalFuncDecodeURIComponent), DontEnum);
329 putDirectFunction(exec, new (exec) PrototypeFunction(exec, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "encodeURI"), globalFuncEncodeURI), DontEnum);
330 putDirectFunction(exec, new (exec) PrototypeFunction(exec, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "encodeURIComponent"), globalFuncEncodeURIComponent), DontEnum);
332 putDirectFunction(exec, new (exec) PrototypeFunction(exec, d()->prototypeFunctionStructure.get(), 1, Identifier(exec, "kjsprint"), globalFuncKJSPrint), DontEnum);
335 resetPrototype(prototype);
338 // Set prototype, and also insert the object prototype at the end of the chain.
339 void JSGlobalObject::resetPrototype(JSValue* prototype)
341 setPrototype(prototype);
342 lastInPrototypeChain(this)->setPrototype(d()->objectPrototype);
345 void JSGlobalObject::setTimeoutTime(unsigned timeoutTime)
347 globalData()->machine->setTimeoutTime(timeoutTime);
350 void JSGlobalObject::startTimeoutCheck()
352 globalData()->machine->startTimeoutCheck();
355 void JSGlobalObject::stopTimeoutCheck()
357 globalData()->machine->stopTimeoutCheck();
360 void JSGlobalObject::mark()
362 JSVariableObject::mark();
364 HashSet<ProgramCodeBlock*>::const_iterator end = codeBlocks().end();
365 for (HashSet<ProgramCodeBlock*>::const_iterator it = codeBlocks().begin(); it != end; ++it)
368 RegisterFile& registerFile = globalData()->machine->registerFile();
369 if (registerFile.globalObject() == this)
370 registerFile.markGlobals(globalData()->heap);
372 markIfNeeded(d()->globalExec->exception());
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);
383 markIfNeeded(d()->evalFunction);
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);
394 markIfNeeded(d()->errorStructure);
396 // No need to mark the other structures, because their prototypes are all
397 // guaranteed to be referenced elsewhere.
399 Register* registerArray = d()->registerArray.get();
403 size_t size = d()->registerArraySize;
404 for (size_t i = 0; i < size; ++i) {
405 Register& r = registerArray[i];
411 JSGlobalObject* JSGlobalObject::toGlobalObject(ExecState*) const
413 return const_cast<JSGlobalObject*>(this);
416 ExecState* JSGlobalObject::globalExec()
418 return d()->globalExec.get();
421 bool JSGlobalObject::isDynamicScope() const
426 void JSGlobalObject::copyGlobalsFrom(RegisterFile& registerFile)
428 ASSERT(!d()->registerArray);
429 ASSERT(!d()->registerArraySize);
431 int numGlobals = registerFile.numGlobals();
437 Register* registerArray = copyRegisterArray(registerFile.lastGlobal(), numGlobals);
438 setRegisters(registerArray + numGlobals, registerArray, numGlobals);
441 void JSGlobalObject::copyGlobalsTo(RegisterFile& registerFile)
443 JSGlobalObject* lastGlobalObject = registerFile.globalObject();
444 if (lastGlobalObject && lastGlobalObject != this)
445 lastGlobalObject->copyGlobalsFrom(registerFile);
447 registerFile.setGlobalObject(this);
448 registerFile.setNumGlobals(symbolTable().size());
450 if (d()->registerArray) {
451 memcpy(registerFile.base() - d()->registerArraySize, d()->registerArray.get(), d()->registerArraySize * sizeof(Register));
452 setRegisters(registerFile.base(), 0, 0);
456 void* JSGlobalObject::operator new(size_t size, JSGlobalData* globalData)
458 #ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
459 return globalData->heap->inlineAllocate(size);
461 return globalData->heap->allocate(size);