pub struct EngineState {
    pub scope: ScopeFrame,
    pub ctrlc: Option<Arc<AtomicBool>>,
    pub env_vars: EnvVars,
    pub previous_env_vars: HashMap<String, Value>,
    pub config: Config,
    pub pipeline_externals_state: Arc<(AtomicU32, AtomicU32)>,
    pub repl_buffer_state: Arc<Mutex<Option<String>>>,
    pub repl_operation_queue: Arc<Mutex<VecDeque<ReplOperation>>>,
    pub history_session_id: i64,
    pub currently_parsed_cwd: Option<PathBuf>,
    pub regex_cache: Arc<Mutex<LruCache<String, Regex>>>,
    /* private fields */
}
Expand description

The core global engine state. This includes all global definitions as well as any global state that will persist for the whole session.

Declarations, variables, blocks, and other forms of data are held in the global state and referenced elsewhere using their IDs. These IDs are simply their index into the global state. This allows us to more easily handle creating blocks, binding variables and callsites, and more, because each of these will refer to the corresponding IDs rather than their definitions directly. At runtime, this means less copying and smaller structures.

Note that the runtime stack is not part of this global state. Runtime stacks are handled differently, but they also rely on using IDs rather than full definitions.

A note on implementation:

Much of the global definitions are built on the Bodil’s ‘im’ crate. This gives us a way of working with lists of definitions in a way that is very cheap to access, while also allowing us to update them at key points in time (often, the transition between parsing and evaluation).

Over the last two years we tried a few different approaches to global state like this. I’ll list them here for posterity, so we can more easily know how we got here:

  • Rc - Rc is cheap, but not thread-safe. The moment we wanted to work with external processes, we needed a way send to stdin/stdout. In Rust, the current practice is to spawn a thread to handle both. These threads would need access to the global state, as they’ll need to process data as it streams out of the data pipeline. Because Rc isn’t thread-safe, this breaks.

  • Arc - Arc is the thread-safe version of the above. Often Arc is used in combination with a Mutex or RwLock, but you can use Arc by itself. We did this a few places in the original Nushell. This can work but because of Arc’s nature of not allowing mutation if there’s a second copy of the Arc around, this ultimately becomes limiting.

  • Arc + Mutex/RwLock - the standard practice for thread-safe containers. Unfortunately, this would have meant we would incur a lock penalty every time we needed to access any declaration or block. As we would be reading far more often than writing, it made sense to explore solutions that favor large amounts of reads.

  • im - the im crate was ultimately chosen because it has some very nice properties: it gives the ability to cheaply clone these structures, which is nice as EngineState may need to be cloned a fair bit to follow ownership rules for closures and iterators. It also is cheap to access. Favoring reads here fits more closely to what we need with Nushell. And, of course, it’s still thread-safe, so we get the same benefits as above.

Fields§

§scope: ScopeFrame§ctrlc: Option<Arc<AtomicBool>>§env_vars: EnvVars§previous_env_vars: HashMap<String, Value>§config: Config§pipeline_externals_state: Arc<(AtomicU32, AtomicU32)>§repl_buffer_state: Arc<Mutex<Option<String>>>§repl_operation_queue: Arc<Mutex<VecDeque<ReplOperation>>>§history_session_id: i64§currently_parsed_cwd: Option<PathBuf>§regex_cache: Arc<Mutex<LruCache<String, Regex>>>

Implementations§

Merges a StateDelta onto the current state. These deltas come from a system, like the parser, that creates a new set of definitions and visible symbols in the current scope. We make this transactional as there are times when we want to run the parser and immediately throw away the results (namely: syntax highlighting and completions).

When we want to preserve what the parser has created, we can take its output (the StateDelta) and use this function to merge it into the global state.

Merge the environment from the runtime Stack into the engine state

Mark a starting point if it is a script (e.g., nu spam.nu)

Translate overlay IDs from other to IDs in self

Get all aliases within scope, sorted by the alias names

Get all commands within scope, sorted by the commads’ names

Get signatures of all commands within scope.

Get signatures of all commands within scope.

In addition to signatures, it returns whether each command is: a) a plugin b) custom

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Set the foreground color generically Read more
Set the background color generically. Read more
Change the foreground color to black
Change the background color to black
Change the foreground color to red
Change the background color to red
Change the foreground color to green
Change the background color to green
Change the foreground color to yellow
Change the background color to yellow
Change the foreground color to blue
Change the background color to blue
Change the foreground color to magenta
Change the background color to magenta
Change the foreground color to purple
Change the background color to purple
Change the foreground color to cyan
Change the background color to cyan
Change the foreground color to white
Change the background color to white
Change the foreground color to the terminal default
Change the background color to the terminal default
Change the foreground color to bright black
Change the background color to bright black
Change the foreground color to bright red
Change the background color to bright red
Change the foreground color to bright green
Change the background color to bright green
Change the foreground color to bright yellow
Change the background color to bright yellow
Change the foreground color to bright blue
Change the background color to bright blue
Change the foreground color to bright magenta
Change the background color to bright magenta
Change the foreground color to bright purple
Change the background color to bright purple
Change the foreground color to bright cyan
Change the background color to bright cyan
Change the foreground color to bright white
Change the background color to bright white
Make the text bold
Make the text dim
Make the text italicized
Make the text italicized
Make the text blink
Make the text blink (but fast!)
Swap the foreground and background colors
Hide the text
Cross out the text
Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Set the foreground color to a specific RGB value.
Set the background color to a specific RGB value.
Sets the foreground color to an RGB value.
Sets the background color to an RGB value.
Apply a runtime-determined style
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.