1mod manifest;2mod stdlib;3mod tla;4mod trace;56use std::{env, marker::PhantomData, path::PathBuf};78use clap::Parser;9use jrsonnet_evaluator::{error::Result, stack::set_stack_depth_limit, FileImportResolver, State};10use jrsonnet_gcmodule::with_thread_object_space;11pub use manifest::*;12pub use stdlib::*;13pub use tla::*;14pub use trace::*;1516pub trait ConfigureState {17 type Guards;1819 fn configure(&self, s: &State) -> Result<Self::Guards>;20}2122#[derive(Parser)]23#[clap(next_help_heading = "INPUT")]24pub struct InputOpts {25 26 #[clap(long, short = 'e')]27 pub exec: bool,2829 30 pub input: String,31}3233#[derive(Parser)]34#[clap(next_help_heading = "OPTIONS")]35pub struct MiscOpts {36 37 38 #[clap(long, short = 's', default_value = "200")]39 max_stack: usize,4041 42 43 44 45 #[clap(long, short = 'J')]46 jpath: Vec<PathBuf>,47}48impl ConfigureState for MiscOpts {49 type Guards = ();50 fn configure(&self, s: &State) -> Result<Self::Guards> {51 let mut library_paths = self.jpath.clone();52 library_paths.reverse();53 if let Some(path) = env::var_os("JSONNET_PATH") {54 library_paths.extend(env::split_paths(path.as_os_str()));55 }5657 s.set_import_resolver(FileImportResolver::new(library_paths));5859 set_stack_depth_limit(self.max_stack);60 Ok(())61 }62}636465#[derive(Parser)]66#[clap(name = "jrsonnet", version, author)]67pub struct GeneralOpts {68 #[clap(flatten)]69 misc: MiscOpts,7071 #[clap(flatten)]72 tla: TlaOpts,73 #[clap(flatten)]74 std: StdOpts,7576 #[clap(flatten)]77 gc: GcOpts,78}7980impl ConfigureState for GeneralOpts {81 type Guards = (82 <TlaOpts as ConfigureState>::Guards,83 <GcOpts as ConfigureState>::Guards,84 );85 fn configure(&self, s: &State) -> Result<Self::Guards> {86 87 self.misc.configure(s)?;88 let tla_guards = self.tla.configure(s)?;89 self.std.configure(s)?;90 let gc_guards = self.gc.configure(s)?;91 Ok((tla_guards, gc_guards))92 }93}9495#[derive(Parser)]96#[clap(next_help_heading = "GARBAGE COLLECTION")]97pub struct GcOpts {98 99 #[clap(long)]100 gc_collect_on_exit: bool,101 102 #[clap(long)]103 gc_print_stats: bool,104 105 106 107 #[clap(long)]108 gc_collect_before_printing_stats: bool,109}110impl ConfigureState for GcOpts {111 type Guards = (Option<GcStatsPrinter>, Option<LeakSpace>);112113 fn configure(&self, _s: &State) -> Result<Self::Guards> {114 115 #[allow(clippy::unnecessary_lazy_evaluations)]116 Ok((117 self.gc_print_stats.then(|| GcStatsPrinter {118 collect_before_printing_stats: self.gc_collect_before_printing_stats,119 }),120 (!self.gc_collect_on_exit).then(|| LeakSpace(PhantomData)),121 ))122 }123}124125pub struct LeakSpace(PhantomData<()>);126127impl Drop for LeakSpace {128 fn drop(&mut self) {129 with_thread_object_space(|s| s.leak())130 }131}132133pub struct GcStatsPrinter {134 collect_before_printing_stats: bool,135}136impl Drop for GcStatsPrinter {137 fn drop(&mut self) {138 eprintln!("=== GC STATS ===");139 if self.collect_before_printing_stats {140 let collected = jrsonnet_gcmodule::collect_thread_cycles();141 eprintln!("Collected: {}", collected);142 }143 eprintln!("Tracked: {}", jrsonnet_gcmodule::count_thread_tracked())144 }145}