Lines
93.48 %
Functions
67.65 %
Branches
100 %
use std::cell::RefCell;
use std::sync::{Arc, Mutex};
use crate::ctx::ScriptCtx;
/// Wasmtime Store data type for the rpc eval channel. Carries the
/// per-session user context (`ScriptCtx`) so native fns reach
/// `caller.data().ctx().user_id` directly. The legacy
/// `EvalContext` capture-protocol companion field retired alongside
/// the capture imports in P4 A6.c — `nomi-eval` now returns its
/// final value via the function's `(ref null any)` return slot.
///
/// Native fns are async (`Linker::func_wrap_async`) — they `.await`
/// `server::command::*` futures directly on whatever runtime drives the
/// surrounding `Session::handle_form` call. No owned runtime, no
/// `spawn_blocking`, no thread-local pool concerns.
pub struct SessionData {
ctx: ScriptCtx,
/// Per-request stdout sink. `env.log` (PRINT/DISPLAY/NEWLINE) appends here so
/// the mREPL (`nms --slynk-port`) can surface script output as
/// `:write-string` separately from the final value. Shared with the owning
/// `Session`, which drains it per request. The non-mrepl paths (sshd / the
/// rpc text REPL) ignore it; `env.log` still tees to `tracing` for them.
output: Arc<Mutex<String>>,
/// Render-only draft accumulator. `Some` only on the restricted template
/// render path ([`crate::template`]); the draft natives mutate it and the
/// render entry point reads it back via `store.into_data()`. `None` on the
/// normal eval channel, where the draft natives are not even linked.
draft: Option<RefCell<crate::draft::TransactionDraft>>,
/// Per-request notices: warnings a native gives alongside success. The
/// wire carries them as the response's `:notices`.
notices: Arc<Mutex<Vec<String>>>,
}
impl SessionData {
pub(crate) fn new(ctx: ScriptCtx, output: Arc<Mutex<String>>) -> Self {
Self {
ctx,
output,
draft: None,
notices: Arc::default(),
/// Collect notices into `notices`, shared with the owning `Session`.
#[must_use]
pub(crate) fn with_notices(mut self, notices: Arc<Mutex<Vec<String>>>) -> Self {
self.notices = notices;
self
/// Adds a notice to the current request's reply. A poisoned lock drops it.
pub fn push_notice(&self, notice: String) {
if let Ok(mut notices) = self.notices.lock() {
notices.push(notice);
/// Builds session data with a draft accumulator armed — the render path's
/// constructor. Draft natives require `draft` to be `Some`.
pub(crate) fn for_render(ctx: ScriptCtx, output: Arc<Mutex<String>>) -> Self {
draft: Some(RefCell::new(crate::draft::TransactionDraft::new())),
pub fn ctx(&self) -> &ScriptCtx {
&self.ctx
/// Mutates the draft accumulator if armed (render path). Returns an error
/// on the normal eval path where no draft is present — a draft native
/// reaching a non-render Store is a wiring bug, surfaced as a trap.
pub fn with_draft<F>(&self, f: F) -> wasmtime::Result<()>
where
F: FnOnce(&mut crate::draft::TransactionDraft),
{
let cell = self
.draft
.as_ref()
.ok_or_else(|| wasmtime::Error::msg("draft native invoked outside render mode"))?;
f(&mut cell.borrow_mut());
Ok(())
/// Consumes the accumulated draft, if any. Called after a render run via
/// `store.into_data()`.
pub fn into_draft(self) -> Option<crate::draft::TransactionDraft> {
self.draft.map(RefCell::into_inner)
/// Appends a script-output line to the per-request buffer. Called by the
/// `env.log` host fn. A poisoned lock is swallowed (output capture is
/// best-effort telemetry, never a reason to fail an eval).
pub fn push_output(&self, msg: &str) {
if let Ok(mut buf) = self.output.lock() {
buf.push_str(msg);