Skip to main content

ReplSession

Struct ReplSession 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Default for ReplSession

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.