1use std::{2 any::Any,3 cell::{Cell, RefCell},4 clone::Clone,5 cmp::Reverse,6 collections::hash_map::Entry,7 fmt::{self, Debug},8 hash::{Hash, Hasher},9 num::Saturating,10 ops::ControlFlow,11};1213use educe::Educe;14use jrsonnet_gcmodule::{cc_dyn, Acyclic, Cc, Trace, Weak};15use jrsonnet_interner::IStr;16use jrsonnet_ir::Span;17use rustc_hash::{FxHashMap, FxHashSet};1819mod oop;2021pub use jrsonnet_ir::Visibility;22pub use oop::ObjValueBuilder;2324use crate::{25 arr::{PickObjectKeyValues, PickObjectValues},26 bail,27 error::{suggest_object_fields, ErrorKind::*},28 identity_hash,29 operator::evaluate_add_op,30 val::{ArrValue, ThunkValue},31 CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,32};3334#[cfg(not(feature = "exp-preserve-order"))]35pub mod ordering {36 #![allow(37 38 clippy::unused_self,39 )]4041 use jrsonnet_gcmodule::Trace;4243 #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]44 pub struct FieldIndex(());45 impl FieldIndex {46 pub fn absolute(_v: u32) -> Self {47 Self(())48 }49 #[must_use]50 pub const fn next(self) -> Self {51 Self(())52 }53 }5455 #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]56 pub struct SuperDepth(());57 impl SuperDepth {58 pub(super) fn deepen(self) {}59 }60}6162#[cfg(feature = "exp-preserve-order")]63pub mod ordering {64 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 absolute(v: u32) -> Self {70 Self(v)71 }72 #[must_use]73 pub fn next(self) -> Self {74 Self(self.0 + 1)75 }76 }7778 #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]79 pub struct SuperDepth(u32);80 impl SuperDepth {81 pub(super) fn deepen(&mut self) {82 self.0 += 1;83 }84 }85}8687use ordering::{FieldIndex, SuperDepth};8889#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]90pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);91impl FieldSortKey {92 pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {93 Self(Reverse(depth), index)94 }95}96979899#[derive(Clone, Copy)]100pub struct ObjFieldFlags(u8);101impl ObjFieldFlags {102 fn new(add: bool, visibility: Visibility) -> Self {103 let mut v = 0;104 if add {105 v |= 1;106 }107 v |= match visibility {108 Visibility::Normal => 0b000,109 Visibility::Hidden => 0b010,110 Visibility::Unhide => 0b100,111 };112 Self(v)113 }114 pub fn add(&self) -> bool {115 self.0 & 1 != 0116 }117 pub fn visibility(&self) -> Visibility {118 match (self.0 & 0b110) >> 1 {119 0b00 => Visibility::Normal,120 0b01 => Visibility::Hidden,121 0b10 => Visibility::Unhide,122 _ => unreachable!(),123 }124 }125}126impl Debug for ObjFieldFlags {127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {128 f.debug_struct("ObjFieldFlags")129 .field("add", &self.add())130 .field("visibility", &self.visibility())131 .finish()132 }133}134135#[allow(clippy::module_name_repetitions)]136#[derive(Debug, Trace)]137pub struct ObjMember {138 #[trace(skip)]139 flags: ObjFieldFlags,140 original_index: FieldIndex,141 pub invoke: MaybeUnbound,142 pub location: Option<Span>,143}144145cc_dyn!(CcObjectAssertion, ObjectAssertion);146pub trait ObjectAssertion: Trace {147 fn run(&self, sup_this: SupThis) -> Result<()>;148}149150151152#[derive(Trace, Debug)]153enum CacheValue {154 Cached(Result<Option<Val>>),155 Pending,156}157158pub type EnumFieldsHandler<'a> =159 dyn FnMut(SuperDepth, FieldIndex, IStr, EnumFields) -> ControlFlow<()> + 'a;160161pub enum EnumFields {162 Normal(Visibility),163 Omit(Skip),164}165166#[derive(Trace, Clone)]167pub enum GetFor {168 169 Final(Val),170 171 SuperPlus(Val),172 173 Omit(#[trace(skip)] Skip),174 NotFound,175}176177#[derive(Acyclic, Clone)]178pub enum FieldVisibility {179 Found(Visibility),180 Omit(Skip),181 NotFound,182}183184#[derive(Acyclic, Clone)]185pub enum HasFieldIncludeHidden {186 Exists,187 NotFound,188 Omit(Skip),189}190191type Skip = Saturating<usize>;192193pub trait ObjectCore: Trace + Any + Debug {194 195 fn enum_fields_core(196 &self,197 super_depth: &mut SuperDepth,198 handler: &mut EnumFieldsHandler<'_>,199 ) -> bool;200201 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden;202203 fn get_for_core(&self, key: IStr, sup_this: SupThis, omit_only: bool) -> Result<GetFor>;204 fn field_visibility_core(&self, field: IStr) -> FieldVisibility;205206 fn run_assertions_core(&self, sup_this: SupThis) -> Result<()>;207}208209#[derive(Clone, Trace)]210pub struct WeakObjValue(#[trace(skip)] Weak<ObjValueInner>);211impl Debug for WeakObjValue {212 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {213 f.debug_tuple("WeakObjValue").finish()214 }215}216217impl PartialEq for WeakObjValue {218 fn eq(&self, other: &Self) -> bool {219 Weak::ptr_eq(&self.0, &other.0)220 }221}222223impl Eq for WeakObjValue {}224impl Hash for WeakObjValue {225 fn hash<H: Hasher>(&self, hasher: &mut H) {226 227 let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };228 hasher.write_usize(addr);229 }230}231232cc_dyn!(233 #[derive(Clone, Debug)]234 CcObjectCore, ObjectCore,235 pub fn new() {...}236);237#[derive(Trace, Educe)]238#[educe(Debug)]239struct ObjValueInner {240 cores: Vec<CcObjectCore>,241 assertions_ran: Cell<bool>,242 has_assertions: bool,243 value_cache: RefCell<FxHashMap<(IStr, CoreIdx), CacheValue>>,244}245246thread_local! {247 static RUNNING_ASSERTIONS: RefCell<FxHashSet<ObjValue>> = RefCell::default();248}249fn is_asserting(obj: &ObjValue) -> bool {250 RUNNING_ASSERTIONS.with_borrow(|v| v.contains(obj))251}252253fn start_asserting(obj: &ObjValue) -> bool {254 RUNNING_ASSERTIONS.with_borrow_mut(|v| v.insert(obj.clone()))255}256fn finish_asserting(obj: &ObjValue) {257 RUNNING_ASSERTIONS.with_borrow_mut(|v| {258 let r = v.remove(obj);259 debug_assert!(260 r,261 "finish_asserting was called before start_asserting or twice"262 );263 });264}265266thread_local! {267 static EMPTY_OBJ: ObjValue = ObjValue(Cc::new(ObjValueInner {268 cores: vec![],269 assertions_ran: Cell::new(true),270 has_assertions: false,271 value_cache: RefCell::default(),272 }))273}274275#[allow(clippy::module_name_repetitions)]276#[derive(Clone, Trace, Debug, Educe)]277#[educe(PartialEq, Hash, Eq)]278pub struct ObjValue(279 #[educe(PartialEq(method(Cc::ptr_eq)), Hash(method(identity_hash)))] Cc<ObjValueInner>,280);281282impl ObjValue {283 pub fn empty() -> Self {284 EMPTY_OBJ.with(Clone::clone)285 }286 pub fn is_empty(&self) -> bool {287 self.0.cores.is_empty() || self.len() == 0288 }289}290291#[derive(Trace, Debug)]292struct StandaloneSuperCore {293 sup: CoreIdx,294 this: ObjValue,295}296impl ObjectCore for StandaloneSuperCore {297 fn enum_fields_core(298 &self,299 super_depth: &mut SuperDepth,300 handler: &mut EnumFieldsHandler<'_>,301 ) -> bool {302 self.this.enum_fields_idx(super_depth, handler, self.sup)303 }304305 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {306 if self.this.has_field_include_hidden_idx(name, self.sup) {307 HasFieldIncludeHidden::Exists308 } else {309 HasFieldIncludeHidden::NotFound310 }311 }312313 fn get_for_core(&self, key: IStr, _sup_this: SupThis, omit_only: bool) -> Result<GetFor> {314 if omit_only {315 return Ok(GetFor::NotFound);316 }317 let v = self.this.get_idx(key, self.sup)?;318 Ok(v.map_or(GetFor::NotFound, GetFor::Final))319 }320321 fn field_visibility_core(&self, field: IStr) -> FieldVisibility {322 self.this323 .field_visibility_idx(field, self.sup)324 .map_or(FieldVisibility::NotFound, FieldVisibility::Found)325 }326327 fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {328 self.this.run_assertions()329 }330}331332#[derive(Debug, Acyclic)]333struct OmitFieldsCore {334 omit: FxHashSet<IStr>,335 prev_layers: usize,336}337impl ObjectCore for OmitFieldsCore {338 fn enum_fields_core(339 &self,340 super_depth: &mut SuperDepth,341 handler: &mut EnumFieldsHandler<'_>,342 ) -> bool {343 let mut fi = FieldIndex::default();344 for f in &self.omit {345 if handler(346 *super_depth,347 fi,348 f.clone(),349 EnumFields::Omit(Saturating(self.prev_layers)),350 ) == ControlFlow::Break(())351 {352 return false;353 }354 fi = fi.next();355 }356 true357 }358359 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {360 if self.omit.contains(&name) {361 return HasFieldIncludeHidden::Omit(Saturating(self.prev_layers));362 }363 HasFieldIncludeHidden::NotFound364 }365366 fn get_for_core(&self, key: IStr, _sup_this: SupThis, _omit_only: bool) -> Result<GetFor> {367 if self.omit.contains(&key) {368 return Ok(GetFor::Omit(Saturating(self.prev_layers)));369 }370 Ok(GetFor::NotFound)371 }372373 fn field_visibility_core(&self, field: IStr) -> FieldVisibility {374 if self.omit.contains(&field) {375 return FieldVisibility::Omit(Saturating(self.prev_layers));376 }377 FieldVisibility::NotFound378 }379380 fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {381 Ok(())382 }383}384385#[derive(Hash, PartialEq, Eq, Trace, Clone, Copy, Debug)]386struct CoreIdx {387 idx: usize,388}389impl CoreIdx {390 fn super_exists(self) -> bool {391 self.idx != 0392 }393}394#[derive(Trace, Clone, PartialEq, Eq, Hash, Debug)]395pub struct SupThis {396 sup: CoreIdx,397 this: ObjValue,398}399impl SupThis {400 pub fn has_super(&self) -> bool {401 self.sup.super_exists()402 }403 404 405 406 407 pub fn field_in_super(&self, field: IStr) -> bool {408 self.this.has_field_include_hidden_idx(field, self.sup)409 }410 411 412 413 414 pub fn get_super(&self, field: IStr) -> Result<Option<Val>> {415 if !self.sup.super_exists() {416 bail!(NoSuperFound);417 }418 self.this.get_idx(field, self.sup)419 }420 421 422 423 424 425 pub fn standalone_super(&self) -> Result<ObjValue> {426 if !self.sup.super_exists() {427 bail!(NoSuperFound)428 }429 let mut out = ObjValue::builder();430 out.reserve_cores(1).extend_with_core(StandaloneSuperCore {431 sup: self.sup,432 this: self.this.clone(),433 });434 Ok(out.build())435 }436 pub fn this(&self) -> &ObjValue {437 &self.this438 }439 pub fn downgrade(self) -> WeakSupThis {440 WeakSupThis {441 sup: self.sup,442 this: self.this.downgrade(),443 }444 }445}446#[derive(Trace, PartialEq, Eq, Hash, Debug)]447pub struct WeakSupThis {448 sup: CoreIdx,449 this: WeakObjValue,450}451452impl ObjValue {453 pub fn builder() -> ObjValueBuilder {454 ObjValueBuilder::new()455 }456 pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {457 ObjValueBuilder::with_capacity(capacity)458 }459 pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {460 let mut out = ObjValueBuilder::with_capacity(1);461 out.with_super(self);462 let mut member = out.field(key);463 if value.flags.add() {464 member = member.add();465 }466 if let Some(loc) = value.location {467 member = member.with_location(loc);468 }469 let _ = member470 .with_visibility(value.flags.visibility())471 .binding(value.invoke);472 out.build()473 }474 pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {475 ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())476 }477478 pub fn extend(&mut self) -> ObjValueBuilder {479 let mut out = ObjValueBuilder::new();480 out.with_super(self.clone());481 out482 }483484 #[must_use]485 pub fn extend_from(&self, sup: Self) -> Self {486 let mut cores = Vec::with_capacity(sup.0.cores.len() + self.0.cores.len());487 cores.extend(sup.0.cores.iter().cloned());488 cores.extend(self.0.cores.iter().cloned());489 let has_assertions = sup.0.has_assertions || self.0.has_assertions;490 ObjValue(Cc::new(ObjValueInner {491 cores,492 value_cache: RefCell::default(),493 assertions_ran: Cell::new(!has_assertions),494 has_assertions,495 }))496 }497 498 499 500 501 502 503 pub fn len(&self) -> usize {504 self.fields_visibility()505 .values()506 .filter(|d| d.visible())507 .count()508 }509 510 511 512 513 pub fn enum_fields(&self, handler: &mut EnumFieldsHandler<'_>) -> bool {514 let mut super_depth = SuperDepth::default();515 self.enum_fields_idx(516 &mut super_depth,517 handler,518 CoreIdx {519 idx: self.0.cores.len(),520 },521 )522 }523 fn enum_fields_idx(524 &self,525 super_depth: &mut SuperDepth,526 handler: &mut EnumFieldsHandler<'_>,527 idx: CoreIdx,528 ) -> bool {529 for core in self.0.cores[..idx.idx].iter().rev() {530 if !core.0.enum_fields_core(super_depth, handler) {531 return false;532 }533 super_depth.deepen();534 }535 true536 }537538 pub fn has_field_include_hidden(&self, name: IStr) -> bool {539 self.has_field_include_hidden_idx(540 name,541 CoreIdx {542 idx: self.0.cores.len(),543 },544 )545 }546 fn has_field_include_hidden_idx(&self, name: IStr, core: CoreIdx) -> bool {547 let mut skip = Saturating(0usize);548 for ele in self.0.cores[..core.idx].iter().rev() {549 match ele.0.has_field_include_hidden_core(name.clone()) {550 HasFieldIncludeHidden::Exists => {551 if skip.0 == 0 {552 return true;553 }554 }555 HasFieldIncludeHidden::Omit(new_skip) => {556 557 skip = skip.max(new_skip + Saturating(1));558 }559 HasFieldIncludeHidden::NotFound => {}560 }561 skip -= 1;562 }563 false564 }565 pub fn has_field(&self, name: IStr) -> bool {566 match self.field_visibility(name) {567 Some(Visibility::Unhide | Visibility::Normal) => true,568 Some(Visibility::Hidden) | None => false,569 }570 }571 pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {572 if include_hidden {573 self.has_field_include_hidden(name)574 } else {575 self.has_field(name)576 }577 }578 pub fn get(&self, key: IStr) -> Result<Option<Val>> {579 self.get_idx(580 key,581 CoreIdx {582 idx: self.0.cores.len(),583 },584 )585 }586587 fn get_idx(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {588 let cache_key = (key.clone(), core);589 {590 let mut cache = self.0.value_cache.borrow_mut();591 592 match cache.entry(cache_key.clone()) {593 Entry::Occupied(v) => match v.get() {594 CacheValue::Cached(v) => return v.clone(),595 CacheValue::Pending => {596 if !is_asserting(self) {597 bail!(InfiniteRecursionDetected);598 }599 }600 },601 Entry::Vacant(v) => {602 v.insert(CacheValue::Pending);603 }604 };605 }606 let result = self.get_idx_uncached(key, core);607 {608 let mut cache = self.0.value_cache.borrow_mut();609 cache.insert(cache_key, CacheValue::Cached(result.clone()));610 }611 result612 }613 fn get_idx_uncached(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {614 self.run_assertions()?;615 let mut first_add = None;616 let mut add_stack: Vec<Val> = Vec::new();617 let mut skip = Saturating(0);618 for (sup, core) in self.0.cores[..core.idx].iter().enumerate().rev() {619 let sup_this = SupThis {620 sup: CoreIdx { idx: sup },621 this: self.clone(),622 };623 match core.0.get_for_core(key.clone(), sup_this, skip.0 != 0)? {624 GetFor::Final(val) if first_add.is_none() => {625 if skip.0 == 0 {626 return Ok(Some(val));627 }628 }629 GetFor::Final(val) => {630 if skip.0 == 0 {631 add_stack.push(val);632 break;633 }634 }635 GetFor::SuperPlus(val) => {636 if skip.0 == 0 {637 if first_add.is_none() {638 first_add = Some(val);639 } else {640 add_stack.push(val);641 }642 }643 }644 GetFor::Omit(new_skip) => {645 skip = skip.max(new_skip + Saturating(1));646 }647 GetFor::NotFound => {}648 }649 skip -= 1;650 }651 let Some(first) = first_add else {652 if add_stack.is_empty() {653 return Ok(None);654 }655 return Ok(Some(add_stack.pop().expect("single element on stack")));656 };657 if add_stack.is_empty() {658 return Ok(Some(first));659 }660 add_stack.insert(0, first);661 let mut values = add_stack.into_iter().rev();662 let init = values.next().expect("at least 2 elements");663664 values665 .try_fold(init, |a, b| evaluate_add_op(&a, &b))666 .map(Some)667 }668669 pub fn get_or_bail(&self, key: IStr) -> Result<Val> {670 let Some(value) = self.get(key.clone())? else {671 let suggestions = suggest_object_fields(self, key.clone());672 bail!(NoSuchField(key, suggestions))673 };674 Ok(value)675 }676677 fn field_visibility(&self, field: IStr) -> Option<Visibility> {678 self.field_visibility_idx(679 field,680 CoreIdx {681 idx: self.0.cores.len(),682 },683 )684 }685 fn field_visibility_idx(&self, field: IStr, core: CoreIdx) -> Option<Visibility> {686 let mut exists = false;687 let mut skip = Saturating(0usize);688 for ele in self.0.cores[..core.idx].iter().rev() {689 let vis = ele.0.field_visibility_core(field.clone());690 match vis {691 FieldVisibility::Found(vis @ (Visibility::Unhide | Visibility::Hidden)) => {692 if skip.0 == 0 {693 return Some(vis);694 }695 }696 FieldVisibility::Found(Visibility::Normal) => {697 if skip.0 == 0 {698 exists = true;699 }700 }701 FieldVisibility::NotFound => {}702 FieldVisibility::Omit(new_skip) => {703 704 skip = skip.max(new_skip + Saturating(1));705 }706 }707 skip -= 1;708 }709 exists.then_some(Visibility::Normal)710 }711712 pub fn run_assertions(&self) -> Result<()> {713 if self.0.assertions_ran.get() {714 return Ok(());715 }716 if !start_asserting(self) {717 return Ok(());718 }719 for (idx, ele) in self.0.cores.iter().enumerate() {720 let sup_this = SupThis {721 sup: CoreIdx { idx },722 this: self.clone(),723 };724 ele.0.run_assertions_core(sup_this).inspect_err(|_e| {725 finish_asserting(self);726 })?;727 }728 finish_asserting(self);729 self.0.assertions_ran.set(true);730 Ok(())731 }732733 pub fn iter(734 &self,735 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,736 ) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {737 let fields = self.fields(738 #[cfg(feature = "exp-preserve-order")]739 preserve_order,740 );741 fields.into_iter().map(|field| {742 (743 field.clone(),744 self.get(field)745 .map(|opt| opt.expect("iterating over keys, field exists")),746 )747 })748 }749 pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {750 #[derive(Trace)]751 struct ObjFieldThunk {752 obj: ObjValue,753 key: IStr,754 }755 impl ThunkValue for ObjFieldThunk {756 type Output = Val;757758 fn get(&self) -> Result<Self::Output> {759 self.obj760 .get(self.key.clone())761 .transpose()762 .expect("field existence checked")763 }764 }765766 if !self.has_field_ex(key.clone(), true) {767 return None;768 }769770 Some(Thunk::new(ObjFieldThunk {771 obj: self.clone(),772 key,773 }))774 }775 pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {776 #[derive(Trace)]777 struct ObjFieldThunk {778 obj: ObjValue,779 key: IStr,780 }781 impl ThunkValue for ObjFieldThunk {782 type Output = Val;783784 fn get(&self) -> Result<Self::Output> {785 self.obj.get_or_bail(self.key.clone())786 }787 }788789 Thunk::new(ObjFieldThunk {790 obj: self.clone(),791 key,792 })793 }794 pub fn ptr_eq(a: &Self, b: &Self) -> bool {795 Cc::ptr_eq(&a.0, &b.0)796 }797 pub fn downgrade(self) -> WeakObjValue {798 WeakObjValue(self.0.downgrade())799 }800}801802#[derive(Debug)]803struct FieldVisibilityData {804 omitted_until: Saturating<usize>,805 exists_visible: Option<Visibility>,806 #[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]807 key: FieldSortKey,808}809impl FieldVisibilityData {810 fn visible(&self) -> bool {811 self.exists_visible812 .expect("non-existing fields shall be dropped at the end of fn fields_visibility()")813 .is_visible()814 }815 #[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]816 fn sort_key(&self) -> FieldSortKey {817 self.key818 }819}820821impl ObjValue {822 fn fields_visibility(&self) -> FxHashMap<IStr, FieldVisibilityData> {823 let mut out = FxHashMap::default();824825 let mut super_depth = SuperDepth::default();826 let mut omit_index = Saturating(0);827 for core in self.0.cores.iter().rev() {828 core.0829 .enum_fields_core(&mut super_depth, &mut |depth, index, name, visibility| {830 let entry = out.entry(name);831 let data = entry.or_insert_with(|| FieldVisibilityData {832 exists_visible: None,833 key: FieldSortKey::new(depth, index),834 omitted_until: omit_index,835 });836 match visibility {837 EnumFields::Omit(new_skip) => {838 839 data.omitted_until = data840 .omitted_until841 .max(omit_index + new_skip + Saturating(1));842 }843 EnumFields::Normal(Visibility::Normal) => {844 if data.omitted_until <= omit_index && data.exists_visible.is_none() {845 data.exists_visible = Some(Visibility::Normal);846 }847 }848 EnumFields::Normal(Visibility::Hidden) => {849 if data.omitted_until <= omit_index {850 data.exists_visible = Some(match data.exists_visible {851 852 Some(Visibility::Unhide) => Visibility::Unhide,853 _ => Visibility::Hidden,854 });855 }856 }857 EnumFields::Normal(Visibility::Unhide) => {858 if data.omitted_until <= omit_index {859 data.exists_visible = Some(match data.exists_visible {860 861 Some(Visibility::Hidden) => Visibility::Hidden,862 _ => Visibility::Unhide,863 });864 }865 }866 }867 ControlFlow::Continue(())868 });869870 super_depth.deepen();871 omit_index += 1;872 }873874 out.retain(|_, v| v.exists_visible.is_some());875876 out877 }878 pub fn fields_ex(879 &self,880 include_hidden: bool,881 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,882 ) -> Vec<IStr> {883 #[cfg(feature = "exp-preserve-order")]884 if preserve_order {885 let (mut fields, mut keys): (Vec<_>, Vec<_>) = self886 .fields_visibility()887 .into_iter()888 .filter(|(_, d)| include_hidden || d.visible())889 .enumerate()890 .map(|(idx, (k, d))| (k, (d.sort_key(), idx)))891 .unzip();892 keys.sort_unstable_by_key(|v| v.0);893 894 for i in 0..fields.len() {895 let x = fields[i].clone();896 let mut j = i;897 loop {898 let k = keys[j].1;899 keys[j].1 = j;900 if k == i {901 break;902 }903 fields[j] = fields[k].clone();904 j = k;905 }906 fields[j] = x;907 }908 return fields;909 }910911 let mut fields: Vec<_> = self912 .fields_visibility()913 .into_iter()914 .filter(|(_, d)| include_hidden || d.visible())915 .map(|(k, _)| k)916 .collect();917 fields.sort_unstable();918 fields919 }920 pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {921 self.fields_ex(922 false,923 #[cfg(feature = "exp-preserve-order")]924 preserve_order,925 )926 }927 pub fn values_ex(928 &self,929 include_hidden: bool,930 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,931 ) -> ArrValue {932 ArrValue::new(PickObjectValues::new(933 self.clone(),934 self.fields_ex(935 include_hidden,936 #[cfg(feature = "exp-preserve-order")]937 preserve_order,938 ),939 ))940 }941 pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {942 self.values_ex(943 false,944 #[cfg(feature = "exp-preserve-order")]945 preserve_order,946 )947 }948 pub fn key_values_ex(949 &self,950 include_hidden: bool,951 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,952 ) -> ArrValue {953 ArrValue::new(PickObjectKeyValues::new(954 self.clone(),955 self.fields_ex(956 include_hidden,957 #[cfg(feature = "exp-preserve-order")]958 preserve_order,959 ),960 ))961 }962 pub fn key_values(963 &self,964 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,965 ) -> ArrValue {966 self.key_values_ex(967 false,968 #[cfg(feature = "exp-preserve-order")]969 preserve_order,970 )971 }972}973974#[allow(clippy::module_name_repetitions)]975#[must_use = "value not added unless binding() was called"]976pub struct ObjMemberBuilder<Kind> {977 kind: Kind,978 name: IStr,979 add: bool,980 visibility: Visibility,981 original_index: FieldIndex,982 location: Option<Span>,983}984985#[allow(clippy::missing_const_for_fn)]986impl<Kind> ObjMemberBuilder<Kind> {987 pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {988 Self {989 kind,990 name,991 original_index,992 add: false,993 visibility: Visibility::Normal,994 location: None,995 }996 }997998 pub const fn with_add(mut self, add: bool) -> Self {999 self.add = add;1000 self1001 }1002 pub fn add(self) -> Self {1003 self.with_add(true)1004 }1005 pub fn with_visibility(mut self, visibility: Visibility) -> Self {1006 self.visibility = visibility;1007 self1008 }1009 pub fn hide(self) -> Self {1010 self.with_visibility(Visibility::Hidden)1011 }1012 pub fn with_location(mut self, location: Span) -> Self {1013 self.location = Some(location);1014 self1015 }1016 fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {1017 (1018 self.kind,1019 self.name,1020 ObjMember {1021 flags: ObjFieldFlags::new(self.add, self.visibility),1022 original_index: self.original_index,1023 invoke: binding,1024 location: self.location,1025 },1026 )1027 }1028}10291030pub struct ExtendBuilder<'v>(&'v mut ObjValue);1031impl ObjMemberBuilder<ExtendBuilder<'_>> {1032 pub fn value(self, value: impl Into<Val>) {1033 self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));1034 }1035 pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {1036 self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));1037 }1038 pub fn binding(self, binding: MaybeUnbound) {1039 let (receiver, name, member) = self.build_member(binding);1040 let new = receiver.0.clone();1041 *receiver.0 = new.extend_with_raw_member(name, member);1042 }1043}