|
1 /* |
|
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org) |
|
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com) |
|
4 * Copyright (C) 2003, 2007, 2008 Apple Inc. All rights reserved. |
|
5 * |
|
6 * This library is free software; you can redistribute it and/or |
|
7 * modify it under the terms of the GNU Library General Public |
|
8 * License as published by the Free Software Foundation; either |
|
9 * version 2 of the License, or (at your option) any later version. |
|
10 * |
|
11 * This library is distributed in the hope that it will be useful, |
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 * Library General Public License for more details. |
|
15 * |
|
16 * You should have received a copy of the GNU Library General Public License |
|
17 * along with this library; see the file COPYING.LIB. If not, write to |
|
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
19 * Boston, MA 02110-1301, USA. |
|
20 * |
|
21 */ |
|
22 |
|
23 #ifndef CallFrame_h |
|
24 #define CallFrame_h |
|
25 |
|
26 #include "JSGlobalData.h" |
|
27 #include "MacroAssemblerCodeRef.h" |
|
28 #include "RegisterFile.h" |
|
29 #include "ScopeChain.h" |
|
30 |
|
31 namespace JSC { |
|
32 |
|
33 class Arguments; |
|
34 class JSActivation; |
|
35 class Interpreter; |
|
36 |
|
37 // Represents the current state of script execution. |
|
38 // Passed as the first argument to most functions. |
|
39 class ExecState : private Register { |
|
40 public: |
|
41 JSObject* callee() const { return this[RegisterFile::Callee].function(); } |
|
42 CodeBlock* codeBlock() const { return this[RegisterFile::CodeBlock].Register::codeBlock(); } |
|
43 ScopeChainNode* scopeChain() const |
|
44 { |
|
45 ASSERT(this[RegisterFile::ScopeChain].Register::scopeChain()); |
|
46 return this[RegisterFile::ScopeChain].Register::scopeChain(); |
|
47 } |
|
48 |
|
49 // Global object in which execution began. |
|
50 JSGlobalObject* dynamicGlobalObject(); |
|
51 |
|
52 // Global object in which the currently executing code was defined. |
|
53 // Differs from dynamicGlobalObject() during function calls across web browser frames. |
|
54 JSGlobalObject* lexicalGlobalObject() const |
|
55 { |
|
56 return scopeChain()->globalObject; |
|
57 } |
|
58 |
|
59 // Differs from lexicalGlobalObject because this will have DOM window shell rather than |
|
60 // the actual DOM window, which can't be "this" for security reasons. |
|
61 JSObject* globalThisValue() const |
|
62 { |
|
63 return scopeChain()->globalThis; |
|
64 } |
|
65 |
|
66 // FIXME: Elsewhere, we use JSGlobalData* rather than JSGlobalData&. |
|
67 // We should make this more uniform and either use a reference everywhere |
|
68 // or a pointer everywhere. |
|
69 JSGlobalData& globalData() const |
|
70 { |
|
71 ASSERT(scopeChain()->globalData); |
|
72 return *scopeChain()->globalData; |
|
73 } |
|
74 |
|
75 // Convenience functions for access to global data. |
|
76 // It takes a few memory references to get from a call frame to the global data |
|
77 // pointer, so these are inefficient, and should be used sparingly in new code. |
|
78 // But they're used in many places in legacy code, so they're not going away any time soon. |
|
79 |
|
80 void clearException() { globalData().exception = JSValue(); } |
|
81 JSValue exception() const { return globalData().exception; } |
|
82 JSValue* exceptionSlot() { return &globalData().exception; } |
|
83 bool hadException() const { return globalData().exception; } |
|
84 |
|
85 const CommonIdentifiers& propertyNames() const { return *globalData().propertyNames; } |
|
86 const MarkedArgumentBuffer& emptyList() const { return *globalData().emptyList; } |
|
87 Interpreter* interpreter() { return globalData().interpreter; } |
|
88 Heap* heap() { return &globalData().heap; } |
|
89 #ifndef NDEBUG |
|
90 void dumpCaller(); |
|
91 #endif |
|
92 static const HashTable* arrayTable(CallFrame* callFrame) { return callFrame->globalData().arrayTable; } |
|
93 static const HashTable* dateTable(CallFrame* callFrame) { return callFrame->globalData().dateTable; } |
|
94 static const HashTable* jsonTable(CallFrame* callFrame) { return callFrame->globalData().jsonTable; } |
|
95 static const HashTable* mathTable(CallFrame* callFrame) { return callFrame->globalData().mathTable; } |
|
96 static const HashTable* numberTable(CallFrame* callFrame) { return callFrame->globalData().numberTable; } |
|
97 static const HashTable* regExpTable(CallFrame* callFrame) { return callFrame->globalData().regExpTable; } |
|
98 static const HashTable* regExpConstructorTable(CallFrame* callFrame) { return callFrame->globalData().regExpConstructorTable; } |
|
99 static const HashTable* stringTable(CallFrame* callFrame) { return callFrame->globalData().stringTable; } |
|
100 |
|
101 static CallFrame* create(Register* callFrameBase) { return static_cast<CallFrame*>(callFrameBase); } |
|
102 Register* registers() { return this; } |
|
103 |
|
104 CallFrame& operator=(const Register& r) { *static_cast<Register*>(this) = r; return *this; } |
|
105 |
|
106 CallFrame* callerFrame() const { return this[RegisterFile::CallerFrame].callFrame(); } |
|
107 #if ENABLE(JIT) |
|
108 ReturnAddressPtr returnPC() const { return ReturnAddressPtr(this[RegisterFile::ReturnPC].vPC()); } |
|
109 #endif |
|
110 #if ENABLE(INTERPRETER) |
|
111 Instruction* returnVPC() const { return this[RegisterFile::ReturnPC].vPC(); } |
|
112 #endif |
|
113 |
|
114 void setCallerFrame(CallFrame* callerFrame) { static_cast<Register*>(this)[RegisterFile::CallerFrame] = callerFrame; } |
|
115 void setScopeChain(ScopeChainNode* scopeChain) { static_cast<Register*>(this)[RegisterFile::ScopeChain] = scopeChain; } |
|
116 |
|
117 ALWAYS_INLINE void init(CodeBlock* codeBlock, Instruction* vPC, ScopeChainNode* scopeChain, |
|
118 CallFrame* callerFrame, int argc, JSObject* callee) |
|
119 { |
|
120 ASSERT(callerFrame); // Use noCaller() rather than 0 for the outer host call frame caller. |
|
121 ASSERT(callerFrame == noCaller() || callerFrame->removeHostCallFrameFlag()->registerFile()->end() >= this); |
|
122 |
|
123 setCodeBlock(codeBlock); |
|
124 setScopeChain(scopeChain); |
|
125 setCallerFrame(callerFrame); |
|
126 setReturnPC(vPC); // This is either an Instruction* or a pointer into JIT generated code stored as an Instruction*. |
|
127 setArgumentCountIncludingThis(argc); // original argument count (for the sake of the "arguments" object) |
|
128 setCallee(callee); |
|
129 } |
|
130 |
|
131 // Read a register from the codeframe (or constant from the CodeBlock). |
|
132 inline Register& r(int); |
|
133 |
|
134 // Access to arguments. |
|
135 int hostThisRegister() { return -RegisterFile::CallFrameHeaderSize - argumentCountIncludingThis(); } |
|
136 JSValue hostThisValue() { return this[hostThisRegister()].jsValue(); } |
|
137 size_t argumentCount() const { return argumentCountIncludingThis() - 1; } |
|
138 size_t argumentCountIncludingThis() const { return this[RegisterFile::ArgumentCount].i(); } |
|
139 JSValue argument(int argumentNumber) |
|
140 { |
|
141 int argumentIndex = -RegisterFile::CallFrameHeaderSize - this[RegisterFile::ArgumentCount].i() + argumentNumber + 1; |
|
142 if (argumentIndex >= -RegisterFile::CallFrameHeaderSize) |
|
143 return jsUndefined(); |
|
144 return this[argumentIndex].jsValue(); |
|
145 } |
|
146 |
|
147 static CallFrame* noCaller() { return reinterpret_cast<CallFrame*>(HostCallFrameFlag); } |
|
148 |
|
149 bool hasHostCallFrameFlag() const { return reinterpret_cast<intptr_t>(this) & HostCallFrameFlag; } |
|
150 CallFrame* addHostCallFrameFlag() const { return reinterpret_cast<CallFrame*>(reinterpret_cast<intptr_t>(this) | HostCallFrameFlag); } |
|
151 CallFrame* removeHostCallFrameFlag() { return reinterpret_cast<CallFrame*>(reinterpret_cast<intptr_t>(this) & ~HostCallFrameFlag); } |
|
152 |
|
153 void setArgumentCountIncludingThis(int count) { static_cast<Register*>(this)[RegisterFile::ArgumentCount] = Register::withInt(count); } |
|
154 void setCallee(JSObject* callee) { static_cast<Register*>(this)[RegisterFile::Callee] = Register::withCallee(callee); } |
|
155 void setCodeBlock(CodeBlock* codeBlock) { static_cast<Register*>(this)[RegisterFile::CodeBlock] = codeBlock; } |
|
156 void setReturnPC(void* value) { static_cast<Register*>(this)[RegisterFile::ReturnPC] = (Instruction*)value; } |
|
157 |
|
158 private: |
|
159 static const intptr_t HostCallFrameFlag = 1; |
|
160 #ifndef NDEBUG |
|
161 RegisterFile* registerFile(); |
|
162 #endif |
|
163 ExecState(); |
|
164 ~ExecState(); |
|
165 }; |
|
166 |
|
167 } // namespace JSC |
|
168 |
|
169 #endif // CallFrame_h |