2 * This file is part of the KDE libraries
3 * Copyright (C) 2003 Apple Computer, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #ifndef KJS_SCOPE_CHAIN_H
23 #define KJS_SCOPE_CHAIN_H
25 #include <wtf/Assertions.h>
32 class ScopeChainNode
{
34 ScopeChainNode(ScopeChainNode
*n
, JSObject
*o
)
35 : next(n
), object(o
), refCount(1) { }
42 class ScopeChainIterator
{
44 ScopeChainIterator(ScopeChainNode
*node
) : m_node(node
) {}
46 JSObject
* const & operator*() const { return m_node
->object
; }
47 JSObject
* const * operator->() const { return &(operator*()); }
49 ScopeChainIterator
& operator++() { m_node
= m_node
->next
; return *this; }
51 // postfix ++ intentionally omitted
53 bool operator==(const ScopeChainIterator
& other
) const { return m_node
== other
.m_node
; }
54 bool operator!=(const ScopeChainIterator
& other
) const { return m_node
!= other
.m_node
; }
57 ScopeChainNode
*m_node
;
62 typedef ScopeChainIterator const_iterator
;
63 typedef JSObject
* ValueType
;
65 ScopeChain() : _node(0) { }
66 ~ScopeChain() { deref(); }
68 ScopeChain(const ScopeChain
&c
) : _node(c
._node
)
69 { if (_node
) ++_node
->refCount
; }
70 ScopeChain
&operator=(const ScopeChain
&);
72 bool isEmpty() const { return !_node
; }
73 JSObject
*top() const { return _node
->object
; }
75 JSObject
*bottom() const;
77 ScopeChainIterator
begin() const { return ScopeChainIterator(_node
); }
78 ScopeChainIterator
end() const { return ScopeChainIterator(0); }
80 void clear() { deref(); _node
= 0; }
81 void push(JSObject
*);
82 void push(const ScopeChain
&);
83 void replaceTop(JSObject
*);
93 ScopeChainNode
*_node
;
95 void deref() { if (_node
&& --_node
->refCount
== 0) release(); }
101 inline void ScopeChain::ref() const
103 for (ScopeChainNode
*n
= _node
; n
; n
= n
->next
) {
104 if (n
->refCount
++ != 0)
109 inline ScopeChain
&ScopeChain::operator=(const ScopeChain
&c
)
117 inline JSObject
*ScopeChain::bottom() const
119 ScopeChainNode
*last
= 0;
120 for (ScopeChainNode
*n
= _node
; n
; n
= n
->next
)
127 inline void ScopeChain::push(JSObject
*o
)
130 _node
= new ScopeChainNode(_node
, o
);
133 inline void ScopeChain::replaceTop(JSObject
* o
)
139 inline void ScopeChain::pop()
141 ScopeChainNode
*oldNode
= _node
;
143 ScopeChainNode
*newNode
= oldNode
->next
;
146 if (--oldNode
->refCount
!= 0) {
156 #endif // KJS_SCOPE_CHAIN_H