Skip to main content

cljrs_stdlib/
lib.rs

1//! Built-in standard library namespaces for clojurust.
2//!
3//! Registers `clojure.string`, `clojure.set`, and `clojure.test` into a
4//! [`GlobalEnv`] so they are available via `(require ...)` without needing
5//! source files on disk.
6//!
7//! ## Entry points
8//!
9//! - [`standard_env()`] — full environment for the `cljrs` binary
10//! - [`standard_env_with_paths()`] — same, plus user source paths
11//! - [`register()`] — add stdlib to an existing env (e.g. for testing)
12
13use std::sync::Arc;
14
15use cljrs_eval::GlobalEnv;
16use cljrs_gc::GcConfig;
17
18mod core_async;
19mod edn;
20pub mod io;
21mod set;
22mod string;
23// ── Embedded sources ──────────────────────────────────────────────────────────
24
25const CLOJURE_TEST_SRC: &str = include_str!("clojure/test.cljrs");
26const CLOJURE_STRING_SRC: &str = include_str!("clojure/string.cljrs");
27const CLOJURE_SET_SRC: &str = include_str!("clojure/set.cljrs");
28const CLOJURE_TEMPLATE_SRC: &str = include_str!("clojure/template.cljrs");
29const CLOJURE_RUST_IO_SRC: &str = include_str!("clojure/rust/io.cljrs");
30const CLOJURE_EDN_SRC: &str = include_str!("clojure/edn.cljrs");
31const CLOJURE_WALK_SRC: &str = include_str!("clojure/walk.cljrs");
32const CLOJURE_DATA_SRC: &str = include_str!("clojure/data.cljrs");
33const COLJURE_ZIP_SRC: &str = include_str!("clojure/zip.cljrs");
34
35// ── Macro: register a batch of native fns into a namespace ───────────────────
36
37/// Register a slice of `(name, arity, fn)` triples as `NativeFunction` values
38/// in `$globals` under namespace `$ns`.
39macro_rules! register_fns {
40    ($globals:expr, $ns:expr, [ $( ($name:expr, $arity:expr, $func:expr) ),* $(,)? ]) => {{
41        use cljrs_gc::GcPtr;
42        use cljrs_value::{NativeFn, Value};
43        let ns: &str = $ns;
44        $(
45            {
46                let nf = NativeFn::new($name, $arity, $func);
47                $globals.intern(ns, std::sync::Arc::from($name), Value::NativeFunction(GcPtr::new(nf)));
48            }
49        )*
50    }};
51}
52
53pub(crate) use register_fns;
54
55// ── Public API ────────────────────────────────────────────────────────────────
56
57/// Register all built-in stdlib namespaces into `globals`.
58///
59/// This is idempotent: calling it again does not re-evaluate sources
60/// (already-loaded guard in `load_ns` prevents that), but it will
61/// overwrite native fn registrations in the namespace tables.
62/// In practice, call it once right after `standard_env_minimal()`.
63pub fn register(globals: &Arc<GlobalEnv>) {
64    // clojure.string ─ pre-register native fns, then register source for
65    // the lazy (ns clojure.string) form to run on first require.
66    string::register(globals, "clojure.string");
67    globals.register_builtin_source("clojure.string", CLOJURE_STRING_SRC);
68
69    // clojure.set ─ same pattern.
70    set::register(globals, "clojure.set");
71    globals.register_builtin_source("clojure.set", CLOJURE_SET_SRC);
72
73    // clojure.template ─ pure Clojure, no native helpers.
74    globals.register_builtin_source("clojure.template", CLOJURE_TEMPLATE_SRC);
75
76    // clojure.test ─ pure Clojure, no native helpers.
77    globals.register_builtin_source("clojure.test", CLOJURE_TEST_SRC);
78
79    // clojure.rust.io ─ I/O resources.
80    io::register(globals, "clojure.rust.io");
81    globals.register_builtin_source("clojure.rust.io", CLOJURE_RUST_IO_SRC);
82
83    // clojure.edn ─ EDN reader.
84    edn::register(globals, "clojure.edn");
85    globals.register_builtin_source("clojure.edn", CLOJURE_EDN_SRC);
86
87    // clojure.walk ─ pure Clojure, no native helpers.
88    globals.register_builtin_source("clojure.walk", CLOJURE_WALK_SRC);
89
90    // clojure.data ─ pure Clojure, no native helpers.
91    globals.register_builtin_source("clojure.data", CLOJURE_DATA_SRC);
92
93    // clojure.zip
94    globals.register_builtin_source("clojure.zip", COLJURE_ZIP_SRC);
95}
96
97/// Prebuilt IR bundle for clojure.core (generated at build time).
98/// When the `prebuild-ir` feature is enabled, this contains serialized IR
99/// for all lowerable functions; otherwise it's an empty slice.
100#[cfg(feature = "prebuild-ir")]
101static PREBUILT_IR: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/core_ir.bin"));
102
103/// Create a `GlobalEnv` with all built-ins and stdlib registered.
104///
105/// Prefer this over `cljrs_eval::standard_env()` in the `cljrs` binary so that
106/// stdlib namespaces are loaded lazily (only on first `require`) instead of
107/// eagerly at startup.
108pub fn standard_env() -> Arc<GlobalEnv> {
109    let globals = cljrs_eval::standard_env_minimal();
110    register(&globals);
111
112    // Try loading prebuilt IR first (skips compiler loading entirely).
113    #[cfg(feature = "prebuild-ir")]
114    {
115        match cljrs_ir::deserialize_bundle(PREBUILT_IR) {
116            Ok(bundle) if !bundle.is_empty() => {
117                // Register compiler sources (needed for any functions not in the
118                // prebuilt bundle, and for user code that triggers IR lowering).
119                cljrs_eval::register_compiler_sources(&globals);
120
121                let loaded = cljrs_eval::load_prebuilt_ir(&globals, &bundle);
122                cljrs_logging::feat_debug!(
123                    "ir",
124                    "loaded {loaded} prebuilt IR arities from bundle ({} entries)",
125                    bundle.len()
126                );
127
128                // Still load the compiler on a background thread so new user
129                // functions can be eagerly lowered at definition time.
130                let g = globals.clone();
131                let _ = std::thread::Builder::new()
132                    .stack_size(16 * 1024 * 1024)
133                    .spawn(move || {
134                        let mut env = cljrs_eval::Env::new(g.clone(), "user");
135                        cljrs_eval::ensure_compiler_loaded(&g, &mut env);
136                    });
137
138                return globals;
139            }
140            _ => {
141                // Empty or corrupt bundle — fall through to runtime lowering.
142            }
143        }
144    }
145
146    // Fallback: register and load compiler namespaces so IR lowering is
147    // available at runtime. Loading runs on a thread with a large stack
148    // because the Clojure compiler sources are deeply recursive.
149    cljrs_eval::register_compiler_sources(&globals);
150    {
151        let g = globals.clone();
152        let _ = std::thread::Builder::new()
153            .stack_size(16 * 1024 * 1024)
154            .spawn(move || {
155                let mut env = cljrs_eval::Env::new(g.clone(), "user");
156                cljrs_eval::ensure_compiler_loaded(&g, &mut env);
157            })
158            .and_then(|h| h.join().map_err(|_| std::io::Error::other("join failed")));
159    }
160
161    globals
162}
163
164/// Like [`standard_env()`] but also sets user source paths for `require`.
165pub fn standard_env_with_paths(source_paths: Vec<std::path::PathBuf>) -> Arc<GlobalEnv> {
166    let globals = standard_env();
167    globals.set_source_paths(source_paths);
168    globals
169}
170
171/// Like [`standard_env_with_paths()`] but also sets GC configuration.
172pub fn standard_env_with_paths_and_config(
173    source_paths: Vec<std::path::PathBuf>,
174    gc_config: Arc<GcConfig>,
175) -> Arc<GlobalEnv> {
176    let globals = standard_env();
177    globals.set_source_paths(source_paths);
178    globals.set_gc_config(gc_config.clone());
179    // Configure the global GC heap with the same limits
180    cljrs_gc::HEAP.set_config(gc_config);
181    // Register GlobalEnv namespaces as GC roots so automatic collection
182    // can trace all live values reachable from namespace bindings.
183    let roots_globals = globals.clone();
184    cljrs_gc::HEAP.register_root_tracer(move |visitor| {
185        use cljrs_gc::GcVisitor as _;
186        let namespaces = roots_globals.namespaces.read().unwrap();
187        for (_name, ns_ptr) in namespaces.iter() {
188            visitor.visit(ns_ptr);
189        }
190    });
191    globals
192}
193
194// ── Tests ─────────────────────────────────────────────────────────────────────
195
196#[cfg(test)]
197mod tests {
198    use super::*;
199    use cljrs_eval::{Env, EvalResult, eval};
200    use cljrs_reader::Parser;
201    use cljrs_value::Value;
202
203    fn make_env() -> (Arc<GlobalEnv>, Env) {
204        let globals = standard_env();
205        let env = Env::new(globals.clone(), "user");
206        (globals, env)
207    }
208
209    fn run(src: &str, env: &mut Env) -> EvalResult {
210        let mut parser = Parser::new(src.to_string(), "<test>".to_string());
211        let forms = parser.parse_all().expect("parse error");
212        let mut result = Value::Nil;
213        for form in forms {
214            result = eval(&form, env)?;
215        }
216        Ok(result)
217    }
218
219    // ── clojure.string ────────────────────────────────────────────────────────
220
221    #[test]
222    fn test_string_upper_lower() {
223        let (_, mut env) = make_env();
224        run("(require '[clojure.string :as str])", &mut env).unwrap();
225        assert_eq!(
226            run("(str/upper-case \"hello\")", &mut env).unwrap(),
227            Value::string("HELLO")
228        );
229        assert_eq!(
230            run("(str/lower-case \"WORLD\")", &mut env).unwrap(),
231            Value::string("world")
232        );
233    }
234
235    #[test]
236    fn test_string_trim() {
237        let (_, mut env) = make_env();
238        run("(require '[clojure.string :as str])", &mut env).unwrap();
239        assert_eq!(
240            run("(str/trim \"  hello  \")", &mut env).unwrap(),
241            Value::string("hello")
242        );
243        assert_eq!(
244            run("(str/triml \"  hi\")", &mut env).unwrap(),
245            Value::string("hi")
246        );
247        assert_eq!(
248            run("(str/trimr \"hi  \")", &mut env).unwrap(),
249            Value::string("hi")
250        );
251    }
252
253    #[test]
254    fn test_string_predicates() {
255        let (_, mut env) = make_env();
256        run("(require '[clojure.string :as str])", &mut env).unwrap();
257        assert_eq!(
258            run("(str/blank? \"  \")", &mut env).unwrap(),
259            Value::Bool(true)
260        );
261        assert_eq!(
262            run("(str/blank? \"x\")", &mut env).unwrap(),
263            Value::Bool(false)
264        );
265        assert_eq!(
266            run("(str/starts-with? \"hello\" \"hel\")", &mut env).unwrap(),
267            Value::Bool(true)
268        );
269        assert_eq!(
270            run("(str/ends-with? \"hello\" \"llo\")", &mut env).unwrap(),
271            Value::Bool(true)
272        );
273        assert_eq!(
274            run("(str/includes? \"hello\" \"ell\")", &mut env).unwrap(),
275            Value::Bool(true)
276        );
277    }
278
279    #[test]
280    fn test_string_replace() {
281        let (_, mut env) = make_env();
282        run("(require '[clojure.string :as str])", &mut env).unwrap();
283        assert_eq!(
284            run("(str/replace \"aabbcc\" \"bb\" \"XX\")", &mut env).unwrap(),
285            Value::string("aaXXcc")
286        );
287        assert_eq!(
288            run("(str/replace-first \"aabbcc\" \"a\" \"X\")", &mut env).unwrap(),
289            Value::string("Xabbcc")
290        );
291    }
292
293    #[test]
294    fn test_string_split_join() {
295        let (_, mut env) = make_env();
296        run("(require '[clojure.string :as str])", &mut env).unwrap();
297        let v = run("(str/split \"a,b,c\" \",\")", &mut env).unwrap();
298        assert!(matches!(v, Value::Vector(_)));
299        assert_eq!(
300            run("(str/join \"-\" [\"a\" \"b\" \"c\"])", &mut env).unwrap(),
301            Value::string("a-b-c")
302        );
303    }
304
305    #[test]
306    fn test_string_capitalize() {
307        let (_, mut env) = make_env();
308        run("(require '[clojure.string :as str])", &mut env).unwrap();
309        assert_eq!(
310            run("(str/capitalize \"hello world\")", &mut env).unwrap(),
311            Value::string("Hello world")
312        );
313    }
314
315    #[test]
316    fn test_string_split_lines() {
317        let (_, mut env) = make_env();
318        run("(require '[clojure.string :as str])", &mut env).unwrap();
319        let v = run("(str/split-lines \"a\\nb\\nc\")", &mut env).unwrap();
320        assert!(matches!(v, Value::Vector(_)));
321    }
322
323    // ── clojure.set ───────────────────────────────────────────────────────────
324
325    #[test]
326    fn test_set_union() {
327        let (_, mut env) = make_env();
328        run("(require '[clojure.set :as s])", &mut env).unwrap();
329        let v = run("(s/union #{1 2} #{2 3})", &mut env).unwrap();
330        match v {
331            Value::Set(s) => assert_eq!(s.count(), 3),
332            other => panic!("expected set, got {other:?}"),
333        }
334    }
335
336    #[test]
337    fn test_set_intersection() {
338        let (_, mut env) = make_env();
339        run("(require '[clojure.set :as s])", &mut env).unwrap();
340        let v = run("(s/intersection #{1 2 3} #{2 3 4})", &mut env).unwrap();
341        match v {
342            Value::Set(s) => assert_eq!(s.count(), 2),
343            other => panic!("expected set, got {other:?}"),
344        }
345    }
346
347    #[test]
348    fn test_set_difference() {
349        let (_, mut env) = make_env();
350        run("(require '[clojure.set :as s])", &mut env).unwrap();
351        let v = run("(s/difference #{1 2 3} #{2 3})", &mut env).unwrap();
352        match v {
353            Value::Set(s) => assert_eq!(s.count(), 1),
354            other => panic!("expected set, got {other:?}"),
355        }
356    }
357
358    #[test]
359    fn test_set_subset_superset() {
360        let (_, mut env) = make_env();
361        run("(require '[clojure.set :as s])", &mut env).unwrap();
362        assert_eq!(
363            run("(s/subset? #{1 2} #{1 2 3})", &mut env).unwrap(),
364            Value::Bool(true)
365        );
366        assert_eq!(
367            run("(s/superset? #{1 2 3} #{1 2})", &mut env).unwrap(),
368            Value::Bool(true)
369        );
370    }
371
372    #[test]
373    fn test_set_map_invert() {
374        let (_, mut env) = make_env();
375        run("(require '[clojure.set :as s])", &mut env).unwrap();
376        let v = run("(s/map-invert {:a 1 :b 2})", &mut env).unwrap();
377        assert!(matches!(v, Value::Map(_)));
378    }
379
380    // ── clojure.test (via stdlib registry) ───────────────────────────────────
381
382    #[test]
383    fn test_clojure_test_lazy_load() {
384        // Run on a thread with adequate stack: the `is` macro expansion
385        // triggers eager IR lowering, which calls the Clojure compiler
386        // (deeply recursive — needs more than the default 2MB test thread stack).
387        std::thread::Builder::new()
388            .stack_size(16 * 1024 * 1024)
389            .spawn(|| {
390                let (_, mut env) = make_env();
391                // clojure.test is NOT pre-loaded in standard_env_minimal();
392                // it should load lazily from the registry.
393                run(
394                    "(require '[clojure.test :refer [is deftest run-tests]])",
395                    &mut env,
396                )
397                .unwrap();
398                let v = run("(is (= 1 1))", &mut env).unwrap();
399                assert_eq!(v, Value::Bool(true));
400            })
401            .unwrap()
402            .join()
403            .unwrap();
404    }
405}