1 // -*- mode: c++; c-basic-offset: 4 -*-
3 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
4 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
5 * Copyright (C) 2003, 2007, 2008 Apple Inc. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
27 #include "LabelStack.h"
28 #include "LocalStorage.h"
29 #include "completion.h"
31 #include "scope_chain.h"
36 class CommonIdentifiers
;
38 class FunctionBodyNode
;
43 class JSVariableObject
;
47 enum CodeType
{ GlobalCode
, EvalCode
, FunctionCode
};
49 typedef Vector
<ExecState
*, 16> ExecStateStack
;
51 // Represents the current state of script execution.
52 // Passed as the first argument to most functions.
53 class ExecState
: Noncopyable
{
55 // Global object that was in scope when the current script started executing.
56 JSGlobalObject
* dynamicGlobalObject() const { return m_globalObject
; }
58 // Global object that was in scope when the current body of code was defined.
59 JSGlobalObject
* lexicalGlobalObject() const;
61 void setException(JSValue
* e
) { m_exception
= e
; }
62 void clearException() { m_exception
= 0; }
63 JSValue
* exception() const { return m_exception
; }
64 JSValue
** exceptionSlot() { return &m_exception
; }
65 bool hadException() const { return !!m_exception
; }
67 const ScopeChain
& scopeChain() const { return m_scopeChain
; }
68 void pushScope(JSObject
* s
) { m_scopeChain
.push(s
); }
69 void popScope() { m_scopeChain
.pop(); }
70 void replaceScopeChainTop(JSObject
* o
) { m_scopeChain
.replaceTop(o
); }
72 JSVariableObject
* variableObject() const { return m_variableObject
; }
73 void setVariableObject(JSVariableObject
* v
) { m_variableObject
= v
; }
75 JSObject
* thisValue() const { return m_thisValue
; }
77 ExecState
* callingExecState() { return m_callingExec
; }
79 ActivationImp
* activationObject() { return m_activation
; }
80 void setActivationObject(ActivationImp
* a
) { m_activation
= a
; }
81 CodeType
codeType() { return m_codeType
; }
82 ScopeNode
* scopeNode() { return m_scopeNode
; }
83 FunctionImp
* function() const { return m_function
; }
84 const List
* arguments() const { return m_arguments
; }
86 LabelStack
& seenLabels() { return m_labelStack
; }
88 void pushIteration() { m_iterationDepth
++; }
89 void popIteration() { m_iterationDepth
--; }
90 bool inIteration() const { return (m_iterationDepth
> 0); }
92 void pushSwitch() { m_switchDepth
++; }
93 void popSwitch() { m_switchDepth
--; }
94 bool inSwitch() const { return (m_switchDepth
> 0); }
96 // These pointers are used to avoid accessing global variables for these,
97 // to avoid taking PIC branches in Mach-O binaries.
98 const CommonIdentifiers
& propertyNames() const { return *m_propertyNames
; }
99 const List
& emptyList() const { return *m_emptyList
; }
101 LocalStorage
& localStorage() { return *m_localStorage
; }
102 void setLocalStorage(LocalStorage
* s
) { m_localStorage
= s
; }
104 // These are only valid right after calling execute().
105 ComplType
completionType() const { return m_completionType
; }
106 const Identifier
& breakOrContinueTarget() const
108 ASSERT(m_completionType
== Break
|| m_completionType
== Continue
);
109 return *m_breakOrContinueTarget
;
112 // Only for use in the implementation of execute().
113 void setCompletionType(ComplType type
)
115 ASSERT(type
!= Break
);
116 ASSERT(type
!= Continue
);
117 m_completionType
= type
;
119 JSValue
* setNormalCompletion()
121 ASSERT(!hadException());
122 m_completionType
= Normal
;
125 JSValue
* setNormalCompletion(JSValue
* value
)
127 ASSERT(!hadException());
128 m_completionType
= Normal
;
131 JSValue
* setBreakCompletion(const Identifier
* target
)
133 ASSERT(!hadException());
134 m_completionType
= Break
;
135 m_breakOrContinueTarget
= target
;
138 JSValue
* setContinueCompletion(const Identifier
* target
)
140 ASSERT(!hadException());
141 m_completionType
= Continue
;
142 m_breakOrContinueTarget
= target
;
145 JSValue
* setReturnValueCompletion(JSValue
* returnValue
)
147 ASSERT(!hadException());
149 m_completionType
= ReturnValue
;
152 JSValue
* setThrowCompletion(JSValue
* exception
)
154 ASSERT(!hadException());
156 m_completionType
= Throw
;
159 JSValue
* setInterruptedCompletion()
161 ASSERT(!hadException());
162 m_completionType
= Interrupted
;
165 JSValue
* setInterruptedCompletion(JSValue
* returnValue
)
167 ASSERT(!hadException());
169 m_completionType
= Interrupted
;
173 static void markActiveExecStates();
174 static ExecStateStack
& activeExecStates();
177 ExecState(JSGlobalObject
*);
178 ExecState(JSGlobalObject
*, JSObject
* thisObject
, ProgramNode
*);
179 ExecState(JSGlobalObject
*, EvalNode
*, ExecState
* callingExecState
);
180 ExecState(JSGlobalObject
*, JSObject
* thisObject
, FunctionBodyNode
*,
181 ExecState
* callingExecState
, FunctionImp
*, const List
& args
);
184 // ExecStates are always stack-allocated, and the garbage collector
185 // marks the stack, so we don't need to protect the objects below from GC.
187 JSGlobalObject
* m_globalObject
;
188 JSValue
* m_exception
;
189 CommonIdentifiers
* m_propertyNames
;
190 const List
* m_emptyList
;
192 ExecState
* m_callingExec
;
194 ScopeNode
* m_scopeNode
;
196 FunctionImp
* m_function
;
197 const List
* m_arguments
;
198 ActivationImp
* m_activation
;
199 LocalStorage
* m_localStorage
;
201 ScopeChain m_scopeChain
;
202 JSVariableObject
* m_variableObject
;
203 JSObject
* m_thisValue
;
205 LabelStack m_labelStack
;
206 int m_iterationDepth
;
210 ComplType m_completionType
;
211 const Identifier
* m_breakOrContinueTarget
;
214 class GlobalExecState
: public ExecState
{
216 GlobalExecState(JSGlobalObject
*);
220 class InterpreterExecState
: public ExecState
{
222 InterpreterExecState(JSGlobalObject
*, JSObject
* thisObject
, ProgramNode
*);
223 ~InterpreterExecState();
226 class EvalExecState
: public ExecState
{
228 EvalExecState(JSGlobalObject
*, EvalNode
*, ExecState
* callingExecState
);
232 class FunctionExecState
: public ExecState
{
234 FunctionExecState(JSGlobalObject
*, JSObject
* thisObject
, FunctionBodyNode
*,
235 ExecState
* callingExecState
, FunctionImp
*, const List
& args
);
236 ~FunctionExecState();
241 #endif // ExecState_h