tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / interpreter / CallFrame.h
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, 2011 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
30 namespace JSC  {
31
32     class Arguments;
33     class JSActivation;
34     class Interpreter;
35     class ScopeChainNode;
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         JSValue calleeAsValue() const { return this[RegisterFile::Callee].jsValue(); }
42         JSObject* callee() const { return this[RegisterFile::Callee].function(); }
43         CodeBlock* codeBlock() const { return this[RegisterFile::CodeBlock].Register::codeBlock(); }
44         ScopeChainNode* scopeChain() const
45         {
46             ASSERT(this[RegisterFile::ScopeChain].Register::scopeChain());
47             return this[RegisterFile::ScopeChain].Register::scopeChain();
48         }
49
50         // Global object in which execution began.
51         JSGlobalObject* dynamicGlobalObject();
52
53         // Global object in which the currently executing code was defined.
54         // Differs from dynamicGlobalObject() during function calls across web browser frames.
55         inline JSGlobalObject* lexicalGlobalObject() const;
56
57         // Differs from lexicalGlobalObject because this will have DOM window shell rather than
58         // the actual DOM window, which can't be "this" for security reasons.
59         inline JSObject* globalThisValue() const;
60
61         inline JSGlobalData& globalData() const;
62
63         // Convenience functions for access to global data.
64         // It takes a few memory references to get from a call frame to the global data
65         // pointer, so these are inefficient, and should be used sparingly in new code.
66         // But they're used in many places in legacy code, so they're not going away any time soon.
67
68         void clearException() { globalData().exception = JSValue(); }
69         JSValue exception() const { return globalData().exception; }
70         bool hadException() const { return globalData().exception; }
71
72         const CommonIdentifiers& propertyNames() const { return *globalData().propertyNames; }
73         const MarkedArgumentBuffer& emptyList() const { return *globalData().emptyList; }
74         Interpreter* interpreter() { return globalData().interpreter; }
75         Heap* heap() { return &globalData().heap; }
76 #ifndef NDEBUG
77         void dumpCaller();
78 #endif
79         static const HashTable* arrayConstructorTable(CallFrame* callFrame) { return callFrame->globalData().arrayConstructorTable; }
80         static const HashTable* arrayPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().arrayPrototypeTable; }
81         static const HashTable* booleanPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().booleanPrototypeTable; }
82         static const HashTable* dateTable(CallFrame* callFrame) { return callFrame->globalData().dateTable; }
83         static const HashTable* dateConstructorTable(CallFrame* callFrame) { return callFrame->globalData().dateConstructorTable; }
84         static const HashTable* errorPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().errorPrototypeTable; }
85         static const HashTable* globalObjectTable(CallFrame* callFrame) { return callFrame->globalData().globalObjectTable; }
86         static const HashTable* jsonTable(CallFrame* callFrame) { return callFrame->globalData().jsonTable; }
87         static const HashTable* mathTable(CallFrame* callFrame) { return callFrame->globalData().mathTable; }
88         static const HashTable* numberConstructorTable(CallFrame* callFrame) { return callFrame->globalData().numberConstructorTable; }
89         static const HashTable* numberPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().numberPrototypeTable; }
90         static const HashTable* objectConstructorTable(CallFrame* callFrame) { return callFrame->globalData().objectConstructorTable; }
91         static const HashTable* objectPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().objectPrototypeTable; }
92         static const HashTable* regExpTable(CallFrame* callFrame) { return callFrame->globalData().regExpTable; }
93         static const HashTable* regExpConstructorTable(CallFrame* callFrame) { return callFrame->globalData().regExpConstructorTable; }
94         static const HashTable* regExpPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().regExpPrototypeTable; }
95         static const HashTable* stringTable(CallFrame* callFrame) { return callFrame->globalData().stringTable; }
96         static const HashTable* stringConstructorTable(CallFrame* callFrame) { return callFrame->globalData().stringConstructorTable; }
97
98         static CallFrame* create(Register* callFrameBase) { return static_cast<CallFrame*>(callFrameBase); }
99         Register* registers() { return this; }
100
101         CallFrame& operator=(const Register& r) { *static_cast<Register*>(this) = r; return *this; }
102
103         CallFrame* callerFrame() const { return this[RegisterFile::CallerFrame].callFrame(); }
104 #if ENABLE(JIT)
105         ReturnAddressPtr returnPC() const { return ReturnAddressPtr(this[RegisterFile::ReturnPC].vPC()); }
106 #endif
107 #if ENABLE(DFG_JIT)
108         InlineCallFrame* inlineCallFrame() const { return this[RegisterFile::ReturnPC].asInlineCallFrame(); }
109 #else
110         // This will never be called if !ENABLE(DFG_JIT) since all calls should be guarded by
111         // isInlineCallFrame(). But to make it easier to write code without having a bunch of
112         // #if's, we make a dummy implementation available anyway.
113         InlineCallFrame* inlineCallFrame() const
114         {
115             ASSERT_NOT_REACHED();
116             return 0;
117         }
118 #endif
119 #if ENABLE(INTERPRETER)
120         Instruction* returnVPC() const { return this[RegisterFile::ReturnPC].vPC(); }
121 #endif
122
123         void setCallerFrame(CallFrame* callerFrame) { static_cast<Register*>(this)[RegisterFile::CallerFrame] = callerFrame; }
124         void setScopeChain(ScopeChainNode* scopeChain) { static_cast<Register*>(this)[RegisterFile::ScopeChain] = scopeChain; }
125
126         ALWAYS_INLINE void init(CodeBlock* codeBlock, Instruction* vPC, ScopeChainNode* scopeChain,
127             CallFrame* callerFrame, int argc, JSObject* callee)
128         {
129             ASSERT(callerFrame); // Use noCaller() rather than 0 for the outer host call frame caller.
130             ASSERT(callerFrame == noCaller() || callerFrame->removeHostCallFrameFlag()->registerFile()->end() >= this);
131
132             setCodeBlock(codeBlock);
133             setScopeChain(scopeChain);
134             setCallerFrame(callerFrame);
135             setReturnPC(vPC); // This is either an Instruction* or a pointer into JIT generated code stored as an Instruction*.
136             setArgumentCountIncludingThis(argc); // original argument count (for the sake of the "arguments" object)
137             setCallee(callee);
138         }
139
140         // Read a register from the codeframe (or constant from the CodeBlock).
141         inline Register& r(int);
142         // Read a register for a non-constant 
143         inline Register& uncheckedR(int);
144
145         // Access to arguments.
146         int hostThisRegister() { return -RegisterFile::CallFrameHeaderSize - argumentCountIncludingThis(); }
147         JSValue hostThisValue() { return this[hostThisRegister()].jsValue(); }
148         size_t argumentCount() const { return argumentCountIncludingThis() - 1; }
149         size_t argumentCountIncludingThis() const { return this[RegisterFile::ArgumentCount].i(); }
150         JSValue argument(int argumentNumber)
151         {
152             int argumentIndex = -RegisterFile::CallFrameHeaderSize - this[RegisterFile::ArgumentCount].i() + argumentNumber + 1;
153             if (argumentIndex >= -RegisterFile::CallFrameHeaderSize)
154                 return jsUndefined();
155             return this[argumentIndex].jsValue();
156         }
157
158         static CallFrame* noCaller() { return reinterpret_cast<CallFrame*>(HostCallFrameFlag); }
159
160         bool hasHostCallFrameFlag() const { return reinterpret_cast<intptr_t>(this) & HostCallFrameFlag; }
161         CallFrame* addHostCallFrameFlag() const { return reinterpret_cast<CallFrame*>(reinterpret_cast<intptr_t>(this) | HostCallFrameFlag); }
162         CallFrame* removeHostCallFrameFlag() { return reinterpret_cast<CallFrame*>(reinterpret_cast<intptr_t>(this) & ~HostCallFrameFlag); }
163
164         void setArgumentCountIncludingThis(int count) { static_cast<Register*>(this)[RegisterFile::ArgumentCount] = Register::withInt(count); }
165         void setCallee(JSObject* callee) { static_cast<Register*>(this)[RegisterFile::Callee] = Register::withCallee(callee); }
166         void setCodeBlock(CodeBlock* codeBlock) { static_cast<Register*>(this)[RegisterFile::CodeBlock] = codeBlock; }
167         void setReturnPC(void* value) { static_cast<Register*>(this)[RegisterFile::ReturnPC] = (Instruction*)value; }
168         
169 #if ENABLE(DFG_JIT)
170         bool isInlineCallFrame();
171         
172         void setInlineCallFrame(InlineCallFrame* inlineCallFrame) { static_cast<Register*>(this)[RegisterFile::ReturnPC] = inlineCallFrame; }
173         
174         // Call this to get the semantically correct JS CallFrame*. This resolves issues
175         // surrounding inlining and the HostCallFrameFlag stuff.
176         CallFrame* trueCallerFrame();
177 #else
178         bool isInlineCallFrame() { return false; }
179         
180         CallFrame* trueCallerFrame() { return callerFrame()->removeHostCallFrameFlag(); }
181 #endif
182
183 #if ENABLE(TIZEN_JS_EXT_API)
184         int getExecutionState() { return globalData().getExecutionState(); }
185         void setExecutionState(bool b) { globalData().setExecutionState(b); }
186 #endif
187
188     private:
189         static const intptr_t HostCallFrameFlag = 1;
190 #ifndef NDEBUG
191         RegisterFile* registerFile();
192 #endif
193 #if ENABLE(DFG_JIT)
194         bool isInlineCallFrameSlow();
195 #endif
196         ExecState();
197         ~ExecState();
198     };
199
200 } // namespace JSC
201
202 #endif // CallFrame_h