@@ -6,17 +6,17 @@ namespace Sass {
6
6
7
7
template <typename T>
8
8
Environment<T>::Environment(bool is_shadow)
9
- : local_frame_(std::map <std::string, T>()),
9
+ : local_frame_(environment_map <std::string, T>()),
10
10
parent_ (0 ), is_shadow_(false )
11
11
{ }
12
12
template <typename T>
13
13
Environment<T>::Environment(Environment<T>* env, bool is_shadow)
14
- : local_frame_(std::map <std::string, T>()),
14
+ : local_frame_(environment_map <std::string, T>()),
15
15
parent_ (env), is_shadow_(is_shadow)
16
16
{ }
17
17
template <typename T>
18
18
Environment<T>::Environment(Environment<T>& env, bool is_shadow)
19
- : local_frame_(std::map <std::string, T>()),
19
+ : local_frame_(environment_map <std::string, T>()),
20
20
parent_ (&env), is_shadow_(is_shadow)
21
21
{ }
22
22
@@ -45,20 +45,33 @@ namespace Sass {
45
45
}
46
46
47
47
template <typename T>
48
- std::map <std::string, T>& Environment<T>::local_frame() {
48
+ environment_map <std::string, T>& Environment<T>::local_frame() {
49
49
return local_frame_;
50
50
}
51
51
52
52
template <typename T>
53
53
bool Environment<T>::has_local(const std::string& key) const
54
54
{ return local_frame_.find (key) != local_frame_.end (); }
55
55
56
+ template <typename T> EnvResult
57
+ Environment<T>::find_local(const std::string& key)
58
+ {
59
+ auto end = local_frame_.end ();
60
+ auto it = local_frame_.find (key);
61
+ return EnvResult (it, it != end);
62
+ }
63
+
56
64
template <typename T>
57
65
T& Environment<T>::get_local(const std::string& key)
58
66
{ return local_frame_[key]; }
59
67
60
68
template <typename T>
61
- void Environment<T>::set_local(const std::string& key, T val)
69
+ void Environment<T>::set_local(const std::string& key, const T& val)
70
+ {
71
+ local_frame_[key] = val;
72
+ }
73
+ template <typename T>
74
+ void Environment<T>::set_local(const std::string& key, T&& val)
62
75
{
63
76
local_frame_[key] = val;
64
77
}
@@ -86,7 +99,12 @@ namespace Sass {
86
99
{ return (*global_env ())[key]; }
87
100
88
101
template <typename T>
89
- void Environment<T>::set_global(const std::string& key, T val)
102
+ void Environment<T>::set_global(const std::string& key, const T& val)
103
+ {
104
+ global_env ()->local_frame_ [key] = val;
105
+ }
106
+ template <typename T>
107
+ void Environment<T>::set_global(const std::string& key, T&& val)
90
108
{
91
109
global_env ()->local_frame_ [key] = val;
92
110
}
@@ -126,13 +144,31 @@ namespace Sass {
126
144
// either update already existing lexical value
127
145
// or if flag is set, we create one if no lexical found
128
146
template <typename T>
129
- void Environment<T>::set_lexical(const std::string& key, T val)
147
+ void Environment<T>::set_lexical(const std::string& key, const T& val)
130
148
{
131
- auto cur = this ;
149
+ Environment<T>* cur = this ;
132
150
bool shadow = false ;
133
151
while ((cur && cur->is_lexical ()) || shadow) {
134
- if (cur->has_local (key)) {
135
- cur->set_local (key, val);
152
+ EnvResult rv (cur->find_local (key));
153
+ if (rv.found ) {
154
+ rv.it ->second = val;
155
+ return ;
156
+ }
157
+ shadow = cur->is_shadow ();
158
+ cur = cur->parent_ ;
159
+ }
160
+ set_local (key, val);
161
+ }
162
+ // this one moves the value
163
+ template <typename T>
164
+ void Environment<T>::set_lexical(const std::string& key, T&& val)
165
+ {
166
+ Environment<T>* cur = this ;
167
+ bool shadow = false ;
168
+ while ((cur && cur->is_lexical ()) || shadow) {
169
+ EnvResult rv (cur->find_local (key));
170
+ if (rv.found ) {
171
+ rv.it ->second = val;
136
172
return ;
137
173
}
138
174
shadow = cur->is_shadow ();
@@ -156,6 +192,20 @@ namespace Sass {
156
192
return false ;
157
193
}
158
194
195
+ // look on the full stack for key
196
+ // include all scopes available
197
+ template <typename T> EnvResult
198
+ Environment<T>::find(const std::string& key)
199
+ {
200
+ auto cur = this ;
201
+ while (true ) {
202
+ EnvResult rv (cur->find_local (key));
203
+ if (rv.found ) return rv;
204
+ cur = cur->parent_ ;
205
+ if (!cur) return rv;
206
+ }
207
+ };
208
+
159
209
// use array access for getter and setter functions
160
210
template <typename T>
161
211
T& Environment<T>::operator [](const std::string& key)
@@ -177,7 +227,7 @@ namespace Sass {
177
227
size_t indent = 0 ;
178
228
if (parent_) indent = parent_->print (prefix) + 1 ;
179
229
std::cerr << prefix << std::string (indent, ' ' ) << " == " << this << std::endl;
180
- for (typename std::map <std::string, T>::iterator i = local_frame_.begin (); i != local_frame_.end (); ++i) {
230
+ for (typename environment_map <std::string, T>::iterator i = local_frame_.begin (); i != local_frame_.end (); ++i) {
181
231
if (!ends_with (i->first , " [f]" ) && !ends_with (i->first , " [f]4" ) && !ends_with (i->first , " [f]2" )) {
182
232
std::cerr << prefix << std::string (indent, ' ' ) << i->first << " " << i->second ;
183
233
if (Value_Ptr val = Cast<Value>(i->second ))
0 commit comments