git.delta.rocks / jrsonnet / refs/commits / 4a793257644c

difftreelog

feat sync jsonnet stdlib changes

yzozlsyrYaroslav Bolyukin2026-02-08parent: #6551086.patch.diff
in: master

8 files changed

modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
413 val.value_type()413 val.value_type()
414 )414 )
415 };415 };
416 if !arr.is_empty() {
417 for (i, v) in arr.iter().enumerate() {416 for (i, v) in arr.iter().enumerate() {
417 if i != 0 {
418 out.push('\n');
419 }
418 let v = v.with_description(|| format!("elem <{i}> evaluation"))?;420 let v = v.with_description(|| format!("elem <{i}> evaluation"))?;
419 out.push_str("---\n");421 out.push_str("---\n");
420 in_description_frame(422 in_description_frame(
421 || format!("elem <{i}> manifestification"),423 || format!("elem <{i}> manifestification"),
422 || self.inner.manifest_buf(v, out),424 || self.inner.manifest_buf(v, out),
423 )?;425 )?;
424 out.push('\n');426 }
425 }
426 }
427 if self.c_document_end {427 if self.c_document_end {
428 out.push('\n');
428 out.push_str("...");429 out.push_str("...");
429 }430 }
430 if self.end_newline {431 if self.end_newline {
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
237 Expr::ArrComp(expr, specs)237 Expr::ArrComp(expr, specs)
238 }238 }
239 pub rule number_expr(s: &ParserSettings) -> Expr239 pub rule number_expr(s: &ParserSettings) -> Expr
240 = n:number() { expr::Expr::Num(n) }240 = n:number() {? if n.is_finite() {
241 Ok(expr::Expr::Num(n))
242 } else {
243 Err("!!!numbers are finite")
244 }}
241 pub rule var_expr(s: &ParserSettings) -> Expr245 pub rule var_expr(s: &ParserSettings) -> Expr
242 = n:id() { expr::Expr::Var(n) }246 = n:id() { expr::Expr::Var(n) }
243 pub rule id_loc(s: &ParserSettings) -> LocExpr247 pub rule id_loc(s: &ParserSettings) -> LocExpr
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
3use std::{3use std::{
4 cell::{Ref, RefCell, RefMut},4 cell::{Ref, RefCell, RefMut},
5 collections::HashMap,5 collections::HashMap,
6 f64,
6 rc::Rc,7 rc::Rc,
7};8};
89
14 error::{ErrorKind::*, Result},15 error::{ErrorKind::*, Result},
15 function::{CallLocation, FuncVal, TlaArg},16 function::{CallLocation, FuncVal, TlaArg},
16 trace::PathResolver,17 trace::PathResolver,
18 val::NumValue,
17 ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val,19 ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val,
18};20};
19use jrsonnet_gcmodule::{Acyclic, Cc, Trace};21use jrsonnet_gcmodule::{Acyclic, Cc, Trace};
63 ("isObject", builtin_is_object::INST),65 ("isObject", builtin_is_object::INST),
64 ("isArray", builtin_is_array::INST),66 ("isArray", builtin_is_array::INST),
65 ("isFunction", builtin_is_function::INST),67 ("isFunction", builtin_is_function::INST),
68 ("isNull", builtin_is_null::INST),
66 // Arrays69 // Arrays
67 ("makeArray", builtin_make_array::INST),70 ("makeArray", builtin_make_array::INST),
68 ("repeat", builtin_repeat::INST),71 ("repeat", builtin_repeat::INST),
104 ("floor", builtin_floor::INST),107 ("floor", builtin_floor::INST),
105 ("ceil", builtin_ceil::INST),108 ("ceil", builtin_ceil::INST),
106 ("log", builtin_log::INST),109 ("log", builtin_log::INST),
110 ("log2", builtin_log2::INST),
111 ("log10", builtin_log10::INST),
107 ("pow", builtin_pow::INST),112 ("pow", builtin_pow::INST),
108 ("sqrt", builtin_sqrt::INST),113 ("sqrt", builtin_sqrt::INST),
109 ("sin", builtin_sin::INST),114 ("sin", builtin_sin::INST),
121 ("isOdd", builtin_is_odd::INST),126 ("isOdd", builtin_is_odd::INST),
122 ("isInteger", builtin_is_integer::INST),127 ("isInteger", builtin_is_integer::INST),
123 ("isDecimal", builtin_is_decimal::INST),128 ("isDecimal", builtin_is_decimal::INST),
129 ("deg2rad", builtin_deg2rad::INST),
130 ("rad2deg", builtin_rad2deg::INST),
131 ("hypot", builtin_hypot::INST),
124 // Operator132 // Operator
125 ("mod", builtin_mod::INST),133 ("mod", builtin_mod::INST),
126 ("primitiveEquals", builtin_primitive_equals::INST),134 ("primitiveEquals", builtin_primitive_equals::INST),
201 ("lstripChars", builtin_lstrip_chars::INST),209 ("lstripChars", builtin_lstrip_chars::INST),
202 ("rstripChars", builtin_rstrip_chars::INST),210 ("rstripChars", builtin_rstrip_chars::INST),
203 ("stripChars", builtin_strip_chars::INST),211 ("stripChars", builtin_strip_chars::INST),
212 ("trim", builtin_trim::INST),
204 // Misc213 // Misc
205 ("length", builtin_length::INST),214 ("length", builtin_length::INST),
206 ("get", builtin_get::INST),215 ("get", builtin_get::INST),
248 builder.method("trace", builtin_trace { settings });257 builder.method("trace", builtin_trace { settings });
249 builder.method("id", FuncVal::Id);258 builder.method("id", FuncVal::Id);
259
260 builder.field("pi").hide().value(Val::Num(
261 NumValue::new(f64::consts::PI).expect("pi is finite"),
262 ));
250263
251 #[cfg(feature = "exp-regex")]264 #[cfg(feature = "exp-regex")]
252 {265 {
modifiedcrates/jrsonnet-stdlib/src/math.rsdiffbeforeafterboth
1use std::f64;
2
1use jrsonnet_evaluator::{function::builtin, typed::PositiveF64};3use jrsonnet_evaluator::{function::builtin, typed::PositiveF64};
24
55 x.ln()57 x.ln()
56}58}
59
60#[builtin]
61pub fn builtin_log2(x: f64) -> f64 {
62 x.log2()
63}
64
65#[builtin]
66pub fn builtin_log10(x: f64) -> f64 {
67 x.log10()
68}
5769
58#[builtin]70#[builtin]
59pub fn builtin_pow(x: f64, n: f64) -> f64 {71pub fn builtin_pow(x: f64, n: f64) -> f64 {
154 builtin_round(x) != x166 builtin_round(x) != x
155}167}
168
169#[builtin]
170pub fn builtin_deg2rad(x: f64) -> f64 {
171 x * f64::consts::PI / 180.0
172}
173
174#[builtin]
175pub fn builtin_rad2deg(x: f64) -> f64 {
176 x * 180.0 / f64::consts::PI
177}
178
179#[builtin]
180pub fn builtin_hypot(x: f64, y: f64) -> f64 {
181 x.hypot(y)
182}
156183
modifiedcrates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth
156 #[cfg(feature = "exp-preserve-order")]156 #[cfg(feature = "exp-preserve-order")]
157 true,157 true,
158 );158 );
159 let a = a.manifest(&format).description("<a> manifestification")?;159 let a = if let Some(a) = a.as_str() {
160 format!("<A>\n{a}\n</A>")
161 } else {
162 a.manifest(&format).description("<a> manifestification")?
163 };
160 let b = b.manifest(&format).description("<b> manifestification")?;164 let b = if let Some(b) = b.as_str() {
165 format!("<B>\n{b}\n</B>")
166 } else {
167 b.manifest(&format).description("<b> manifestification")?
168 };
161 bail!("assertion failed: A != B\nA: {a}\nB: {b}")169 bail!("assertion failed: A != B\nA: {a}\nB: {b}")
162}170}
163171
166 let Some(patch) = patch.as_obj() else {174 let Some(patch) = patch.as_obj() else {
167 return Ok(patch);175 return Ok(patch);
168 };176 };
169 let Some(target) = target.as_obj() else {177 let target = target.as_obj().unwrap_or_else(|| ObjValue::new_empty());
170 return Ok(Val::Obj(patch));
171 };
172 let target_fields = target178 let target_fields = target
173 .fields(179 .fields(
174 // FIXME: Makes no sense to preserve order for BTreeSet, it would be better to use IndexSet here?180 // FIXME: Makes no sense to preserve order for BTreeSet, it would be better to use IndexSet here?
203 if matches!(field_patch, Val::Null) {209 if matches!(field_patch, Val::Null) {
204 continue;210 continue;
205 }211 }
206 let Some(field_target) = target.get(field.clone())? else {212 let field_target = target.get(field.clone())?.unwrap_or(Val::Null);
207 out.field(field.clone()).value(field_patch);
208 continue;
209 };
210 out.field(field.clone())213 out.field(field.clone())
211 .value(builtin_merge_patch(field_target, field_patch)?);214 .value(builtin_merge_patch(field_target, field_patch)?);
212 }215 }
modifiedcrates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth
1use jrsonnet_evaluator::{1use jrsonnet_evaluator::{
2 function::builtin,2 function::builtin,
3 rustc_hash::FxHashSet,
3 val::{ArrValue, Val},4 val::{ArrValue, Val},
4 IStr, ObjValue, ObjValueBuilder,5 IStr, MaybeUnbound, ObjValue, ObjValueBuilder, Thunk,
5};6};
67
7#[builtin]8#[builtin]
166 preserve_order: bool,167 preserve_order: bool,
167) -> ObjValue {168) -> ObjValue {
168 let mut new_obj = ObjValueBuilder::with_capacity(obj.len() - 1);169 let mut new_obj = ObjValueBuilder::with_capacity(obj.len() - 1);
169 for (k, v) in obj.iter(170 let all_fields = obj.fields_ex(
171 true,
170 #[cfg(feature = "exp-preserve-order")]172 #[cfg(feature = "exp-preserve-order")]
171 preserve_order,173 preserve_order,
172 ) {174 );
175 let visible_fields = obj
176 .fields_ex(
177 false,
178 #[cfg(feature = "exp-preserve-order")]
179 preserve_order,
180 )
181 .into_iter()
182 .collect::<FxHashSet<_>>();
183
184 for field in &all_fields {
173 if k == key {185 if *field == key {
174 continue;186 continue;
175 }187 }
176 new_obj.field(k).value(v.unwrap());188 let mut b = new_obj.field(field.clone());
189 if !visible_fields.contains(&field) {
190 b = b.hide();
191 }
192 let _ = b.binding(MaybeUnbound::Bound(Thunk::result(
193 obj.get(field.clone()).transpose().expect("field exists"),
194 )));
177 }195 }
178196
179 new_obj.build()197 new_obj.build()
modifiedcrates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth
254 Ok(str.as_str().trim_matches(pattern).into())254 Ok(str.as_str().trim_matches(pattern).into())
255}255}
256
257#[builtin]
258pub fn builtin_trim(str: IStr) -> String {
259 let filter =
260 |v: char| {
261 v == ' '
262 || v == '\t' || v == '\n'
263 || v == '\u{000c}'
264 || v == '\r' || v == '\u{0085}'
265 || v == '\u{00a0}'
266 };
267 str.as_str().trim_matches(filter).to_string()
268}
256269
257fn new_trim_pattern(chars: IndexableVal) -> Result<impl Fn(char) -> bool> {270fn new_trim_pattern(chars: IndexableVal) -> Result<impl Fn(char) -> bool> {
258 let chars: BTreeSet<char> = match chars {271 let chars: BTreeSet<char> = match chars {
modifiedcrates/jrsonnet-stdlib/src/types.rsdiffbeforeafterboth
29pub fn builtin_is_function(v: Val) -> bool {29pub fn builtin_is_function(v: Val) -> bool {
30 matches!(v, Val::Func(_))30 matches!(v, Val::Func(_))
31}31}
32#[builtin]
33pub fn builtin_is_null(v: Val) -> bool {
34 matches!(v, Val::Null)
35}
3236