1mod manifest;2mod stdlib;3mod tla;4mod trace;56use std::{env, marker::PhantomData, path::PathBuf};78use clap::Parser;9use jrsonnet_evaluator::{10 error::Result, stack::StackDepthLimitOverrideGuard, FileImportResolver, State,11};12use jrsonnet_gcmodule::with_thread_object_space;13pub use manifest::*;14pub use stdlib::*;15pub use tla::*;16pub use trace::*;1718pub trait ConfigureState {19 type Guards;2021 fn configure(&self, s: &State) -> Result<Self::Guards>;22}2324#[derive(Parser)]25#[clap(next_help_heading = "INPUT")]26pub struct InputOpts {27 28 #[clap(long, short = 'e')]29 pub exec: bool,3031 32 pub input: String,33}3435#[derive(Parser)]36#[clap(next_help_heading = "OPTIONS")]37pub struct MiscOpts {38 39 40 #[clap(long, short = 's', default_value = "200")]41 max_stack: usize,4243 44 45 46 47 #[clap(long, short = 'J')]48 jpath: Vec<PathBuf>,49}50impl ConfigureState for MiscOpts {51 type Guards = StackDepthLimitOverrideGuard;52 fn configure(&self, s: &State) -> Result<Self::Guards> {53 let mut library_paths = self.jpath.clone();54 library_paths.reverse();55 if let Some(path) = env::var_os("JSONNET_PATH") {56 library_paths.extend(env::split_paths(path.as_os_str()));57 }5859 s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));6061 let _depth_limit = jrsonnet_evaluator::stack::limit_stack_depth(self.max_stack);62 Ok(_depth_limit)63 }64}656667#[derive(Parser)]68#[clap(name = "jrsonnet", version, author)]69pub struct GeneralOpts {70 #[clap(flatten)]71 misc: MiscOpts,7273 #[clap(flatten)]74 tla: TlaOpts,75 #[clap(flatten)]76 std: StdOpts,7778 #[clap(flatten)]79 trace: TraceOpts,8081 #[clap(flatten)]82 gc: GcOpts,83}8485impl ConfigureState for GeneralOpts {86 type Guards = (87 <MiscOpts as ConfigureState>::Guards,88 <TlaOpts as ConfigureState>::Guards,89 <GcOpts as ConfigureState>::Guards,90 );91 fn configure(&self, s: &State) -> Result<Self::Guards> {92 93 self.trace.configure(s)?;94 let misc_guards = self.misc.configure(s)?;95 let tla_guards = self.tla.configure(s)?;96 self.std.configure(s)?;97 let gc_guards = self.gc.configure(s)?;98 Ok((misc_guards, tla_guards, gc_guards))99 }100}101102#[derive(Parser)]103#[clap(next_help_heading = "GARBAGE COLLECTION")]104pub struct GcOpts {105 106 #[clap(long)]107 gc_collect_on_exit: bool,108 109 #[clap(long)]110 gc_print_stats: bool,111 112 113 114 #[clap(long)]115 gc_collect_before_printing_stats: bool,116}117impl ConfigureState for GcOpts {118 type Guards = (Option<GcStatsPrinter>, Option<LeakSpace>);119120 fn configure(&self, _s: &State) -> Result<Self::Guards> {121 122 #[allow(clippy::unnecessary_lazy_evaluations)]123 Ok((124 self.gc_print_stats.then(|| GcStatsPrinter {125 collect_before_printing_stats: self.gc_collect_before_printing_stats,126 }),127 (!self.gc_collect_on_exit).then(|| LeakSpace(PhantomData)),128 ))129 }130}131132pub struct LeakSpace(PhantomData<()>);133134impl Drop for LeakSpace {135 fn drop(&mut self) {136 with_thread_object_space(|s| s.leak())137 }138}139140pub struct GcStatsPrinter {141 collect_before_printing_stats: bool,142}143impl Drop for GcStatsPrinter {144 fn drop(&mut self) {145 eprintln!("=== GC STATS ===");146 if self.collect_before_printing_stats {147 let collected = jrsonnet_gcmodule::collect_thread_cycles();148 eprintln!("Collected: {}", collected);149 }150 eprintln!("Tracked: {}", jrsonnet_gcmodule::count_thread_tracked())151 }152}