|
1 /* |
|
2 * This file is part of the KDE libraries |
|
3 * Copyright (C) 2003 Apple Computer, Inc. |
|
4 * |
|
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. |
|
9 * |
|
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. |
|
14 * |
|
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. |
|
19 * |
|
20 */ |
|
21 |
|
22 #ifndef KJS_SCOPE_CHAIN_H |
|
23 #define KJS_SCOPE_CHAIN_H |
|
24 |
|
25 #include <assert.h> |
|
26 |
|
27 namespace KJS { |
|
28 |
|
29 class JSObject; |
|
30 class ExecState; |
|
31 |
|
32 class ScopeChainNode { |
|
33 public: |
|
34 ScopeChainNode(ScopeChainNode *n, JSObject *o) |
|
35 : next(n), object(o), refCount(1) { } |
|
36 |
|
37 ScopeChainNode *next; |
|
38 JSObject *object; |
|
39 int refCount; |
|
40 }; |
|
41 |
|
42 class ScopeChainIterator { |
|
43 public: |
|
44 ScopeChainIterator(ScopeChainNode *node) : m_node(node) {} |
|
45 |
|
46 JSObject * const & operator*() const { return m_node->object; } |
|
47 JSObject * const * operator->() const { return &(operator*()); } |
|
48 |
|
49 ScopeChainIterator& operator++() { m_node = m_node->next; return *this; } |
|
50 |
|
51 // postfix ++ intentionally omitted |
|
52 |
|
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; } |
|
55 |
|
56 private: |
|
57 ScopeChainNode *m_node; |
|
58 }; |
|
59 |
|
60 class ScopeChain { |
|
61 public: |
|
62 ScopeChain() : _node(0) { } |
|
63 ~ScopeChain() { deref(); } |
|
64 |
|
65 ScopeChain(const ScopeChain &c) : _node(c._node) |
|
66 { if (_node) ++_node->refCount; } |
|
67 ScopeChain &operator=(const ScopeChain &); |
|
68 |
|
69 bool isEmpty() const { return !_node; } |
|
70 JSObject *top() const { return _node->object; } |
|
71 |
|
72 JSObject *bottom() const; |
|
73 |
|
74 ScopeChainIterator begin() const { return ScopeChainIterator(_node); } |
|
75 ScopeChainIterator end() const { return ScopeChainIterator(0); } |
|
76 |
|
77 void clear() { deref(); _node = 0; } |
|
78 void push(JSObject *); |
|
79 void push(const ScopeChain &); |
|
80 void pop(); |
|
81 |
|
82 void mark(); |
|
83 |
|
84 #ifndef NDEBUG |
|
85 void print(); |
|
86 #endif |
|
87 |
|
88 private: |
|
89 ScopeChainNode *_node; |
|
90 |
|
91 void deref() { if (_node && --_node->refCount == 0) release(); } |
|
92 void ref() const; |
|
93 |
|
94 void release(); |
|
95 }; |
|
96 |
|
97 inline void ScopeChain::ref() const |
|
98 { |
|
99 for (ScopeChainNode *n = _node; n; n = n->next) { |
|
100 if (n->refCount++ != 0) |
|
101 break; |
|
102 } |
|
103 } |
|
104 |
|
105 inline ScopeChain &ScopeChain::operator=(const ScopeChain &c) |
|
106 { |
|
107 c.ref(); |
|
108 deref(); |
|
109 _node = c._node; |
|
110 return *this; |
|
111 } |
|
112 |
|
113 inline JSObject *ScopeChain::bottom() const |
|
114 { |
|
115 ScopeChainNode *last = 0; |
|
116 for (ScopeChainNode *n = _node; n; n = n->next) |
|
117 last = n; |
|
118 if (!last) |
|
119 return 0; |
|
120 return last->object; |
|
121 } |
|
122 |
|
123 inline void ScopeChain::push(JSObject *o) |
|
124 { |
|
125 assert(o); |
|
126 _node = new ScopeChainNode(_node, o); |
|
127 } |
|
128 |
|
129 inline void ScopeChain::pop() |
|
130 { |
|
131 ScopeChainNode *oldNode = _node; |
|
132 assert(oldNode); |
|
133 ScopeChainNode *newNode = oldNode->next; |
|
134 _node = newNode; |
|
135 |
|
136 if (--oldNode->refCount != 0) { |
|
137 if (newNode) |
|
138 ++newNode->refCount; |
|
139 } else { |
|
140 delete oldNode; |
|
141 } |
|
142 } |
|
143 |
|
144 } // namespace KJS |
|
145 |
|
146 #endif // KJS_SCOPE_CHAIN_H |