mod manifest;
mod stdlib;
mod tla;
mod trace;
use std::{env, marker::PhantomData, path::PathBuf};
use clap::Parser;
use jrsonnet_evaluator::{error::Result, stack::set_stack_depth_limit, FileImportResolver, State};
use jrsonnet_gcmodule::with_thread_object_space;
pub use manifest::*;
pub use stdlib::*;
pub use tla::*;
pub use trace::*;
pub trait ConfigureState {
type Guards;
fn configure(&self, s: &State) -> Result<Self::Guards>;
}
#[derive(Parser)]
#[clap(next_help_heading = "INPUT")]
pub struct InputOpts {
#[clap(long, short = 'e')]
pub exec: bool,
pub input: String,
}
#[derive(Parser)]
#[clap(next_help_heading = "OPTIONS")]
pub struct MiscOpts {
#[clap(long, short = 's', default_value = "200")]
max_stack: usize,
#[clap(long, short = 'J')]
jpath: Vec<PathBuf>,
}
impl ConfigureState for MiscOpts {
type Guards = ();
fn configure(&self, s: &State) -> Result<Self::Guards> {
let mut library_paths = self.jpath.clone();
library_paths.reverse();
if let Some(path) = env::var_os("JSONNET_PATH") {
library_paths.extend(env::split_paths(path.as_os_str()));
}
s.set_import_resolver(FileImportResolver::new(library_paths));
set_stack_depth_limit(self.max_stack);
Ok(())
}
}
#[derive(Parser)]
#[clap(name = "jrsonnet", version, author)]
pub struct GeneralOpts {
#[clap(flatten)]
misc: MiscOpts,
#[clap(flatten)]
tla: TlaOpts,
#[clap(flatten)]
std: StdOpts,
#[clap(flatten)]
gc: GcOpts,
}
impl ConfigureState for GeneralOpts {
type Guards = (
<TlaOpts as ConfigureState>::Guards,
<GcOpts as ConfigureState>::Guards,
);
fn configure(&self, s: &State) -> Result<Self::Guards> {
self.misc.configure(s)?;
let tla_guards = self.tla.configure(s)?;
self.std.configure(s)?;
let gc_guards = self.gc.configure(s)?;
Ok((tla_guards, gc_guards))
}
}
#[derive(Parser)]
#[clap(next_help_heading = "GARBAGE COLLECTION")]
pub struct GcOpts {
#[clap(long)]
gc_collect_on_exit: bool,
#[clap(long)]
gc_print_stats: bool,
#[clap(long)]
gc_collect_before_printing_stats: bool,
}
impl ConfigureState for GcOpts {
type Guards = (Option<GcStatsPrinter>, Option<LeakSpace>);
fn configure(&self, _s: &State) -> Result<Self::Guards> {
#[allow(clippy::unnecessary_lazy_evaluations)]
Ok((
self.gc_print_stats.then(|| GcStatsPrinter {
collect_before_printing_stats: self.gc_collect_before_printing_stats,
}),
(!self.gc_collect_on_exit).then(|| LeakSpace(PhantomData)),
))
}
}
pub struct LeakSpace(PhantomData<()>);
impl Drop for LeakSpace {
fn drop(&mut self) {
with_thread_object_space(|s| s.leak())
}
}
pub struct GcStatsPrinter {
collect_before_printing_stats: bool,
}
impl Drop for GcStatsPrinter {
fn drop(&mut self) {
eprintln!("=== GC STATS ===");
if self.collect_before_printing_stats {
let collected = jrsonnet_gcmodule::collect_thread_cycles();
eprintln!("Collected: {}", collected);
}
eprintln!("Tracked: {}", jrsonnet_gcmodule::count_thread_tracked())
}
}