2 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
3 * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #ifndef JSGlobalObject_h
23 #define JSGlobalObject_h
26 #include "JSGlobalData.h"
27 #include "JSGlobalThis.h"
28 #include "JSVariableObject.h"
29 #include "JSWeakObjectMapRefInternal.h"
30 #include "NumberPrototype.h"
31 #include "StringPrototype.h"
32 #include "StructureChain.h"
33 #include <wtf/HashSet.h>
34 #include <wtf/OwnPtr.h>
35 #include <wtf/RandomNumber.h>
40 class BooleanPrototype;
43 class ErrorConstructor;
44 class FunctionPrototype;
46 class GlobalCodeBlock;
47 class NativeErrorConstructor;
48 class ProgramCodeBlock;
49 class RegExpConstructor;
50 class RegExpPrototype;
53 struct ActivationStackNode;
56 typedef Vector<ExecState*, 16> ExecStateStack;
58 struct GlobalObjectMethodTable {
59 typedef bool (*SupportsProfilingFunctionPtr)(const JSGlobalObject*);
60 SupportsProfilingFunctionPtr supportsProfiling;
62 typedef bool (*SupportsRichSourceInfoFunctionPtr)(const JSGlobalObject*);
63 SupportsRichSourceInfoFunctionPtr supportsRichSourceInfo;
65 typedef bool (*ShouldInterruptScriptFunctionPtr)(const JSGlobalObject*);
66 ShouldInterruptScriptFunctionPtr shouldInterruptScript;
69 class JSGlobalObject : public JSVariableObject {
71 typedef HashSet<RefPtr<OpaqueJSWeakObjectMap> > WeakMapSet;
73 struct JSGlobalObjectRareData {
74 JSGlobalObjectRareData()
80 unsigned profileGroup;
85 RefPtr<JSGlobalData> m_globalData;
87 size_t m_registerArraySize;
88 Register m_globalCallFrame[RegisterFile::CallFrameHeaderSize];
90 WriteBarrier<ScopeChainNode> m_globalScopeChain;
91 WriteBarrier<JSObject> m_methodCallDummy;
93 WriteBarrier<RegExpConstructor> m_regExpConstructor;
94 WriteBarrier<ErrorConstructor> m_errorConstructor;
95 WriteBarrier<NativeErrorConstructor> m_evalErrorConstructor;
96 WriteBarrier<NativeErrorConstructor> m_rangeErrorConstructor;
97 WriteBarrier<NativeErrorConstructor> m_referenceErrorConstructor;
98 WriteBarrier<NativeErrorConstructor> m_syntaxErrorConstructor;
99 WriteBarrier<NativeErrorConstructor> m_typeErrorConstructor;
100 WriteBarrier<NativeErrorConstructor> m_URIErrorConstructor;
102 WriteBarrier<JSFunction> m_evalFunction;
103 WriteBarrier<JSFunction> m_callFunction;
104 WriteBarrier<JSFunction> m_applyFunction;
105 WriteBarrier<GetterSetter> m_throwTypeErrorGetterSetter;
107 WriteBarrier<ObjectPrototype> m_objectPrototype;
108 WriteBarrier<FunctionPrototype> m_functionPrototype;
109 WriteBarrier<ArrayPrototype> m_arrayPrototype;
110 WriteBarrier<BooleanPrototype> m_booleanPrototype;
111 WriteBarrier<StringPrototype> m_stringPrototype;
112 WriteBarrier<NumberPrototype> m_numberPrototype;
113 WriteBarrier<DatePrototype> m_datePrototype;
114 WriteBarrier<RegExpPrototype> m_regExpPrototype;
116 WriteBarrier<Structure> m_argumentsStructure;
117 WriteBarrier<Structure> m_arrayStructure;
118 WriteBarrier<Structure> m_booleanObjectStructure;
119 WriteBarrier<Structure> m_callbackConstructorStructure;
120 WriteBarrier<Structure> m_callbackFunctionStructure;
121 WriteBarrier<Structure> m_callbackObjectStructure;
122 WriteBarrier<Structure> m_dateStructure;
123 WriteBarrier<Structure> m_emptyObjectStructure;
124 WriteBarrier<Structure> m_nullPrototypeObjectStructure;
125 WriteBarrier<Structure> m_errorStructure;
126 WriteBarrier<Structure> m_functionStructure;
127 WriteBarrier<Structure> m_boundFunctionStructure;
128 WriteBarrier<Structure> m_namedFunctionStructure;
129 size_t m_functionNameOffset;
130 WriteBarrier<Structure> m_numberObjectStructure;
131 WriteBarrier<Structure> m_regExpMatchesArrayStructure;
132 WriteBarrier<Structure> m_regExpStructure;
133 WriteBarrier<Structure> m_stringObjectStructure;
134 WriteBarrier<Structure> m_internalFunctionStructure;
135 WriteBarrier<Structure> m_strictModeTypeErrorFunctionStructure;
137 Debugger* m_debugger;
139 OwnPtr<JSGlobalObjectRareData> m_rareData;
141 WeakRandom m_weakRandom;
143 SymbolTable m_symbolTable;
147 static JS_EXPORTDATA const GlobalObjectMethodTable s_globalObjectMethodTable;
148 const GlobalObjectMethodTable* m_globalObjectMethodTable;
150 void createRareDataIfNeeded()
154 m_rareData = adoptPtr(new JSGlobalObjectRareData);
155 Heap::heap(this)->addFinalizer(this, clearRareData);
159 typedef JSVariableObject Base;
161 static JSGlobalObject* create(JSGlobalData& globalData, Structure* structure)
163 JSGlobalObject* globalObject = new (allocateCell<JSGlobalObject>(globalData.heap)) JSGlobalObject(globalData, structure);
164 globalObject->finishCreation(globalData);
168 static JS_EXPORTDATA const ClassInfo s_info;
171 explicit JSGlobalObject(JSGlobalData& globalData, Structure* structure, const GlobalObjectMethodTable* globalObjectMethodTable = 0)
172 : JSVariableObject(globalData, structure, &m_symbolTable, 0)
173 , m_registerArraySize(0)
174 , m_globalScopeChain()
175 , m_weakRandom(static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0)))
176 , m_evalEnabled(true)
177 , m_globalObjectMethodTable(globalObjectMethodTable ? globalObjectMethodTable : &s_globalObjectMethodTable)
181 void finishCreation(JSGlobalData& globalData)
183 Base::finishCreation(globalData);
184 structure()->setGlobalObject(globalData, this);
188 void finishCreation(JSGlobalData& globalData, JSGlobalThis* thisValue)
190 Base::finishCreation(globalData);
191 structure()->setGlobalObject(globalData, this);
196 virtual ~JSGlobalObject();
198 static void visitChildren(JSCell*, SlotVisitor&);
200 static bool getOwnPropertySlot(JSCell*, ExecState*, const Identifier&, PropertySlot&);
201 static bool getOwnPropertyDescriptor(JSObject*, ExecState*, const Identifier&, PropertyDescriptor&);
202 bool hasOwnPropertyForWrite(ExecState*, const Identifier&);
203 static void put(JSCell*, ExecState*, const Identifier&, JSValue, PutPropertySlot&);
205 static void putWithAttributes(JSObject*, ExecState*, const Identifier& propertyName, JSValue, unsigned attributes);
207 static void defineGetter(JSObject*, ExecState*, const Identifier& propertyName, JSObject* getterFunc, unsigned attributes);
208 static void defineSetter(JSObject*, ExecState*, const Identifier& propertyName, JSObject* setterFunc, unsigned attributes);
210 // We use this in the code generator as we perform symbol table
211 // lookups prior to initializing the properties
212 bool symbolTableHasProperty(const Identifier& propertyName);
214 // The following accessors return pristine values, even if a script
215 // replaces the global object's associated property.
217 RegExpConstructor* regExpConstructor() const { return m_regExpConstructor.get(); }
219 ErrorConstructor* errorConstructor() const { return m_errorConstructor.get(); }
220 NativeErrorConstructor* evalErrorConstructor() const { return m_evalErrorConstructor.get(); }
221 NativeErrorConstructor* rangeErrorConstructor() const { return m_rangeErrorConstructor.get(); }
222 NativeErrorConstructor* referenceErrorConstructor() const { return m_referenceErrorConstructor.get(); }
223 NativeErrorConstructor* syntaxErrorConstructor() const { return m_syntaxErrorConstructor.get(); }
224 NativeErrorConstructor* typeErrorConstructor() const { return m_typeErrorConstructor.get(); }
225 NativeErrorConstructor* URIErrorConstructor() const { return m_URIErrorConstructor.get(); }
227 JSFunction* evalFunction() const { return m_evalFunction.get(); }
228 JSFunction* callFunction() const { return m_callFunction.get(); }
229 JSFunction* applyFunction() const { return m_applyFunction.get(); }
230 GetterSetter* throwTypeErrorGetterSetter(ExecState* exec)
232 if (!m_throwTypeErrorGetterSetter)
233 createThrowTypeError(exec);
234 return m_throwTypeErrorGetterSetter.get();
237 ObjectPrototype* objectPrototype() const { return m_objectPrototype.get(); }
238 FunctionPrototype* functionPrototype() const { return m_functionPrototype.get(); }
239 ArrayPrototype* arrayPrototype() const { return m_arrayPrototype.get(); }
240 BooleanPrototype* booleanPrototype() const { return m_booleanPrototype.get(); }
241 StringPrototype* stringPrototype() const { return m_stringPrototype.get(); }
242 NumberPrototype* numberPrototype() const { return m_numberPrototype.get(); }
243 DatePrototype* datePrototype() const { return m_datePrototype.get(); }
244 RegExpPrototype* regExpPrototype() const { return m_regExpPrototype.get(); }
246 JSObject* methodCallDummy() const { return m_methodCallDummy.get(); }
248 Structure* argumentsStructure() const { return m_argumentsStructure.get(); }
249 Structure* arrayStructure() const { return m_arrayStructure.get(); }
250 Structure* booleanObjectStructure() const { return m_booleanObjectStructure.get(); }
251 Structure* callbackConstructorStructure() const { return m_callbackConstructorStructure.get(); }
252 Structure* callbackFunctionStructure() const { return m_callbackFunctionStructure.get(); }
253 Structure* callbackObjectStructure() const { return m_callbackObjectStructure.get(); }
254 Structure* dateStructure() const { return m_dateStructure.get(); }
255 Structure* emptyObjectStructure() const { return m_emptyObjectStructure.get(); }
256 Structure* nullPrototypeObjectStructure() const { return m_nullPrototypeObjectStructure.get(); }
257 Structure* errorStructure() const { return m_errorStructure.get(); }
258 Structure* functionStructure() const { return m_functionStructure.get(); }
259 Structure* boundFunctionStructure() const { return m_boundFunctionStructure.get(); }
260 Structure* namedFunctionStructure() const { return m_namedFunctionStructure.get(); }
261 size_t functionNameOffset() const { return m_functionNameOffset; }
262 Structure* numberObjectStructure() const { return m_numberObjectStructure.get(); }
263 Structure* internalFunctionStructure() const { return m_internalFunctionStructure.get(); }
264 Structure* strictModeTypeErrorFunctionStructure() const { return m_strictModeTypeErrorFunctionStructure.get(); }
265 Structure* regExpMatchesArrayStructure() const { return m_regExpMatchesArrayStructure.get(); }
266 Structure* regExpStructure() const { return m_regExpStructure.get(); }
267 Structure* stringObjectStructure() const { return m_stringObjectStructure.get(); }
269 void setProfileGroup(unsigned value) { createRareDataIfNeeded(); m_rareData->profileGroup = value; }
270 unsigned profileGroup() const
274 return m_rareData->profileGroup;
277 Debugger* debugger() const { return m_debugger; }
278 void setDebugger(Debugger* debugger) { m_debugger = debugger; }
280 const GlobalObjectMethodTable* globalObjectMethodTable() const { return m_globalObjectMethodTable; }
282 static bool supportsProfiling(const JSGlobalObject*) { return false; }
283 static bool supportsRichSourceInfo(const JSGlobalObject*) { return true; }
285 ScopeChainNode* globalScopeChain() { return m_globalScopeChain.get(); }
287 ExecState* globalExec();
289 static bool shouldInterruptScript(const JSGlobalObject*) { return true; }
291 bool isDynamicScope(bool& requiresDynamicChecks) const;
293 void setEvalEnabled(bool enabled) { m_evalEnabled = enabled; }
294 bool evalEnabled() { return m_evalEnabled; }
296 void resizeRegisters(size_t newSize);
298 void resetPrototype(JSGlobalData&, JSValue prototype);
300 JSGlobalData& globalData() const { return *m_globalData.get(); }
302 static Structure* createStructure(JSGlobalData& globalData, JSValue prototype)
304 return Structure::create(globalData, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), &s_info);
307 void registerWeakMap(OpaqueJSWeakObjectMap* map)
309 createRareDataIfNeeded();
310 m_rareData->weakMaps.add(map);
313 void unregisterWeakMap(OpaqueJSWeakObjectMap* map)
316 m_rareData->weakMaps.remove(map);
319 double weakRandomNumber() { return m_weakRandom.get(); }
322 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesVisitChildren | OverridesGetPropertyNames | JSVariableObject::StructureFlags;
324 struct GlobalPropertyInfo {
325 GlobalPropertyInfo(const Identifier& i, JSValue v, unsigned a)
332 const Identifier identifier;
336 void addStaticGlobals(GlobalPropertyInfo*, int count);
339 // FIXME: Fold reset into init.
340 void init(JSObject* thisValue);
341 void reset(JSValue prototype);
343 void createThrowTypeError(ExecState*);
345 void setRegisters(WriteBarrier<Unknown>* registers, PassOwnArrayPtr<WriteBarrier<Unknown> > registerArray, size_t count);
346 static void clearRareData(JSCell*);
349 JSGlobalObject* asGlobalObject(JSValue);
351 inline JSGlobalObject* asGlobalObject(JSValue value)
353 ASSERT(asObject(value)->isGlobalObject());
354 return static_cast<JSGlobalObject*>(asObject(value));
357 inline void JSGlobalObject::setRegisters(WriteBarrier<Unknown>* registers, PassOwnArrayPtr<WriteBarrier<Unknown> > registerArray, size_t count)
359 JSVariableObject::setRegisters(registers, registerArray);
360 m_registerArraySize = count;
363 inline bool JSGlobalObject::hasOwnPropertyForWrite(ExecState* exec, const Identifier& propertyName)
366 if (JSVariableObject::getOwnPropertySlot(this, exec, propertyName, slot))
368 bool slotIsWriteable;
369 return symbolTableGet(propertyName, slot, slotIsWriteable);
372 inline bool JSGlobalObject::symbolTableHasProperty(const Identifier& propertyName)
374 SymbolTableEntry entry = symbolTable().inlineGet(propertyName.impl());
375 return !entry.isNull();
378 inline JSValue Structure::prototypeForLookup(ExecState* exec) const
381 return m_prototype.get();
383 ASSERT(typeInfo().type() == StringType);
384 return exec->lexicalGlobalObject()->stringPrototype();
387 inline StructureChain* Structure::prototypeChain(ExecState* exec) const
389 // We cache our prototype chain so our clients can share it.
390 if (!isValid(exec, m_cachedPrototypeChain.get())) {
391 JSValue prototype = prototypeForLookup(exec);
392 m_cachedPrototypeChain.set(exec->globalData(), this, StructureChain::create(exec->globalData(), prototype.isNull() ? 0 : asObject(prototype)->structure()));
394 return m_cachedPrototypeChain.get();
397 inline bool Structure::isValid(ExecState* exec, StructureChain* cachedPrototypeChain) const
399 if (!cachedPrototypeChain)
402 JSValue prototype = prototypeForLookup(exec);
403 WriteBarrier<Structure>* cachedStructure = cachedPrototypeChain->head();
404 while(*cachedStructure && !prototype.isNull()) {
405 if (asObject(prototype)->structure() != cachedStructure->get())
408 prototype = asObject(prototype)->prototype();
410 return prototype.isNull() && !*cachedStructure;
413 inline JSGlobalObject* ExecState::dynamicGlobalObject()
415 if (this == lexicalGlobalObject()->globalExec())
416 return lexicalGlobalObject();
418 // For any ExecState that's not a globalExec, the
419 // dynamic global object must be set since code is running
420 ASSERT(globalData().dynamicGlobalObject);
421 return globalData().dynamicGlobalObject;
424 inline JSObject* constructEmptyObject(ExecState* exec, JSGlobalObject* globalObject)
426 return constructEmptyObject(exec, globalObject->emptyObjectStructure());
429 inline JSObject* constructEmptyObject(ExecState* exec)
431 return constructEmptyObject(exec, exec->lexicalGlobalObject());
434 inline JSArray* constructEmptyArray(ExecState* exec, JSGlobalObject* globalObject)
436 return JSArray::create(exec->globalData(), globalObject->arrayStructure());
439 inline JSArray* constructEmptyArray(ExecState* exec)
441 return constructEmptyArray(exec, exec->lexicalGlobalObject());
444 inline JSArray* constructEmptyArray(ExecState* exec, JSGlobalObject* globalObject, unsigned initialLength)
446 return JSArray::create(exec->globalData(), globalObject->arrayStructure(), initialLength, CreateInitialized);
449 inline JSArray* constructEmptyArray(ExecState* exec, unsigned initialLength)
451 return constructEmptyArray(exec, exec->lexicalGlobalObject(), initialLength);
454 inline JSArray* constructArray(ExecState* exec, JSGlobalObject* globalObject, JSValue singleItemValue)
456 MarkedArgumentBuffer values;
457 values.append(singleItemValue);
458 return JSArray::create(exec->globalData(), globalObject->arrayStructure(), values);
461 inline JSArray* constructArray(ExecState* exec, JSValue singleItemValue)
463 return constructArray(exec, exec->lexicalGlobalObject(), singleItemValue);
466 inline JSArray* constructArray(ExecState* exec, JSGlobalObject* globalObject, const ArgList& values)
468 return JSArray::create(exec->globalData(), globalObject->arrayStructure(), values);
471 inline JSArray* constructArray(ExecState* exec, const ArgList& values)
473 return constructArray(exec, exec->lexicalGlobalObject(), values);
476 inline JSArray* constructArray(ExecState* exec, const JSValue* values, size_t length)
478 return JSArray::create(exec->globalData(), exec->lexicalGlobalObject()->arrayStructure(), values, length);
481 class DynamicGlobalObjectScope {
482 WTF_MAKE_NONCOPYABLE(DynamicGlobalObjectScope);
484 DynamicGlobalObjectScope(JSGlobalData&, JSGlobalObject*);
486 ~DynamicGlobalObjectScope()
488 m_dynamicGlobalObjectSlot = m_savedDynamicGlobalObject;
492 JSGlobalObject*& m_dynamicGlobalObjectSlot;
493 JSGlobalObject* m_savedDynamicGlobalObject;
496 inline bool JSGlobalObject::isDynamicScope(bool&) const
503 #endif // JSGlobalObject_h