pub struct ShellState {Show 15 fields
pub env: ShellEnv,
pub positional: Vec<SmolStr>,
pub last_status: i32,
pub cwd: String,
pub lineno: u32,
pub random_seed: Cell<u32>,
pub start_time: Instant,
pub func_stack: Vec<SmolStr>,
pub source_stack: Vec<SmolStr>,
pub script_name: Option<SmolStr>,
pub shell_pid: u32,
pub last_background_pid: Option<u32>,
pub last_argument: SmolStr,
pub dir_stack: Vec<SmolStr>,
pub umask: u32,
}Expand description
Complete shell state: variables, positional params, status, cwd, functions.
Fields§
§env: ShellEnvVariable scopes.
positional: Vec<SmolStr>Positional parameters ($1, $2, …).
last_status: i32Last exit status ($?).
cwd: StringCurrent working directory ($PWD).
lineno: u32Current line number ($LINENO), updated by the runtime.
random_seed: Cell<u32>PRNG seed for $RANDOM (XorShift32). Uses Cell so get_var can remain &self.
start_time: InstantSeconds elapsed since shell start ($SECONDS).
func_stack: Vec<SmolStr>Function call stack for $FUNCNAME.
source_stack: Vec<SmolStr>Source file stack for $BASH_SOURCE.
script_name: Option<SmolStr>Override for $0 when executing a script (e.g. bash script.sh).
When None, $0 returns the default "wasmsh".
shell_pid: u32Pseudo shell pid used for $$.
last_background_pid: Option<u32>Last background pid used for $!.
last_argument: SmolStrLast argument of the previously executed command ($_).
dir_stack: Vec<SmolStr>Directory stack used by dirs/pushd/popd.
umask: u32Shell umask used by the umask builtin.
Implementations§
Source§impl ShellState
impl ShellState
pub fn new() -> Self
Sourcepub fn get_var(&self, name: &str) -> Option<SmolStr>
pub fn get_var(&self, name: &str) -> Option<SmolStr>
Get the value of a special parameter or named variable.
For arrays accessed as scalars, returns all values joined by space.
Dynamic variables ($RANDOM, $LINENO, $SECONDS, $FUNCNAME, $BASH_SOURCE) are
resolved on access.
Sourcepub fn set_last_argument(&mut self, arg: impl Into<SmolStr>)
pub fn set_last_argument(&mut self, arg: impl Into<SmolStr>)
Record the last argument for $_.
Sourcepub fn set_last_background_pid(&mut self, pid: Option<u32>)
pub fn set_last_background_pid(&mut self, pid: Option<u32>)
Update the tracked background pid used by $!.
Sourcepub fn set_var(&mut self, name: SmolStr, value: SmolStr)
pub fn set_var(&mut self, name: SmolStr, value: SmolStr)
Set a named variable (not a special parameter).
Preserves exported and readonly flags if the variable already exists.
If the variable is readonly, the write is silently skipped (like bash).
Setting a scalar value on an existing array variable replaces element 0
for indexed arrays, or replaces it entirely with a scalar for assoc arrays.
Sourcepub fn set_var_checked(
&mut self,
name: SmolStr,
value: SmolStr,
) -> Result<(), String>
pub fn set_var_checked( &mut self, name: SmolStr, value: SmolStr, ) -> Result<(), String>
Set a variable, returning an error if it is readonly.
Sourcepub fn set_readonly(&mut self, name: SmolStr, value: SmolStr)
pub fn set_readonly(&mut self, name: SmolStr, value: SmolStr)
Mark a variable as readonly with the given value.
Sourcepub fn unset_var(&mut self, name: &str) -> Result<(), String>
pub fn unset_var(&mut self, name: &str) -> Result<(), String>
Remove a variable. Returns error if readonly.
Sourcepub fn set_nounset_error(&mut self, name: &str)
pub fn set_nounset_error(&mut self, name: &str)
Record that name was expanded while nounset was active. The pending
error is consumed by ShellState::take_nounset_error at the next
command dispatch point, which matches bash semantics of reporting the
first offender and aborting the current simple command.
Sourcepub fn take_nounset_error(&mut self) -> Option<SmolStr>
pub fn take_nounset_error(&mut self) -> Option<SmolStr>
Consume the pending nounset error, if any. Returns the offending variable name and clears the sentinel so the next command starts clean.
Sourcepub fn var_names_with_prefix(&self, prefix: &str) -> Vec<SmolStr>
pub fn var_names_with_prefix(&self, prefix: &str) -> Vec<SmolStr>
Return all variable names (across all scopes) that start with the given prefix. Innermost scope wins for shadowed names. Results are sorted alphabetically.
Sourcepub fn get_array_element(&self, name: &str, index: &str) -> Option<SmolStr>
pub fn get_array_element(&self, name: &str, index: &str) -> Option<SmolStr>
Get a single element from an array (or scalar when index is “0”).
For indexed arrays, the index is parsed as usize.
For associative arrays, the index is used as-is.
For scalars, index “0” returns the scalar value.
Sourcepub fn set_array_element(&mut self, name: SmolStr, index: &str, value: SmolStr)
pub fn set_array_element(&mut self, name: SmolStr, index: &str, value: SmolStr)
Set a single element in an array. Creates an indexed array if the variable does not exist. Converts a scalar to an indexed array if needed.
Sourcepub fn get_array_keys(&self, name: &str) -> Vec<String>
pub fn get_array_keys(&self, name: &str) -> Vec<String>
Get all keys/indices of an array variable.
Sourcepub fn get_array_values(&self, name: &str) -> Vec<SmolStr>
pub fn get_array_values(&self, name: &str) -> Vec<SmolStr>
Get all values of an array variable.
Sourcepub fn get_array_length(&self, name: &str) -> usize
pub fn get_array_length(&self, name: &str) -> usize
Get the number of elements in an array.
Sourcepub fn append_array(&mut self, name: &str, values: Vec<SmolStr>)
pub fn append_array(&mut self, name: &str, values: Vec<SmolStr>)
Append values to an indexed array (arr+=(val1 val2)).
If the variable is a scalar, it is first converted to an indexed array
with the scalar as element 0.
Sourcepub fn unset_array_element(&mut self, name: &str, index: &str)
pub fn unset_array_element(&mut self, name: &str, index: &str)
Remove a single element from an array.
Sourcepub fn init_indexed_array(&mut self, name: SmolStr)
pub fn init_indexed_array(&mut self, name: SmolStr)
Initialize an empty indexed array variable.
Sourcepub fn init_assoc_array(&mut self, name: SmolStr)
pub fn init_assoc_array(&mut self, name: SmolStr)
Initialize an empty associative array variable.
Trait Implementations§
Source§impl Clone for ShellState
impl Clone for ShellState
Source§fn clone(&self) -> ShellState
fn clone(&self) -> ShellState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more