cljrs 0.1.249

clojurust CLI — run, repl, compile, and eval
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! The runtime session a subcommand evaluates in: how it is built, and how
//! source is run inside it.
//!
//! Everything here is shared by more than one subcommand. `run`, `repl`,
//! `eval`, `test`, and `nrepl` all build their environment with
//! [`setup_globals`] and evaluate through [`eval_in`] / [`eval_form`], so the
//! `cljrs.edn` wiring, the JIT policy, and the async driver are identical no
//! matter which one the user typed.

use std::path::{Path, PathBuf};
use std::sync::Arc;

use cljrs_gc::GcConfig;
use cljrs_runtime::tiered::{Env, EvalError, GlobalEnv, eval};
use cljrs_value::Value;

use crate::native;

/// Build GC config from CLI flags, or use defaults if not specified.
pub fn build_gc_config(
    soft_limit_mb: Option<usize>,
    hard_limit_mb: Option<usize>,
) -> Arc<GcConfig> {
    match (soft_limit_mb, hard_limit_mb) {
        (Some(soft), Some(hard)) => Arc::new(GcConfig::with_limits(
            soft * 1024 * 1024,
            hard * 1024 * 1024,
        )),
        (Some(soft), None) => Arc::new(GcConfig::with_hard_limit(soft * 1024 * 1024)),
        (None, Some(hard)) => Arc::new(GcConfig::with_hard_limit(hard * 1024 * 1024)),
        (None, None) => Arc::new(GcConfig::new()),
    }
}

/// CLI-level versioned-symbol policy flags, threaded into `setup_globals`.
#[derive(Clone, Copy, Default)]
pub struct VersioningFlags {
    /// `--verify-commit-signatures` / `:verify-commit-signatures`.
    pub verify_commit_signatures: bool,
    /// `--enforce-native-versions` / `:enforce-native-versions`.
    pub enforce_native_versions: bool,
}

/// Whether runtimes built by this process get a JIT tier attached.
static JIT_ENABLED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Decide the JIT policy from CLI flags and env vars.
///
/// JIT is enabled by default; disable with `CLJRS_NO_JIT=1` or `--jit-threshold 0`.
/// The backend itself is attached per runtime, in [`setup_globals`].
pub fn configure_jit(threshold: Option<u32>) {
    if std::env::var("CLJRS_NO_JIT").is_ok() {
        return;
    }
    if let Some(0) = threshold {
        return; // Explicitly disabled via --jit-threshold 0.
    }
    if let Some(t) = threshold {
        cljrs_runtime::tiered::jit_state::set_jit_threshold(t);
    }
    JIT_ENABLED.store(true, std::sync::atomic::Ordering::Relaxed);
}

/// Create a fully initialised `GlobalEnv` with stdlib, user source paths, GC
/// config, and any `cljrs.edn` found in the current working directory.
///
/// Paths declared in `:paths` of `cljrs.edn` are appended to `src_paths` (CLI
/// flags take precedence).  The parsed `DepsConfig` is stored in
/// `GlobalEnv.deps_config` so that versioned symbol resolution and the
/// `deps fetch`/`deps status` commands share the same config object.
pub fn setup_globals(
    src_paths: Vec<PathBuf>,
    gc_config: Arc<GcConfig>,
    versioning: VersioningFlags,
) -> Arc<GlobalEnv> {
    let runtime = cljrs_runtime::Runtime::builder()
        .execution_mode(cljrs_runtime::ExecutionMode::Tiered)
        .source_paths(src_paths)
        .gc_config(gc_config)
        .build()
        .unwrap_or_else(|e| {
            eprintln!("failed to start the runtime: {e}");
            std::process::exit(1);
        });
    // Attach the JIT tier to this runtime (unless disabled on the command
    // line or by `CLJRS_NO_JIT`); nothing has been lowered yet, so no promotion
    // can be missed.
    if JIT_ENABLED.load(std::sync::atomic::Ordering::Relaxed) {
        cljrs_compiler::jit::install(&runtime);
    }
    cljrs_stdlib::install(&runtime);
    let globals = runtime.into_globals();
    if versioning.verify_commit_signatures {
        globals
            .verify_commit_signatures
            .store(true, std::sync::atomic::Ordering::Relaxed);
    }
    if versioning.enforce_native_versions {
        globals.set_enforce_native_versions(true);
    }
    // Opt-in pinned native packages (:rust/load :dylib in cljrs.edn).
    native::pinned::install(&globals);
    if let Ok(cwd) = std::env::current_dir() {
        apply_deps_config(&globals, &cwd);
    }
    // Initialise async inside the LocalSet so `cljrs_async::init` can spawn its
    // background GC-service task (it calls `spawn_local`, which requires a
    // LocalSet context). The task persists on the LocalSet and is serviced by
    // each subsequent per-form drive in `eval_form`.
    #[cfg(feature = "async")]
    ASYNC_DRIVER.with(|d| {
        let guard = d.borrow();
        let init = |g: &Arc<GlobalEnv>| {
            cljrs_async::init(g);
            cljrs_io::init(g);
            #[cfg(feature = "net")]
            cljrs_net::init(g);
            #[cfg(feature = "charset")]
            cljrs_charset::init(g);
        };
        match guard.as_ref() {
            Some(drv) => drv.local.block_on(&drv.rt, async { init(&globals) }),
            None => init(&globals),
        }
    });
    #[cfg(feature = "base64")]
    cljrs_base64::init(&globals);
    globals
}

/// Load the nearest `cljrs.edn` and wire its data into `globals`.
///
/// Silently does nothing when no config file is found; prints a warning to
/// stderr when the file exists but cannot be parsed.
fn apply_deps_config(globals: &Arc<GlobalEnv>, cwd: &Path) {
    match cljrs_project::config::load_config(cwd) {
        Ok(Some(config)) => {
            // Append edn :paths to the source-path list (CLI paths come first).
            {
                let mut paths = globals.source_paths.write().unwrap();
                for p in &config.paths {
                    if !paths.contains(p) {
                        paths.push(p.clone());
                    }
                }
            }
            // Append each dependency's own source roots so a plain `require` of
            // a dep's namespace resolves (git deps are materialized from the
            // local cache — run `cljrs deps fetch` first; no network here).
            add_dep_source_paths(globals, &config);
            if config.verify_commit_signatures {
                globals
                    .verify_commit_signatures
                    .store(true, std::sync::atomic::Ordering::Relaxed);
            }
            globals.load_trusted_signers(&config);
            if config.enforce_native_versions {
                globals.set_enforce_native_versions(true);
            }
            // Load the native shared library (if :rust is configured) so that
            // native functions are registered before any Clojure code runs.
            if let Some(rust_config) = &config.rust {
                native::load_project_lib(rust_config, globals);
            }
            *globals.deps_config.write().unwrap() = Some(Arc::new(config));
        }
        Ok(None) => {}
        Err(e) => eprintln!("cljrs: warning: could not load cljrs.edn: {e}"),
    }
}

/// Append each declared dependency's own source roots to `globals.source_paths`
/// so a plain `(require '[dep.ns :as …])` resolves namespaces provided by a
/// dependency rather than only by the running binary or the local project.
///
/// - **Local deps** (`:local/root`) contribute paths from the directory on disk.
/// - **Git deps** are materialized from the local bare cache at their pinned
///   `:git/sha` (no network — `cljrs deps fetch` must have populated it).  A
///   missing cache warns and is skipped rather than aborting the run.
///
/// Each dep's roots come from its own `cljrs.edn` `:paths` (resolved relative to
/// the dep root), defaulting to `src/`.  Only directories that actually exist
/// are added; pure-native deps (no Clojure source) contribute nothing here and
/// are brought in by the native-`require` loader instead.
fn add_dep_source_paths(globals: &Arc<GlobalEnv>, config: &cljrs_project::config::DepsConfig) {
    for (name, dep) in &config.deps {
        let root = match dep {
            cljrs_project::config::Dependency::Local { root } => {
                if root.is_dir() {
                    root.clone()
                } else {
                    eprintln!(
                        "cljrs: warning: local dep {name} not found at {}",
                        root.display()
                    );
                    continue;
                }
            }
            cljrs_project::config::Dependency::Git(git) => {
                match cljrs_project::vcs::worktree_at_commit(&git.url, &git.sha) {
                    Ok(p) => p,
                    Err(e) => {
                        eprintln!(
                            "cljrs: warning: git dep {name} ({}) is not available ({e}); \
                             run `cljrs deps fetch`",
                            git.url
                        );
                        continue;
                    }
                }
            }
        };
        let mut paths = globals.source_paths.write().unwrap();
        for p in dep_source_paths(&root) {
            if p.is_dir() && !paths.contains(&p) {
                paths.push(p);
            }
        }
    }
}

/// Collect source paths contributed by every declared dependency in `config`.
///
/// Git deps are resolved from the local cache (run `cljrs deps fetch` first);
/// missing deps emit a warning and are skipped.
pub fn collect_dep_src_paths(config: &cljrs_project::config::DepsConfig) -> Vec<PathBuf> {
    let mut paths = Vec::new();
    for (name, dep) in &config.deps {
        let root = match dep {
            cljrs_project::config::Dependency::Local { root } => {
                if root.is_dir() {
                    root.clone()
                } else {
                    eprintln!(
                        "cljrs: warning: local dep {name} not found at {}",
                        root.display()
                    );
                    continue;
                }
            }
            cljrs_project::config::Dependency::Git(git) => {
                match cljrs_project::vcs::worktree_at_commit(&git.url, &git.sha) {
                    Ok(p) => p,
                    Err(e) => {
                        eprintln!(
                            "cljrs: warning: git dep {name} ({}) is not available ({e}); \
                             run `cljrs deps fetch`",
                            git.url
                        );
                        continue;
                    }
                }
            }
        };
        for p in dep_source_paths(&root) {
            if p.is_dir() && !paths.contains(&p) {
                paths.push(p);
            }
        }
    }
    paths
}

/// The source roots a dependency at `root` contributes: its `cljrs.edn`
/// `:paths` (already resolved to absolute paths relative to the dep root) if a
/// parseable config is present, else the conventional `<root>/src`.
fn dep_source_paths(root: &Path) -> Vec<PathBuf> {
    let cfg_path = root.join("cljrs.edn");
    if cfg_path.exists()
        && let Ok(src) = std::fs::read_to_string(&cfg_path)
        && let Ok(parsed) = cljrs_project::config::parse_config(&src, &cfg_path)
        && !parsed.paths.is_empty()
    {
        return parsed.paths;
    }
    vec![root.join("src")]
}

/// Evaluate all forms in `src`, printing nothing. Returns the last value.
/// Convert a file path relative to the source root into a Clojure namespace name.
/// e.g. `test/clojure/core_test/juxt.cljc` relative to `test/` → `clojure.core-test.juxt`
pub fn file_to_namespace(root: &PathBuf, file: &Path) -> Option<String> {
    let rel = file.strip_prefix(root).ok()?;
    let stem = rel.with_extension(""); // remove .cljc / .cljrs
    let ns = stem
        .to_string_lossy()
        .replace(std::path::MAIN_SEPARATOR, ".")
        .replace('_', "-");
    Some(ns)
}

pub fn eval_source(src: &str, filename: &str, globals: Arc<GlobalEnv>) -> miette::Result<Value> {
    let mut env = Env::new(globals, "user");
    eval_in(&mut env, src, filename)
}

/// Run a source file: evaluate all top-level forms, then call `-main` if defined.
pub fn run_source(
    src: &str,
    filename: &str,
    globals: Arc<GlobalEnv>,
    args: &[String],
) -> miette::Result<()> {
    let mut env = Env::new(globals, "user");
    eval_in(&mut env, src, filename)?;
    call_main_if_defined(&mut env, args)?;
    Ok(())
}

/// Call `-main` in the current namespace if it is defined, passing `args` as
/// individual string arguments. Silently skips if `-main` is not defined.
fn call_main_if_defined(env: &mut Env, args: &[String]) -> miette::Result<()> {
    // resolve returns nil for undefined symbols; swallow lookup errors defensively.
    let resolved = eval_in(env, "(resolve '-main)", "<main-check>").unwrap_or(Value::Nil);
    if resolved == Value::Nil {
        return Ok(());
    }
    let escaped: Vec<String> = args.iter().map(|s| escape_clojure_string(s)).collect();
    let call = format!("(-main {})", escaped.join(" "));
    let result = eval_in(env, &call, "<main>")?;
    // An `^:async` `-main` returns a `Future` immediately, with its body
    // queued as a task on the shared `LocalSet`. Await that future so the body
    // (and anything it spawns) runs to completion before the process exits;
    // for a synchronous `-main` this is a no-op pass-through.
    await_main_result(result)?;
    Ok(())
}

/// Drive `value` to a settled value on the shared async `LocalSet`. If `value`
/// is a `Future`/`Promise` (e.g. the result of an `^:async` `-main`), this
/// yields until it resolves; any other value is returned unchanged.
#[cfg(feature = "async")]
fn await_main_result(value: Value) -> miette::Result<()> {
    ASYNC_DRIVER.with(|d| {
        let guard = d.borrow();
        match guard.as_ref() {
            Some(drv) => {
                drv.local
                    .block_on(&drv.rt, cljrs_async::eval_async::await_value(value))
                    .map_err(format_eval_error)?;
                Ok(())
            }
            // No driver installed: nothing to await against, so leave as-is.
            None => Ok(()),
        }
    })
}

#[cfg(not(feature = "async"))]
fn await_main_result(_value: Value) -> miette::Result<()> {
    Ok(())
}

/// Produce a Clojure string literal (double-quoted, with escapes) for `s`.
fn escape_clojure_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for ch in s.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// Evaluate `src` in an existing `Env`. Returns the last value.
pub fn eval_in(env: &mut Env, src: &str, filename: &str) -> miette::Result<Value> {
    let mut parser = cljrs_reader::Parser::new(src.to_string(), filename.to_string());
    let forms = parser.parse_all().map_err(miette::Report::from)?;

    let mut result = Value::Nil;
    for form in forms {
        let _alloc_frame = cljrs_gc::push_alloc_frame();
        result = eval_form(&form, env).map_err(format_eval_error)?;
    }
    Ok(result)
}

/// The Tokio runtime + `LocalSet` that drive async evaluation, installed once
/// by [`with_async_driver`] and reused for the lifetime of the process.
#[cfg(feature = "async")]
struct AsyncDriver {
    rt: tokio::runtime::Runtime,
    local: tokio::task::LocalSet,
}

#[cfg(feature = "async")]
thread_local! {
    static ASYNC_DRIVER: std::cell::RefCell<Option<AsyncDriver>> =
        const { std::cell::RefCell::new(None) };
}

/// Run `f` with an async driver installed on this thread.
///
/// Builds the single-threaded runtime + `LocalSet` that every async task
/// (core.async producers, `^:async` calls, `cljrs-io` readers) runs on, and
/// stashes it so [`eval_form`] can drive each top-level form on it.  We
/// deliberately do *not* wrap `f` in a single `block_on`/`run_until`:
/// top-level evaluation is synchronous (it reads stdin, dispatches commands),
/// and each form is driven individually via `LocalSet::block_on`, which would
/// panic if nested inside an outer `block_on`.
#[cfg(feature = "async")]
pub fn with_async_driver<T>(f: impl FnOnce() -> T) -> T {
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .expect("failed to build Tokio runtime");
    let local = tokio::task::LocalSet::new();
    ASYNC_DRIVER.with(|d| *d.borrow_mut() = Some(AsyncDriver { rt, local }));
    let result = f();
    ASYNC_DRIVER.with(|d| *d.borrow_mut() = None);
    result
}

/// Evaluate a single top-level form.
///
/// With the `async` feature, the form is driven on the shared `LocalSet` via
/// [`tokio::task::LocalSet::block_on`], so spawned tasks — core.async channel
/// producers, `^:async` calls, the `cljrs-io` readers/writers — make progress
/// and a top-level `await` resolves. Tasks that outlive a form (e.g. a producer
/// feeding a channel `def`d in one REPL line and consumed in the next) stay
/// queued on the `LocalSet` and continue on the next form's drive. Without the
/// feature it is a plain synchronous `eval`.
#[cfg(feature = "async")]
#[allow(clippy::result_large_err)]
pub fn eval_form(form: &cljrs_reader::Form, env: &mut Env) -> Result<Value, EvalError> {
    ASYNC_DRIVER.with(|d| {
        let guard = d.borrow();
        match guard.as_ref() {
            Some(drv) => drv
                .local
                .block_on(&drv.rt, cljrs_async::eval_async::eval_async(form, env)),
            // No driver installed (shouldn't happen once `main` runs): fall back
            // to a synchronous evaluation rather than panicking.
            None => eval(form, env),
        }
    })
}

#[cfg(not(feature = "async"))]
#[allow(clippy::result_large_err)]
pub fn eval_form(form: &cljrs_reader::Form, env: &mut Env) -> Result<Value, EvalError> {
    eval(form, env)
}

pub fn format_eval_error(e: EvalError) -> miette::Report {
    match e {
        EvalError::Thrown(val) => miette::miette!("Unhandled exception: {}", val),
        EvalError::UnboundSymbol(s) => miette::miette!("Unable to resolve symbol: {}", s),
        EvalError::Arity {
            name,
            expected,
            got,
        } => miette::miette!("Wrong number of args ({got}) passed to {name}; expected {expected}"),
        EvalError::NotCallable(s) => miette::miette!("Not a function: {}", s),
        EvalError::Runtime(msg) => miette::miette!("{}", msg),
        EvalError::GasExhausted => miette::miette!("gas exhausted"),
        EvalError::ForbiddenEffect(operation) => {
            miette::miette!("effect forbidden in transaction function: {operation}")
        }
        EvalError::Read(e) => miette::Report::from(e),
        EvalError::Recur(_) => miette::miette!("recur outside of loop/fn"),
        EvalError::CommitSignatureVerificationFailed { commit, reason } => {
            miette::miette!("commit {commit:?} failed signature verification: {reason}")
        }
    }
}