difftreelog
refactor use stack for first object field add
in: master
1 file changed
crates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth1use std::{2 any::Any, cell::{Cell, RefCell}, clone::Clone, cmp::Reverse, collections::hash_map::Entry, fmt::{self, Debug}, hash::{Hash, Hasher}, num::Saturating, ops::ControlFlow3};45use educe::Educe;6use jrsonnet_gcmodule::{cc_dyn, Acyclic, Cc, Trace, Weak};7use jrsonnet_interner::IStr;8use jrsonnet_ir::Span;9use rustc_hash::{FxHashMap, FxHashSet};1011mod oop;1213pub use jrsonnet_ir::Visibility;14pub use oop::ObjValueBuilder;1516use crate::{17 arr::{PickObjectKeyValues, PickObjectValues},18 bail,19 error::{suggest_object_fields, ErrorKind::*},20 identity_hash,21 operator::evaluate_add_op,22 val::{ArrValue, ThunkValue},23 CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,24};2526#[cfg(not(feature = "exp-preserve-order"))]27pub mod 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, PartialEq, Eq, PartialOrd, Ord)]36 pub struct FieldIndex(());37 impl FieldIndex {38 pub fn absolute(_v: u32) -> Self {39 Self(())40 }41 #[must_use]42 pub const fn next(self) -> Self {43 Self(())44 }45 }4647 #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]48 pub struct SuperDepth(());49 impl SuperDepth {50 pub(super) fn deepen(self) {}51 }52}5354#[cfg(feature = "exp-preserve-order")]55pub mod ordering {56 use jrsonnet_gcmodule::Trace;5758 #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]59 pub struct FieldIndex(u32);60 impl FieldIndex {61 pub fn absolute(v: u32) -> Self {62 Self(v)63 }64 #[must_use]65 pub fn next(self) -> Self {66 Self(self.0 + 1)67 }68 }6970 #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]71 pub struct SuperDepth(u32);72 impl SuperDepth {73 pub(super) fn deepen(&mut self) {74 self.0 += 1;75 }76 }77}7879use ordering::{FieldIndex, SuperDepth};8081#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]82pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);83impl FieldSortKey {84 pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {85 Self(Reverse(depth), index)86 }87}8889// 0 - add90// 12 - visibility91#[derive(Clone, Copy)]92pub struct ObjFieldFlags(u8);93impl ObjFieldFlags {94 fn new(add: bool, visibility: Visibility) -> Self {95 let mut v = 0;96 if add {97 v |= 1;98 }99 v |= match visibility {100 Visibility::Normal => 0b000,101 Visibility::Hidden => 0b010,102 Visibility::Unhide => 0b100,103 };104 Self(v)105 }106 pub fn add(&self) -> bool {107 self.0 & 1 != 0108 }109 pub fn visibility(&self) -> Visibility {110 match (self.0 & 0b110) >> 1 {111 0b00 => Visibility::Normal,112 0b01 => Visibility::Hidden,113 0b10 => Visibility::Unhide,114 _ => unreachable!(),115 }116 }117}118impl Debug for ObjFieldFlags {119 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {120 f.debug_struct("ObjFieldFlags")121 .field("add", &self.add())122 .field("visibility", &self.visibility())123 .finish()124 }125}126127#[allow(clippy::module_name_repetitions)]128#[derive(Debug, Trace)]129pub struct ObjMember {130 #[trace(skip)]131 flags: ObjFieldFlags,132 original_index: FieldIndex,133 pub invoke: MaybeUnbound,134 pub location: Option<Span>,135}136137cc_dyn!(CcObjectAssertion, ObjectAssertion);138pub trait ObjectAssertion: Trace {139 fn run(&self, sup_this: SupThis) -> Result<()>;140}141142// Field => This143144#[derive(Trace, Debug)]145enum CacheValue {146 Cached(Result<Option<Val>>),147 Pending,148}149150pub type EnumFieldsHandler<'a> =151 dyn FnMut(SuperDepth, FieldIndex, IStr, EnumFields) -> ControlFlow<()> + 'a;152153pub enum EnumFields {154 Normal(Visibility),155 Omit(Skip),156}157158#[derive(Trace, Clone)]159pub enum GetFor {160 // Return value161 Final(Val),162 // Continue iterating over cores, add current value to sum stack163 SuperPlus(Val),164 // Ignore the field value, stop at this layer instead165 Omit(#[trace(skip)] Skip),166 NotFound,167}168169#[derive(Acyclic, Clone)]170pub enum FieldVisibility {171 Found(Visibility),172 Omit(Skip),173 NotFound,174}175176#[derive(Acyclic, Clone)]177pub enum HasFieldIncludeHidden {178 Exists,179 NotFound,180 Omit(Skip),181}182183type Skip = Saturating<usize>;184185pub trait ObjectCore: Trace + Any + Debug {186 // If callback returns false, iteration stops, and this call returns false.187 fn enum_fields_core(188 &self,189 super_depth: &mut SuperDepth,190 handler: &mut EnumFieldsHandler<'_>,191 ) -> bool;192193 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden;194195 fn get_for_core(&self, key: IStr, sup_this: SupThis, omit_only: bool) -> Result<GetFor>;196 fn field_visibility_core(&self, field: IStr) -> FieldVisibility;197198 fn run_assertions_core(&self, sup_this: SupThis) -> Result<()>;199}200201#[derive(Clone, Trace)]202pub struct WeakObjValue(#[trace(skip)] Weak<ObjValueInner>);203impl Debug for WeakObjValue {204 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {205 f.debug_tuple("WeakObjValue").finish()206 }207}208209impl PartialEq for WeakObjValue {210 fn eq(&self, other: &Self) -> bool {211 Weak::ptr_eq(&self.0, &other.0)212 }213}214215impl Eq for WeakObjValue {}216impl Hash for WeakObjValue {217 fn hash<H: Hasher>(&self, hasher: &mut H) {218 // Safety: usize is POD219 let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };220 hasher.write_usize(addr);221 }222}223224cc_dyn!(225 #[derive(Clone, Debug)]226 CcObjectCore, ObjectCore,227 pub fn new() {...}228);229#[derive(Trace, Educe)]230#[educe(Debug)]231struct ObjValueInner {232 cores: Vec<CcObjectCore>,233 assertions_ran: Cell<bool>,234 has_assertions: bool,235 value_cache: RefCell<FxHashMap<(IStr, CoreIdx), CacheValue>>,236}237238thread_local! {239 static RUNNING_ASSERTIONS: RefCell<FxHashSet<ObjValue>> = RefCell::default();240}241fn is_asserting(obj: &ObjValue) -> bool {242 RUNNING_ASSERTIONS.with_borrow(|v| v.contains(obj))243}244/// Returns false if already asserting245fn start_asserting(obj: &ObjValue) -> bool {246 RUNNING_ASSERTIONS.with_borrow_mut(|v| v.insert(obj.clone()))247}248fn finish_asserting(obj: &ObjValue) {249 RUNNING_ASSERTIONS.with_borrow_mut(|v| {250 let r = v.remove(obj);251 debug_assert!(252 r,253 "finish_asserting was called before start_asserting or twice"254 );255 });256}257258thread_local! {259 static EMPTY_OBJ: ObjValue = ObjValue(Cc::new(ObjValueInner {260 cores: vec![],261 assertions_ran: Cell::new(true),262 has_assertions: false,263 value_cache: RefCell::default(),264 }))265}266267#[allow(clippy::module_name_repetitions)]268#[derive(Clone, Trace, Debug, Educe)]269#[educe(PartialEq, Hash, Eq)]270pub struct ObjValue(271 #[educe(PartialEq(method(Cc::ptr_eq)), Hash(method(identity_hash)))] Cc<ObjValueInner>,272);273274impl ObjValue {275 pub fn empty() -> Self {276 EMPTY_OBJ.with(Clone::clone)277 }278 pub fn is_empty(&self) -> bool {279 self.0.cores.is_empty() || self.len() == 0280 }281}282283#[derive(Trace, Debug)]284struct StandaloneSuperCore {285 sup: CoreIdx,286 this: ObjValue,287}288impl ObjectCore for StandaloneSuperCore {289 fn enum_fields_core(290 &self,291 super_depth: &mut SuperDepth,292 handler: &mut EnumFieldsHandler<'_>,293 ) -> bool {294 self.this.enum_fields_idx(super_depth, handler, self.sup)295 }296297 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {298 if self.this.has_field_include_hidden_idx(name, self.sup) {299 HasFieldIncludeHidden::Exists300 } else {301 HasFieldIncludeHidden::NotFound302 }303 }304305 fn get_for_core(&self, key: IStr, _sup_this: SupThis, omit_only: bool) -> Result<GetFor> {306 if omit_only {307 return Ok(GetFor::NotFound);308 }309 let v = self.this.get_idx(key, self.sup)?;310 Ok(v.map_or(GetFor::NotFound, GetFor::Final))311 }312313 fn field_visibility_core(&self, field: IStr) -> FieldVisibility {314 self.this315 .field_visibility_idx(field, self.sup)316 .map_or(FieldVisibility::NotFound, FieldVisibility::Found)317 }318319 fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {320 self.this.run_assertions()321 }322}323324#[derive(Debug, Acyclic)]325struct OmitFieldsCore {326 omit: FxHashSet<IStr>,327 prev_layers: usize,328}329impl ObjectCore for OmitFieldsCore {330 fn enum_fields_core(331 &self,332 super_depth: &mut SuperDepth,333 handler: &mut EnumFieldsHandler<'_>,334 ) -> bool {335 let mut fi = FieldIndex::default();336 for f in &self.omit {337 if handler(338 *super_depth,339 fi,340 f.clone(),341 EnumFields::Omit(Saturating(self.prev_layers)),342 ) == ControlFlow::Break(())343 {344 return false;345 }346 fi = fi.next();347 }348 true349 }350351 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {352 if self.omit.contains(&name) {353 return HasFieldIncludeHidden::Omit(Saturating(self.prev_layers));354 }355 HasFieldIncludeHidden::NotFound356 }357358 fn get_for_core(&self, key: IStr, _sup_this: SupThis, _omit_only: bool) -> Result<GetFor> {359 if self.omit.contains(&key) {360 return Ok(GetFor::Omit(Saturating(self.prev_layers)));361 }362 Ok(GetFor::NotFound)363 }364365 fn field_visibility_core(&self, field: IStr) -> FieldVisibility {366 if self.omit.contains(&field) {367 return FieldVisibility::Omit(Saturating(self.prev_layers));368 }369 FieldVisibility::NotFound370 }371372 fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {373 Ok(())374 }375}376377#[derive(Hash, PartialEq, Eq, Trace, Clone, Copy, Debug)]378struct CoreIdx {379 idx: usize,380}381impl CoreIdx {382 fn super_exists(self) -> bool {383 self.idx != 0384 }385}386#[derive(Trace, Clone, PartialEq, Eq, Hash, Debug)]387pub struct SupThis {388 sup: CoreIdx,389 this: ObjValue,390}391impl SupThis {392 pub fn has_super(&self) -> bool {393 self.sup.super_exists()394 }395 /// Implementation of `"field" in super` operation,396 /// works faster than standalone super path.397 ///398 /// In case of no `super` existence, returns false.399 pub fn field_in_super(&self, field: IStr) -> bool {400 self.this.has_field_include_hidden_idx(field, self.sup)401 }402 /// Implementation of `super.field` operation,403 /// works faster than standalone super path.404 ///405 /// In case of no `super` existence, returns `NoSuperFound`406 pub fn get_super(&self, field: IStr) -> Result<Option<Val>> {407 if !self.sup.super_exists() {408 bail!(NoSuperFound);409 }410 self.this.get_idx(field, self.sup)411 }412 /// `super` with `self` overriden for top-level lookups.413 /// Exists when super appears outside of `super.field`/`"field" in super` expressions414 /// Exclusive to jrsonnet.415 ///416 /// Might return `NoSuperFound` error.417 pub fn standalone_super(&self) -> Result<ObjValue> {418 if !self.sup.super_exists() {419 bail!(NoSuperFound)420 }421 let mut out = ObjValue::builder();422 out.reserve_cores(1).extend_with_core(StandaloneSuperCore {423 sup: self.sup,424 this: self.this.clone(),425 });426 Ok(out.build())427 }428 pub fn this(&self) -> &ObjValue {429 &self.this430 }431 pub fn downgrade(self) -> WeakSupThis {432 WeakSupThis {433 sup: self.sup,434 this: self.this.downgrade(),435 }436 }437}438#[derive(Trace, PartialEq, Eq, Hash, Debug)]439pub struct WeakSupThis {440 sup: CoreIdx,441 this: WeakObjValue,442}443444impl ObjValue {445 pub fn builder() -> ObjValueBuilder {446 ObjValueBuilder::new()447 }448 pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {449 ObjValueBuilder::with_capacity(capacity)450 }451 pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {452 let mut out = ObjValueBuilder::with_capacity(1);453 out.with_super(self);454 let mut member = out.field(key);455 if value.flags.add() {456 member = member.add();457 }458 if let Some(loc) = value.location {459 member = member.with_location(loc);460 }461 let _ = member462 .with_visibility(value.flags.visibility())463 .binding(value.invoke);464 out.build()465 }466 pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {467 ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())468 }469470 pub fn extend(&mut self) -> ObjValueBuilder {471 let mut out = ObjValueBuilder::new();472 out.with_super(self.clone());473 out474 }475476 #[must_use]477 pub fn extend_from(&self, sup: Self) -> Self {478 let mut cores = sup.0.cores.clone();479 cores.extend(self.0.cores.iter().cloned());480 let has_assertions = sup.0.has_assertions || self.0.has_assertions;481 ObjValue(Cc::new(ObjValueInner {482 cores,483 value_cache: RefCell::default(),484 assertions_ran: Cell::new(!has_assertions),485 has_assertions,486 }))487 }488 // #[must_use]489 // pub fn with_this(&self, this: Self) -> Self {490 // self.0.with_this(self.clone(), this)491 // }492 /// Returns amount of visible object fields493 /// If object only contains hidden fields - may return zero.494 pub fn len(&self) -> usize {495 self.fields_visibility()496 .values()497 .filter(|d| d.visible())498 .count()499 }500 /// For each field, calls callback.501 /// If callback returns false - ends iteration prematurely.502 ///503 /// Returns false if ended prematurely504 pub fn enum_fields(&self, handler: &mut EnumFieldsHandler<'_>) -> bool {505 let mut super_depth = SuperDepth::default();506 self.enum_fields_idx(507 &mut super_depth,508 handler,509 CoreIdx {510 idx: self.0.cores.len(),511 },512 )513 }514 fn enum_fields_idx(515 &self,516 super_depth: &mut SuperDepth,517 handler: &mut EnumFieldsHandler<'_>,518 idx: CoreIdx,519 ) -> bool {520 for core in self.0.cores[..idx.idx].iter().rev() {521 if !core.0.enum_fields_core(super_depth, handler) {522 return false;523 }524 super_depth.deepen();525 }526 true527 }528529 pub fn has_field_include_hidden(&self, name: IStr) -> bool {530 self.has_field_include_hidden_idx(531 name,532 CoreIdx {533 idx: self.0.cores.len(),534 },535 )536 }537 fn has_field_include_hidden_idx(&self, name: IStr, core: CoreIdx) -> bool {538 let mut skip = Saturating(0usize);539 for ele in self.0.cores[..core.idx].iter().rev() {540 match ele.0.has_field_include_hidden_core(name.clone()) {541 HasFieldIncludeHidden::Exists => {542 if skip.0 == 0 {543 return true;544 }545 }546 HasFieldIncludeHidden::Omit(new_skip) => {547 // +1 including this core548 skip = skip.max(new_skip + Saturating(1));549 }550 HasFieldIncludeHidden::NotFound => {}551 }552 skip -= 1;553 }554 false555 }556 pub fn has_field(&self, name: IStr) -> bool {557 match self.field_visibility(name) {558 Some(Visibility::Unhide | Visibility::Normal) => true,559 Some(Visibility::Hidden) | None => false,560 }561 }562 pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {563 if include_hidden {564 self.has_field_include_hidden(name)565 } else {566 self.has_field(name)567 }568 }569 pub fn get(&self, key: IStr) -> Result<Option<Val>> {570 self.get_idx(571 key,572 CoreIdx {573 idx: self.0.cores.len(),574 },575 )576 }577578 fn get_idx(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {579 let cache_key = (key.clone(), core);580 {581 let mut cache = self.0.value_cache.borrow_mut();582 // entry_ref candidate?583 match cache.entry(cache_key.clone()) {584 Entry::Occupied(v) => match v.get() {585 CacheValue::Cached(v) => return v.clone(),586 CacheValue::Pending => {587 if !is_asserting(self) {588 bail!(InfiniteRecursionDetected);589 }590 }591 },592 Entry::Vacant(v) => {593 v.insert(CacheValue::Pending);594 }595 };596 }597 let result = self.get_idx_uncached(key, core);598 {599 let mut cache = self.0.value_cache.borrow_mut();600 cache.insert(cache_key, CacheValue::Cached(result.clone()));601 }602 result603 }604 fn get_idx_uncached(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {605 self.run_assertions()?;606 let mut add_stack = Vec::with_capacity(2);607 let mut skip = Saturating(0);608 for (sup, core) in self.0.cores[..core.idx].iter().enumerate().rev() {609 let sup_this = SupThis {610 sup: CoreIdx { idx: sup },611 this: self.clone(),612 };613 match core.0.get_for_core(key.clone(), sup_this, skip.0 != 0)? {614 GetFor::Final(val) if add_stack.is_empty() => {615 if skip.0 == 0 {616 return Ok(Some(val));617 }618 }619 GetFor::Final(val) => {620 if skip.0 == 0 {621 add_stack.push(val);622 break;623 }624 }625 GetFor::SuperPlus(val) => {626 if skip.0 == 0 {627 add_stack.push(val);628 }629 }630 GetFor::Omit(new_skip) => {631 // +1 including this core632 skip = skip.max(new_skip + Saturating(1));633 }634 GetFor::NotFound => {}635 }636 skip -= 1;637 }638 if add_stack.is_empty() {639 // None of layers had this field640 return Ok(None);641 } else if add_stack.len() == 1 {642 // A layer had this field, but it wanted this field to be added with super.643 // However, no super had this field, fail-safe644 return Ok(Some(add_stack.pop().expect("single element on stack")));645 }646 let mut values = add_stack.into_iter().rev();647 let init = values.next().expect("at least 2 elements");648649 values650 .try_fold(init, |a, b| evaluate_add_op(&a, &b))651 .map(Some)652653 // self.0.get_raw(key, this)654 }655656 pub fn get_or_bail(&self, key: IStr) -> Result<Val> {657 let Some(value) = self.get(key.clone())? else {658 let suggestions = suggest_object_fields(self, key.clone());659 bail!(NoSuchField(key, suggestions))660 };661 Ok(value)662 }663664 fn field_visibility(&self, field: IStr) -> Option<Visibility> {665 self.field_visibility_idx(666 field,667 CoreIdx {668 idx: self.0.cores.len(),669 },670 )671 }672 fn field_visibility_idx(&self, field: IStr, core: CoreIdx) -> Option<Visibility> {673 let mut exists = false;674 let mut skip = Saturating(0usize);675 for ele in self.0.cores[..core.idx].iter().rev() {676 let vis = ele.0.field_visibility_core(field.clone());677 match vis {678 FieldVisibility::Found(vis @ (Visibility::Unhide | Visibility::Hidden)) => {679 if skip.0 == 0 {680 return Some(vis);681 }682 }683 FieldVisibility::Found(Visibility::Normal) => {684 if skip.0 == 0 {685 exists = true;686 }687 }688 FieldVisibility::NotFound => {}689 FieldVisibility::Omit(new_skip) => {690 // +1 including this core691 skip = skip.max(new_skip + Saturating(1));692 }693 }694 skip -= 1;695 }696 exists.then_some(Visibility::Normal)697 }698699 pub fn run_assertions(&self) -> Result<()> {700 if self.0.assertions_ran.get() {701 return Ok(());702 }703 if !start_asserting(self) {704 return Ok(());705 }706 for (idx, ele) in self.0.cores.iter().enumerate() {707 let sup_this = SupThis {708 sup: CoreIdx { idx },709 this: self.clone(),710 };711 ele.0.run_assertions_core(sup_this).inspect_err(|_e| {712 finish_asserting(self);713 })?;714 }715 finish_asserting(self);716 self.0.assertions_ran.set(true);717 Ok(())718 }719720 pub fn iter(721 &self,722 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,723 ) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {724 let fields = self.fields(725 #[cfg(feature = "exp-preserve-order")]726 preserve_order,727 );728 fields.into_iter().map(|field| {729 (730 field.clone(),731 self.get(field)732 .map(|opt| opt.expect("iterating over keys, field exists")),733 )734 })735 }736 pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {737 #[derive(Trace)]738 struct ObjFieldThunk {739 obj: ObjValue,740 key: IStr,741 }742 impl ThunkValue for ObjFieldThunk {743 type Output = Val;744745 fn get(&self) -> Result<Self::Output> {746 self.obj747 .get(self.key.clone())748 .transpose()749 .expect("field existence checked")750 }751 }752753 if !self.has_field_ex(key.clone(), true) {754 return None;755 }756757 Some(Thunk::new(ObjFieldThunk {758 obj: self.clone(),759 key,760 }))761 }762 pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {763 #[derive(Trace)]764 struct ObjFieldThunk {765 obj: ObjValue,766 key: IStr,767 }768 impl ThunkValue for ObjFieldThunk {769 type Output = Val;770771 fn get(&self) -> Result<Self::Output> {772 self.obj.get_or_bail(self.key.clone())773 }774 }775776 Thunk::new(ObjFieldThunk {777 obj: self.clone(),778 key,779 })780 }781 pub fn ptr_eq(a: &Self, b: &Self) -> bool {782 Cc::ptr_eq(&a.0, &b.0)783 }784 pub fn downgrade(self) -> WeakObjValue {785 WeakObjValue(self.0.downgrade())786 }787}788789#[derive(Debug)]790struct FieldVisibilityData {791 omitted_until: Saturating<usize>,792 exists_visible: Option<Visibility>,793 #[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]794 key: FieldSortKey,795}796impl FieldVisibilityData {797 fn visible(&self) -> bool {798 self.exists_visible799 .expect("non-existing fields shall be dropped at the end of fn fields_visibility()")800 .is_visible()801 }802 #[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]803 fn sort_key(&self) -> FieldSortKey {804 self.key805 }806}807808impl ObjValue {809 fn fields_visibility(&self) -> FxHashMap<IStr, FieldVisibilityData> {810 let mut out = FxHashMap::default();811812 let mut super_depth = SuperDepth::default();813 let mut omit_index = Saturating(0);814 for core in self.0.cores.iter().rev() {815 core.0816 .enum_fields_core(&mut super_depth, &mut |depth, index, name, visibility| {817 let entry = out.entry(name);818 let data = entry.or_insert_with(|| FieldVisibilityData {819 exists_visible: None,820 key: FieldSortKey::new(depth, index),821 omitted_until: omit_index,822 });823 match visibility {824 EnumFields::Omit(new_skip) => {825 // +1 including this core826 data.omitted_until = data827 .omitted_until828 .max(omit_index + new_skip + Saturating(1));829 }830 EnumFields::Normal(Visibility::Normal) => {831 if data.omitted_until <= omit_index && data.exists_visible.is_none() {832 data.exists_visible = Some(Visibility::Normal);833 }834 }835 EnumFields::Normal(Visibility::Hidden) => {836 if data.omitted_until <= omit_index {837 data.exists_visible = Some(match data.exists_visible {838 // We're iterating in reverse, later unhide is preserved839 Some(Visibility::Unhide) => Visibility::Unhide,840 _ => Visibility::Hidden,841 });842 }843 }844 EnumFields::Normal(Visibility::Unhide) => {845 if data.omitted_until <= omit_index {846 data.exists_visible = Some(match data.exists_visible {847 // We're iterating in reverse, later hide is preserved848 Some(Visibility::Hidden) => Visibility::Hidden,849 _ => Visibility::Unhide,850 });851 }852 }853 }854 ControlFlow::Continue(())855 });856857 super_depth.deepen();858 omit_index += 1;859 }860861 out.retain(|_, v| v.exists_visible.is_some());862863 out864 }865 pub fn fields_ex(866 &self,867 include_hidden: bool,868 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,869 ) -> Vec<IStr> {870 #[cfg(feature = "exp-preserve-order")]871 if preserve_order {872 let (mut fields, mut keys): (Vec<_>, Vec<_>) = self873 .fields_visibility()874 .into_iter()875 .filter(|(_, d)| include_hidden || d.visible())876 .enumerate()877 .map(|(idx, (k, d))| (k, (d.sort_key(), idx)))878 .unzip();879 keys.sort_unstable_by_key(|v| v.0);880 // Reorder in-place by resulting indexes881 for i in 0..fields.len() {882 let x = fields[i].clone();883 let mut j = i;884 loop {885 let k = keys[j].1;886 keys[j].1 = j;887 if k == i {888 break;889 }890 fields[j] = fields[k].clone();891 j = k;892 }893 fields[j] = x;894 }895 return fields;896 }897898 let mut fields: Vec<_> = self899 .fields_visibility()900 .into_iter()901 .filter(|(_, d)| include_hidden || d.visible())902 .map(|(k, _)| k)903 .collect();904 fields.sort_unstable();905 fields906 }907 pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {908 self.fields_ex(909 false,910 #[cfg(feature = "exp-preserve-order")]911 preserve_order,912 )913 }914 pub fn values_ex(915 &self,916 include_hidden: bool,917 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,918 ) -> ArrValue {919 ArrValue::new(PickObjectValues::new(920 self.clone(),921 self.fields_ex(922 include_hidden,923 #[cfg(feature = "exp-preserve-order")]924 preserve_order,925 ),926 ))927 }928 pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {929 self.values_ex(930 false,931 #[cfg(feature = "exp-preserve-order")]932 preserve_order,933 )934 }935 pub fn key_values_ex(936 &self,937 include_hidden: bool,938 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,939 ) -> ArrValue {940 ArrValue::new(PickObjectKeyValues::new(941 self.clone(),942 self.fields_ex(943 include_hidden,944 #[cfg(feature = "exp-preserve-order")]945 preserve_order,946 ),947 ))948 }949 pub fn key_values(950 &self,951 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,952 ) -> ArrValue {953 self.key_values_ex(954 false,955 #[cfg(feature = "exp-preserve-order")]956 preserve_order,957 )958 }959}960961#[allow(clippy::module_name_repetitions)]962#[must_use = "value not added unless binding() was called"]963pub struct ObjMemberBuilder<Kind> {964 kind: Kind,965 name: IStr,966 add: bool,967 visibility: Visibility,968 original_index: FieldIndex,969 location: Option<Span>,970}971972#[allow(clippy::missing_const_for_fn)]973impl<Kind> ObjMemberBuilder<Kind> {974 pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {975 Self {976 kind,977 name,978 original_index,979 add: false,980 visibility: Visibility::Normal,981 location: None,982 }983 }984985 pub const fn with_add(mut self, add: bool) -> Self {986 self.add = add;987 self988 }989 pub fn add(self) -> Self {990 self.with_add(true)991 }992 pub fn with_visibility(mut self, visibility: Visibility) -> Self {993 self.visibility = visibility;994 self995 }996 pub fn hide(self) -> Self {997 self.with_visibility(Visibility::Hidden)998 }999 pub fn with_location(mut self, location: Span) -> Self {1000 self.location = Some(location);1001 self1002 }1003 fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {1004 (1005 self.kind,1006 self.name,1007 ObjMember {1008 flags: ObjFieldFlags::new(self.add, self.visibility),1009 original_index: self.original_index,1010 invoke: binding,1011 location: self.location,1012 },1013 )1014 }1015}10161017pub struct ExtendBuilder<'v>(&'v mut ObjValue);1018impl ObjMemberBuilder<ExtendBuilder<'_>> {1019 pub fn value(self, value: impl Into<Val>) {1020 self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));1021 }1022 pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {1023 self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));1024 }1025 pub fn binding(self, binding: MaybeUnbound) {1026 let (receiver, name, member) = self.build_member(binding);1027 let new = receiver.0.clone();1028 *receiver.0 = new.extend_with_raw_member(name, member);1029 }1030}