git.delta.rocks / jrsonnet / refs/commits / 974f2c15c0a2

difftreelog

source

crates/jrsonnet-cli/src/lib.rs2.9 KiBsourcehistory
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, StackDepthLimitOverrideGuard, limit_stack_depth}, FileImportResolver, State, ImportResolver};10use jrsonnet_gcmodule::with_thread_object_space;11pub use manifest::*;12pub use stdlib::*;13pub use tla::*;14pub use trace::*;1516#[derive(Parser)]17#[clap(next_help_heading = "INPUT")]18pub struct InputOpts {19	/// Treat input as code, evaluate them instead of reading file20	#[clap(long, short = 'e')]21	pub exec: bool,2223	/// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself24	pub input: String,25}2627#[derive(Parser)]28#[clap(next_help_heading = "OPTIONS")]29pub struct MiscOpts {30	/// Maximal allowed number of stack frames,31	/// stack overflow error will be raised if this number gets exceeded.32	#[clap(long, short = 's', default_value = "200")]33	max_stack: usize,3435	/// Library search dirs. (right-most wins)36	/// Any not found `imported` file will be searched in these.37	/// This can also be specified via `JSONNET_PATH` variable,38	/// which should contain a colon-separated (semicolon-separated on Windows) list of directories.39	#[clap(long, short = 'J')]40	jpath: Vec<PathBuf>,41}42impl MiscOpts {43	pub fn import_resolver(&self) -> FileImportResolver {44		let mut library_paths = self.jpath.clone();45		library_paths.reverse();46		if let Some(path) = env::var_os("JSONNET_PATH") {47			library_paths.extend(env::split_paths(path.as_os_str()));48		}4950		FileImportResolver::new(library_paths)51	}52	pub fn stack_size_override(&self) -> StackDepthLimitOverrideGuard {53		limit_stack_depth(self.max_stack)54	}55}5657#[derive(Parser)]58#[clap(next_help_heading = "GARBAGE COLLECTION")]59pub struct GcOpts {60	/// Do not skip gc on exit61	#[clap(long)]62	gc_collect_on_exit: bool,63	/// Print gc stats before exit64	#[clap(long)]65	gc_print_stats: bool,66	/// Force garbage collection before printing stats67	/// Useful for checking for memory leaks68	/// Does nothing useless --gc-print-stats is specified69	#[clap(long)]70	gc_collect_before_printing_stats: bool,71}72impl GcOpts {73	pub fn stats_printer(&self) -> Option<GcStatsPrinter> {74		self.gc_print_stats.then(|| GcStatsPrinter {75			collect_before_printing_stats: self.gc_collect_before_printing_stats,76		})77	}78	pub fn leak_on_exit(&self) -> Option<LeakSpace> {79		(!self.gc_collect_on_exit).then(|| LeakSpace(PhantomData))80	}81}8283pub struct LeakSpace(PhantomData<()>);8485impl Drop for LeakSpace {86	fn drop(&mut self) {87		with_thread_object_space(|s| s.leak())88	}89}9091pub struct GcStatsPrinter {92	collect_before_printing_stats: bool,93}94impl Drop for GcStatsPrinter {95	fn drop(&mut self) {96		eprintln!("=== GC STATS ===");97		if self.collect_before_printing_stats {98			let collected = jrsonnet_gcmodule::collect_thread_cycles();99			eprintln!("Collected: {}", collected);100		}101		eprintln!("Tracked: {}", jrsonnet_gcmodule::count_thread_tracked())102	}103}