1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
use codemap::{Span, Spanned};
use crate::{
ast::{AstForwardRule, Configuration, Mixin},
builtin::modules::{ForwardedModule, Module, ModuleScope, Modules},
common::Identifier,
error::SassResult,
selector::ExtensionStore,
value::{SassFunction, Value},
};
use std::{cell::RefCell, collections::BTreeMap, sync::Arc};
use super::{scope::Scopes, visitor::CallableContentBlock};
#[derive(Debug, Clone)]
pub(crate) struct Environment {
pub scopes: Scopes,
pub modules: Arc<RefCell<Modules>>,
pub global_modules: Vec<Arc<RefCell<Module>>>,
pub content: Option<Arc<CallableContentBlock>>,
pub forwarded_modules: Arc<RefCell<Vec<Arc<RefCell<Module>>>>>,
}
impl Environment {
pub fn new() -> Self {
Self {
scopes: Scopes::new(),
modules: Arc::new(RefCell::new(Modules::new())),
global_modules: Vec::new(),
content: None,
forwarded_modules: Arc::new(RefCell::new(Vec::new())),
}
}
pub fn new_closure(&self) -> Self {
Self {
scopes: self.scopes.new_closure(),
modules: Arc::clone(&self.modules),
global_modules: self.global_modules.iter().map(Arc::clone).collect(),
content: self.content.as_ref().map(Arc::clone),
forwarded_modules: Arc::clone(&self.forwarded_modules),
}
}
pub fn for_import(&self) -> Self {
Self {
scopes: self.scopes.new_closure(),
modules: Arc::new(RefCell::new(Modules::new())),
global_modules: Vec::new(),
content: self.content.as_ref().map(Arc::clone),
forwarded_modules: Arc::clone(&self.forwarded_modules),
}
}
pub fn to_dummy_module(&self, span: Span) -> Module {
Module::Environment {
scope: ModuleScope::new(),
upstream: Vec::new(),
extension_store: ExtensionStore::new(span),
env: self.clone(),
}
}
pub fn import_forwards(&mut self, _env: Module) {
// if (module is _EnvironmentModule) {
// var forwarded = module._environment._forwardedModules;
// if (forwarded == null) return;
// // Omit modules from [forwarded] that are already globally available and
// // forwarded in this module.
// var forwardedModules = _forwardedModules;
// if (forwardedModules != null) {
// forwarded = {
// for (var entry in forwarded.entries)
// if (!forwardedModules.containsKey(entry.key) ||
// !_globalModules.containsKey(entry.key))
// entry.key: entry.value,
// };
// } else {
// forwardedModules = _forwardedModules ??= {};
// }
// var forwardedVariableNames =
// forwarded.keys.expand((module) => module.variables.keys).toSet();
// var forwardedFunctionNames =
// forwarded.keys.expand((module) => module.functions.keys).toSet();
// var forwardedMixinNames =
// forwarded.keys.expand((module) => module.mixins.keys).toSet();
// if (atRoot) {
// // Hide members from modules that have already been imported or
// // forwarded that would otherwise conflict with the @imported members.
// for (var entry in _importedModules.entries.toList()) {
// var module = entry.key;
// var shadowed = ShadowedModuleView.ifNecessary(module,
// variables: forwardedVariableNames,
// mixins: forwardedMixinNames,
// functions: forwardedFunctionNames);
// if (shadowed != null) {
// _importedModules.remove(module);
// if (!shadowed.isEmpty) _importedModules[shadowed] = entry.value;
// }
// }
// for (var entry in forwardedModules.entries.toList()) {
// var module = entry.key;
// var shadowed = ShadowedModuleView.ifNecessary(module,
// variables: forwardedVariableNames,
// mixins: forwardedMixinNames,
// functions: forwardedFunctionNames);
// if (shadowed != null) {
// forwardedModules.remove(module);
// if (!shadowed.isEmpty) forwardedModules[shadowed] = entry.value;
// }
// }
// _importedModules.addAll(forwarded);
// forwardedModules.addAll(forwarded);
// } else {
// (_nestedForwardedModules ??=
// List.generate(_variables.length - 1, (_) => []))
// .last
// .addAll(forwarded.keys);
// }
// // Remove existing member definitions that are now shadowed by the
// // forwarded modules.
// for (var variable in forwardedVariableNames) {
// _variableIndices.remove(variable);
// _variables.last.remove(variable);
// _variableNodes.last.remove(variable);
// }
// for (var function in forwardedFunctionNames) {
// _functionIndices.remove(function);
// _functions.last.remove(function);
// }
// for (var mixin in forwardedMixinNames) {
// _mixinIndices.remove(mixin);
// _mixins.last.remove(mixin);
// }
// }
// todo!()
}
pub fn to_implicit_configuration(&self) -> Configuration {
// var configuration = <String, ConfiguredValue>{};
// for (var i = 0; i < _variables.length; i++) {
// var values = _variables[i];
// var nodes = _variableNodes[i];
// for (var entry in values.entries) {
// // Implicit configurations are never invalid, making [configurationSpan]
// // unnecessary, so we pass null here to avoid having to compute it.
// configuration[entry.key] =
// ConfiguredValue.implicit(entry.value, nodes[entry.key]!);
// }
// }
// return Configuration.implicit(configuration);
todo!()
}
pub fn forward_module(&mut self, module: Arc<RefCell<Module>>, rule: AstForwardRule) {
let view = ForwardedModule::if_necessary(module, rule);
(*self.forwarded_modules).borrow_mut().push(view);
// todo: assertnoconflicts
}
pub fn insert_mixin(&mut self, name: Identifier, mixin: Mixin) {
self.scopes.insert_mixin(name, mixin);
}
pub fn mixin_exists(&self, name: Identifier) -> bool {
self.scopes.mixin_exists(name)
}
pub fn get_mixin(
&self,
name: Spanned<Identifier>,
namespace: Option<Spanned<Identifier>>,
) -> SassResult<Mixin> {
if let Some(namespace) = namespace {
let modules = (*self.modules).borrow();
let module = modules.get(namespace.node, namespace.span)?;
return (*module).borrow().get_mixin(name);
}
match self.scopes.get_mixin(name) {
Ok(v) => Ok(v),
Err(e) => {
if let Some(v) = self.get_mixin_from_global_modules(name.node) {
return Ok(v);
}
Err(e)
}
}
}
pub fn insert_fn(&mut self, func: SassFunction) {
self.scopes.insert_fn(func);
}
pub fn fn_exists(&self, name: Identifier) -> bool {
self.scopes.fn_exists(name)
}
pub fn get_fn(
&self,
name: Identifier,
namespace: Option<Spanned<Identifier>>,
) -> SassResult<Option<SassFunction>> {
if let Some(namespace) = namespace {
let modules = (*self.modules).borrow();
let module = modules.get(namespace.node, namespace.span)?;
return Ok((*module).borrow().get_fn(name));
}
Ok(self
.scopes
.get_fn(name)
.or_else(|| self.get_function_from_global_modules(name)))
}
pub fn var_exists(
&self,
name: Identifier,
namespace: Option<Spanned<Identifier>>,
) -> SassResult<bool> {
if let Some(namespace) = namespace {
let modules = (*self.modules).borrow();
let module = modules.get(namespace.node, namespace.span)?;
return Ok((*module).borrow().var_exists(name));
}
Ok(self.scopes.var_exists(name))
}
pub fn get_var(
&mut self,
name: Spanned<Identifier>,
namespace: Option<Spanned<Identifier>>,
) -> SassResult<Value> {
if let Some(namespace) = namespace {
let modules = (*self.modules).borrow();
let module = modules.get(namespace.node, namespace.span)?;
return (*module).borrow().get_var(name);
}
match self.scopes.get_var(name) {
Ok(v) => Ok(v),
Err(e) => {
if let Some(v) = self.get_variable_from_global_modules(name.node) {
Ok(v)
} else {
Err(e)
}
}
}
}
pub fn insert_var(
&mut self,
name: Spanned<Identifier>,
namespace: Option<Spanned<Identifier>>,
value: Value,
is_global: bool,
in_semi_global_scope: bool,
) -> SassResult<()> {
if let Some(namespace) = namespace {
let mut modules = (*self.modules).borrow_mut();
let module = modules.get_mut(namespace.node, namespace.span)?;
(*module).borrow_mut().update_var(name, value)?;
return Ok(());
}
if is_global || self.at_root() {
// // Don't set the index if there's already a variable with the given name,
// // since local accesses should still return the local variable.
// _variableIndices.putIfAbsent(name, () {
// _lastVariableName = name;
// _lastVariableIndex = 0;
// return 0;
// });
// // If this module doesn't already contain a variable named [name], try
// // setting it in a global module.
// if (!_variables.first.containsKey(name)) {
// var moduleWithName = _fromOneModule(name, "variable",
// (module) => module.variables.containsKey(name) ? module : null);
// if (moduleWithName != null) {
// moduleWithName.setVariable(name, value, nodeWithSpan);
// return;
// }
// }
self.scopes.insert_var(0, name.node, value);
return Ok(());
}
let mut index = self
.scopes
.find_var(name.node)
.unwrap_or(self.scopes.len() - 1);
if !in_semi_global_scope && index == 0 {
index = self.scopes.len() - 1;
}
self.scopes.last_variable_index = Some((name.node, index));
self.scopes.insert_var(index, name.node, value);
Ok(())
}
pub fn at_root(&self) -> bool {
self.scopes.len() == 1
}
pub fn scopes_mut(&mut self) -> &mut Scopes {
&mut self.scopes
}
pub fn global_vars(&self) -> Arc<RefCell<BTreeMap<Identifier, Value>>> {
self.scopes.global_variables()
}
pub fn global_mixins(&self) -> Arc<RefCell<BTreeMap<Identifier, Mixin>>> {
self.scopes.global_mixins()
}
pub fn global_functions(&self) -> Arc<RefCell<BTreeMap<Identifier, SassFunction>>> {
self.scopes.global_functions()
}
fn get_variable_from_global_modules(&self, name: Identifier) -> Option<Value> {
for module in &self.global_modules {
if (**module).borrow().var_exists(name) {
return (**module).borrow().get_var_no_err(name);
}
}
None
}
fn get_function_from_global_modules(&self, name: Identifier) -> Option<SassFunction> {
for module in &self.global_modules {
if (**module).borrow().fn_exists(name) {
return (**module).borrow().get_fn(name);
}
}
None
}
fn get_mixin_from_global_modules(&self, name: Identifier) -> Option<Mixin> {
for module in &self.global_modules {
if (**module).borrow().mixin_exists(name) {
return (**module).borrow().get_mixin_no_err(name);
}
}
None
}
pub fn add_module(
&mut self,
namespace: Option<Identifier>,
module: Arc<RefCell<Module>>,
span: Span,
) -> SassResult<()> {
match namespace {
Some(namespace) => {
(*self.modules)
.borrow_mut()
.insert(namespace, module, span)?;
}
None => {
for name in (*self.scopes.global_variables()).borrow().keys() {
if (*module).borrow().var_exists(*name) {
return Err((
format!("This module and the new module both define a variable named \"${name}\".", name = name)
, span).into());
}
}
self.global_modules.push(module);
}
}
Ok(())
}
pub fn to_module(self, extension_store: ExtensionStore) -> Arc<RefCell<Module>> {
debug_assert!(self.at_root());
Arc::new(RefCell::new(Module::new_env(self, extension_store)))
}
}