pub struct ReplSession { /* private fields */ }Expand description
Persistent state for an interactive REPL session. Unlike
the one-shot run / [Evaluator::run] pair, a
ReplSession carries its scopes, fns, user types, method
tables, and import cache across calls, so
session.eval("let x = 5") followed by
session.eval("print(x)") sees the same x.
Persistent state for an interactive REPL session. Build
once with Self::new, then call Self::eval for each
fresh line / block the user types. State carried across
calls: every let / const / fn / struct / enum /
method declaration, every use’d module’s bindings and
aliases, the global rng seed, and the import cache.
A ReplSession is not a sandboxing boundary — embedders
that want a fresh identity between inputs should construct
a new session. Resource limits (steps, memory) are supplied
per eval call since they reset each time.
Implementations§
Source§impl ReplSession
impl ReplSession
Sourcepub fn new() -> Self
pub fn new() -> Self
Build a fresh session. Type tables are pre-seeded with
the engine builtins (Result, RuntimeError) so
Result::Ok(...) resolves without an explicit use.
Sourcepub fn get(&self, name: &str) -> Option<Value>
pub fn get(&self, name: &str) -> Option<Value>
Look up a binding by name. Convenience for tests and
embedders that want to peek at the session’s state
between eval calls — e.g. checking that let x = 5
actually stuck.
Sourcepub fn binding_names(&self) -> Vec<String>
pub fn binding_names(&self) -> Vec<String>
Every currently-bound name in the root scope, sorted.
Handy for REPL introspection commands (:vars) and
for tab-completers that want to stay honest about
what’s actually in scope rather than guessing from
observed tokens.
Sourcepub fn eval<H: BopHost>(
&mut self,
source: &str,
host: &mut H,
limits: &BopLimits,
) -> Result<Option<Value>, BopError>
pub fn eval<H: BopHost>( &mut self, source: &str, host: &mut H, limits: &BopLimits, ) -> Result<Option<Value>, BopError>
Parse source and run its statements against this
session’s accumulated state.
If the last statement is a bare expression
(ExprStmt), the session evaluates it and returns its
value as Ok(Some(v)) so the REPL can echo the
result. Every other shape — let x = ..., fn foo,
struct, use, loops — returns Ok(None).
Partial failure semantics: if an earlier statement errors, the session’s state reflects whatever ran before the failure. That matches what a user would expect from an interactive prompt.