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 "JSVariableObject.h"
28 #include "JSWeakObjectMapRefInternal.h"
29 #include "NumberPrototype.h"
30 #include "StringPrototype.h"
31 #include "StructureChain.h"
32 #include <wtf/HashSet.h>
33 #include <wtf/OwnPtr.h>
34 #include <wtf/RandomNumber.h>
39 class BooleanPrototype;
42 class ErrorConstructor;
43 class FunctionPrototype;
44 class GlobalCodeBlock;
45 class NativeErrorConstructor;
46 class ProgramCodeBlock;
47 class RegExpConstructor;
48 class RegExpPrototype;
51 struct ActivationStackNode;
54 typedef Vector<ExecState*, 16> ExecStateStack;
56 class JSGlobalObject : public JSVariableObject {
58 typedef HashSet<RefPtr<OpaqueJSWeakObjectMap> > WeakMapSet;
60 class WeakMapsFinalizer : public WeakHandleOwner {
62 virtual void finalize(Handle<Unknown>, void* context);
65 struct JSGlobalObjectRareData {
66 JSGlobalObjectRareData()
72 unsigned profileGroup;
73 Weak<JSGlobalObject> weakMapsFinalizer;
78 RefPtr<JSGlobalData> m_globalData;
80 size_t m_registerArraySize;
81 Register m_globalCallFrame[RegisterFile::CallFrameHeaderSize];
83 WriteBarrier<ScopeChainNode> m_globalScopeChain;
84 WriteBarrier<JSObject> m_methodCallDummy;
86 WriteBarrier<RegExpConstructor> m_regExpConstructor;
87 WriteBarrier<ErrorConstructor> m_errorConstructor;
88 WriteBarrier<NativeErrorConstructor> m_evalErrorConstructor;
89 WriteBarrier<NativeErrorConstructor> m_rangeErrorConstructor;
90 WriteBarrier<NativeErrorConstructor> m_referenceErrorConstructor;
91 WriteBarrier<NativeErrorConstructor> m_syntaxErrorConstructor;
92 WriteBarrier<NativeErrorConstructor> m_typeErrorConstructor;
93 WriteBarrier<NativeErrorConstructor> m_URIErrorConstructor;
95 WriteBarrier<JSFunction> m_evalFunction;
96 WriteBarrier<JSFunction> m_callFunction;
97 WriteBarrier<JSFunction> m_applyFunction;
99 WriteBarrier<ObjectPrototype> m_objectPrototype;
100 WriteBarrier<FunctionPrototype> m_functionPrototype;
101 WriteBarrier<ArrayPrototype> m_arrayPrototype;
102 WriteBarrier<BooleanPrototype> m_booleanPrototype;
103 WriteBarrier<StringPrototype> m_stringPrototype;
104 WriteBarrier<NumberPrototype> m_numberPrototype;
105 WriteBarrier<DatePrototype> m_datePrototype;
106 WriteBarrier<RegExpPrototype> m_regExpPrototype;
108 WriteBarrier<Structure> m_argumentsStructure;
109 WriteBarrier<Structure> m_arrayStructure;
110 WriteBarrier<Structure> m_booleanObjectStructure;
111 WriteBarrier<Structure> m_callbackConstructorStructure;
112 WriteBarrier<Structure> m_callbackFunctionStructure;
113 WriteBarrier<Structure> m_callbackObjectStructure;
114 WriteBarrier<Structure> m_dateStructure;
115 WriteBarrier<Structure> m_emptyObjectStructure;
116 WriteBarrier<Structure> m_nullPrototypeObjectStructure;
117 WriteBarrier<Structure> m_errorStructure;
118 WriteBarrier<Structure> m_functionStructure;
119 WriteBarrier<Structure> m_namedFunctionStructure;
120 size_t m_functionNameOffset;
121 WriteBarrier<Structure> m_numberObjectStructure;
122 WriteBarrier<Structure> m_regExpMatchesArrayStructure;
123 WriteBarrier<Structure> m_regExpStructure;
124 WriteBarrier<Structure> m_stringObjectStructure;
125 WriteBarrier<Structure> m_internalFunctionStructure;
127 Debugger* m_debugger;
129 OwnPtr<JSGlobalObjectRareData> m_rareData;
130 static WeakMapsFinalizer* weakMapsFinalizer();
132 WeakRandom m_weakRandom;
134 SymbolTable m_symbolTable;
138 void createRareDataIfNeeded()
141 m_rareData = adoptPtr(new JSGlobalObjectRareData);
145 typedef JSVariableObject Base;
147 static JSGlobalObject* create(JSGlobalData& globalData, Structure* structure)
149 JSGlobalObject* globalObject = new (allocateCell<JSGlobalObject>(globalData.heap)) JSGlobalObject(globalData, structure);
150 globalObject->finishCreation(globalData, globalObject);
154 static JS_EXPORTDATA const ClassInfo s_info;
157 explicit JSGlobalObject(JSGlobalData& globalData, Structure* structure)
158 : JSVariableObject(globalData, structure, &m_symbolTable, 0)
159 , m_registerArraySize(0)
160 , m_globalScopeChain()
161 , m_weakRandom(static_cast<unsigned>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0)))
162 , m_evalEnabled(true)
166 void finishCreation(JSGlobalData& globalData, JSObject* thisValue)
168 Base::finishCreation(globalData);
169 structure()->setGlobalObject(globalData, this);
170 putThisToAnonymousValue(0);
175 virtual ~JSGlobalObject();
177 virtual void visitChildren(SlotVisitor&);
179 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
180 virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
181 virtual bool hasOwnPropertyForWrite(ExecState*, const Identifier&);
182 virtual void put(ExecState*, const Identifier&, JSValue, PutPropertySlot&);
183 virtual void putWithAttributes(ExecState*, const Identifier& propertyName, JSValue value, unsigned attributes);
185 virtual void defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunc, unsigned attributes);
186 virtual void defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunc, unsigned attributes);
188 // We use this in the code generator as we perform symbol table
189 // lookups prior to initializing the properties
190 bool symbolTableHasProperty(const Identifier& propertyName);
192 // The following accessors return pristine values, even if a script
193 // replaces the global object's associated property.
195 RegExpConstructor* regExpConstructor() const { return m_regExpConstructor.get(); }
197 ErrorConstructor* errorConstructor() const { return m_errorConstructor.get(); }
198 NativeErrorConstructor* evalErrorConstructor() const { return m_evalErrorConstructor.get(); }
199 NativeErrorConstructor* rangeErrorConstructor() const { return m_rangeErrorConstructor.get(); }
200 NativeErrorConstructor* referenceErrorConstructor() const { return m_referenceErrorConstructor.get(); }
201 NativeErrorConstructor* syntaxErrorConstructor() const { return m_syntaxErrorConstructor.get(); }
202 NativeErrorConstructor* typeErrorConstructor() const { return m_typeErrorConstructor.get(); }
203 NativeErrorConstructor* URIErrorConstructor() const { return m_URIErrorConstructor.get(); }
205 JSFunction* evalFunction() const { return m_evalFunction.get(); }
206 JSFunction* callFunction() const { return m_callFunction.get(); }
207 JSFunction* applyFunction() const { return m_applyFunction.get(); }
209 ObjectPrototype* objectPrototype() const { return m_objectPrototype.get(); }
210 FunctionPrototype* functionPrototype() const { return m_functionPrototype.get(); }
211 ArrayPrototype* arrayPrototype() const { return m_arrayPrototype.get(); }
212 BooleanPrototype* booleanPrototype() const { return m_booleanPrototype.get(); }
213 StringPrototype* stringPrototype() const { return m_stringPrototype.get(); }
214 NumberPrototype* numberPrototype() const { return m_numberPrototype.get(); }
215 DatePrototype* datePrototype() const { return m_datePrototype.get(); }
216 RegExpPrototype* regExpPrototype() const { return m_regExpPrototype.get(); }
218 JSObject* methodCallDummy() const { return m_methodCallDummy.get(); }
220 Structure* argumentsStructure() const { return m_argumentsStructure.get(); }
221 Structure* arrayStructure() const { return m_arrayStructure.get(); }
222 Structure* booleanObjectStructure() const { return m_booleanObjectStructure.get(); }
223 Structure* callbackConstructorStructure() const { return m_callbackConstructorStructure.get(); }
224 Structure* callbackFunctionStructure() const { return m_callbackFunctionStructure.get(); }
225 Structure* callbackObjectStructure() const { return m_callbackObjectStructure.get(); }
226 Structure* dateStructure() const { return m_dateStructure.get(); }
227 Structure* emptyObjectStructure() const { return m_emptyObjectStructure.get(); }
228 Structure* nullPrototypeObjectStructure() const { return m_nullPrototypeObjectStructure.get(); }
229 Structure* errorStructure() const { return m_errorStructure.get(); }
230 Structure* functionStructure() const { return m_functionStructure.get(); }
231 Structure* namedFunctionStructure() const { return m_namedFunctionStructure.get(); }
232 size_t functionNameOffset() const { return m_functionNameOffset; }
233 Structure* numberObjectStructure() const { return m_numberObjectStructure.get(); }
234 Structure* internalFunctionStructure() const { return m_internalFunctionStructure.get(); }
235 Structure* regExpMatchesArrayStructure() const { return m_regExpMatchesArrayStructure.get(); }
236 Structure* regExpStructure() const { return m_regExpStructure.get(); }
237 Structure* stringObjectStructure() const { return m_stringObjectStructure.get(); }
239 void setProfileGroup(unsigned value) { createRareDataIfNeeded(); m_rareData->profileGroup = value; }
240 unsigned profileGroup() const
244 return m_rareData->profileGroup;
247 Debugger* debugger() const { return m_debugger; }
248 void setDebugger(Debugger* debugger) { m_debugger = debugger; }
250 virtual bool supportsProfiling() const { return false; }
251 virtual bool supportsRichSourceInfo() const { return true; }
253 ScopeChainNode* globalScopeChain() { return m_globalScopeChain.get(); }
255 virtual bool isGlobalObject() const { return true; }
257 ExecState* globalExec();
259 virtual bool shouldInterruptScript() const { return true; }
261 virtual bool allowsAccessFrom(const JSGlobalObject*) const { return true; }
263 virtual bool isDynamicScope(bool& requiresDynamicChecks) const;
265 void setEvalEnabled(bool enabled) { m_evalEnabled = enabled; }
266 bool evalEnabled() { return m_evalEnabled; }
268 void resizeRegisters(size_t newSize);
270 void resetPrototype(JSGlobalData&, JSValue prototype);
272 JSGlobalData& globalData() const { return *m_globalData.get(); }
274 static Structure* createStructure(JSGlobalData& globalData, JSValue prototype)
276 return Structure::create(globalData, 0, prototype, TypeInfo(ObjectType, StructureFlags), &s_info);
279 void registerWeakMap(OpaqueJSWeakObjectMap* map)
281 createRareDataIfNeeded();
282 if (!m_rareData->weakMapsFinalizer)
283 m_rareData->weakMapsFinalizer.set(globalData(), this, weakMapsFinalizer());
284 m_rareData->weakMaps.add(map);
287 void deregisterWeakMap(OpaqueJSWeakObjectMap* map)
290 m_rareData->weakMaps.remove(map);
293 double weakRandomNumber() { return m_weakRandom.get(); }
296 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesVisitChildren | OverridesGetPropertyNames | JSVariableObject::StructureFlags;
298 struct GlobalPropertyInfo {
299 GlobalPropertyInfo(const Identifier& i, JSValue v, unsigned a)
306 const Identifier identifier;
310 void addStaticGlobals(GlobalPropertyInfo*, int count);
313 // FIXME: Fold reset into init.
314 void init(JSObject* thisValue);
315 void reset(JSValue prototype);
317 void setRegisters(WriteBarrier<Unknown>* registers, PassOwnArrayPtr<WriteBarrier<Unknown> > registerArray, size_t count);
320 JSGlobalObject* asGlobalObject(JSValue);
322 inline JSGlobalObject* asGlobalObject(JSValue value)
324 ASSERT(asObject(value)->isGlobalObject());
325 return static_cast<JSGlobalObject*>(asObject(value));
328 inline void JSGlobalObject::setRegisters(WriteBarrier<Unknown>* registers, PassOwnArrayPtr<WriteBarrier<Unknown> > registerArray, size_t count)
330 JSVariableObject::setRegisters(registers, registerArray);
331 m_registerArraySize = count;
334 inline bool JSGlobalObject::hasOwnPropertyForWrite(ExecState* exec, const Identifier& propertyName)
337 if (JSVariableObject::getOwnPropertySlot(exec, propertyName, slot))
339 bool slotIsWriteable;
340 return symbolTableGet(propertyName, slot, slotIsWriteable);
343 inline bool JSGlobalObject::symbolTableHasProperty(const Identifier& propertyName)
345 SymbolTableEntry entry = symbolTable().inlineGet(propertyName.impl());
346 return !entry.isNull();
349 inline JSValue Structure::prototypeForLookup(ExecState* exec) const
351 if (typeInfo().type() == ObjectType)
352 return m_prototype.get();
354 ASSERT(typeInfo().type() == StringType);
355 return exec->lexicalGlobalObject()->stringPrototype();
358 inline StructureChain* Structure::prototypeChain(ExecState* exec) const
360 // We cache our prototype chain so our clients can share it.
361 if (!isValid(exec, m_cachedPrototypeChain.get())) {
362 JSValue prototype = prototypeForLookup(exec);
363 m_cachedPrototypeChain.set(exec->globalData(), this, StructureChain::create(exec->globalData(), prototype.isNull() ? 0 : asObject(prototype)->structure()));
365 return m_cachedPrototypeChain.get();
368 inline bool Structure::isValid(ExecState* exec, StructureChain* cachedPrototypeChain) const
370 if (!cachedPrototypeChain)
373 JSValue prototype = prototypeForLookup(exec);
374 WriteBarrier<Structure>* cachedStructure = cachedPrototypeChain->head();
375 while(*cachedStructure && !prototype.isNull()) {
376 if (asObject(prototype)->structure() != cachedStructure->get())
379 prototype = asObject(prototype)->prototype();
381 return prototype.isNull() && !*cachedStructure;
384 inline JSGlobalObject* ExecState::dynamicGlobalObject()
386 if (this == lexicalGlobalObject()->globalExec())
387 return lexicalGlobalObject();
389 // For any ExecState that's not a globalExec, the
390 // dynamic global object must be set since code is running
391 ASSERT(globalData().dynamicGlobalObject);
392 return globalData().dynamicGlobalObject;
395 inline JSObject* constructEmptyObject(ExecState* exec, JSGlobalObject* globalObject)
397 return constructEmptyObject(exec, globalObject->emptyObjectStructure());
400 inline JSObject* constructEmptyObject(ExecState* exec)
402 return constructEmptyObject(exec, exec->lexicalGlobalObject());
405 inline JSArray* constructEmptyArray(ExecState* exec, JSGlobalObject* globalObject)
407 return JSArray::create(exec->globalData(), globalObject->arrayStructure());
410 inline JSArray* constructEmptyArray(ExecState* exec)
412 return constructEmptyArray(exec, exec->lexicalGlobalObject());
415 inline JSArray* constructEmptyArray(ExecState* exec, JSGlobalObject* globalObject, unsigned initialLength)
417 return JSArray::create(exec->globalData(), globalObject->arrayStructure(), initialLength, CreateInitialized);
420 inline JSArray* constructEmptyArray(ExecState* exec, unsigned initialLength)
422 return constructEmptyArray(exec, exec->lexicalGlobalObject(), initialLength);
425 inline JSArray* constructArray(ExecState* exec, JSGlobalObject* globalObject, JSValue singleItemValue)
427 MarkedArgumentBuffer values;
428 values.append(singleItemValue);
429 return JSArray::create(exec->globalData(), globalObject->arrayStructure(), values);
432 inline JSArray* constructArray(ExecState* exec, JSValue singleItemValue)
434 return constructArray(exec, exec->lexicalGlobalObject(), singleItemValue);
437 inline JSArray* constructArray(ExecState* exec, JSGlobalObject* globalObject, const ArgList& values)
439 return JSArray::create(exec->globalData(), globalObject->arrayStructure(), values);
442 inline JSArray* constructArray(ExecState* exec, const ArgList& values)
444 return constructArray(exec, exec->lexicalGlobalObject(), values);
447 class DynamicGlobalObjectScope {
448 WTF_MAKE_NONCOPYABLE(DynamicGlobalObjectScope);
450 DynamicGlobalObjectScope(JSGlobalData&, JSGlobalObject*);
452 ~DynamicGlobalObjectScope()
454 m_dynamicGlobalObjectSlot = m_savedDynamicGlobalObject;
458 JSGlobalObject*& m_dynamicGlobalObjectSlot;
459 JSGlobalObject* m_savedDynamicGlobalObject;
464 #endif // JSGlobalObject_h