]> git.saurik.com Git - apple/javascriptcore.git/blob - interpreter/CallFrame.h
JavaScriptCore-525.tar.gz
[apple/javascriptcore.git] / 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 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 "RegisterFile.h"
28 #include "ScopeChain.h"
29
30 namespace JSC {
31
32 class Arguments;
33 class JSActivation;
34 class Interpreter;
35
36 // Represents the current state of script execution.
37 // Passed as the first argument to most functions.
38 class ExecState : private Register {
39 public:
40 JSFunction* callee() const { return this[RegisterFile::Callee].function(); }
41 CodeBlock* codeBlock() const { return this[RegisterFile::CodeBlock].Register::codeBlock(); }
42 ScopeChainNode* scopeChain() const { return this[RegisterFile::ScopeChain].Register::scopeChain(); }
43
44 JSValuePtr thisValue();
45
46 // Global object in which execution began.
47 JSGlobalObject* dynamicGlobalObject();
48
49 // Global object in which the currently executing code was defined.
50 // Differs from dynamicGlobalObject() during function calls across web browser frames.
51 JSGlobalObject* lexicalGlobalObject() const
52 {
53 return scopeChain()->globalObject();
54 }
55
56 // Differs from lexicalGlobalObject because this will have DOM window shell rather than
57 // the actual DOM window, which can't be "this" for security reasons.
58 JSObject* globalThisValue() const
59 {
60 return scopeChain()->globalThisObject();
61 }
62
63 // FIXME: Elsewhere, we use JSGlobalData* rather than JSGlobalData&.
64 // We should make this more uniform and either use a reference everywhere
65 // or a pointer everywhere.
66 JSGlobalData& globalData() const
67 {
68 return *scopeChain()->globalData;
69 }
70
71 // Convenience functions for access to global data.
72 // It takes a few memory references to get from a call frame to the global data
73 // pointer, so these are inefficient, and should be used sparingly in new code.
74 // But they're used in many places in legacy code, so they're not going away any time soon.
75
76 void setException(JSValuePtr exception) { globalData().exception = exception; }
77 void clearException() { globalData().exception = noValue(); }
78 JSValuePtr exception() const { return globalData().exception; }
79 JSValuePtr* exceptionSlot() { return &globalData().exception; }
80 bool hadException() const { return globalData().exception; }
81
82 const CommonIdentifiers& propertyNames() const { return *globalData().propertyNames; }
83 const ArgList& emptyList() const { return *globalData().emptyList; }
84 Interpreter* interpreter() { return globalData().interpreter; }
85 Heap* heap() { return &globalData().heap; }
86
87 static const HashTable* arrayTable(CallFrame* callFrame) { return callFrame->globalData().arrayTable; }
88 static const HashTable* dateTable(CallFrame* callFrame) { return callFrame->globalData().dateTable; }
89 static const HashTable* mathTable(CallFrame* callFrame) { return callFrame->globalData().mathTable; }
90 static const HashTable* numberTable(CallFrame* callFrame) { return callFrame->globalData().numberTable; }
91 static const HashTable* regExpTable(CallFrame* callFrame) { return callFrame->globalData().regExpTable; }
92 static const HashTable* regExpConstructorTable(CallFrame* callFrame) { return callFrame->globalData().regExpConstructorTable; }
93 static const HashTable* stringTable(CallFrame* callFrame) { return callFrame->globalData().stringTable; }
94
95 private:
96 friend class Arguments;
97 friend class JSActivation;
98 friend class JSGlobalObject;
99 friend class Interpreter;
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 int argumentCount() const { return this[RegisterFile::ArgumentCount].i(); }
107 CallFrame* callerFrame() const { return this[RegisterFile::CallerFrame].callFrame(); }
108 Arguments* optionalCalleeArguments() const { return this[RegisterFile::OptionalCalleeArguments].arguments(); }
109 Instruction* returnPC() const { return this[RegisterFile::ReturnPC].vPC(); }
110 int returnValueRegister() const { return this[RegisterFile::ReturnValueRegister].i(); }
111
112 void setArgumentCount(int count) { this[RegisterFile::ArgumentCount] = count; }
113 void setCallee(JSFunction* callee) { this[RegisterFile::Callee] = callee; }
114 void setCalleeArguments(Arguments* arguments) { this[RegisterFile::OptionalCalleeArguments] = arguments; }
115 void setCallerFrame(CallFrame* callerFrame) { this[RegisterFile::CallerFrame] = callerFrame; }
116 void setCodeBlock(CodeBlock* codeBlock) { this[RegisterFile::CodeBlock] = codeBlock; }
117 void setScopeChain(ScopeChainNode* scopeChain) { this[RegisterFile::ScopeChain] = scopeChain; }
118
119 ALWAYS_INLINE void init(CodeBlock* codeBlock, Instruction* vPC, ScopeChainNode* scopeChain,
120 CallFrame* callerFrame, int returnValueRegister, int argc, JSFunction* function)
121 {
122 ASSERT(callerFrame); // Use noCaller() rather than 0 for the outer host call frame caller.
123
124 setCodeBlock(codeBlock);
125 setScopeChain(scopeChain);
126 setCallerFrame(callerFrame);
127 this[RegisterFile::ReturnPC] = vPC; // This is either an Instruction* or a pointer into JIT generated code stored as an Instruction*.
128 this[RegisterFile::ReturnValueRegister] = returnValueRegister;
129 setArgumentCount(argc); // original argument count (for the sake of the "arguments" object)
130 setCallee(function);
131 setCalleeArguments(0);
132 }
133
134 static const intptr_t HostCallFrameFlag = 1;
135
136 static CallFrame* noCaller() { return reinterpret_cast<CallFrame*>(HostCallFrameFlag); }
137 bool hasHostCallFrameFlag() const { return reinterpret_cast<intptr_t>(this) & HostCallFrameFlag; }
138 CallFrame* addHostCallFrameFlag() const { return reinterpret_cast<CallFrame*>(reinterpret_cast<intptr_t>(this) | HostCallFrameFlag); }
139 CallFrame* removeHostCallFrameFlag() { return reinterpret_cast<CallFrame*>(reinterpret_cast<intptr_t>(this) & ~HostCallFrameFlag); }
140
141 ExecState();
142 ~ExecState();
143 };
144
145 } // namespace JSC
146
147 #endif // CallFrame_h