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