pub struct ReplSession { /* private fields */ }Expand description
Persistent state for an interactive REPL session. Unlike the one-shot
run API, 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: NyblHost + ?Sized>(
&mut self,
source: &str,
host: &mut H,
limits: &NyblLimits,
) -> Result<Option<Value>, NyblError>
pub fn eval<H: NyblHost + ?Sized>( &mut self, source: &str, host: &mut H, limits: &NyblLimits, ) -> Result<Option<Value>, NyblError>
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.
Sourcepub fn run_stmts<H: NyblHost + ?Sized>(
&mut self,
stmts: &[Stmt],
host: &mut H,
limits: &NyblLimits,
) -> Result<Option<Value>, NyblError>
pub fn run_stmts<H: NyblHost + ?Sized>( &mut self, stmts: &[Stmt], host: &mut H, limits: &NyblLimits, ) -> Result<Option<Value>, NyblError>
Like Self::eval but takes an already-parsed AST.
Useful when the caller has already run a
parse_with_warnings pass and wants to surface
warnings before executing.
Sourcepub fn call_fn<H: NyblHost + ?Sized>(
&mut self,
func: Value,
args: Vec<Value>,
host: &mut H,
limits: &NyblLimits,
) -> Result<Value, NyblError>
pub fn call_fn<H: NyblHost + ?Sized>( &mut self, func: Value, args: Vec<Value>, host: &mut H, limits: &NyblLimits, ) -> Result<Value, NyblError>
Call a Nybl closure value inside this session’s current module state.
Embedders that retain closure values from an earlier evaluation can use this to invoke them later while preserving the same functions, imports, type bindings, and module aliases that normal in-language calls see.