nushell/crates/nu-protocol/src/engine/stack.rs

113 lines
3.6 KiB
Rust
Raw Normal View History

2021-10-25 18:58:58 +02:00
use std::collections::HashMap;
2021-08-16 00:33:34 +02:00
use crate::{Config, ShellError, Value, VarId, CONFIG_VARIABLE_ID};
2021-08-16 00:33:34 +02: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 06:01:02 +02:00
#[derive(Debug, Clone)]
2021-10-25 22:04:23 +02:00
pub struct Stack {
2021-08-16 00:33:34 +02:00
pub vars: HashMap<VarId, Value>,
pub env_vars: HashMap<String, String>,
}
impl Default for Stack {
fn default() -> Self {
Self::new()
}
}
impl Stack {
pub fn new() -> Stack {
2021-10-25 22:04:23 +02:00
Stack {
2021-08-16 00:33:34 +02:00
vars: HashMap::new(),
env_vars: HashMap::new(),
2021-10-25 22:04:23 +02:00
}
2021-08-16 00:33:34 +02:00
}
pub fn get_var(&self, var_id: VarId) -> Result<Value, ShellError> {
2021-10-25 22:04:23 +02:00
if let Some(v) = self.vars.get(&var_id) {
return Ok(v.clone());
2021-08-16 00:33:34 +02:00
}
2021-10-25 06:01:02 +02:00
Err(ShellError::InternalError("variable not found".into()))
2021-08-16 00:33:34 +02:00
}
2021-10-25 06:01:02 +02:00
pub fn add_var(&mut self, var_id: VarId, value: Value) {
2021-10-25 22:04:23 +02:00
self.vars.insert(var_id, value);
2021-08-16 00:33:34 +02:00
}
2021-10-25 06:01:02 +02:00
pub fn add_env_var(&mut self, var: String, value: String) {
2021-10-25 22:04:23 +02:00
self.env_vars.insert(var, value);
2021-08-16 00:33:34 +02:00
}
2021-10-25 22:04:23 +02:00
pub fn collect_captures(&self, captures: &[VarId]) -> Stack {
let mut output = Stack::new();
for capture in captures {
2021-10-25 23:14:21 +02: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
if let Ok(value) = self.get_var(*capture) {
output.vars.insert(*capture, value);
}
2021-10-25 22:04:23 +02:00
}
2021-10-25 06:01:02 +02:00
2021-11-04 03:32:35 +01:00
// FIXME: this is probably slow
output.env_vars = self.env_vars.clone();
let config = self
.get_var(CONFIG_VARIABLE_ID)
.expect("internal error: config is missing");
output.vars.insert(CONFIG_VARIABLE_ID, config);
2021-10-25 06:01:02 +02:00
output
2021-08-16 00:33:34 +02:00
}
2021-09-19 21:29:58 +02:00
pub fn get_env_vars(&self) -> HashMap<String, String> {
2021-10-25 22:04:23 +02:00
self.env_vars.clone()
2021-09-19 21:29:58 +02:00
}
2021-10-02 15:10:28 +02:00
pub fn get_env_var(&self, name: &str) -> Option<String> {
2021-10-25 22:04:23 +02:00
if let Some(v) = self.env_vars.get(name) {
return Some(v.to_string());
2021-10-25 06:01:02 +02:00
}
None
2021-10-02 15:10:28 +02:00
}
pub fn get_config(&self) -> Result<Config, ShellError> {
let config = self.get_var(CONFIG_VARIABLE_ID);
match config {
Ok(config) => config.into_config(),
Err(e) => {
println!("Can't find {} in {:?}", CONFIG_VARIABLE_ID, self);
Err(e)
}
}
}
2021-08-16 00:33:34 +02:00
pub fn print_stack(&self) {
2021-10-25 22:04:23 +02:00
println!("vars:");
for (var, val) in &self.vars {
println!(" {}: {:?}", var, val);
}
println!("env vars:");
for (var, val) in &self.env_vars {
println!(" {}: {:?}", var, val);
2021-08-16 00:33:34 +02:00
}
}
}