calcit 0.13.33

Interpreter and js codegen for Calcit
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
pub mod data;

pub mod builtins;
pub mod calcit;
pub mod call_graph_diff;
pub mod call_stack;
pub mod call_tree;
pub mod cli_args;
pub mod codegen;
pub mod def_diff;
pub mod detailed_snapshot;
pub mod effects_graph;
pub mod program;
pub mod program_diff;
pub mod project_state;
pub mod runner;
pub mod snapshot;
pub mod util;

use calcit::{CalcitErrKind, LocatedWarning};
use call_stack::CallStackList;
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

pub use calcit::{
  Calcit, CalcitErr, CalcitFnTypeAnnotation, CalcitProc, CalcitSyntax, CalcitTypeAnnotation, ProcArity, ProcTypeSignature,
  SyntaxTypeSignature,
};

use crate::util::string::strip_shebang;

pub const DEFAULT_SNAPSHOT_FILE: &str = "calcit.cirru";
pub const LEGACY_SNAPSHOT_FILE: &str = "compact.cirru";
pub const FFI_ABI_VERSION: &str = "0.0.9";

static QUIET_TOOL_OUTPUT: AtomicBool = AtomicBool::new(false);

pub fn set_quiet_tool_output(v: bool) {
  QUIET_TOOL_OUTPUT.store(v, Ordering::Relaxed);
}

pub fn quiet_tool_output() -> bool {
  QUIET_TOOL_OUTPUT.load(Ordering::Relaxed)
}

fn core_snapshot_schema_needs_fallback(snapshot: &snapshot::Snapshot) -> bool {
  let Some(core_file) = snapshot.files.get("calcit.core") else {
    return true;
  };
  let Some(map_entry) = core_file.defs.get("map") else {
    return true;
  };
  let CalcitTypeAnnotation::Fn(fn_annot) = map_entry.schema.as_ref() else {
    return true;
  };

  fn_annot.where_bounds.is_empty() || !matches!(fn_annot.arg_types.get(1).map(|arg| arg.as_ref()), Some(CalcitTypeAnnotation::Fn(_)))
}

fn load_core_snapshot_from_embedded_source() -> Result<snapshot::Snapshot, String> {
  let content = include_str!("../src/cirru/calcit-core.cirru");
  let data = cirru_edn::parse(content).map_err(|e| format!("Failed to parse embedded core snapshot source: {e}"))?;
  snapshot::load_snapshot_data(&data, "calcit-internal://calcit-core.cirru")
}

fn overlay_core_schemas_from_source(snapshot: &mut snapshot::Snapshot) -> Result<(), String> {
  let source_snapshot = load_core_snapshot_from_embedded_source()?;

  for (ns, source_file) in &source_snapshot.files {
    let Some(target_file) = snapshot.files.get_mut(ns) else {
      continue;
    };

    for (def, source_entry) in &source_file.defs {
      let Some(target_entry) = target_file.defs.get_mut(def) else {
        continue;
      };

      if !matches!(source_entry.schema.as_ref(), CalcitTypeAnnotation::Dynamic) {
        target_entry.schema = source_entry.schema.clone();
      }
    }
  }

  Ok(())
}

pub fn load_core_snapshot() -> Result<snapshot::Snapshot, String> {
  // load core libs
  let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/calcit-core.rmp"));
  let mut snapshot = snapshot::decode_binary_snapshot(bytes).map_err(|e| {
    eprintln!("\n{e}");
    "Failed to deserialize core snapshot".to_string()
  })?;
  if core_snapshot_schema_needs_fallback(&snapshot) {
    overlay_core_schemas_from_source(&mut snapshot)?;
  }
  let path = "calcit-internal://calcit-core.cirru";
  let meta_ns = format!("{}.$meta", snapshot.package);
  snapshot.files.insert(meta_ns.to_owned(), snapshot::gen_meta_ns(&meta_ns, path));
  Ok(snapshot)
}

#[derive(Clone, Debug)]
pub struct ProgramEntries {
  pub init_fn: Arc<str>,
  pub init_ns: Arc<str>,
  pub init_def: Arc<str>,
  pub reload_fn: Arc<str>,
  pub reload_ns: Arc<str>,
  pub reload_def: Arc<str>,
}

pub fn run_program(init_ns: Arc<str>, init_def: Arc<str>, params: &[Calcit]) -> Result<Calcit, CalcitErr> {
  run_program_with_docs(init_ns, init_def, params)
}

pub fn resolve_snapshot_path_alias(path: &Path) -> PathBuf {
  if path.exists() {
    return path.to_path_buf();
  }

  match path.file_name().and_then(|name| name.to_str()) {
    Some(DEFAULT_SNAPSHOT_FILE) => {
      let fallback = path.with_file_name(LEGACY_SNAPSHOT_FILE);
      if fallback.exists() { fallback } else { path.to_path_buf() }
    }
    _ => path.to_path_buf(),
  }
}

fn module_path_candidates(path: &str) -> Vec<String> {
  if path.ends_with('/') {
    vec![format!("{path}{DEFAULT_SNAPSHOT_FILE}"), format!("{path}{LEGACY_SNAPSHOT_FILE}")]
  } else if path.ends_with(DEFAULT_SNAPSHOT_FILE) {
    vec![
      path.to_string(),
      format!(
        "{}{}",
        path.strip_suffix(DEFAULT_SNAPSHOT_FILE).unwrap_or(path),
        LEGACY_SNAPSHOT_FILE
      ),
    ]
  } else {
    vec![path.to_string()]
  }
}

fn materialize_module_path(file_path: &str, base_dir: &Path, module_folder: &Path) -> PathBuf {
  if file_path.starts_with("./") {
    base_dir.join(file_path)
  } else if Path::new(file_path).is_absolute() {
    Path::new(file_path).to_owned()
  } else {
    module_folder.join(file_path)
  }
}

/// The module view installed for the project that owns a snapshot.
///
/// `caps` materializes this directory with links into its immutable global store. Runtime
/// module loading must use this view rather than reaching into the store directly, so every
/// project uses the revisions it resolved.
pub fn project_module_folder(base_dir: &Path) -> PathBuf {
  base_dir.join(".calcit/modules")
}

fn module_candidate_display_path(file_path: &str, fullpath: &Path, module_folder: &Path) -> String {
  if file_path.starts_with("./") {
    file_path.to_string()
  } else if Path::new(file_path).is_absolute() {
    if let Ok(stripped) = fullpath.strip_prefix(module_folder) {
      format!("<mods>/{}", stripped.display())
    } else {
      file_path.to_string()
    }
  } else {
    format!("<mods>/{file_path}")
  }
}

pub fn resolve_module_snapshot_candidates(path: &str, base_dir: &Path, module_folder: &Path) -> Vec<(String, PathBuf, String)> {
  let candidates = module_path_candidates(path);
  let mut items = candidates
    .iter()
    .map(|candidate| {
      let fullpath = materialize_module_path(candidate, base_dir, module_folder);
      let display_path = module_candidate_display_path(candidate, &fullpath, module_folder);
      (candidate.clone(), fullpath, display_path)
    })
    .collect::<Vec<_>>();

  if !items.iter().any(|(_, fullpath, _)| fullpath.exists())
    && let Some((file_path, fullpath, display_path)) = items.first().cloned()
  {
    return vec![(file_path, fullpath, display_path)];
  }

  items.retain(|(_, fullpath, _)| fullpath.exists());
  items
}

pub fn resolve_module_snapshot_path(path: &str, base_dir: &Path, module_folder: &Path) -> (String, PathBuf, String) {
  resolve_module_snapshot_candidates(path, base_dir, module_folder)
    .into_iter()
    .next()
    .unwrap_or_else(|| {
      let fullpath = materialize_module_path(path, base_dir, module_folder);
      let display_path = module_candidate_display_path(path, &fullpath, module_folder);
      (path.to_string(), fullpath, display_path)
    })
}

pub fn run_program_with_docs(init_ns: Arc<str>, init_def: Arc<str>, params: &[Calcit]) -> Result<Calcit, CalcitErr> {
  let check_warnings = RefCell::new(LocatedWarning::default_list());

  match runner::preprocess::ensure_ns_def_compiled(&init_ns, &init_def, &check_warnings, &CallStackList::default()) {
    Ok(()) => {}
    Err(failure) => {
      eprintln!("\nfailed preprocessing, {failure}");
      let headline = failure.headline();
      call_stack::display_stack_with_docs(&headline, &failure.stack, failure.location.as_ref(), failure.hint.as_deref())?;
      return CalcitErr::err_str(failure.kind, headline);
    }
  };

  let warnings = check_warnings.borrow();
  if !warnings.is_empty() {
    return Err(CalcitErr {
      kind: CalcitErrKind::Unexpected,
      msg: format!("Found {} warnings, runner blocked", warnings.len()),
      code: None,
      warnings: Box::new(warnings.to_owned()),
      stack: CallStackList::default(),
      location: None,
      hint: None,
    });
  }

  match runner::evaluate_symbol_from_program(&init_def, &init_ns, None, &CallStackList::default()) {
    Ok(entry) => match entry {
      Calcit::Fn { info, .. } => {
        let result = runner::run_fn(params, &info, &CallStackList::default());
        match result {
          Ok(v) => Ok(v),
          Err(failure) => {
            call_stack::display_stack_with_docs(&failure.msg, &failure.stack, failure.location.as_ref(), failure.hint.as_deref())?;
            Err(failure)
          }
        }
      }
      _ => CalcitErr::err_str(CalcitErrKind::Type, format!("expected function entry, got: {entry}")),
    },
    Err(failure) => {
      call_stack::display_stack_with_docs(&failure.msg, &failure.stack, failure.location.as_ref(), failure.hint.as_deref())?;
      Err(failure)
    }
  }
}

pub fn load_module(path: &str, base_dir: &Path, module_folder: &Path) -> Result<snapshot::Snapshot, String> {
  let mut loaded = HashSet::new();
  load_module_recursive(path, base_dir, module_folder, &mut loaded)
}

fn load_module_recursive(
  path: &str,
  base_dir: &Path,
  module_folder: &Path,
  loaded: &mut HashSet<PathBuf>,
) -> Result<snapshot::Snapshot, String> {
  let candidates = resolve_module_snapshot_candidates(path, base_dir, module_folder);
  let mut last_error: Option<String> = None;

  for (_, fullpath, display_path) in candidates {
    if loaded.contains(&fullpath) {
      return Ok(snapshot::Snapshot {
        package: String::new(),
        about: None,
        version: String::new(),
        entries: HashMap::new(),
        files: HashMap::new(),
        active_entry: snapshot::DEFAULT_ENTRY_NAME.to_owned(),
      });
    }
    let mut content = match fs::read_to_string(&fullpath) {
      Ok(content) => content,
      Err(e) => {
        last_error = Some(format!("Failed to read {}: {e}", fullpath.display()));
        continue;
      }
    };

    strip_shebang(&mut content);
    let data = match cirru_edn::parse(&content) {
      Ok(data) => data,
      Err(e) => {
        last_error = Some(format!("Failed to parse file '{}': {e}", fullpath.display()));
        continue;
      }
    };

    match snapshot::load_snapshot_data(&data, &fullpath.display().to_string()) {
      Ok(mut snapshot) => {
        if !loaded.insert(fullpath.clone()) {
          return Ok(snapshot);
        }

        let dependencies = snapshot.active_entry()?.modules.clone();
        for dependency in dependencies {
          let dependency_snapshot = load_module_recursive(&dependency, base_dir, module_folder, loaded)?;
          merge_module_files(&mut snapshot, &dependency_snapshot, &dependency)?;
        }

        if !quiet_tool_output() {
          println!("loading: {display_path}");
        }
        return Ok(snapshot);
      }
      Err(e) => {
        last_error = Some(format!("Failed to load snapshot '{}': {e}", fullpath.display()));
      }
    }
  }

  Err(last_error.unwrap_or_else(|| format!("expected Cirru snapshot for module path: {path}")))
}

/// Merge a module and its transitive dependencies while tolerating the same
/// namespace being listed both directly and transitively. Same-package
/// namespaces are preserved when a cycle revisits them; different content
/// under a cross-package namespace remains an error because it indicates an
/// invalid module graph or a mismatched dependency resolution.
pub fn merge_module_files(target: &mut snapshot::Snapshot, module: &snapshot::Snapshot, module_path: &str) -> Result<(), String> {
  let target_namespace_prefix = format!("{}.", target.package);
  for (namespace, file) in &module.files {
    if module.package == target.package
      && target.files.contains_key(namespace)
      && (namespace == &target.package || namespace.starts_with(&target_namespace_prefix))
    {
      continue;
    }
    if let Some(existing) = target.files.get(namespace) {
      if existing == file {
        continue;
      }
      return Err(format!(
        "namespace `{namespace}` conflicts with existing content when loading module `{module_path}`"
      ));
    }
    target.files.insert(namespace.to_owned(), file.to_owned());
  }
  Ok(())
}

#[cfg(test)]
mod module_resolution_tests {
  use super::{load_module, merge_module_files, project_module_folder, resolve_module_snapshot_candidates};
  use std::fs;
  use std::path::{Path, PathBuf};

  fn temp_root(label: &str) -> PathBuf {
    std::env::temp_dir().join(format!("calcit-module-resolution-{label}-{}", std::process::id()))
  }

  fn write_module(root: &Path, name: &str, dependencies: &[&str], namespace: &str) {
    let module_dir = root.join(".calcit/modules").join(name);
    fs::create_dir_all(&module_dir).unwrap();
    let modules = if dependencies.is_empty() {
      "[]".to_owned()
    } else {
      format!(
        "[] {}",
        dependencies.iter().map(|dep| format!("|{dep}/")).collect::<Vec<_>>().join(" ")
      )
    };
    let content = format!(
      "{} (:package |{name})\n  :version |0.0.0\n  :entries $ {{}}\n    :default $ {{}} (:mode :native) (:init-fn |{namespace}/main!) (:reload-fn |{namespace}/main!)\n      :modules $ {modules}\n  :files $ {{}}\n    |{namespace} $ %{{}} :FileEntry\n      :ns $ %{{}} :CodeEntry (:doc |) (:code $ quote (ns {namespace})) (:examples $ []) (:schema nil)\n      :defs $ {{}}\n        |main! $ %{{}} :CodeEntry (:doc |) (:code $ quote (defn main! () nil)) (:examples $ []) (:schema nil)\n",
      "{}"
    );
    fs::write(module_dir.join("calcit.cirru"), content).unwrap();
  }

  #[test]
  fn project_module_view_is_the_only_root_for_named_modules() {
    let root = temp_root("project-first");
    let project = root.join("project");
    let global = root.join("global");
    fs::create_dir_all(project.join(".calcit/modules/demo")).unwrap();
    fs::create_dir_all(global.join("demo")).unwrap();
    fs::write(project.join(".calcit/modules/demo/compact.cirru"), "project").unwrap();
    fs::write(global.join("demo/calcit.cirru"), "global").unwrap();

    let module_folder = project_module_folder(&project);
    let candidates = resolve_module_snapshot_candidates("demo/", &project, &module_folder);
    assert_eq!(candidates[0].1, project.join(".calcit/modules/demo/compact.cirru"));
    assert_eq!(candidates[0].2, "<mods>/demo/compact.cirru");
    fs::remove_dir_all(root).unwrap();
  }

  #[test]
  fn named_modules_do_not_fall_back_to_the_global_store() {
    let root = temp_root("no-global-fallback");
    let project = root.join("project");
    let global = root.join("global");
    fs::create_dir_all(&project).unwrap();
    fs::create_dir_all(global.join("demo")).unwrap();
    fs::write(global.join("demo/calcit.cirru"), "global").unwrap();

    let module_folder = project_module_folder(&project);
    let candidates = resolve_module_snapshot_candidates("demo/", &project, &module_folder);
    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].1, project.join(".calcit/modules/demo/calcit.cirru"));
    fs::remove_dir_all(root).unwrap();
  }

  #[test]
  fn load_module_includes_transitive_dependencies_and_terminates_cycles() {
    let root = temp_root("transitive");
    write_module(&root, "a", &["b"], "a");
    write_module(&root, "b", &["c"], "b");
    write_module(&root, "c", &[], "c");
    let module_folder = project_module_folder(&root);
    let snapshot = load_module("a/", &root, &module_folder).unwrap();
    assert!(snapshot.files.contains_key("a"));
    assert!(snapshot.files.contains_key("b"));
    assert!(snapshot.files.contains_key("c"));

    write_module(&root, "cycle-a", &["cycle-b"], "cycle-a");
    write_module(&root, "cycle-b", &["cycle-a"], "cycle-b");
    let cycle = load_module("cycle-a/", &root, &module_folder).unwrap();
    assert!(cycle.files.contains_key("cycle-a"));
    assert!(cycle.files.contains_key("cycle-b"));
    fs::remove_dir_all(root).unwrap();
  }

  #[test]
  fn merge_module_files_rejects_conflicting_namespaces() {
    let root = temp_root("conflict");
    write_module(&root, "one", &[], "shared");
    write_module(&root, "two", &[], "shared");
    let second_path = root.join(".calcit/modules/two/calcit.cirru");
    let second = fs::read_to_string(&second_path)
      .unwrap()
      .replace("(defn main! () nil)", "(defn main! (x) x)");
    fs::write(second_path, second).unwrap();
    let module_folder = project_module_folder(&root);
    let mut target = load_module("one/", &root, &module_folder).unwrap();
    let other = load_module("two/", &root, &module_folder).unwrap();
    let error = merge_module_files(&mut target, &other, "two/").unwrap_err();
    assert!(error.contains("namespace `shared` conflicts with existing content"));
    fs::remove_dir_all(root).unwrap();
  }

  #[test]
  fn merge_module_files_reuses_namespaces_for_same_package_modules() {
    let root = temp_root("same-package-meta");
    write_module(&root, "one", &[], "one");
    write_module(&root, "two", &[], "two");
    let module_folder = project_module_folder(&root);
    let mut target = load_module("one/", &root, &module_folder).unwrap();
    let original_meta = target.files.get("one.$meta").unwrap().clone();
    let original_file = target.files.get("one").unwrap().clone();
    let mut dependency = load_module("two/", &root, &module_folder).unwrap();
    dependency.package = target.package.clone();
    dependency.files.remove("two.$meta");
    dependency.files.insert(
      "one.$meta".to_owned(),
      super::snapshot::gen_meta_ns("one.$meta", "dependency/calcit.cirru"),
    );
    dependency
      .files
      .insert("one".to_owned(), super::snapshot::gen_meta_ns("one", "dependency/calcit.cirru"));
    dependency.files.insert(
      "one.extra".to_owned(),
      super::snapshot::gen_meta_ns("one.extra", "dependency/calcit.cirru"),
    );
    merge_module_files(&mut target, &dependency, "two/").unwrap();
    assert_eq!(target.files.get("one.$meta").unwrap(), &original_meta);
    assert_eq!(target.files.get("one").unwrap(), &original_file);
    assert!(target.files.contains_key("one.extra"));
    fs::remove_dir_all(root).unwrap();
  }

  #[test]
  fn merge_module_files_rejects_cross_package_target_namespaces() {
    let root = temp_root("cross-package-target-namespace");
    write_module(&root, "one", &[], "one");
    write_module(&root, "two", &[], "two");
    let module_folder = project_module_folder(&root);
    let mut target = load_module("one/", &root, &module_folder).unwrap();
    let mut dependency = load_module("two/", &root, &module_folder).unwrap();
    dependency
      .files
      .insert("one".to_owned(), super::snapshot::gen_meta_ns("one", "dependency/calcit.cirru"));
    let error = merge_module_files(&mut target, &dependency, "two/").unwrap_err();
    assert!(error.contains("namespace `one` conflicts with existing content"));
    fs::remove_dir_all(root).unwrap();
  }

  #[test]
  fn explicit_relative_modules_do_not_use_project_module_view() {
    let root = temp_root("relative");
    let project = root.join("project");
    let global = root.join("global");
    fs::create_dir_all(&project).unwrap();
    fs::write(project.join("util.cirru"), "relative").unwrap();

    let candidates = resolve_module_snapshot_candidates("./util.cirru", &project, &global);
    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].1, project.join("./util.cirru"));
    fs::remove_dir_all(root).unwrap();
  }

  #[test]
  fn absolute_module_paths_do_not_use_project_module_view() {
    let root = temp_root("absolute");
    let project = root.join("project");
    let global = root.join("global");
    let module = root.join("external.cirru");
    fs::create_dir_all(&project).unwrap();
    fs::write(&module, "absolute").unwrap();

    let candidates = resolve_module_snapshot_candidates(module.to_str().unwrap(), &project, &global);
    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].1, module);
    fs::remove_dir_all(root).unwrap();
  }
}