source: webkit/trunk/JavaScriptCore/kjs/JSGlobalObject.h@ 36851

Last change on this file since 36851 was 36851, checked in by Darin Adler, 17 years ago

2008-09-24 Darin Adler <Darin Adler>

Reviewed by Sam Weinig.

About 1% on v8-raytrace.

  • kjs/JSActivation.cpp: Moved copyRegisters to the header to make it inline.
  • kjs/JSActivation.h: (JSC::JSActivation::copyRegisters): Moved here. Also removed the registerArraySize argument to setRegisters, since the object doesn't need to store the number of registers.
  • kjs/JSGlobalObject.cpp: (JSC::JSGlobalObject::reset): Removed unnecessary clearing left over from when we used this on objects that weren't brand new. These days, this function is really just part of the constructor.
  • kjs/JSGlobalObject.h: Added registerArraySize to JSGlobalObjectData, since JSVariableObjectData no longer needs it. Added a setRegisters override here that handles storing the size.
  • kjs/JSStaticScopeObject.h: Removed code to set registerArraySize, since it no longer exists.
  • kjs/JSVariableObject.cpp: Moved copyRegisterArray and setRegisters to the header to make them inline.
  • kjs/JSVariableObject.h: Removed registerArraySize from JSVariableObjectData, since it was only used for the global object. (JSC::JSVariableObject::copyRegisterArray): Moved here ot make it inline. (JSC::JSVariableObject::setRegisters): Moved here to make it inline. Also removed the code to set registerArraySize and changed an if statement into an assert to save an unnnecessary branch.
  • Property svn:eol-style set to native
File size: 13.9 KB
Line 
1/*
2 * Copyright (C) 2007 Eric Seidel <[email protected]>
3 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
4 *
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.
9 *
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.
14 *
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.
19 *
20 */
21
22#ifndef JSGlobalObject_h
23#define JSGlobalObject_h
24
25#include "JSGlobalData.h"
26#include "JSVariableObject.h"
27#include "NumberPrototype.h"
28#include "StringPrototype.h"
29#include <wtf/HashSet.h>
30#include <wtf/OwnPtr.h>
31
32namespace JSC {
33
34 class ArrayPrototype;
35 class BooleanPrototype;
36 class DatePrototype;
37 class Debugger;
38 class ErrorConstructor;
39 class FunctionPrototype;
40 class GlobalEvalFunction;
41 class NativeErrorConstructor;
42 class ProgramCodeBlock;
43 class RegExpConstructor;
44 class RegExpPrototype;
45 class RegisterFile;
46
47 struct ActivationStackNode;
48 struct HashTable;
49
50 typedef Vector<ExecState*, 16> ExecStateStack;
51
52 class JSGlobalObject : public JSVariableObject {
53 protected:
54 using JSVariableObject::JSVariableObjectData;
55
56 struct JSGlobalObjectData : public JSVariableObjectData {
57 JSGlobalObjectData(JSGlobalObject* globalObject, JSObject* thisValue)
58 : JSVariableObjectData(&symbolTable, 0)
59 , registerArraySize(0)
60 , globalScopeChain(globalObject, thisValue)
61 , regExpConstructor(0)
62 , errorConstructor(0)
63 , evalErrorConstructor(0)
64 , rangeErrorConstructor(0)
65 , referenceErrorConstructor(0)
66 , syntaxErrorConstructor(0)
67 , typeErrorConstructor(0)
68 , URIErrorConstructor(0)
69 , evalFunction(0)
70 , objectPrototype(0)
71 , functionPrototype(0)
72 , arrayPrototype(0)
73 , booleanPrototype(0)
74 , stringPrototype(0)
75 , numberPrototype(0)
76 , datePrototype(0)
77 , regExpPrototype(0)
78 {
79 }
80
81 virtual ~JSGlobalObjectData()
82 {
83 }
84
85 size_t registerArraySize;
86
87 JSGlobalObject* next;
88 JSGlobalObject* prev;
89
90 Debugger* debugger;
91
92 ScopeChain globalScopeChain;
93 OwnPtr<ExecState> globalExec;
94
95 int recursion;
96
97 RegExpConstructor* regExpConstructor;
98 ErrorConstructor* errorConstructor;
99 NativeErrorConstructor* evalErrorConstructor;
100 NativeErrorConstructor* rangeErrorConstructor;
101 NativeErrorConstructor* referenceErrorConstructor;
102 NativeErrorConstructor* syntaxErrorConstructor;
103 NativeErrorConstructor* typeErrorConstructor;
104 NativeErrorConstructor* URIErrorConstructor;
105
106 GlobalEvalFunction* evalFunction;
107
108 ObjectPrototype* objectPrototype;
109 FunctionPrototype* functionPrototype;
110 ArrayPrototype* arrayPrototype;
111 BooleanPrototype* booleanPrototype;
112 StringPrototype* stringPrototype;
113 NumberPrototype* numberPrototype;
114 DatePrototype* datePrototype;
115 RegExpPrototype* regExpPrototype;
116
117 RefPtr<StructureID> argumentsStructure;
118 RefPtr<StructureID> arrayStructure;
119 RefPtr<StructureID> booleanObjectStructure;
120 RefPtr<StructureID> callbackConstructorStructure;
121 RefPtr<StructureID> callbackFunctionStructure;
122 RefPtr<StructureID> callbackObjectStructure;
123 RefPtr<StructureID> dateStructure;
124 RefPtr<StructureID> emptyObjectStructure;
125 RefPtr<StructureID> errorStructure;
126 RefPtr<StructureID> functionStructure;
127 RefPtr<StructureID> numberObjectStructure;
128 RefPtr<StructureID> prototypeFunctionStructure;
129 RefPtr<StructureID> regExpMatchesArrayStructure;
130 RefPtr<StructureID> regExpStructure;
131 RefPtr<StructureID> stringObjectStructure;
132
133 SymbolTable symbolTable;
134 unsigned profileGroup;
135
136 RefPtr<JSGlobalData> globalData;
137
138 HashSet<ProgramCodeBlock*> codeBlocks;
139
140 OwnPtr<HashSet<JSObject*> > arrayVisitedElements; // Global data shared by array prototype functions.
141 };
142
143 public:
144 void* operator new(size_t, JSGlobalData*);
145
146 JSGlobalObject(JSGlobalData* globalData)
147 : JSVariableObject(globalData->nullProtoStructureID, new JSGlobalObjectData(this, this))
148 {
149 init(this);
150 }
151
152 protected:
153 JSGlobalObject(PassRefPtr<StructureID> structure, JSGlobalObjectData* data, JSObject* globalThisValue)
154 : JSVariableObject(structure, data)
155 {
156 init(globalThisValue);
157 }
158
159 public:
160 virtual ~JSGlobalObject();
161
162 virtual void mark();
163
164 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
165 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&, bool& slotIsWriteable);
166 virtual void put(ExecState*, const Identifier&, JSValue*, PutPropertySlot&);
167 virtual void putWithAttributes(ExecState*, const Identifier& propertyName, JSValue* value, unsigned attributes);
168
169 virtual void defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunc);
170 virtual void defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunc);
171
172 // Linked list of all global objects that use the same JSGlobalData.
173 JSGlobalObject*& head() { return d()->globalData->head; }
174 JSGlobalObject* next() { return d()->next; }
175
176 // The following accessors return pristine values, even if a script
177 // replaces the global object's associated property.
178
179 RegExpConstructor* regExpConstructor() const { return d()->regExpConstructor; }
180
181 ErrorConstructor* errorConstructor() const { return d()->errorConstructor; }
182 NativeErrorConstructor* evalErrorConstructor() const { return d()->evalErrorConstructor; }
183 NativeErrorConstructor* rangeErrorConstructor() const { return d()->rangeErrorConstructor; }
184 NativeErrorConstructor* referenceErrorConstructor() const { return d()->referenceErrorConstructor; }
185 NativeErrorConstructor* syntaxErrorConstructor() const { return d()->syntaxErrorConstructor; }
186 NativeErrorConstructor* typeErrorConstructor() const { return d()->typeErrorConstructor; }
187 NativeErrorConstructor* URIErrorConstructor() const { return d()->URIErrorConstructor; }
188
189 GlobalEvalFunction* evalFunction() const { return d()->evalFunction; }
190
191 ObjectPrototype* objectPrototype() const { return d()->objectPrototype; }
192 FunctionPrototype* functionPrototype() const { return d()->functionPrototype; }
193 ArrayPrototype* arrayPrototype() const { return d()->arrayPrototype; }
194 BooleanPrototype* booleanPrototype() const { return d()->booleanPrototype; }
195 StringPrototype* stringPrototype() const { return d()->stringPrototype; }
196 NumberPrototype* numberPrototype() const { return d()->numberPrototype; }
197 DatePrototype* datePrototype() const { return d()->datePrototype; }
198 RegExpPrototype* regExpPrototype() const { return d()->regExpPrototype; }
199
200 StructureID* argumentsStructure() const { return d()->argumentsStructure.get(); }
201 StructureID* arrayStructure() const { return d()->arrayStructure.get(); }
202 StructureID* booleanObjectStructure() const { return d()->booleanObjectStructure.get(); }
203 StructureID* callbackConstructorStructure() const { return d()->callbackConstructorStructure.get(); }
204 StructureID* callbackFunctionStructure() const { return d()->callbackFunctionStructure.get(); }
205 StructureID* callbackObjectStructure() const { return d()->callbackObjectStructure.get(); }
206 StructureID* dateStructure() const { return d()->dateStructure.get(); }
207 StructureID* emptyObjectStructure() const { return d()->emptyObjectStructure.get(); }
208 StructureID* errorStructure() const { return d()->errorStructure.get(); }
209 StructureID* functionStructure() const { return d()->functionStructure.get(); }
210 StructureID* numberObjectStructure() const { return d()->numberObjectStructure.get(); }
211 StructureID* prototypeFunctionStructure() const { return d()->prototypeFunctionStructure.get(); }
212 StructureID* regExpMatchesArrayStructure() const { return d()->regExpMatchesArrayStructure.get(); }
213 StructureID* regExpStructure() const { return d()->regExpStructure.get(); }
214 StructureID* stringObjectStructure() const { return d()->stringObjectStructure.get(); }
215
216 void setProfileGroup(unsigned value) { d()->profileGroup = value; }
217 unsigned profileGroup() const { return d()->profileGroup; }
218
219 void setTimeoutTime(unsigned timeoutTime);
220 void startTimeoutCheck();
221 void stopTimeoutCheck();
222
223 Debugger* debugger() const { return d()->debugger; }
224 void setDebugger(Debugger* debugger) { d()->debugger = debugger; }
225
226 int recursion() { return d()->recursion; }
227 void incRecursion() { ++d()->recursion; }
228 void decRecursion() { --d()->recursion; }
229
230 ScopeChain& globalScopeChain() { return d()->globalScopeChain; }
231
232 virtual bool isGlobalObject() const { return true; }
233 virtual JSGlobalObject* toGlobalObject(ExecState*) const;
234
235 virtual ExecState* globalExec();
236
237 virtual bool shouldInterruptScript() const { return true; }
238
239 virtual bool allowsAccessFrom(const JSGlobalObject*) const { return true; }
240
241 virtual bool isDynamicScope() const;
242
243 HashSet<JSObject*>& arrayVisitedElements() { if (!d()->arrayVisitedElements) d()->arrayVisitedElements.set(new HashSet<JSObject*>); return *d()->arrayVisitedElements; }
244
245 HashSet<ProgramCodeBlock*>& codeBlocks() { return d()->codeBlocks; }
246
247 void copyGlobalsFrom(RegisterFile&);
248 void copyGlobalsTo(RegisterFile&);
249
250 void resetPrototype(JSValue* prototype);
251
252 JSGlobalData* globalData() { return d()->globalData.get(); }
253 JSGlobalObjectData* d() const { return static_cast<JSGlobalObjectData*>(JSVariableObject::d); }
254
255 protected:
256 struct GlobalPropertyInfo {
257 GlobalPropertyInfo(const Identifier& i, JSValue* v, unsigned a)
258 : identifier(i)
259 , value(v)
260 , attributes(a)
261 {
262 }
263
264 const Identifier identifier;
265 JSValue* value;
266 unsigned attributes;
267 };
268 void addStaticGlobals(GlobalPropertyInfo*, int count);
269
270 private:
271 // FIXME: Fold these functions into the constructor.
272 void init(JSObject* thisValue);
273 void reset(JSValue* prototype);
274
275 void setRegisters(Register* registers, Register* registerArray, size_t count);
276
277 void* operator new(size_t); // can only be allocated with JSGlobalData
278 };
279
280 inline void JSGlobalObject::setRegisters(Register* registers, Register* registerArray, size_t count)
281 {
282 JSVariableObject::setRegisters(registers, registerArray);
283 d()->registerArraySize = count;
284 }
285
286 inline void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
287 {
288 size_t oldSize = d()->registerArraySize;
289 size_t newSize = oldSize + count;
290 Register* registerArray = new Register[newSize];
291 if (d()->registerArray)
292 memcpy(registerArray + count, d()->registerArray.get(), oldSize * sizeof(Register));
293 setRegisters(registerArray + newSize, registerArray, newSize);
294
295 for (int i = 0, index = -static_cast<int>(oldSize) - 1; i < count; ++i, --index) {
296 GlobalPropertyInfo& global = globals[i];
297 ASSERT(global.attributes & DontDelete);
298 SymbolTableEntry newEntry(index, global.attributes);
299 symbolTable().add(global.identifier.ustring().rep(), newEntry);
300 registerAt(index) = global.value;
301 }
302 }
303
304 inline bool JSGlobalObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
305 {
306 if (JSVariableObject::getOwnPropertySlot(exec, propertyName, slot))
307 return true;
308 return symbolTableGet(propertyName, slot);
309 }
310
311 inline bool JSGlobalObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot, bool& slotIsWriteable)
312 {
313 if (JSVariableObject::getOwnPropertySlotForWrite(exec, propertyName, slot, slotIsWriteable))
314 return true;
315 return symbolTableGet(propertyName, slot, slotIsWriteable);
316 }
317
318 inline JSGlobalObject* ScopeChainNode::globalObject() const
319 {
320 JSGlobalObject* globalObject = static_cast<JSGlobalObject*>(bottom());
321 ASSERT(globalObject->isGlobalObject());
322 return globalObject;
323 }
324
325 inline JSValue* StructureID::prototypeForLookup(ExecState* exec)
326 {
327 if (typeInfo().type() == ObjectType)
328 return m_prototype;
329
330 if (typeInfo().type() == StringType)
331 return exec->lexicalGlobalObject()->stringPrototype();
332
333 ASSERT(typeInfo().type() == NumberType);
334 return exec->lexicalGlobalObject()->numberPrototype();
335 }
336
337
338} // namespace JSC
339
340#endif // JSGlobalObject_h
Note: See TracBrowser for help on using the repository browser.