1 | /*
|
---|
2 | * Copyright (C) 2007 Eric Seidel <[email protected]>
|
---|
3 | * Copyright (C) 2007, 2008, 2009 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 "NativeFunctionWrapper.h"
|
---|
28 | #include "NumberPrototype.h"
|
---|
29 | #include "StringPrototype.h"
|
---|
30 | #include <wtf/HashSet.h>
|
---|
31 | #include <wtf/OwnPtr.h>
|
---|
32 |
|
---|
33 | namespace JSC {
|
---|
34 |
|
---|
35 | class ArrayPrototype;
|
---|
36 | class BooleanPrototype;
|
---|
37 | class DatePrototype;
|
---|
38 | class Debugger;
|
---|
39 | class ErrorConstructor;
|
---|
40 | class FunctionPrototype;
|
---|
41 | class GlobalCodeBlock;
|
---|
42 | class GlobalEvalFunction;
|
---|
43 | class NativeErrorConstructor;
|
---|
44 | class ProgramCodeBlock;
|
---|
45 | class PrototypeFunction;
|
---|
46 | class RegExpConstructor;
|
---|
47 | class RegExpPrototype;
|
---|
48 | class RegisterFile;
|
---|
49 |
|
---|
50 | struct ActivationStackNode;
|
---|
51 | struct HashTable;
|
---|
52 |
|
---|
53 | typedef Vector<ExecState*, 16> ExecStateStack;
|
---|
54 |
|
---|
55 | class JSGlobalObject : public JSVariableObject {
|
---|
56 | protected:
|
---|
57 | using JSVariableObject::JSVariableObjectData;
|
---|
58 |
|
---|
59 | struct JSGlobalObjectData : public JSVariableObjectData {
|
---|
60 | JSGlobalObjectData()
|
---|
61 | : JSVariableObjectData(&symbolTable, 0)
|
---|
62 | , registerArraySize(0)
|
---|
63 | , globalScopeChain(NoScopeChain())
|
---|
64 | , regExpConstructor(0)
|
---|
65 | , errorConstructor(0)
|
---|
66 | , evalErrorConstructor(0)
|
---|
67 | , rangeErrorConstructor(0)
|
---|
68 | , referenceErrorConstructor(0)
|
---|
69 | , syntaxErrorConstructor(0)
|
---|
70 | , typeErrorConstructor(0)
|
---|
71 | , URIErrorConstructor(0)
|
---|
72 | , evalFunction(0)
|
---|
73 | , callFunction(0)
|
---|
74 | , applyFunction(0)
|
---|
75 | , objectPrototype(0)
|
---|
76 | , functionPrototype(0)
|
---|
77 | , arrayPrototype(0)
|
---|
78 | , booleanPrototype(0)
|
---|
79 | , stringPrototype(0)
|
---|
80 | , numberPrototype(0)
|
---|
81 | , datePrototype(0)
|
---|
82 | , regExpPrototype(0)
|
---|
83 | , methodCallDummy(0)
|
---|
84 | {
|
---|
85 | }
|
---|
86 |
|
---|
87 | virtual ~JSGlobalObjectData()
|
---|
88 | {
|
---|
89 | }
|
---|
90 |
|
---|
91 | size_t registerArraySize;
|
---|
92 |
|
---|
93 | JSGlobalObject* next;
|
---|
94 | JSGlobalObject* prev;
|
---|
95 |
|
---|
96 | Debugger* debugger;
|
---|
97 |
|
---|
98 | ScopeChain globalScopeChain;
|
---|
99 | Register globalCallFrame[RegisterFile::CallFrameHeaderSize];
|
---|
100 |
|
---|
101 | int recursion;
|
---|
102 |
|
---|
103 | RegExpConstructor* regExpConstructor;
|
---|
104 | ErrorConstructor* errorConstructor;
|
---|
105 | NativeErrorConstructor* evalErrorConstructor;
|
---|
106 | NativeErrorConstructor* rangeErrorConstructor;
|
---|
107 | NativeErrorConstructor* referenceErrorConstructor;
|
---|
108 | NativeErrorConstructor* syntaxErrorConstructor;
|
---|
109 | NativeErrorConstructor* typeErrorConstructor;
|
---|
110 | NativeErrorConstructor* URIErrorConstructor;
|
---|
111 |
|
---|
112 | GlobalEvalFunction* evalFunction;
|
---|
113 | NativeFunctionWrapper* callFunction;
|
---|
114 | NativeFunctionWrapper* applyFunction;
|
---|
115 |
|
---|
116 | ObjectPrototype* objectPrototype;
|
---|
117 | FunctionPrototype* functionPrototype;
|
---|
118 | ArrayPrototype* arrayPrototype;
|
---|
119 | BooleanPrototype* booleanPrototype;
|
---|
120 | StringPrototype* stringPrototype;
|
---|
121 | NumberPrototype* numberPrototype;
|
---|
122 | DatePrototype* datePrototype;
|
---|
123 | RegExpPrototype* regExpPrototype;
|
---|
124 |
|
---|
125 | JSObject* methodCallDummy;
|
---|
126 |
|
---|
127 | RefPtr<Structure> argumentsStructure;
|
---|
128 | RefPtr<Structure> arrayStructure;
|
---|
129 | RefPtr<Structure> booleanObjectStructure;
|
---|
130 | RefPtr<Structure> callbackConstructorStructure;
|
---|
131 | RefPtr<Structure> callbackFunctionStructure;
|
---|
132 | RefPtr<Structure> callbackObjectStructure;
|
---|
133 | RefPtr<Structure> dateStructure;
|
---|
134 | RefPtr<Structure> emptyObjectStructure;
|
---|
135 | RefPtr<Structure> errorStructure;
|
---|
136 | RefPtr<Structure> functionStructure;
|
---|
137 | RefPtr<Structure> numberObjectStructure;
|
---|
138 | RefPtr<Structure> prototypeFunctionStructure;
|
---|
139 | RefPtr<Structure> regExpMatchesArrayStructure;
|
---|
140 | RefPtr<Structure> regExpStructure;
|
---|
141 | RefPtr<Structure> stringObjectStructure;
|
---|
142 |
|
---|
143 | SymbolTable symbolTable;
|
---|
144 | unsigned profileGroup;
|
---|
145 |
|
---|
146 | RefPtr<JSGlobalData> globalData;
|
---|
147 |
|
---|
148 | HashSet<GlobalCodeBlock*> codeBlocks;
|
---|
149 | };
|
---|
150 |
|
---|
151 | public:
|
---|
152 | void* operator new(size_t, JSGlobalData*);
|
---|
153 |
|
---|
154 | explicit JSGlobalObject()
|
---|
155 | : JSVariableObject(JSGlobalObject::createStructure(jsNull()), new JSGlobalObjectData)
|
---|
156 | {
|
---|
157 | init(this);
|
---|
158 | }
|
---|
159 |
|
---|
160 | protected:
|
---|
161 | JSGlobalObject(PassRefPtr<Structure> structure, JSGlobalObjectData* data, JSObject* thisValue)
|
---|
162 | : JSVariableObject(structure, data)
|
---|
163 | {
|
---|
164 | init(thisValue);
|
---|
165 | }
|
---|
166 |
|
---|
167 | public:
|
---|
168 | virtual ~JSGlobalObject();
|
---|
169 |
|
---|
170 | virtual void markChildren(MarkStack&);
|
---|
171 |
|
---|
172 | virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
|
---|
173 | virtual bool hasOwnPropertyForWrite(ExecState*, const Identifier&);
|
---|
174 | virtual void put(ExecState*, const Identifier&, JSValue, PutPropertySlot&);
|
---|
175 | virtual void putWithAttributes(ExecState*, const Identifier& propertyName, JSValue value, unsigned attributes);
|
---|
176 |
|
---|
177 | virtual void defineGetter(ExecState*, const Identifier& propertyName, JSObject* getterFunc);
|
---|
178 | virtual void defineSetter(ExecState*, const Identifier& propertyName, JSObject* setterFunc);
|
---|
179 |
|
---|
180 | // Linked list of all global objects that use the same JSGlobalData.
|
---|
181 | JSGlobalObject*& head() { return d()->globalData->head; }
|
---|
182 | JSGlobalObject* next() { return d()->next; }
|
---|
183 |
|
---|
184 | // The following accessors return pristine values, even if a script
|
---|
185 | // replaces the global object's associated property.
|
---|
186 |
|
---|
187 | RegExpConstructor* regExpConstructor() const { return d()->regExpConstructor; }
|
---|
188 |
|
---|
189 | ErrorConstructor* errorConstructor() const { return d()->errorConstructor; }
|
---|
190 | NativeErrorConstructor* evalErrorConstructor() const { return d()->evalErrorConstructor; }
|
---|
191 | NativeErrorConstructor* rangeErrorConstructor() const { return d()->rangeErrorConstructor; }
|
---|
192 | NativeErrorConstructor* referenceErrorConstructor() const { return d()->referenceErrorConstructor; }
|
---|
193 | NativeErrorConstructor* syntaxErrorConstructor() const { return d()->syntaxErrorConstructor; }
|
---|
194 | NativeErrorConstructor* typeErrorConstructor() const { return d()->typeErrorConstructor; }
|
---|
195 | NativeErrorConstructor* URIErrorConstructor() const { return d()->URIErrorConstructor; }
|
---|
196 |
|
---|
197 | GlobalEvalFunction* evalFunction() const { return d()->evalFunction; }
|
---|
198 |
|
---|
199 | ObjectPrototype* objectPrototype() const { return d()->objectPrototype; }
|
---|
200 | FunctionPrototype* functionPrototype() const { return d()->functionPrototype; }
|
---|
201 | ArrayPrototype* arrayPrototype() const { return d()->arrayPrototype; }
|
---|
202 | BooleanPrototype* booleanPrototype() const { return d()->booleanPrototype; }
|
---|
203 | StringPrototype* stringPrototype() const { return d()->stringPrototype; }
|
---|
204 | NumberPrototype* numberPrototype() const { return d()->numberPrototype; }
|
---|
205 | DatePrototype* datePrototype() const { return d()->datePrototype; }
|
---|
206 | RegExpPrototype* regExpPrototype() const { return d()->regExpPrototype; }
|
---|
207 |
|
---|
208 | JSObject* methodCallDummy() const { return d()->methodCallDummy; }
|
---|
209 |
|
---|
210 | Structure* argumentsStructure() const { return d()->argumentsStructure.get(); }
|
---|
211 | Structure* arrayStructure() const { return d()->arrayStructure.get(); }
|
---|
212 | Structure* booleanObjectStructure() const { return d()->booleanObjectStructure.get(); }
|
---|
213 | Structure* callbackConstructorStructure() const { return d()->callbackConstructorStructure.get(); }
|
---|
214 | Structure* callbackFunctionStructure() const { return d()->callbackFunctionStructure.get(); }
|
---|
215 | Structure* callbackObjectStructure() const { return d()->callbackObjectStructure.get(); }
|
---|
216 | Structure* dateStructure() const { return d()->dateStructure.get(); }
|
---|
217 | Structure* emptyObjectStructure() const { return d()->emptyObjectStructure.get(); }
|
---|
218 | Structure* errorStructure() const { return d()->errorStructure.get(); }
|
---|
219 | Structure* functionStructure() const { return d()->functionStructure.get(); }
|
---|
220 | Structure* numberObjectStructure() const { return d()->numberObjectStructure.get(); }
|
---|
221 | Structure* prototypeFunctionStructure() const { return d()->prototypeFunctionStructure.get(); }
|
---|
222 | Structure* regExpMatchesArrayStructure() const { return d()->regExpMatchesArrayStructure.get(); }
|
---|
223 | Structure* regExpStructure() const { return d()->regExpStructure.get(); }
|
---|
224 | Structure* stringObjectStructure() const { return d()->stringObjectStructure.get(); }
|
---|
225 |
|
---|
226 | void setProfileGroup(unsigned value) { d()->profileGroup = value; }
|
---|
227 | unsigned profileGroup() const { return d()->profileGroup; }
|
---|
228 |
|
---|
229 | Debugger* debugger() const { return d()->debugger; }
|
---|
230 | void setDebugger(Debugger* debugger) { d()->debugger = debugger; }
|
---|
231 |
|
---|
232 | virtual bool supportsProfiling() const { return false; }
|
---|
233 |
|
---|
234 | int recursion() { return d()->recursion; }
|
---|
235 | void incRecursion() { ++d()->recursion; }
|
---|
236 | void decRecursion() { --d()->recursion; }
|
---|
237 |
|
---|
238 | ScopeChain& globalScopeChain() { return d()->globalScopeChain; }
|
---|
239 |
|
---|
240 | virtual bool isGlobalObject() const { return true; }
|
---|
241 |
|
---|
242 | virtual ExecState* globalExec();
|
---|
243 |
|
---|
244 | virtual bool shouldInterruptScript() const { return true; }
|
---|
245 |
|
---|
246 | virtual bool allowsAccessFrom(const JSGlobalObject*) const { return true; }
|
---|
247 |
|
---|
248 | virtual bool isDynamicScope() const;
|
---|
249 |
|
---|
250 | HashSet<GlobalCodeBlock*>& codeBlocks() { return d()->codeBlocks; }
|
---|
251 |
|
---|
252 | void copyGlobalsFrom(RegisterFile&);
|
---|
253 | void copyGlobalsTo(RegisterFile&);
|
---|
254 |
|
---|
255 | void resetPrototype(JSValue prototype);
|
---|
256 |
|
---|
257 | JSGlobalData* globalData() { return d()->globalData.get(); }
|
---|
258 | JSGlobalObjectData* d() const { return static_cast<JSGlobalObjectData*>(JSVariableObject::d); }
|
---|
259 |
|
---|
260 | static PassRefPtr<Structure> createStructure(JSValue prototype)
|
---|
261 | {
|
---|
262 | return Structure::create(prototype, TypeInfo(ObjectType));
|
---|
263 | }
|
---|
264 |
|
---|
265 | protected:
|
---|
266 | struct GlobalPropertyInfo {
|
---|
267 | GlobalPropertyInfo(const Identifier& i, JSValue v, unsigned a)
|
---|
268 | : identifier(i)
|
---|
269 | , value(v)
|
---|
270 | , attributes(a)
|
---|
271 | {
|
---|
272 | }
|
---|
273 |
|
---|
274 | const Identifier identifier;
|
---|
275 | JSValue value;
|
---|
276 | unsigned attributes;
|
---|
277 | };
|
---|
278 | void addStaticGlobals(GlobalPropertyInfo*, int count);
|
---|
279 |
|
---|
280 | private:
|
---|
281 | // FIXME: Fold reset into init.
|
---|
282 | void init(JSObject* thisValue);
|
---|
283 | void reset(JSValue prototype);
|
---|
284 |
|
---|
285 | void setRegisters(Register* registers, Register* registerArray, size_t count);
|
---|
286 |
|
---|
287 | void* operator new(size_t); // can only be allocated with JSGlobalData
|
---|
288 | };
|
---|
289 |
|
---|
290 | JSGlobalObject* asGlobalObject(JSValue);
|
---|
291 |
|
---|
292 | inline JSGlobalObject* asGlobalObject(JSValue value)
|
---|
293 | {
|
---|
294 | ASSERT(asObject(value)->isGlobalObject());
|
---|
295 | return static_cast<JSGlobalObject*>(asObject(value));
|
---|
296 | }
|
---|
297 |
|
---|
298 | inline void JSGlobalObject::setRegisters(Register* registers, Register* registerArray, size_t count)
|
---|
299 | {
|
---|
300 | JSVariableObject::setRegisters(registers, registerArray);
|
---|
301 | d()->registerArraySize = count;
|
---|
302 | }
|
---|
303 |
|
---|
304 | inline void JSGlobalObject::addStaticGlobals(GlobalPropertyInfo* globals, int count)
|
---|
305 | {
|
---|
306 | size_t oldSize = d()->registerArraySize;
|
---|
307 | size_t newSize = oldSize + count;
|
---|
308 | Register* registerArray = new Register[newSize];
|
---|
309 | if (d()->registerArray)
|
---|
310 | memcpy(registerArray + count, d()->registerArray.get(), oldSize * sizeof(Register));
|
---|
311 | setRegisters(registerArray + newSize, registerArray, newSize);
|
---|
312 |
|
---|
313 | for (int i = 0, index = -static_cast<int>(oldSize) - 1; i < count; ++i, --index) {
|
---|
314 | GlobalPropertyInfo& global = globals[i];
|
---|
315 | ASSERT(global.attributes & DontDelete);
|
---|
316 | SymbolTableEntry newEntry(index, global.attributes);
|
---|
317 | symbolTable().add(global.identifier.ustring().rep(), newEntry);
|
---|
318 | registerAt(index) = global.value;
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | inline bool JSGlobalObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
|
---|
323 | {
|
---|
324 | if (JSVariableObject::getOwnPropertySlot(exec, propertyName, slot))
|
---|
325 | return true;
|
---|
326 | return symbolTableGet(propertyName, slot);
|
---|
327 | }
|
---|
328 |
|
---|
329 | inline bool JSGlobalObject::hasOwnPropertyForWrite(ExecState* exec, const Identifier& propertyName)
|
---|
330 | {
|
---|
331 | PropertySlot slot;
|
---|
332 | if (JSVariableObject::getOwnPropertySlot(exec, propertyName, slot))
|
---|
333 | return true;
|
---|
334 | bool slotIsWriteable;
|
---|
335 | return symbolTableGet(propertyName, slot, slotIsWriteable);
|
---|
336 | }
|
---|
337 |
|
---|
338 | inline JSGlobalObject* ScopeChainNode::globalObject() const
|
---|
339 | {
|
---|
340 | const ScopeChainNode* n = this;
|
---|
341 | while (n->next)
|
---|
342 | n = n->next;
|
---|
343 | return asGlobalObject(n->object);
|
---|
344 | }
|
---|
345 |
|
---|
346 | inline JSValue Structure::prototypeForLookup(ExecState* exec) const
|
---|
347 | {
|
---|
348 | if (typeInfo().type() == ObjectType)
|
---|
349 | return m_prototype;
|
---|
350 |
|
---|
351 | #if USE(JSVALUE32)
|
---|
352 | if (typeInfo().type() == StringType)
|
---|
353 | return exec->lexicalGlobalObject()->stringPrototype();
|
---|
354 |
|
---|
355 | ASSERT(typeInfo().type() == NumberType);
|
---|
356 | return exec->lexicalGlobalObject()->numberPrototype();
|
---|
357 | #else
|
---|
358 | ASSERT(typeInfo().type() == StringType);
|
---|
359 | return exec->lexicalGlobalObject()->stringPrototype();
|
---|
360 | #endif
|
---|
361 | }
|
---|
362 |
|
---|
363 | inline StructureChain* Structure::prototypeChain(ExecState* exec) const
|
---|
364 | {
|
---|
365 | // We cache our prototype chain so our clients can share it.
|
---|
366 | if (!isValid(exec, m_cachedPrototypeChain.get())) {
|
---|
367 | JSValue prototype = prototypeForLookup(exec);
|
---|
368 | m_cachedPrototypeChain = StructureChain::create(prototype.isNull() ? 0 : asObject(prototype)->structure());
|
---|
369 | }
|
---|
370 | return m_cachedPrototypeChain.get();
|
---|
371 | }
|
---|
372 |
|
---|
373 | inline bool Structure::isValid(ExecState* exec, StructureChain* cachedPrototypeChain) const
|
---|
374 | {
|
---|
375 | if (!cachedPrototypeChain)
|
---|
376 | return false;
|
---|
377 |
|
---|
378 | JSValue prototype = prototypeForLookup(exec);
|
---|
379 | RefPtr<Structure>* cachedStructure = cachedPrototypeChain->head();
|
---|
380 | while(*cachedStructure && !prototype.isNull()) {
|
---|
381 | if (asObject(prototype)->structure() != *cachedStructure)
|
---|
382 | return false;
|
---|
383 | ++cachedStructure;
|
---|
384 | prototype = asObject(prototype)->prototype();
|
---|
385 | }
|
---|
386 | return prototype.isNull() && !*cachedStructure;
|
---|
387 | }
|
---|
388 |
|
---|
389 | inline JSGlobalObject* ExecState::dynamicGlobalObject()
|
---|
390 | {
|
---|
391 | if (this == lexicalGlobalObject()->globalExec())
|
---|
392 | return lexicalGlobalObject();
|
---|
393 |
|
---|
394 | // For any ExecState that's not a globalExec, the
|
---|
395 | // dynamic global object must be set since code is running
|
---|
396 | ASSERT(globalData().dynamicGlobalObject);
|
---|
397 | return globalData().dynamicGlobalObject;
|
---|
398 | }
|
---|
399 |
|
---|
400 | class DynamicGlobalObjectScope : public Noncopyable {
|
---|
401 | public:
|
---|
402 | DynamicGlobalObjectScope(CallFrame* callFrame, JSGlobalObject* dynamicGlobalObject)
|
---|
403 | : m_dynamicGlobalObjectSlot(callFrame->globalData().dynamicGlobalObject)
|
---|
404 | , m_savedDynamicGlobalObject(m_dynamicGlobalObjectSlot)
|
---|
405 | {
|
---|
406 | m_dynamicGlobalObjectSlot = dynamicGlobalObject;
|
---|
407 | }
|
---|
408 |
|
---|
409 | ~DynamicGlobalObjectScope()
|
---|
410 | {
|
---|
411 | m_dynamicGlobalObjectSlot = m_savedDynamicGlobalObject;
|
---|
412 | }
|
---|
413 |
|
---|
414 | private:
|
---|
415 | JSGlobalObject*& m_dynamicGlobalObjectSlot;
|
---|
416 | JSGlobalObject* m_savedDynamicGlobalObject;
|
---|
417 | };
|
---|
418 |
|
---|
419 | } // namespace JSC
|
---|
420 |
|
---|
421 | #endif // JSGlobalObject_h
|
---|