2022-01-04 22:30:34 +00:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2021-08-15 22:33:34 +00:00
|
|
|
|
2022-01-04 22:30:34 +00:00
|
|
|
use crate::engine::EngineState;
|
2022-04-18 22:28:01 +00:00
|
|
|
use crate::{ShellError, Span, Value, VarId};
|
2021-08-15 22:33:34 +00:00
|
|
|
|
2021-11-02 19:53:48 +00:00
|
|
|
/// A runtime value stack used during evaluation
|
|
|
|
///
|
|
|
|
/// A note on implementation:
|
|
|
|
///
|
|
|
|
/// We previously set up the stack in a traditional way, where stack frames had parents which would
|
|
|
|
/// represent other frames that you might return to when exiting a function.
|
|
|
|
///
|
|
|
|
/// While experimenting with blocks, we found that we needed to have closure captures of variables
|
|
|
|
/// seen outside of the blocks, so that they blocks could be run in a way that was both thread-safe
|
|
|
|
/// and followed the restrictions for closures applied to iterators. The end result left us with
|
|
|
|
/// closure-captured single stack frames that blocks could see.
|
|
|
|
///
|
|
|
|
/// Blocks make up the only scope and stack definition abstraction in Nushell. As a result, we were
|
|
|
|
/// creating closure captures at any point we wanted to have a Block value we could safely evaluate
|
|
|
|
/// in any context. This meant that the parents were going largely unused, with captured variables
|
|
|
|
/// taking their place. The end result is this, where we no longer have separate frames, but instead
|
|
|
|
/// use the Stack as a way of representing the local and closure-captured state.
|
2021-10-25 04:01:02 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2021-10-25 20:04:23 +00:00
|
|
|
pub struct Stack {
|
2021-11-30 06:14:05 +00:00
|
|
|
/// Variables
|
2021-08-15 22:33:34 +00:00
|
|
|
pub vars: HashMap<VarId, Value>,
|
2021-11-30 06:14:05 +00:00
|
|
|
/// Environment variables arranged as a stack to be able to recover values from parent scopes
|
2021-12-17 01:04:54 +00:00
|
|
|
pub env_vars: Vec<HashMap<String, Value>>,
|
2022-01-04 22:30:34 +00:00
|
|
|
/// Tells which environment variables from engine state are hidden. We don't need to track the
|
|
|
|
/// env vars in the stack since we can just delete them.
|
|
|
|
pub env_hidden: HashSet<String>,
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Stack {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stack {
|
|
|
|
pub fn new() -> Stack {
|
2021-10-25 20:04:23 +00:00
|
|
|
Stack {
|
2021-08-15 22:33:34 +00:00
|
|
|
vars: HashMap::new(),
|
2021-11-30 06:14:05 +00:00
|
|
|
env_vars: vec![],
|
2022-01-04 22:30:34 +00:00
|
|
|
env_hidden: HashSet::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_env(&mut self, env_vars: &[HashMap<String, Value>], env_hidden: &HashSet<String>) {
|
2022-01-05 22:21:26 +00:00
|
|
|
// Do not clone the environment if it hasn't changed
|
|
|
|
if self.env_vars.iter().any(|scope| !scope.is_empty()) {
|
2022-01-04 22:30:34 +00:00
|
|
|
self.env_vars = env_vars.to_owned();
|
|
|
|
}
|
|
|
|
|
2022-01-05 22:21:26 +00:00
|
|
|
if !self.env_hidden.is_empty() {
|
2022-01-04 22:30:34 +00:00
|
|
|
self.env_hidden = env_hidden.clone();
|
2021-10-25 20:04:23 +00:00
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
2021-11-15 23:16:06 +00:00
|
|
|
|
2022-01-29 13:00:48 +00:00
|
|
|
pub fn get_var(&self, var_id: VarId, span: Span) -> Result<Value, ShellError> {
|
2021-10-25 20:04:23 +00:00
|
|
|
if let Some(v) = self.vars.get(&var_id) {
|
2022-01-29 13:00:48 +00:00
|
|
|
return Ok(v.clone().with_span(span));
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
2021-11-14 19:25:57 +00:00
|
|
|
|
2022-01-29 13:00:48 +00:00
|
|
|
Err(ShellError::VariableNotFoundAtRuntime(span))
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 14:39:51 +00:00
|
|
|
pub fn get_var_with_origin(&self, var_id: VarId, span: Span) -> Result<Value, ShellError> {
|
|
|
|
if let Some(v) = self.vars.get(&var_id) {
|
|
|
|
return Ok(v.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(ShellError::VariableNotFoundAtRuntime(span))
|
|
|
|
}
|
|
|
|
|
2021-10-25 04:01:02 +00:00
|
|
|
pub fn add_var(&mut self, var_id: VarId, value: Value) {
|
2021-10-25 20:04:23 +00:00
|
|
|
self.vars.insert(var_id, value);
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 01:04:54 +00:00
|
|
|
pub fn add_env_var(&mut self, var: String, value: Value) {
|
2022-01-04 22:30:34 +00:00
|
|
|
// if the env var was hidden, let's activate it again
|
|
|
|
self.env_hidden.remove(&var);
|
|
|
|
|
2021-11-30 06:14:05 +00:00
|
|
|
if let Some(scope) = self.env_vars.last_mut() {
|
|
|
|
scope.insert(var, value);
|
|
|
|
} else {
|
|
|
|
self.env_vars.push(HashMap::from([(var, value)]));
|
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 04:06:56 +00:00
|
|
|
pub fn captures_to_stack(&self, captures: &HashMap<VarId, Value>) -> Stack {
|
|
|
|
let mut output = Stack::new();
|
|
|
|
|
|
|
|
output.vars = captures.clone();
|
|
|
|
|
|
|
|
// FIXME: this is probably slow
|
|
|
|
output.env_vars = self.env_vars.clone();
|
|
|
|
output.env_vars.push(HashMap::new());
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gather_captures(&self, captures: &[VarId]) -> Stack {
|
2021-10-25 20:04:23 +00:00
|
|
|
let mut output = Stack::new();
|
|
|
|
|
2022-01-29 13:00:48 +00:00
|
|
|
let fake_span = Span::new(0, 0);
|
|
|
|
|
2021-10-25 20:04:23 +00:00
|
|
|
for capture in captures {
|
2021-10-25 21:14:21 +00:00
|
|
|
// Note: this assumes we have calculated captures correctly and that commands
|
|
|
|
// that take in a var decl will manually set this into scope when running the blocks
|
2022-01-29 13:00:48 +00:00
|
|
|
if let Ok(value) = self.get_var(*capture, fake_span) {
|
2021-10-25 21:14:21 +00:00
|
|
|
output.vars.insert(*capture, value);
|
|
|
|
}
|
2021-10-25 20:04:23 +00:00
|
|
|
}
|
2021-10-25 04:01:02 +00:00
|
|
|
|
2021-11-04 02:32:35 +00:00
|
|
|
// FIXME: this is probably slow
|
|
|
|
output.env_vars = self.env_vars.clone();
|
2021-11-30 06:14:05 +00:00
|
|
|
output.env_vars.push(HashMap::new());
|
2021-11-04 02:32:35 +00:00
|
|
|
|
2021-10-25 04:01:02 +00:00
|
|
|
output
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 06:14:05 +00:00
|
|
|
/// Flatten the env var scope frames into one frame
|
2022-01-04 22:30:34 +00:00
|
|
|
pub fn get_env_vars(&self, engine_state: &EngineState) -> HashMap<String, Value> {
|
|
|
|
// TODO: We're collecting im::HashMap to HashMap here. It might make sense to make these
|
|
|
|
// the same data structure.
|
|
|
|
let mut result: HashMap<String, Value> = engine_state
|
|
|
|
.env_vars
|
|
|
|
.iter()
|
|
|
|
.filter(|(k, _)| !self.env_hidden.contains(*k))
|
|
|
|
.map(|(k, v)| (k.clone(), v.clone()))
|
|
|
|
.collect();
|
2021-11-30 06:14:05 +00:00
|
|
|
|
|
|
|
for scope in &self.env_vars {
|
|
|
|
result.extend(scope.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
2021-09-19 19:29:58 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 18:02:03 +00:00
|
|
|
/// Same as get_env_vars, but returns only the names as a HashSet
|
|
|
|
pub fn get_env_var_names(&self, engine_state: &EngineState) -> HashSet<String> {
|
|
|
|
let mut result: HashSet<String> = engine_state
|
|
|
|
.env_vars
|
|
|
|
.keys()
|
|
|
|
.filter(|k| !self.env_hidden.contains(*k))
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
for scope in &self.env_vars {
|
|
|
|
let scope_keys: HashSet<String> = scope.keys().cloned().collect();
|
|
|
|
result.extend(scope_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2022-01-04 22:30:34 +00:00
|
|
|
pub fn get_env_var(&self, engine_state: &EngineState, name: &str) -> Option<Value> {
|
2021-11-30 06:14:05 +00:00
|
|
|
for scope in self.env_vars.iter().rev() {
|
|
|
|
if let Some(v) = scope.get(name) {
|
2021-12-17 01:04:54 +00:00
|
|
|
return Some(v.clone());
|
2021-11-30 06:14:05 +00:00
|
|
|
}
|
2021-10-25 04:01:02 +00:00
|
|
|
}
|
2021-11-30 06:14:05 +00:00
|
|
|
|
2022-01-04 22:30:34 +00:00
|
|
|
if self.env_hidden.contains(name) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
engine_state.env_vars.get(name).cloned()
|
|
|
|
}
|
2021-10-02 13:10:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 18:02:03 +00:00
|
|
|
pub fn has_env_var(&self, engine_state: &EngineState, name: &str) -> bool {
|
|
|
|
for scope in self.env_vars.iter().rev() {
|
|
|
|
if scope.contains_key(name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.env_hidden.contains(name) {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
engine_state.env_vars.contains_key(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 22:30:34 +00:00
|
|
|
pub fn remove_env_var(&mut self, engine_state: &EngineState, name: &str) -> Option<Value> {
|
2021-11-30 06:14:05 +00:00
|
|
|
for scope in self.env_vars.iter_mut().rev() {
|
|
|
|
if let Some(v) = scope.remove(name) {
|
|
|
|
return Some(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 22:30:34 +00:00
|
|
|
if self.env_hidden.contains(name) {
|
|
|
|
// the environment variable is already hidden
|
|
|
|
None
|
|
|
|
} else if let Some(val) = engine_state.env_vars.get(name) {
|
|
|
|
// the environment variable was found in the engine state => mark it as hidden
|
|
|
|
self.env_hidden.insert(name.to_string());
|
|
|
|
Some(val.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2021-11-15 23:16:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 22:28:01 +00:00
|
|
|
// pub fn get_config(&self) -> Result<Config, ShellError> {
|
|
|
|
// let config = self.get_var(CONFIG_VARIABLE_ID, Span::new(0, 0));
|
|
|
|
|
|
|
|
// match config {
|
|
|
|
// Ok(config) => config.into_config(),
|
|
|
|
// Err(e) => Err(e),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// pub fn update_config(&mut self, name: &str, value: Value) {
|
|
|
|
// if let Some(Value::Record { cols, vals, .. }) = self.vars.get_mut(&CONFIG_VARIABLE_ID) {
|
|
|
|
// for col_val in cols.iter().zip(vals.iter_mut()) {
|
|
|
|
// if col_val.0 == name {
|
|
|
|
// *col_val.1 = value;
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// cols.push(name.to_string());
|
|
|
|
// vals.push(value);
|
|
|
|
// }
|
|
|
|
// }
|
2021-11-14 19:25:57 +00:00
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
pub fn print_stack(&self) {
|
2021-10-25 20:04:23 +00:00
|
|
|
println!("vars:");
|
|
|
|
for (var, val) in &self.vars {
|
|
|
|
println!(" {}: {:?}", var, val);
|
|
|
|
}
|
2021-11-30 06:14:05 +00:00
|
|
|
for (i, scope) in self.env_vars.iter().rev().enumerate() {
|
|
|
|
println!("env vars, scope {} (from the last);", i);
|
|
|
|
for (var, val) in scope {
|
2021-12-17 01:04:54 +00:00
|
|
|
println!(" {}: {:?}", var, val.clone().debug_value());
|
2021-11-30 06:14:05 +00:00
|
|
|
}
|
2021-08-15 22:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|