difftreelog
perf implement std.prune in native
in: master
6 files changed
cmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet-fmt/src/main.rs
+++ b/cmds/jrsonnet-fmt/src/main.rs
@@ -356,16 +356,16 @@
impl Printable for Member {
fn print(&self, out: &mut PrintItems) {
match self {
- Member::MemberBindStmt(b) => {
+ Self::MemberBindStmt(b) => {
p!(out, { b.obj_local() })
}
- Member::MemberAssertStmt(ass) => {
+ Self::MemberAssertStmt(ass) => {
p!(out, { ass.assertion() })
}
- Member::MemberFieldNormal(n) => {
+ Self::MemberFieldNormal(n) => {
p!(out, {n.field_name()} if(n.plus_token().is_some())({n.plus_token()}) {n.visibility()} str(" ") {n.expr()})
}
- Member::MemberFieldMethod(m) => {
+ Self::MemberFieldMethod(m) => {
p!(out, {m.field_name()} {m.params_desc()} {m.visibility()} str(" ") {m.expr()})
}
}
crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -1,6 +1,6 @@
use std::{borrow::Cow, fmt::Write};
-use crate::{bail, Result, State, Val};
+use crate::{bail, Result, ResultExt, State, Val};
pub trait ManifestFormat {
fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;
@@ -235,7 +235,8 @@
}
}
buf.push_str(cur_padding);
- manifest_json_ex_buf(&item?, buf, cur_padding, options)?;
+ manifest_json_ex_buf(&item?, buf, cur_padding, options)
+ .with_description(|| format!("elem <{i}> manifestification"))?;
}
cur_padding.truncate(old_len);
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth1use std::{2 any::Any,3 cell::RefCell,4 fmt::Debug,5 hash::{Hash, Hasher},6 ptr::addr_of,7};89use jrsonnet_gcmodule::{Cc, Trace, Weak};10use jrsonnet_interner::IStr;11use jrsonnet_parser::{ExprLocation, Visibility};12use rustc_hash::FxHashMap;1314use crate::{15 arr::{PickObjectKeyValues, PickObjectValues},16 bail,17 error::{suggest_object_fields, Error, ErrorKind::*},18 function::{CallLocation, FuncVal},19 gc::{GcHashMap, GcHashSet, TraceBox},20 operator::evaluate_add_op,21 tb,22 val::{ArrValue, ThunkValue},23 MaybeUnbound, Result, State, Thunk, Unbound, Val,24};2526#[cfg(not(feature = "exp-preserve-order"))]27mod ordering {28 #![allow(29 // This module works as stub for preserve-order feature30 clippy::unused_self,31 )]3233 use jrsonnet_gcmodule::Trace;3435 #[derive(Clone, Copy, Default, Debug, Trace)]36 pub struct FieldIndex(());37 impl FieldIndex {38 pub const fn next(self) -> Self {39 Self(())40 }41 }4243 #[derive(Clone, Copy, Default, Debug, Trace)]44 pub struct SuperDepth(());45 impl SuperDepth {46 pub const fn deeper(self) -> Self {47 Self(())48 }49 }5051 #[derive(Clone, Copy)]52 pub struct FieldSortKey(());53 impl FieldSortKey {54 pub const fn new(_: SuperDepth, _: FieldIndex) -> Self {55 Self(())56 }57 }58}5960#[cfg(feature = "exp-preserve-order")]61mod ordering {62 use std::cmp::Reverse;6364 use jrsonnet_gcmodule::Trace;6566 #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]67 pub struct FieldIndex(u32);68 impl FieldIndex {69 pub fn next(self) -> Self {70 Self(self.0 + 1)71 }72 }7374 #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]75 pub struct SuperDepth(u32);76 impl SuperDepth {77 pub fn deeper(self) -> Self {78 Self(self.0 + 1)79 }80 }8182 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]83 pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);84 impl FieldSortKey {85 pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {86 Self(Reverse(depth), index)87 }88 }89}9091use ordering::*;9293// 0 - add94// 12 - visibility95#[derive(Clone, Copy)]96pub struct ObjFieldFlags(u8);97impl ObjFieldFlags {98 fn new(add: bool, visibility: Visibility) -> Self {99 let mut v = 0;100 if add {101 v |= 1;102 }103 v |= match visibility {104 Visibility::Normal => 0b000,105 Visibility::Hidden => 0b010,106 Visibility::Unhide => 0b100,107 };108 Self(v)109 }110 pub fn add(&self) -> bool {111 self.0 & 1 != 0112 }113 pub fn visibility(&self) -> Visibility {114 match (self.0 & 0b110) >> 1 {115 0b00 => Visibility::Normal,116 0b01 => Visibility::Hidden,117 0b10 => Visibility::Unhide,118 _ => unreachable!(),119 }120 }121}122impl Debug for ObjFieldFlags {123 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {124 f.debug_struct("ObjFieldFlags")125 .field("add", &self.add())126 .field("visibility", &self.visibility())127 .finish()128 }129}130131#[allow(clippy::module_name_repetitions)]132#[derive(Debug, Trace)]133pub struct ObjMember {134 #[trace(skip)]135 flags: ObjFieldFlags,136 original_index: FieldIndex,137 pub invoke: MaybeUnbound,138 pub location: Option<ExprLocation>,139}140141pub trait ObjectAssertion: Trace {142 fn run(&self, super_obj: Option<ObjValue>, this: Option<ObjValue>) -> Result<()>;143}144145// Field => This146147#[derive(Trace)]148enum CacheValue {149 Cached(Val),150 NotFound,151 Pending,152 Errored(Error),153}154155#[allow(clippy::module_name_repetitions)]156#[derive(Trace)]157#[trace(tracking(force))]158pub struct OopObject {159 sup: Option<ObjValue>,160 // this: Option<ObjValue>,161 assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,162 assertions_ran: RefCell<GcHashSet<ObjValue>>,163 this_entries: Cc<GcHashMap<IStr, ObjMember>>,164 value_cache: RefCell<GcHashMap<(IStr, Option<WeakObjValue>), CacheValue>>,165}166impl Debug for OopObject {167 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {168 f.debug_struct("OopObject")169 .field("sup", &self.sup)170 // .field("assertions", &self.assertions)171 // .field("assertions_ran", &self.assertions_ran)172 .field("this_entries", &self.this_entries)173 // .field("value_cache", &self.value_cache)174 .finish_non_exhaustive()175 }176}177178type EnumFieldsHandler<'a> = dyn FnMut(SuperDepth, FieldIndex, IStr, Visibility) -> bool + 'a;179180pub trait ObjectLike: Trace + Any + Debug {181 fn extend_from(&self, sup: ObjValue) -> ObjValue;182 /// When using standalone super in object, `this.super_obj.with_this(this)` is executed183 fn with_this(&self, me: ObjValue, this: ObjValue) -> ObjValue {184 ObjValue::new(ThisOverride { inner: me, this })185 }186 fn this(&self) -> Option<ObjValue> {187 None188 }189 fn len(&self) -> usize;190 fn is_empty(&self) -> bool;191 // If callback returns false, iteration stops192 fn enum_fields(&self, depth: SuperDepth, handler: &mut EnumFieldsHandler<'_>) -> bool;193194 fn has_field_include_hidden(&self, name: IStr) -> bool;195 fn has_field(&self, name: IStr) -> bool;196197 fn get_for(&self, key: IStr, this: ObjValue) -> Result<Option<Val>>;198 fn get_for_uncached(&self, key: IStr, this: ObjValue) -> Result<Option<Val>>;199 fn field_visibility(&self, field: IStr) -> Option<Visibility>;200201 fn run_assertions_raw(&self, this: ObjValue) -> Result<()>;202}203204#[derive(Clone, Trace)]205pub struct WeakObjValue(#[trace(skip)] pub(crate) Weak<TraceBox<dyn ObjectLike>>);206207impl PartialEq for WeakObjValue {208 fn eq(&self, other: &Self) -> bool {209 Weak::ptr_eq(&self.0, &other.0)210 }211}212213impl Eq for WeakObjValue {}214impl Hash for WeakObjValue {215 fn hash<H: Hasher>(&self, hasher: &mut H) {216 // Safety: usize is POD217 let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };218 hasher.write_usize(addr);219 }220}221222#[allow(clippy::module_name_repetitions)]223#[derive(Clone, Trace, Debug)]224pub struct ObjValue(pub(crate) Cc<TraceBox<dyn ObjectLike>>);225226#[derive(Debug, Trace)]227struct EmptyObject;228impl ObjectLike for EmptyObject {229 fn extend_from(&self, sup: ObjValue) -> ObjValue {230 // obj + {} == obj231 sup232 }233234 fn this(&self) -> Option<ObjValue> {235 None236 }237238 fn len(&self) -> usize {239 0240 }241242 fn is_empty(&self) -> bool {243 true244 }245246 fn enum_fields(&self, _depth: SuperDepth, _handler: &mut EnumFieldsHandler<'_>) -> bool {247 false248 }249250 fn has_field_include_hidden(&self, _name: IStr) -> bool {251 false252 }253254 fn has_field(&self, _name: IStr) -> bool {255 false256 }257258 fn get_for(&self, _key: IStr, _this: ObjValue) -> Result<Option<Val>> {259 Ok(None)260 }261 fn get_for_uncached(&self, _key: IStr, _this: ObjValue) -> Result<Option<Val>> {262 Ok(None)263 }264265 fn run_assertions_raw(&self, _this: ObjValue) -> Result<()> {266 Ok(())267 }268269 fn field_visibility(&self, _field: IStr) -> Option<Visibility> {270 None271 }272}273274#[derive(Trace, Debug)]275struct ThisOverride {276 inner: ObjValue,277 this: ObjValue,278}279impl ObjectLike for ThisOverride {280 fn with_this(&self, _me: ObjValue, this: ObjValue) -> ObjValue {281 ObjValue::new(Self {282 inner: self.inner.clone(),283 this,284 })285 }286287 fn extend_from(&self, sup: ObjValue) -> ObjValue {288 self.inner.extend_from(sup).with_this(self.this.clone())289 }290291 fn this(&self) -> Option<ObjValue> {292 Some(self.this.clone())293 }294295 fn len(&self) -> usize {296 self.inner.len()297 }298299 fn is_empty(&self) -> bool {300 self.inner.is_empty()301 }302303 fn enum_fields(&self, depth: SuperDepth, handler: &mut EnumFieldsHandler<'_>) -> bool {304 self.inner.enum_fields(depth, handler)305 }306307 fn has_field_include_hidden(&self, name: IStr) -> bool {308 self.inner.has_field_include_hidden(name)309 }310311 fn has_field(&self, name: IStr) -> bool {312 self.inner.has_field(name)313 }314315 fn get_for(&self, key: IStr, this: ObjValue) -> Result<Option<Val>> {316 self.inner.get_for(key, this)317 }318319 fn get_for_uncached(&self, key: IStr, this: ObjValue) -> Result<Option<Val>> {320 self.inner.get_raw(key, this)321 }322323 fn field_visibility(&self, field: IStr) -> Option<Visibility> {324 self.inner.field_visibility(field)325 }326327 fn run_assertions_raw(&self, this: ObjValue) -> Result<()> {328 self.inner.run_assertions_raw(this)329 }330}331332impl ObjValue {333 pub fn new(v: impl ObjectLike) -> Self {334 Self(Cc::new(tb!(v)))335 }336 pub fn new_empty() -> Self {337 Self::new(EmptyObject)338 }339 pub fn builder() -> ObjValueBuilder {340 ObjValueBuilder::new()341 }342 pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {343 ObjValueBuilder::with_capacity(capacity)344 }345 pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {346 let mut out = ObjValueBuilder::with_capacity(1);347 out.with_super(self);348 let mut member = out.field(key);349 if value.flags.add() {350 member = member.add();351 }352 if let Some(loc) = value.location {353 member = member.with_location(loc);354 }355 let _ = member356 .with_visibility(value.flags.visibility())357 .binding(value.invoke);358 out.build()359 }360 pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {361 ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())362 }363364 #[must_use]365 pub fn extend_from(&self, sup: Self) -> Self {366 self.0.extend_from(sup)367 }368 #[must_use]369 pub fn with_this(&self, this: Self) -> Self {370 self.0.with_this(self.clone(), this)371 }372 pub fn len(&self) -> usize {373 self.0.len()374 }375 pub fn is_empty(&self) -> bool {376 self.0.is_empty()377 }378 pub fn enum_fields(&self, depth: SuperDepth, handler: &mut EnumFieldsHandler<'_>) -> bool {379 self.0.enum_fields(depth, handler)380 }381382 pub fn has_field_include_hidden(&self, name: IStr) -> bool {383 self.0.has_field_include_hidden(name)384 }385 pub fn has_field(&self, name: IStr) -> bool {386 self.0.has_field(name)387 }388 pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {389 if include_hidden {390 self.has_field_include_hidden(name)391 } else {392 self.has_field(name)393 }394 }395396 pub fn get(&self, key: IStr) -> Result<Option<Val>> {397 self.run_assertions()?;398 self.get_for(key, self.0.this().unwrap_or_else(|| self.clone()))399 }400401 pub fn get_for(&self, key: IStr, this: Self) -> Result<Option<Val>> {402 self.0.get_for(key, this)403 }404405 pub fn get_or_bail(&self, key: IStr) -> Result<Val> {406 let Some(value) = self.get(key.clone())? else {407 let suggestions = suggest_object_fields(self, key.clone());408 bail!(NoSuchField(key, suggestions))409 };410 Ok(value)411 }412413 fn get_raw(&self, key: IStr, this: Self) -> Result<Option<Val>> {414 self.0.get_for_uncached(key, this)415 }416417 fn field_visibility(&self, field: IStr) -> Option<Visibility> {418 self.0.field_visibility(field)419 }420421 pub fn run_assertions(&self) -> Result<()> {422 // FIXME: Should it use `self.0.this()` in case of standalone super?423 self.run_assertions_raw(self.clone())424 }425 fn run_assertions_raw(&self, this: Self) -> Result<()> {426 self.0.run_assertions_raw(this)427 }428429 pub fn iter(430 &self,431 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,432 ) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {433 let fields = self.fields(434 #[cfg(feature = "exp-preserve-order")]435 preserve_order,436 );437 fields.into_iter().map(|field| {438 (439 field.clone(),440 self.get(field)441 .map(|opt| opt.expect("iterating over keys, field exists")),442 )443 })444 }445 pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {446 #[derive(Trace)]447 struct ThunkGet {448 obj: ObjValue,449 key: IStr,450 }451 impl ThunkValue for ThunkGet {452 type Output = Val;453454 fn get(self: Box<Self>) -> Result<Self::Output> {455 Ok(self.obj.get(self.key)?.expect("field exists"))456 }457 }458459 if !self.has_field_ex(key.clone(), true) {460 return None;461 }462 Some(Thunk::new(ThunkGet {463 obj: self.clone(),464 key,465 }))466 }467 pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {468 #[derive(Trace)]469 struct ThunkGet {470 obj: ObjValue,471 key: IStr,472 }473 impl ThunkValue for ThunkGet {474 type Output = Val;475476 fn get(self: Box<Self>) -> Result<Self::Output> {477 self.obj.get_or_bail(self.key)478 }479 }480481 Thunk::new(ThunkGet {482 obj: self.clone(),483 key,484 })485 }486 pub fn ptr_eq(a: &Self, b: &Self) -> bool {487 Cc::ptr_eq(&a.0, &b.0)488 }489 pub fn downgrade(self) -> WeakObjValue {490 WeakObjValue(self.0.downgrade())491 }492 fn fields_visibility(&self) -> FxHashMap<IStr, (bool, FieldSortKey)> {493 let mut out = FxHashMap::default();494 self.enum_fields(495 SuperDepth::default(),496 &mut |depth, index, name, visibility| {497 let new_sort_key = FieldSortKey::new(depth, index);498 let entry = out.entry(name);499 let (visible, _) = entry.or_insert((true, new_sort_key));500 match visibility {501 Visibility::Normal => {}502 Visibility::Hidden => {503 *visible = false;504 }505 Visibility::Unhide => {506 *visible = true;507 }508 };509 false510 },511 );512 out513 }514 pub fn fields_ex(515 &self,516 include_hidden: bool,517 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,518 ) -> Vec<IStr> {519 #[cfg(feature = "exp-preserve-order")]520 if preserve_order {521 let (mut fields, mut keys): (Vec<_>, Vec<_>) = self522 .fields_visibility()523 .into_iter()524 .filter(|(_, (visible, _))| include_hidden || *visible)525 .enumerate()526 .map(|(idx, (k, (_, sk)))| (k, (sk, idx)))527 .unzip();528 keys.sort_unstable_by_key(|v| v.0);529 // Reorder in-place by resulting indexes530 for i in 0..fields.len() {531 let x = fields[i].clone();532 let mut j = i;533 loop {534 let k = keys[j].1;535 keys[j].1 = j;536 if k == i {537 break;538 }539 fields[j] = fields[k].clone();540 j = k;541 }542 fields[j] = x;543 }544 return fields;545 }546547 let mut fields: Vec<_> = self548 .fields_visibility()549 .into_iter()550 .filter(|(_, (visible, _))| include_hidden || *visible)551 .map(|(k, _)| k)552 .collect();553 fields.sort_unstable();554 fields555 }556 pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {557 self.fields_ex(558 false,559 #[cfg(feature = "exp-preserve-order")]560 preserve_order,561 )562 }563 pub fn values_ex(564 &self,565 include_hidden: bool,566 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,567 ) -> ArrValue {568 ArrValue::new(PickObjectValues::new(569 self.clone(),570 self.fields_ex(571 include_hidden,572 #[cfg(feature = "exp-preserve-order")]573 preserve_order,574 ),575 ))576 }577 pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {578 self.values_ex(579 false,580 #[cfg(feature = "exp-preserve-order")]581 preserve_order,582 )583 }584 pub fn key_values_ex(585 &self,586 include_hidden: bool,587 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,588 ) -> ArrValue {589 ArrValue::new(PickObjectKeyValues::new(590 self.clone(),591 self.fields_ex(592 include_hidden,593 #[cfg(feature = "exp-preserve-order")]594 preserve_order,595 ),596 ))597 }598 pub fn key_values(599 &self,600 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,601 ) -> ArrValue {602 self.key_values_ex(603 false,604 #[cfg(feature = "exp-preserve-order")]605 preserve_order,606 )607 }608}609610impl OopObject {611 pub fn new(612 sup: Option<ObjValue>,613 this_entries: Cc<GcHashMap<IStr, ObjMember>>,614 assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,615 ) -> Self {616 Self {617 sup,618 // this: None,619 assertions,620 assertions_ran: RefCell::new(GcHashSet::new()),621 this_entries,622 value_cache: RefCell::new(GcHashMap::new()),623 }624 }625626 fn evaluate_this(&self, v: &ObjMember, real_this: ObjValue) -> Result<Val> {627 v.invoke.evaluate(self.sup.clone(), Some(real_this))628 }629630 // FIXME: Duplication between ObjValue and OopObject631 fn fields_visibility(&self) -> FxHashMap<IStr, (bool, FieldSortKey)> {632 let mut out = FxHashMap::default();633 self.enum_fields(634 SuperDepth::default(),635 &mut |depth, index, name, visibility| {636 let new_sort_key = FieldSortKey::new(depth, index);637 let entry = out.entry(name);638 let (visible, _) = entry.or_insert((true, new_sort_key));639 match visibility {640 Visibility::Normal => {}641 Visibility::Hidden => {642 *visible = false;643 }644 Visibility::Unhide => {645 *visible = true;646 }647 };648 false649 },650 );651 out652 }653}654655impl ObjectLike for OopObject {656 fn extend_from(&self, sup: ObjValue) -> ObjValue {657 ObjValue::new(match &self.sup {658 None => Self::new(659 Some(sup),660 self.this_entries.clone(),661 self.assertions.clone(),662 ),663 Some(v) => Self::new(664 Some(v.extend_from(sup)),665 self.this_entries.clone(),666 self.assertions.clone(),667 ),668 })669 }670671 fn len(&self) -> usize {672 self.fields_visibility()673 .into_iter()674 .filter(|(_, (visible, _))| *visible)675 .count()676 }677678 fn is_empty(&self) -> bool {679 if !self.this_entries.is_empty() {680 return false;681 }682 self.sup.as_ref().map_or(true, ObjValue::is_empty)683 }684685 /// Run callback for every field found in object686 ///687 /// Returns true if ended prematurely688 fn enum_fields(&self, depth: SuperDepth, handler: &mut EnumFieldsHandler<'_>) -> bool {689 if let Some(s) = &self.sup {690 if s.enum_fields(depth.deeper(), handler) {691 return true;692 }693 }694 for (name, member) in self.this_entries.iter() {695 if handler(696 depth,697 member.original_index,698 name.clone(),699 member.flags.visibility(),700 ) {701 return true;702 }703 }704 false705 }706707 fn has_field_include_hidden(&self, name: IStr) -> bool {708 if self.this_entries.contains_key(&name) {709 true710 } else if let Some(super_obj) = &self.sup {711 super_obj.has_field_include_hidden(name)712 } else {713 false714 }715 }716 fn has_field(&self, name: IStr) -> bool {717 self.field_visibility(name)718 .map_or(false, |v| v.is_visible())719 }720721 fn get_for(&self, key: IStr, this: ObjValue) -> Result<Option<Val>> {722 let cache_key = (key.clone(), Some(this.clone().downgrade()));723 if let Some(v) = self.value_cache.borrow().get(&cache_key) {724 return Ok(match v {725 CacheValue::Cached(v) => Some(v.clone()),726 CacheValue::NotFound => None,727 CacheValue::Pending => bail!(InfiniteRecursionDetected),728 CacheValue::Errored(e) => return Err(e.clone()),729 });730 }731 self.value_cache732 .borrow_mut()733 .insert(cache_key.clone(), CacheValue::Pending);734 let value = self.get_for_uncached(key, this).map_err(|e| {735 self.value_cache736 .borrow_mut()737 .insert(cache_key.clone(), CacheValue::Errored(e.clone()));738 e739 })?;740 self.value_cache.borrow_mut().insert(741 cache_key,742 value743 .as_ref()744 .map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),745 );746 Ok(value)747 }748 fn get_for_uncached(&self, key: IStr, real_this: ObjValue) -> Result<Option<Val>> {749 match (self.this_entries.get(&key), &self.sup) {750 (Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),751 (Some(k), Some(super_obj)) => {752 let our = self.evaluate_this(k, real_this.clone())?;753 if k.flags.add() {754 super_obj755 .get_raw(key, real_this)?756 .map_or(Ok(Some(our.clone())), |v| {757 Ok(Some(evaluate_add_op(&v, &our)?))758 })759 } else {760 Ok(Some(our))761 }762 }763 (None, Some(super_obj)) => super_obj.get_raw(key, real_this),764 (None, None) => Ok(None),765 }766 }767 fn field_visibility(&self, name: IStr) -> Option<Visibility> {768 if let Some(m) = self.this_entries.get(&name) {769 Some(match &m.flags.visibility() {770 Visibility::Normal => self771 .sup772 .as_ref()773 .and_then(|super_obj| super_obj.field_visibility(name))774 .unwrap_or(Visibility::Normal),775 v => *v,776 })777 } else if let Some(super_obj) = &self.sup {778 super_obj.field_visibility(name)779 } else {780 None781 }782 }783784 fn run_assertions_raw(&self, real_this: ObjValue) -> Result<()> {785 if self.assertions.is_empty() {786 if let Some(super_obj) = &self.sup {787 super_obj.run_assertions_raw(real_this)?;788 }789 return Ok(());790 }791 if self.assertions_ran.borrow_mut().insert(real_this.clone()) {792 for assertion in self.assertions.iter() {793 if let Err(e) = assertion.run(self.sup.clone(), Some(real_this.clone())) {794 self.assertions_ran.borrow_mut().remove(&real_this);795 return Err(e);796 }797 }798 if let Some(super_obj) = &self.sup {799 super_obj.run_assertions_raw(real_this)?;800 }801 }802 Ok(())803 }804}805806impl PartialEq for ObjValue {807 fn eq(&self, other: &Self) -> bool {808 Cc::ptr_eq(&self.0, &other.0)809 }810}811812impl Eq for ObjValue {}813impl Hash for ObjValue {814 fn hash<H: Hasher>(&self, hasher: &mut H) {815 hasher.write_usize(addr_of!(*self.0) as usize);816 }817}818819#[allow(clippy::module_name_repetitions)]820pub struct ObjValueBuilder {821 sup: Option<ObjValue>,822 map: GcHashMap<IStr, ObjMember>,823 assertions: Vec<TraceBox<dyn ObjectAssertion>>,824 next_field_index: FieldIndex,825}826impl ObjValueBuilder {827 pub fn new() -> Self {828 Self::with_capacity(0)829 }830 pub fn with_capacity(capacity: usize) -> Self {831 Self {832 sup: None,833 map: GcHashMap::with_capacity(capacity),834 assertions: Vec::new(),835 next_field_index: FieldIndex::default(),836 }837 }838 pub fn reserve_asserts(&mut self, capacity: usize) -> &mut Self {839 self.assertions.reserve_exact(capacity);840 self841 }842 pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {843 self.sup = Some(super_obj);844 self845 }846847 pub fn assert(&mut self, assertion: impl ObjectAssertion + 'static) -> &mut Self {848 self.assertions.push(tb!(assertion));849 self850 }851 pub fn field(&mut self, name: impl Into<IStr>) -> ObjMemberBuilder<ValueBuilder<'_>> {852 let field_index = self.next_field_index;853 self.next_field_index = self.next_field_index.next();854 ObjMemberBuilder::new(ValueBuilder(self), name.into(), field_index)855 }856 /// Preset for common method definiton pattern:857 /// Create a hidden field with the function value.858 ///859 /// `.field(name).hide().value(Val::function(value))`860 pub fn method(&mut self, name: impl Into<IStr>, value: impl Into<FuncVal>) -> &mut Self {861 self.field(name).hide().value(Val::Func(value.into()));862 self863 }864 pub fn try_method(865 &mut self,866 name: impl Into<IStr>,867 value: impl Into<FuncVal>,868 ) -> Result<&mut Self> {869 self.field(name).hide().try_value(Val::Func(value.into()))?;870 Ok(self)871 }872873 pub fn build(self) -> ObjValue {874 if self.sup.is_none() && self.map.is_empty() && self.assertions.is_empty() {875 return ObjValue::new_empty();876 }877 ObjValue::new(OopObject::new(878 self.sup,879 Cc::new(self.map),880 Cc::new(self.assertions),881 ))882 }883}884impl Default for ObjValueBuilder {885 fn default() -> Self {886 Self::with_capacity(0)887 }888}889890#[allow(clippy::module_name_repetitions)]891#[must_use = "value not added unless binding() was called"]892pub struct ObjMemberBuilder<Kind> {893 kind: Kind,894 name: IStr,895 add: bool,896 visibility: Visibility,897 original_index: FieldIndex,898 location: Option<ExprLocation>,899}900901#[allow(clippy::missing_const_for_fn)]902impl<Kind> ObjMemberBuilder<Kind> {903 pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {904 Self {905 kind,906 name,907 original_index,908 add: false,909 visibility: Visibility::Normal,910 location: None,911 }912 }913914 pub const fn with_add(mut self, add: bool) -> Self {915 self.add = add;916 self917 }918 pub fn add(self) -> Self {919 self.with_add(true)920 }921 pub fn with_visibility(mut self, visibility: Visibility) -> Self {922 self.visibility = visibility;923 self924 }925 pub fn hide(self) -> Self {926 self.with_visibility(Visibility::Hidden)927 }928 pub fn with_location(mut self, location: ExprLocation) -> Self {929 self.location = Some(location);930 self931 }932 fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {933 (934 self.kind,935 self.name,936 ObjMember {937 flags: ObjFieldFlags::new(self.add, self.visibility),938 original_index: self.original_index,939 invoke: binding,940 location: self.location,941 },942 )943 }944}945946pub struct ValueBuilder<'v>(&'v mut ObjValueBuilder);947impl ObjMemberBuilder<ValueBuilder<'_>> {948 /// Inserts value, replacing if it is already defined949 pub fn value(self, value: impl Into<Val>) {950 let (receiver, name, member) =951 self.build_member(MaybeUnbound::Bound(Thunk::evaluated(value.into())));952 let entry = receiver.0.map.entry(name);953 entry.insert(member);954 }955956 /// Tries to insert value, returns an error if it was already defined957 pub fn try_value(self, value: impl Into<Val>) -> Result<()> {958 self.thunk(Thunk::evaluated(value.into()))959 }960 pub fn thunk(self, value: impl Into<Thunk<Val>>) -> Result<()> {961 self.binding(MaybeUnbound::Bound(value.into()))962 }963 pub fn bindable(self, bindable: impl Unbound<Bound = Val>) -> Result<()> {964 self.binding(MaybeUnbound::Unbound(Cc::new(tb!(bindable))))965 }966 pub fn binding(self, binding: MaybeUnbound) -> Result<()> {967 let (receiver, name, member) = self.build_member(binding);968 let location = member.location.clone();969 let old = receiver.0.map.insert(name.clone(), member);970 if old.is_some() {971 State::push(972 CallLocation(location.as_ref()),973 || format!("field <{}> initializtion", name.clone()),974 || bail!(DuplicateFieldName(name.clone())),975 )?;976 }977 Ok(())978 }979}980981pub struct ExtendBuilder<'v>(&'v mut ObjValue);982impl ObjMemberBuilder<ExtendBuilder<'_>> {983 pub fn value(self, value: impl Into<Val>) {984 self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));985 }986 pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Val>>) {987 self.binding(MaybeUnbound::Unbound(Cc::new(bindable)));988 }989 pub fn binding(self, binding: MaybeUnbound) {990 let (receiver, name, member) = self.build_member(binding);991 let new = receiver.0.clone();992 *receiver.0 = new.extend_with_raw_member(name, member);993 }994}crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -6,7 +6,7 @@
runtime_error,
typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},
val::{equals, ArrValue, IndexableVal},
- Either, IStr, Result, Thunk, Val,
+ Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,
};
pub(crate) fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {
@@ -21,16 +21,17 @@
pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {
if *sz == 0 {
return Ok(ArrValue::empty());
- }
- if let Some(trivial) = func.evaluate_trivial() {
- let mut out = Vec::with_capacity(*sz as usize);
- for _ in 0..*sz {
- out.push(trivial.clone())
- }
- Ok(ArrValue::eager(out))
- } else {
- Ok(ArrValue::range_exclusive(0, *sz).map(func))
}
+ func.evaluate_trivial().map_or_else(
+ || Ok(ArrValue::range_exclusive(0, *sz).map(func)),
+ |trivial| {
+ let mut out = Vec::with_capacity(*sz as usize);
+ for _ in 0..*sz {
+ out.push(trivial.clone());
+ }
+ Ok(ArrValue::eager(out))
+ },
+ )
}
#[builtin]
@@ -180,7 +181,7 @@
out += &sep;
}
first = false;
- write!(out, "{item}").unwrap()
+ write!(out, "{item}").unwrap();
} else if matches!(item, Val::Null) {
continue;
} else {
@@ -320,3 +321,61 @@
process(value, &mut out)?;
Ok(out)
}
+
+#[builtin]
+pub fn builtin_prune(
+ a: Val,
+ #[cfg(feature = "exp-preserve-order")] preserve_order: bool,
+) -> Result<Val> {
+ fn is_content(val: &Val) -> bool {
+ match val {
+ Val::Null => false,
+ Val::Arr(a) => !a.is_empty(),
+ Val::Obj(o) => !o.is_empty(),
+ _ => true,
+ }
+ }
+ Ok(match a {
+ Val::Arr(a) => {
+ let mut out = Vec::new();
+ for (i, ele) in a.iter().enumerate() {
+ let ele = ele
+ .and_then(|v| {
+ builtin_prune(
+ v,
+ #[cfg(feature = "exp-preserve-order")]
+ preserve_order,
+ )
+ })
+ .with_description(|| format!("elem <{i}> pruning"))?;
+ if is_content(&ele) {
+ out.push(ele);
+ }
+ }
+ Val::Arr(ArrValue::eager(out))
+ }
+ Val::Obj(o) => {
+ let mut out = ObjValueBuilder::new();
+ for (name, value) in o.iter(
+ #[cfg(feature = "exp-preserve-order")]
+ preserve_order,
+ ) {
+ let value = value
+ .and_then(|v| {
+ builtin_prune(
+ v,
+ #[cfg(feature = "exp-preserve-order")]
+ preserve_order,
+ )
+ })
+ .with_description(|| format!("field <{name}> pruning"))?;
+ if !is_content(&value) {
+ continue;
+ }
+ out.field(name).value(value);
+ }
+ Val::Obj(out.build())
+ }
+ _ => a,
+ })
+}
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -92,6 +92,7 @@
("remove", builtin_remove::INST),
("flattenArrays", builtin_flatten_arrays::INST),
("flattenDeepArray", builtin_flatten_deep_array::INST),
+ ("prune", builtin_prune::INST),
("filterMap", builtin_filter_map::INST),
// Math
("abs", builtin_abs::INST),
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -209,25 +209,6 @@
local arr = std.split(f, '/');
std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),
- prune(a)::
- local isContent(b) =
- if b == null then
- false
- else if std.isArray(b) then
- std.length(b) > 0
- else if std.isObject(b) then
- std.length(b) > 0
- else
- true;
- if std.isArray(a) then
- [std.prune(x) for x in a if isContent($.prune(x))]
- else if std.isObject(a) then {
- [x]: $.prune(a[x])
- for x in std.objectFields(a)
- if isContent(std.prune(a[x]))
- } else
- a,
-
find(value, arr)::
if !std.isArray(arr) then
error 'find second parameter should be an array, got ' + std.type(arr)