git.delta.rocks / jrsonnet / refs/commits / 7af406eaa740

difftreelog

source

crates/jrsonnet-stdlib/src/lib.rs12.9 KiBsourcehistory
1#![allow(clippy::similar_names)]23use std::{4	cell::{Ref, RefCell, RefMut},5	collections::HashMap,6	f64,7	rc::Rc,8};910pub use arrays::*;11pub use compat::*;12pub use encoding::*;13pub use hash::*;14use jrsonnet_evaluator::{15	ContextBuilder, IStr, NumValue, ObjValue, ObjValueBuilder, Thunk, Val,16	error::Result,17	function::{CallLocation, FuncVal, builtin_id},18	tla::TlaArg,19	trace::PathResolver,20	typed::SerializeTypedObj as _,21};22use jrsonnet_gcmodule::{Acyclic, Cc, Trace};23use jrsonnet_ir::Source;24use jrsonnet_macros::{IntoUntyped, Typed};25pub use manifest::*;26pub use math::*;27pub use misc::*;28pub use objects::*;29pub use operator::*;30pub use parse::*;31pub use sets::*;32pub use sort::*;33pub use strings::*;34pub use types::*;3536#[cfg(feature = "exp-regex")]37pub use crate::regex::*;3839mod arrays;40mod compat;41mod encoding;42mod hash;43mod keyf;44mod manifest;45mod math;46mod misc;47mod objects;48mod operator;49mod parse;50#[cfg(feature = "exp-regex")]51mod regex;52mod sets;53mod sort;54mod strings;55mod types;5657#[derive(Typed, IntoUntyped, Default)]58#[allow(non_snake_case)]59struct Builtins {60	#[typed(method)]61	id: builtin_id,62	// Types63	#[typed(method, rename = "type")]64	r#type: builtin_type,65	#[typed(method)]66	isString: builtin_is_string,67	#[typed(method)]68	isNumber: builtin_is_number,69	#[typed(method)]70	isBoolean: builtin_is_boolean,71	#[typed(method)]72	isObject: builtin_is_object,73	#[typed(method)]74	isArray: builtin_is_array,75	#[typed(method)]76	isFunction: builtin_is_function,77	#[typed(method)]78	isNull: builtin_is_null,79	// Arrays80	#[typed(method)]81	makeArray: builtin_make_array,82	#[typed(method)]83	repeat: builtin_repeat,84	#[typed(method)]85	slice: builtin_slice,86	#[typed(method)]87	map: builtin_map,88	#[typed(method)]89	mapWithIndex: builtin_map_with_index,90	#[typed(method)]91	mapWithKey: builtin_map_with_key,92	#[typed(method)]93	flatMap: builtin_flatmap,94	#[typed(method)]95	filter: builtin_filter,96	#[typed(method)]97	foldl: builtin_foldl,98	#[typed(method)]99	foldr: builtin_foldr,100	#[typed(method)]101	range: builtin_range,102	#[typed(method)]103	join: builtin_join,104	#[typed(method)]105	lines: builtin_lines,106	#[typed(method)]107	resolvePath: builtin_resolve_path,108	#[typed(method)]109	deepJoin: builtin_deep_join,110	#[typed(method)]111	reverse: builtin_reverse,112	#[typed(method)]113	any: builtin_any,114	#[typed(method)]115	all: builtin_all,116	#[typed(method)]117	member: builtin_member,118	#[typed(method)]119	find: builtin_find,120	#[typed(method)]121	contains: builtin_contains,122	#[typed(method)]123	count: builtin_count,124	#[typed(method)]125	avg: builtin_avg,126	#[typed(method)]127	removeAt: builtin_remove_at,128	#[typed(method)]129	remove: builtin_remove,130	#[typed(method)]131	flattenArrays: builtin_flatten_arrays,132	#[typed(method)]133	flattenDeepArray: builtin_flatten_deep_array,134	#[typed(method)]135	prune: builtin_prune,136	#[typed(method)]137	filterMap: builtin_filter_map,138	// Math139	#[typed(method)]140	abs: builtin_abs,141	#[typed(method)]142	sign: builtin_sign,143	#[typed(method)]144	max: builtin_max,145	#[typed(method)]146	min: builtin_min,147	#[typed(method)]148	clamp: builtin_clamp,149	#[typed(method)]150	sum: builtin_sum,151	#[typed(method)]152	modulo: builtin_modulo,153	#[typed(method)]154	floor: builtin_floor,155	#[typed(method)]156	ceil: builtin_ceil,157	#[typed(method)]158	log: builtin_log,159	#[typed(method)]160	log2: builtin_log2,161	#[typed(method)]162	log10: builtin_log10,163	#[typed(method)]164	pow: builtin_pow,165	#[typed(method)]166	sqrt: builtin_sqrt,167	#[typed(method)]168	sin: builtin_sin,169	#[typed(method)]170	cos: builtin_cos,171	#[typed(method)]172	tan: builtin_tan,173	#[typed(method)]174	asin: builtin_asin,175	#[typed(method)]176	acos: builtin_acos,177	#[typed(method)]178	atan: builtin_atan,179	#[typed(method)]180	atan2: builtin_atan2,181	#[typed(method)]182	exp: builtin_exp,183	#[typed(method)]184	mantissa: builtin_mantissa,185	#[typed(method)]186	exponent: builtin_exponent,187	#[typed(method)]188	round: builtin_round,189	#[typed(method)]190	isEven: builtin_is_even,191	#[typed(method)]192	isOdd: builtin_is_odd,193	#[typed(method)]194	isInteger: builtin_is_integer,195	#[typed(method)]196	isDecimal: builtin_is_decimal,197	#[typed(method)]198	deg2rad: builtin_deg2rad,199	#[typed(method)]200	rad2deg: builtin_rad2deg,201	#[typed(method)]202	hypot: builtin_hypot,203	// Operator204	#[typed(rename = "mod", method)]205	r#mod: builtin_mod,206	#[typed(method)]207	primitiveEquals: builtin_primitive_equals,208	#[typed(method)]209	equals: builtin_equals,210	#[typed(method)]211	xor: builtin_xor,212	#[typed(method)]213	xnor: builtin_xnor,214	#[typed(method)]215	format: builtin_format,216	// Sort217	#[typed(method)]218	sort: builtin_sort,219	#[typed(method)]220	uniq: builtin_uniq,221	#[typed(method)]222	set: builtin_set,223	#[typed(method)]224	minArray: builtin_min_array,225	#[typed(method)]226	maxArray: builtin_max_array,227	// Hash228	#[typed(method)]229	md5: builtin_md5,230	#[typed(method)]231	sha1: builtin_sha1,232	#[typed(method)]233	sha256: builtin_sha256,234	#[typed(method)]235	sha512: builtin_sha512,236	#[typed(method)]237	sha3: builtin_sha3,238	// Encoding239	#[typed(method)]240	encodeUTF8: builtin_encode_utf8,241	#[typed(method)]242	decodeUTF8: builtin_decode_utf8,243	#[typed(method)]244	base64: builtin_base64,245	#[typed(method)]246	base64Decode: builtin_base64_decode,247	#[typed(method)]248	base64DecodeBytes: builtin_base64_decode_bytes,249	// Objects250	#[typed(method)]251	objectFieldsEx: builtin_object_fields_ex,252	#[typed(method)]253	objectFields: builtin_object_fields,254	#[typed(method)]255	objectFieldsAll: builtin_object_fields_all,256	#[typed(method)]257	objectValues: builtin_object_values,258	#[typed(method)]259	objectValuesAll: builtin_object_values_all,260	#[typed(method)]261	objectKeysValues: builtin_object_keys_values,262	#[typed(method)]263	objectKeysValuesAll: builtin_object_keys_values_all,264	#[typed(method)]265	objectHasEx: builtin_object_has_ex,266	#[typed(method)]267	objectHas: builtin_object_has,268	#[typed(method)]269	objectHasAll: builtin_object_has_all,270	#[typed(method)]271	objectRemoveKey: builtin_object_remove_key,272	// Manifest273	#[typed(method)]274	escapeStringJson: builtin_escape_string_json,275	#[typed(method)]276	escapeStringPython: builtin_escape_string_python,277	#[typed(method)]278	escapeStringXML: builtin_escape_string_xml,279	#[typed(method)]280	manifestJsonEx: builtin_manifest_json_ex,281	#[typed(method)]282	manifestJson: builtin_manifest_json,283	#[typed(method)]284	manifestJsonMinified: builtin_manifest_json_minified,285	#[typed(method)]286	manifestYamlDoc: builtin_manifest_yaml_doc,287	#[typed(method)]288	manifestYamlStream: builtin_manifest_yaml_stream,289	#[typed(method)]290	manifestTomlEx: builtin_manifest_toml_ex,291	#[typed(method)]292	manifestToml: builtin_manifest_toml,293	#[typed(method)]294	toString: builtin_to_string,295	#[typed(method)]296	manifestPython: builtin_manifest_python,297	#[typed(method)]298	manifestPythonVars: builtin_manifest_python_vars,299	#[typed(method)]300	manifestXmlJsonml: builtin_manifest_xml_jsonml,301	#[typed(method)]302	manifestIni: builtin_manifest_ini,303	// Parse304	#[typed(method)]305	parseJson: builtin_parse_json,306	#[typed(method)]307	parseYaml: builtin_parse_yaml,308	// Strings309	#[typed(method)]310	codepoint: builtin_codepoint,311	#[typed(method)]312	substr: builtin_substr,313	#[typed(method)]314	char: builtin_char,315	#[typed(method)]316	strReplace: builtin_str_replace,317	#[typed(method)]318	escapeStringBash: builtin_escape_string_bash,319	#[typed(method)]320	escapeStringDollars: builtin_escape_string_dollars,321	#[typed(method)]322	isEmpty: builtin_is_empty,323	#[typed(method)]324	equalsIgnoreCase: builtin_equals_ignore_case,325	#[typed(method)]326	splitLimit: builtin_splitlimit,327	#[typed(method)]328	splitLimitR: builtin_splitlimitr,329	#[typed(method)]330	split: builtin_split,331	#[typed(method)]332	asciiUpper: builtin_ascii_upper,333	#[typed(method)]334	asciiLower: builtin_ascii_lower,335	#[typed(method)]336	findSubstr: builtin_find_substr,337	#[typed(method)]338	parseInt: builtin_parse_int,339	#[cfg(feature = "exp-bigint")]340	#[typed(method)]341	bigint: builtin_bigint,342	#[typed(method)]343	parseOctal: builtin_parse_octal,344	#[typed(method)]345	parseHex: builtin_parse_hex,346	#[typed(method)]347	stringChars: builtin_string_chars,348	#[typed(method)]349	lstripChars: builtin_lstrip_chars,350	#[typed(method)]351	rstripChars: builtin_rstrip_chars,352	#[typed(method)]353	stripChars: builtin_strip_chars,354	#[typed(method)]355	trim: builtin_trim,356	// Misc357	#[typed(method)]358	length: builtin_length,359	#[typed(method)]360	get: builtin_get,361	#[typed(method)]362	startsWith: builtin_starts_with,363	#[typed(method)]364	endsWith: builtin_ends_with,365	#[typed(method)]366	assertEqual: builtin_assert_equal,367	#[typed(method)]368	mergePatch: builtin_merge_patch,369	// Sets370	#[typed(method)]371	setMember: builtin_set_member,372	#[typed(method)]373	setInter: builtin_set_inter,374	#[typed(method)]375	setDiff: builtin_set_diff,376	#[typed(method)]377	setUnion: builtin_set_union,378	// Regex379	#[cfg(feature = "exp-regex")]380	#[typed(method)]381	regexQuoteMeta: builtin_regex_quote_meta,382	// Compat383	#[typed(method)]384	__compare: builtin___compare,385	#[typed(method)]386	__compare_array: builtin___compare_array,387	#[typed(method)]388	__array_less: builtin___array_less,389	#[typed(method)]390	__array_greater: builtin___array_greater,391	#[typed(method)]392	__array_less_or_equal: builtin___array_less_or_equal,393	#[typed(method)]394	__array_greater_or_equal: builtin___array_greater_or_equal,395}396397#[allow(clippy::too_many_lines)]398pub fn stdlib_uncached(settings: Cc<RefCell<Settings>>) -> ObjValue {399	let mut builder = ObjValueBuilder::new();400401	let builtins = Builtins::default();402	builtins.serialize(&mut builder).expect("no conflicts");403404	builder.method(405		"extVar",406		builtin_ext_var {407			settings: settings.clone(),408		},409	);410	builder.method(411		"native",412		builtin_native {413			settings: settings.clone(),414		},415	);416	builder.method("trace", builtin_trace { settings });417418	builder.field("pi").hide().value(Val::Num(419		NumValue::new(f64::consts::PI).expect("pi is finite"),420	));421422	#[cfg(feature = "exp-regex")]423	{424		// Regex425		let regex_cache = RegexCache::default();426		builder.method(427			"regexFullMatch",428			builtin_regex_full_match {429				cache: regex_cache.clone(),430			},431		);432		builder.method(433			"regexPartialMatch",434			builtin_regex_partial_match {435				cache: regex_cache.clone(),436			},437		);438		builder.method(439			"regexReplace",440			builtin_regex_replace {441				cache: regex_cache.clone(),442			},443		);444		builder.method(445			"regexGlobalReplace",446			builtin_regex_global_replace { cache: regex_cache },447		);448	};449450	builder.build()451}452453pub trait TracePrinter: Acyclic {454	fn print_trace(&self, loc: CallLocation, value: IStr);455}456457#[derive(Acyclic)]458pub struct StdTracePrinter {459	resolver: PathResolver,460}461impl StdTracePrinter {462	pub fn new(resolver: PathResolver) -> Self {463		Self { resolver }464	}465}466impl TracePrinter for StdTracePrinter {467	fn print_trace(&self, loc: CallLocation, value: IStr) {468		eprint!("TRACE:");469		if let Some(loc) = loc.0 {470			let locs = loc.0.map_source_locations(&[loc.1]);471			eprint!(472				" {}:{}",473				loc.0.source_path().path().map_or_else(474					|| loc.0.source_path().to_string(),475					|p| self.resolver.resolve(p)476				),477				locs[0].line478			);479		}480		eprintln!(" {value}");481	}482}483484#[derive(Clone, Trace)]485pub struct Settings {486	/// Used for `std.extVar`487	pub ext_vars: HashMap<IStr, TlaArg>,488	/// Used for `std.native`489	pub ext_natives: HashMap<IStr, FuncVal>,490	/// Used for `std.trace`491	pub trace_printer: Rc<dyn TracePrinter>,492	/// Used for `std.thisFile`493	pub path_resolver: PathResolver,494}495496#[derive(Trace, Clone)]497pub struct ContextInitializer {498	/// std without applied thisFile overlay499	stdlib_obj: ObjValue,500	settings: Cc<RefCell<Settings>>,501}502impl ContextInitializer {503	pub fn new(resolver: PathResolver) -> Self {504		let settings = Settings {505			ext_vars: HashMap::new(),506			ext_natives: HashMap::new(),507			trace_printer: Rc::new(StdTracePrinter::new(resolver.clone())),508			path_resolver: resolver,509		};510		let settings = Cc::new(RefCell::new(settings));511		let stdlib_obj = stdlib_uncached(settings.clone());512		Self {513			stdlib_obj,514			settings,515		}516	}517	pub fn settings(&self) -> Ref<'_, Settings> {518		self.settings.borrow()519	}520	pub fn settings_mut(&self) -> RefMut<'_, Settings> {521		self.settings.borrow_mut()522	}523	pub fn add_ext_var(&self, name: IStr, value: Val) {524		self.settings_mut()525			.ext_vars526			.insert(name, TlaArg::Val(value));527	}528	pub fn add_ext_str(&self, name: IStr, value: IStr) {529		self.settings_mut()530			.ext_vars531			.insert(name, TlaArg::String(value));532	}533	pub fn add_ext_code(&self, name: &str, code: impl AsRef<str>) -> Result<()> {534		// self.data_mut().volatile_files.insert(source_name, code);535		self.settings_mut()536			.ext_vars537			.insert(name.into(), TlaArg::InlineCode(code.as_ref().to_owned()));538		Ok(())539	}540	pub fn add_native(&self, name: impl Into<IStr>, cb: impl Into<FuncVal>) {541		self.settings_mut()542			.ext_natives543			.insert(name.into(), cb.into());544	}545}546impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {547	fn populate(&self, source: Source, builder: &mut ContextBuilder) {548		let mut std = ObjValueBuilder::new();549		std.with_super(self.stdlib_obj.clone());550		std.field("thisFile").hide().value({551			let source_path = source.source_path();552			source_path.path().map_or_else(553				|| source_path.to_string(),554				|p| self.settings().path_resolver.resolve(p),555			)556		});557		let stdlib_with_this_file = std.build();558559		builder.bind("std", Thunk::evaluated(Val::Obj(stdlib_with_this_file)));560	}561	fn as_any(&self) -> &dyn std::any::Any {562		self563	}564}