1mod ext;2mod manifest;3mod tla;4mod trace;56use std::{env, path::PathBuf};78use clap::Parser;9pub use ext::*;10use jrsonnet_evaluator::{error::Result, FileImportResolver, State};11use jrsonnet_gcmodule::with_thread_object_space;12pub use manifest::*;13pub use tla::*;14pub use trace::*;1516pub trait ConfigureState {17 fn configure(&self, s: &State) -> Result<()>;18}1920#[derive(Parser)]21#[clap(next_help_heading = "INPUT")]22pub struct InputOpts {23 24 #[clap(long, short = 'e')]25 pub exec: bool,2627 28 pub input: String,29}3031#[derive(Parser)]32#[clap(next_help_heading = "OPTIONS")]33pub struct MiscOpts {34 35 36 37 38 #[clap(long)]39 no_stdlib: bool,4041 42 43 #[clap(long, short = 's', default_value = "200")]44 max_stack: usize,4546 47 48 49 50 #[clap(long, short = 'J', multiple_occurrences = true)]51 jpath: Vec<PathBuf>,52}53impl ConfigureState for MiscOpts {54 fn configure(&self, s: &State) -> Result<()> {55 if !self.no_stdlib {56 s.with_stdlib();57 }5859 let mut library_paths = self.jpath.clone();60 library_paths.reverse();61 if let Some(path) = env::var_os("JSONNET_PATH") {62 library_paths.extend(env::split_paths(path.as_os_str()));63 }6465 s.set_import_resolver(Box::new(FileImportResolver { library_paths }));6667 s.set_max_stack(self.max_stack);68 Ok(())69 }70}717273#[derive(Parser)]74#[clap(name = "jrsonnet", version, author)]75pub struct GeneralOpts {76 #[clap(flatten)]77 misc: MiscOpts,7879 #[clap(flatten)]80 tla: TLAOpts,81 #[clap(flatten)]82 ext: ExtVarOpts,8384 #[clap(flatten)]85 trace: TraceOpts,86}8788impl ConfigureState for GeneralOpts {89 fn configure(&self, s: &State) -> Result<()> {90 91 self.trace.configure(s)?;92 self.misc.configure(s)?;93 self.tla.configure(s)?;94 self.ext.configure(s)?;95 Ok(())96 }97}9899#[derive(Parser)]100#[clap(next_help_heading = "GARBAGE COLLECTION")]101pub struct GcOpts {102 103 #[clap(long)]104 gc_collect_on_exit: bool,105 106 #[clap(long)]107 gc_print_stats: bool,108 109 110 111 #[clap(long)]112 gc_collect_before_printing_stats: bool,113}114impl GcOpts {115 pub fn stats_printer(&self) -> (Option<GcStatsPrinter>, Option<LeakSpace>) {116 (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 {}),121 )122 }123}124125pub struct LeakSpace {}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}