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.
25 #include "ExecState.h"
27 #include "Activation.h"
28 #include "JSGlobalObject.h"
31 #include "scope_chain_mark.h"
35 static inline List
* globalEmptyList()
37 static List staticEmptyList
;
38 return &staticEmptyList
;
43 // The constructor for the globalExec pseudo-ExecState
44 inline ExecState::ExecState(JSGlobalObject
* globalObject
)
45 : m_globalObject(globalObject
)
47 , m_propertyNames(CommonIdentifiers::shared())
48 , m_emptyList(globalEmptyList())
54 , m_localStorage(&globalObject
->localStorage())
55 , m_variableObject(globalObject
)
56 , m_thisValue(globalObject
)
59 , m_codeType(GlobalCode
)
61 m_scopeChain
.push(globalObject
);
64 inline ExecState::ExecState(JSGlobalObject
* globalObject
, JSObject
* /*thisObject*/, ProgramNode
* programNode
)
65 : m_globalObject(globalObject
)
67 , m_propertyNames(CommonIdentifiers::shared())
68 , m_emptyList(globalEmptyList())
70 , m_scopeNode(programNode
)
74 , m_localStorage(&globalObject
->localStorage())
75 , m_variableObject(globalObject
)
76 , m_thisValue(globalObject
)
79 , m_codeType(GlobalCode
)
81 // FIXME: This function ignores the "thisObject" parameter, which means that the API for evaluating
82 // a script with a this object that's not the same as the global object is broken, and probably
83 // has been for some time.
85 m_scopeChain
.push(globalObject
);
88 inline ExecState::ExecState(JSGlobalObject
* globalObject
, EvalNode
* evalNode
, ExecState
* callingExec
)
89 : m_globalObject(globalObject
)
91 , m_propertyNames(callingExec
->m_propertyNames
)
92 , m_emptyList(callingExec
->m_emptyList
)
93 , m_callingExec(callingExec
)
94 , m_scopeNode(evalNode
)
98 , m_localStorage(callingExec
->m_localStorage
)
99 , m_scopeChain(callingExec
->m_scopeChain
)
100 , m_variableObject(callingExec
->m_variableObject
)
101 , m_thisValue(callingExec
->m_thisValue
)
102 , m_iterationDepth(0)
104 , m_codeType(EvalCode
)
109 inline ExecState::ExecState(JSGlobalObject
* globalObject
, JSObject
* thisObject
,
110 FunctionBodyNode
* functionBodyNode
, ExecState
* callingExec
,
111 FunctionImp
* func
, const List
& args
)
112 : m_globalObject(globalObject
)
114 , m_propertyNames(callingExec
->m_propertyNames
)
115 , m_emptyList(callingExec
->m_emptyList
)
116 , m_callingExec(callingExec
)
117 , m_scopeNode(functionBodyNode
)
120 , m_scopeChain(func
->scope())
121 , m_thisValue(thisObject
)
122 , m_iterationDepth(0)
124 , m_codeType(FunctionCode
)
128 ActivationImp
* activation
= globalObject
->pushActivation(this);
129 m_activation
= activation
;
130 m_localStorage
= &activation
->localStorage();
131 m_variableObject
= activation
;
132 m_scopeChain
.push(activation
);
135 inline ExecState::~ExecState()
139 JSGlobalObject
* ExecState::lexicalGlobalObject() const
141 JSObject
* object
= m_scopeChain
.bottom();
142 if (object
&& object
->isGlobalObject())
143 return static_cast<JSGlobalObject
*>(object
);
144 return m_globalObject
;
147 void ExecState::markActiveExecStates()
149 ExecStateStack::const_iterator end
= activeExecStates().end();
150 for (ExecStateStack::const_iterator it
= activeExecStates().begin(); it
!= end
; ++it
)
151 (*it
)->m_scopeChain
.mark();
154 static inline ExecStateStack
& inlineActiveExecStates()
156 static ExecStateStack staticActiveExecStates
;
157 return staticActiveExecStates
;
160 ExecStateStack
& ExecState::activeExecStates()
162 return inlineActiveExecStates();
165 GlobalExecState::GlobalExecState(JSGlobalObject
* globalObject
)
166 : ExecState(globalObject
)
170 GlobalExecState::~GlobalExecState()
174 InterpreterExecState::InterpreterExecState(JSGlobalObject
* globalObject
, JSObject
* thisObject
, ProgramNode
* programNode
)
175 : ExecState(globalObject
, thisObject
, programNode
)
177 inlineActiveExecStates().append(this);
180 InterpreterExecState::~InterpreterExecState()
182 ASSERT(inlineActiveExecStates().last() == this);
183 inlineActiveExecStates().removeLast();
186 EvalExecState::EvalExecState(JSGlobalObject
* globalObject
, EvalNode
* evalNode
, ExecState
* callingExec
)
187 : ExecState(globalObject
, evalNode
, callingExec
)
189 inlineActiveExecStates().append(this);
192 EvalExecState::~EvalExecState()
194 ASSERT(inlineActiveExecStates().last() == this);
195 inlineActiveExecStates().removeLast();
198 FunctionExecState::FunctionExecState(JSGlobalObject
* globalObject
, JSObject
* thisObject
,
199 FunctionBodyNode
* functionBodyNode
, ExecState
* callingExec
,
200 FunctionImp
* func
, const List
& args
)
201 : ExecState(globalObject
, thisObject
, functionBodyNode
, callingExec
, func
, args
)
203 inlineActiveExecStates().append(this);
206 FunctionExecState::~FunctionExecState()
208 ASSERT(inlineActiveExecStates().last() == this);
209 inlineActiveExecStates().removeLast();
211 if (m_activation
->needsPop())
212 m_globalObject
->popActivation();