difftreelog
feat immutable obj core list
in: master
7 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -163,6 +163,15 @@
checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
[[package]]
+name = "bitmaps"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "031043d04099746d8db04daf1fa424b2bc8bd69d92b25962dcde24da39ab64a2"
+dependencies = [
+ "typenum",
+]
+
+[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -682,6 +691,20 @@
checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954"
[[package]]
+name = "im-rc"
+version = "15.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "af1955a75fa080c677d3972822ec4bad316169ab1cfc6c257a942c2265dbe5fe"
+dependencies = [
+ "bitmaps",
+ "rand_core 0.6.4",
+ "rand_xoshiro",
+ "sized-chunks",
+ "typenum",
+ "version_check",
+]
+
+[[package]]
name = "indexmap"
version = "2.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -793,6 +816,7 @@
"anyhow",
"educe",
"hi-doc",
+ "im-rc",
"jrsonnet-gcmodule",
"jrsonnet-interner",
"jrsonnet-ir",
@@ -837,18 +861,19 @@
[[package]]
name = "jrsonnet-gcmodule"
-version = "0.4.2"
+version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f95b976a79e4000bb9e07ff0709dca0ea27bcf1952d4c17d91fb7364d6145683"
+checksum = "8a6a63a6e55ba82764e483d7f8a181f25db95a8f25da8ae6520e95a5fe39c6a6"
dependencies = [
+ "im-rc",
"jrsonnet-gcmodule-derive",
]
[[package]]
name = "jrsonnet-gcmodule-derive"
-version = "0.4.2"
+version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "51d928626220a310ff0cec815e80cf7fe104697184352ca21c40534e0b0d72d9"
+checksum = "095fe3c4c0acf32de80205a8a479ef63c216b9efb0024dec9eb7fe1c5ef1f1a1"
dependencies = [
"proc-macro2",
"quote",
@@ -1319,7 +1344,7 @@
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [
"rand_chacha",
- "rand_core",
+ "rand_core 0.9.5",
]
[[package]]
@@ -1329,11 +1354,17 @@
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
- "rand_core",
+ "rand_core 0.9.5",
]
[[package]]
name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+
+[[package]]
+name = "rand_core"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
@@ -1342,6 +1373,15 @@
]
[[package]]
+name = "rand_xoshiro"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa"
+dependencies = [
+ "rand_core 0.6.4",
+]
+
+[[package]]
name = "random_color"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1587,6 +1627,16 @@
checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa"
[[package]]
+name = "sized-chunks"
+version = "0.6.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "16d69225bde7a69b235da73377861095455d298f2b970996eec25ddbb42b3d1e"
+dependencies = [
+ "bitmaps",
+ "typenum",
+]
+
+[[package]]
name = "smallvec"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
Cargo.tomldiffbeforeafterboth--- a/Cargo.toml
+++ b/Cargo.toml
@@ -22,7 +22,7 @@
jrsonnet-cli = { path = "./crates/jrsonnet-cli", version = "0.5.0-pre98" }
jrsonnet-types = { path = "./crates/jrsonnet-types", version = "0.5.0-pre98" }
jrsonnet-formatter = { path = "./crates/jrsonnet-formatter", version = "0.5.0-pre98" }
-jrsonnet-gcmodule = { version = "0.4.2" }
+jrsonnet-gcmodule = { version = "0.4.3", features = ["im-rc"] }
# Diagnostics.
# hi-doc is my library, which handles text formatting very well, but isn't polished enough yet
# Previous implementation was based on annotate-snippets, which I don't like for many reasons.
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -76,6 +76,7 @@
"Hash",
"PartialEq",
] }
+im-rc = "15.1.0"
[build-dependencies]
rustversion = "1.0.22"
crates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -144,9 +144,7 @@
Thunk!(move || {
let full = full.evaluate()?;
let mut builder = ObjValueBuilder::new();
- builder
- .reserve_cores(1)
- .extend_with_core(full.as_standalone());
+ builder.extend_with_core(full.as_standalone());
builder.with_fields_omitted(captured_fields);
Ok(Val::Obj(builder.build()))
}),
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -410,7 +410,7 @@
let locals = obj.locals.clone();
evaluate_comp(ctx, &obj.compspecs, 0, &mut |ctx, reserve| {
let uctx = evaluate_object_locals(ctx.clone(), locals.clone());
- builder.reserve_cores(reserve);
+ builder.reserve_fields(reserve);
evaluate_field_member(&mut builder, ctx, uctx, &obj.field)
})?;
crates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth1use 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::{Acyclic, Cc, Trace, Weak, cc_dyn};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 CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,26 arr::{PickObjectKeyValues, PickObjectValues},27 bail,28 error::{ErrorKind::*, suggest_object_fields},29 identity_hash,30 operator::evaluate_add_op,31 val::{ArrValue, ThunkValue},32};3334#[cfg(not(feature = "exp-preserve-order"))]35pub mod ordering {36 #![allow(37 // This module works as stub for preserve-order feature38 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}9697// 0 - add98// 12 - visibility99#[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}149150// Field => This151152#[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;160161#[derive(Debug)]162pub enum EnumFields {163 Normal(Visibility),164 Omit(Skip),165}166167#[derive(Trace, Clone)]168pub enum GetFor {169 // Return value170 Final(Val),171 // Continue iterating over cores, add current value to sum stack172 SuperPlus(Val),173 // Ignore the field value, stop at this layer instead174 Omit(#[trace(skip)] Skip),175 NotFound,176}177178#[derive(Acyclic, Clone)]179pub enum FieldVisibility {180 Found(Visibility),181 Omit(Skip),182 NotFound,183}184185#[derive(Acyclic, Clone)]186pub enum HasFieldIncludeHidden {187 Exists,188 NotFound,189 Omit(Skip),190}191192type Skip = Saturating<usize>;193194pub trait ObjectCore: Trace + Any + Debug {195 // If callback returns false, iteration stops, and this call returns false.196 fn enum_fields_core(197 &self,198 super_depth: &mut SuperDepth,199 handler: &mut EnumFieldsHandler<'_>,200 ) -> bool;201202 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden;203204 fn get_for_core(&self, key: IStr, sup_this: SupThis, omit_only: bool) -> Result<GetFor>;205 fn field_visibility_core(&self, field: IStr) -> FieldVisibility;206207 fn run_assertions_core(&self, sup_this: SupThis) -> Result<()>;208}209210#[derive(Clone, Trace)]211pub struct WeakObjValue(#[trace(skip)] Weak<ObjValueInner>);212impl Debug for WeakObjValue {213 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {214 f.debug_tuple("WeakObjValue").finish()215 }216}217218impl PartialEq for WeakObjValue {219 fn eq(&self, other: &Self) -> bool {220 Weak::ptr_eq(&self.0, &other.0)221 }222}223224impl Eq for WeakObjValue {}225impl Hash for WeakObjValue {226 fn hash<H: Hasher>(&self, hasher: &mut H) {227 // Safety: usize is POD228 let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };229 hasher.write_usize(addr);230 }231}232233cc_dyn!(234 #[derive(Clone, Debug)]235 CcObjectCore, ObjectCore,236 pub fn new() {...}237);238#[derive(Trace, Educe)]239#[educe(Debug)]240struct ObjValueInner {241 cores: Vec<CcObjectCore>,242 assertions_ran: Cell<bool>,243 has_assertions: bool,244 value_cache: RefCell<FxHashMap<(IStr, CoreIdx), CacheValue>>,245}246247thread_local! {248 static RUNNING_ASSERTIONS: RefCell<FxHashSet<ObjValue>> = RefCell::default();249}250fn is_asserting(obj: &ObjValue) -> bool {251 RUNNING_ASSERTIONS.with_borrow(|v| v.contains(obj))252}253/// Returns false if already asserting254fn start_asserting(obj: &ObjValue) -> bool {255 RUNNING_ASSERTIONS.with_borrow_mut(|v| v.insert(obj.clone()))256}257fn finish_asserting(obj: &ObjValue) {258 RUNNING_ASSERTIONS.with_borrow_mut(|v| {259 let r = v.remove(obj);260 debug_assert!(261 r,262 "finish_asserting was called before start_asserting or twice"263 );264 });265}266267thread_local! {268 static EMPTY_OBJ: ObjValue = ObjValue(Cc::new(ObjValueInner {269 cores: vec![],270 assertions_ran: Cell::new(true),271 has_assertions: false,272 value_cache: RefCell::default(),273 }))274}275276#[allow(clippy::module_name_repetitions)]277#[derive(Clone, Trace, Debug, Educe)]278#[educe(PartialEq, Hash, Eq)]279pub struct ObjValue(280 #[educe(PartialEq(method(Cc::ptr_eq)), Hash(method(identity_hash)))] Cc<ObjValueInner>,281);282283impl ObjValue {284 pub fn empty() -> Self {285 EMPTY_OBJ.with(Clone::clone)286 }287 pub fn is_empty(&self) -> bool {288 self.0.cores.is_empty() || self.len() == 0289 }290}291292#[derive(Trace, Debug)]293pub(crate) struct StandaloneSuperCore {294 sup: CoreIdx,295 this: ObjValue,296}297impl ObjectCore for StandaloneSuperCore {298 fn enum_fields_core(299 &self,300 super_depth: &mut SuperDepth,301 handler: &mut EnumFieldsHandler<'_>,302 ) -> bool {303 self.this.enum_fields_idx(super_depth, handler, self.sup)304 }305306 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {307 if self.this.has_field_include_hidden_idx(name, self.sup) {308 HasFieldIncludeHidden::Exists309 } else {310 HasFieldIncludeHidden::NotFound311 }312 }313314 fn get_for_core(&self, key: IStr, _sup_this: SupThis, omit_only: bool) -> Result<GetFor> {315 if omit_only {316 return Ok(GetFor::NotFound);317 }318 let v = self.this.get_idx(key, self.sup)?;319 Ok(v.map_or(GetFor::NotFound, GetFor::Final))320 }321322 fn field_visibility_core(&self, field: IStr) -> FieldVisibility {323 self.this324 .field_visibility_idx(field, self.sup)325 .map_or(FieldVisibility::NotFound, FieldVisibility::Found)326 }327328 fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {329 self.this.run_assertions()330 }331}332333#[derive(Debug, Acyclic)]334struct OmitFieldsCore {335 omit: FxHashSet<IStr>,336 prev_layers: usize,337}338impl ObjectCore for OmitFieldsCore {339 fn enum_fields_core(340 &self,341 super_depth: &mut SuperDepth,342 handler: &mut EnumFieldsHandler<'_>,343 ) -> bool {344 let mut fi = FieldIndex::default();345 for f in &self.omit {346 if handler(347 *super_depth,348 fi,349 f.clone(),350 EnumFields::Omit(Saturating(self.prev_layers)),351 ) == ControlFlow::Break(())352 {353 return false;354 }355 fi = fi.next();356 }357 true358 }359360 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {361 if self.omit.contains(&name) {362 return HasFieldIncludeHidden::Omit(Saturating(self.prev_layers));363 }364 HasFieldIncludeHidden::NotFound365 }366367 fn get_for_core(&self, key: IStr, _sup_this: SupThis, _omit_only: bool) -> Result<GetFor> {368 if self.omit.contains(&key) {369 return Ok(GetFor::Omit(Saturating(self.prev_layers)));370 }371 Ok(GetFor::NotFound)372 }373374 fn field_visibility_core(&self, field: IStr) -> FieldVisibility {375 if self.omit.contains(&field) {376 return FieldVisibility::Omit(Saturating(self.prev_layers));377 }378 FieldVisibility::NotFound379 }380381 fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {382 Ok(())383 }384}385386#[derive(Hash, PartialEq, Eq, Trace, Clone, Copy, Debug)]387struct CoreIdx {388 idx: usize,389}390impl CoreIdx {391 fn super_exists(self) -> bool {392 self.idx != 0393 }394}395#[derive(Trace, Clone, PartialEq, Eq, Hash, Debug)]396pub struct SupThis {397 sup: CoreIdx,398 this: ObjValue,399}400impl SupThis {401 pub fn has_super(&self) -> bool {402 self.sup.super_exists()403 }404 /// Implementation of `"field" in super` operation,405 /// works faster than standalone super path.406 ///407 /// In case of no `super` existence, returns false.408 pub fn field_in_super(&self, field: IStr) -> bool {409 self.this.has_field_include_hidden_idx(field, self.sup)410 }411 /// Implementation of `super.field` operation,412 /// works faster than standalone super path.413 ///414 /// In case of no `super` existence, returns `NoSuperFound`415 pub fn get_super(&self, field: IStr) -> Result<Option<Val>> {416 if !self.sup.super_exists() {417 bail!(NoSuperFound);418 }419 self.this.get_idx(field, self.sup)420 }421 /// `super` with `self` overriden for top-level lookups.422 /// Exists when super appears outside of `super.field`/`"field" in super` expressions423 /// Exclusive to jrsonnet.424 ///425 /// Might return `NoSuperFound` error.426 pub fn standalone_super(&self) -> Result<ObjValue> {427 if !self.sup.super_exists() {428 bail!(NoSuperFound)429 }430 let mut out = ObjValue::builder();431 out.reserve_cores(1).extend_with_core(StandaloneSuperCore {432 sup: self.sup,433 this: self.this.clone(),434 });435 Ok(out.build())436 }437 pub fn this(&self) -> &ObjValue {438 &self.this439 }440 pub fn downgrade(self) -> WeakSupThis {441 WeakSupThis {442 sup: self.sup,443 this: self.this.downgrade(),444 }445 }446}447#[derive(Trace, PartialEq, Eq, Hash, Debug)]448pub struct WeakSupThis {449 sup: CoreIdx,450 this: WeakObjValue,451}452453impl ObjValue {454 pub fn builder() -> ObjValueBuilder {455 ObjValueBuilder::new()456 }457 pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {458 ObjValueBuilder::with_capacity(capacity)459 }460 pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {461 let mut out = ObjValueBuilder::with_capacity(1);462 out.with_super(self);463 let mut member = out.field(key);464 if value.flags.add() {465 member = member.add();466 }467 if let Some(loc) = value.location {468 member = member.with_location(loc);469 }470 let _ = member471 .with_visibility(value.flags.visibility())472 .binding(value.invoke);473 out.build()474 }475 pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {476 ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())477 }478479 pub fn extend(&mut self) -> ObjValueBuilder {480 let mut out = ObjValueBuilder::new();481 out.with_super(self.clone());482 out483 }484485 #[must_use]486 pub fn extend_from(&self, sup: Self) -> Self {487 let mut cores = Vec::with_capacity(sup.0.cores.len() + self.0.cores.len());488 cores.extend(sup.0.cores.iter().cloned());489 cores.extend(self.0.cores.iter().cloned());490 let has_assertions = sup.0.has_assertions || self.0.has_assertions;491 ObjValue(Cc::new(ObjValueInner {492 cores,493 value_cache: RefCell::default(),494 assertions_ran: Cell::new(!has_assertions),495 has_assertions,496 }))497 }498 // #[must_use]499 // pub fn with_this(&self, this: Self) -> Self {500 // self.0.with_this(self.clone(), this)501 // }502 /// Returns amount of visible object fields503 /// If object only contains hidden fields - may return zero.504 pub fn len(&self) -> usize {505 self.fields_visibility()506 .values()507 .filter(|d| d.visible())508 .count()509 }510 /// For each field, calls callback.511 /// If callback returns false - ends iteration prematurely.512 ///513 /// Returns false if ended prematurely514 pub fn enum_fields(&self, handler: &mut EnumFieldsHandler<'_>) -> bool {515 let mut super_depth = SuperDepth::default();516 self.enum_fields_idx(517 &mut super_depth,518 handler,519 CoreIdx {520 idx: self.0.cores.len(),521 },522 )523 }524 fn enum_fields_idx(525 &self,526 super_depth: &mut SuperDepth,527 handler: &mut EnumFieldsHandler<'_>,528 idx: CoreIdx,529 ) -> bool {530 for core in self.0.cores[..idx.idx].iter().rev() {531 if !core.0.enum_fields_core(super_depth, handler) {532 return false;533 }534 super_depth.deepen();535 }536 true537 }538539 pub fn has_field_include_hidden(&self, name: IStr) -> bool {540 self.has_field_include_hidden_idx(541 name,542 CoreIdx {543 idx: self.0.cores.len(),544 },545 )546 }547 fn has_field_include_hidden_idx(&self, name: IStr, core: CoreIdx) -> bool {548 let mut skip = Saturating(0usize);549 for ele in self.0.cores[..core.idx].iter().rev() {550 match ele.0.has_field_include_hidden_core(name.clone()) {551 HasFieldIncludeHidden::Exists => {552 if skip.0 == 0 {553 return true;554 }555 }556 HasFieldIncludeHidden::Omit(new_skip) => {557 // +1 including this core558 skip = skip.max(new_skip + Saturating(1));559 }560 HasFieldIncludeHidden::NotFound => {}561 }562 skip -= 1;563 }564 false565 }566 pub fn has_field(&self, name: IStr) -> bool {567 match self.field_visibility(name) {568 Some(Visibility::Unhide | Visibility::Normal) => true,569 Some(Visibility::Hidden) | None => false,570 }571 }572 pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {573 if include_hidden {574 self.has_field_include_hidden(name)575 } else {576 self.has_field(name)577 }578 }579 pub fn get(&self, key: IStr) -> Result<Option<Val>> {580 self.get_idx(581 key,582 CoreIdx {583 idx: self.0.cores.len(),584 },585 )586 }587588 fn get_idx(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {589 let cache_key = (key.clone(), core);590 {591 let mut cache = self.0.value_cache.borrow_mut();592 // entry_ref candidate?593 match cache.entry(cache_key.clone()) {594 Entry::Occupied(v) => match v.get() {595 CacheValue::Cached(v) => return v.clone(),596 CacheValue::Pending => {597 if !is_asserting(self) {598 bail!(InfiniteRecursionDetected);599 }600 }601 },602 Entry::Vacant(v) => {603 v.insert(CacheValue::Pending);604 }605 };606 }607 let result = self.get_idx_uncached(key, core);608 {609 let mut cache = self.0.value_cache.borrow_mut();610 cache.insert(cache_key, CacheValue::Cached(result.clone()));611 }612 result613 }614 fn get_idx_uncached(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {615 self.run_assertions()?;616 let mut first_add = None;617 let mut add_stack: Vec<Val> = Vec::new();618 let mut skip = Saturating(0);619 for (sup, core) in self.0.cores[..core.idx].iter().enumerate().rev() {620 let sup_this = SupThis {621 sup: CoreIdx { idx: sup },622 this: self.clone(),623 };624 match core.0.get_for_core(key.clone(), sup_this, skip.0 != 0)? {625 GetFor::Final(val) if first_add.is_none() => {626 if skip.0 == 0 {627 return Ok(Some(val));628 }629 }630 GetFor::Final(val) => {631 if skip.0 == 0 {632 add_stack.push(val);633 break;634 }635 }636 GetFor::SuperPlus(val) => {637 if skip.0 == 0 {638 if first_add.is_none() {639 first_add = Some(val);640 } else {641 add_stack.push(val);642 }643 }644 }645 GetFor::Omit(new_skip) => {646 skip = skip.max(new_skip + Saturating(1));647 }648 GetFor::NotFound => {}649 }650 skip -= 1;651 }652 let Some(first) = first_add else {653 if add_stack.is_empty() {654 return Ok(None);655 }656 return Ok(Some(add_stack.pop().expect("single element on stack")));657 };658 if add_stack.is_empty() {659 return Ok(Some(first));660 }661 add_stack.insert(0, first);662 let mut values = add_stack.into_iter().rev();663 let init = values.next().expect("at least 2 elements");664665 values666 .try_fold(init, |a, b| evaluate_add_op(&a, &b))667 .map(Some)668 }669670 pub fn get_or_bail(&self, key: IStr) -> Result<Val> {671 let Some(value) = self.get(key.clone())? else {672 let suggestions = suggest_object_fields(self, key.clone());673 bail!(NoSuchField(key, suggestions))674 };675 Ok(value)676 }677678 fn field_visibility(&self, field: IStr) -> Option<Visibility> {679 self.field_visibility_idx(680 field,681 CoreIdx {682 idx: self.0.cores.len(),683 },684 )685 }686 fn field_visibility_idx(&self, field: IStr, core: CoreIdx) -> Option<Visibility> {687 let mut exists = false;688 let mut skip = Saturating(0usize);689 for ele in self.0.cores[..core.idx].iter().rev() {690 let vis = ele.0.field_visibility_core(field.clone());691 match vis {692 FieldVisibility::Found(vis @ (Visibility::Unhide | Visibility::Hidden)) => {693 if skip.0 == 0 {694 return Some(vis);695 }696 }697 FieldVisibility::Found(Visibility::Normal) => {698 if skip.0 == 0 {699 exists = true;700 }701 }702 FieldVisibility::NotFound => {}703 FieldVisibility::Omit(new_skip) => {704 // +1 including this core705 skip = skip.max(new_skip + Saturating(1));706 }707 }708 skip -= 1;709 }710 exists.then_some(Visibility::Normal)711 }712713 pub fn run_assertions(&self) -> Result<()> {714 if self.0.assertions_ran.get() {715 return Ok(());716 }717 if !start_asserting(self) {718 return Ok(());719 }720 for (idx, ele) in self.0.cores.iter().enumerate() {721 let sup_this = SupThis {722 sup: CoreIdx { idx },723 this: self.clone(),724 };725 ele.0.run_assertions_core(sup_this).inspect_err(|_e| {726 finish_asserting(self);727 })?;728 }729 finish_asserting(self);730 self.0.assertions_ran.set(true);731 Ok(())732 }733734 pub fn iter(735 &self,736 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,737 ) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {738 let fields = self.fields(739 #[cfg(feature = "exp-preserve-order")]740 preserve_order,741 );742 fields.into_iter().map(|field| {743 (744 field.clone(),745 self.get(field)746 .map(|opt| opt.expect("iterating over keys, field exists")),747 )748 })749 }750 pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {751 #[derive(Trace)]752 struct ObjFieldThunk {753 obj: ObjValue,754 key: IStr,755 }756 impl ThunkValue for ObjFieldThunk {757 type Output = Val;758759 fn get(&self) -> Result<Self::Output> {760 self.obj761 .get(self.key.clone())762 .transpose()763 .expect("field existence checked")764 }765 }766767 if !self.has_field_ex(key.clone(), true) {768 return None;769 }770771 Some(Thunk::new(ObjFieldThunk {772 obj: self.clone(),773 key,774 }))775 }776 pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {777 #[derive(Trace)]778 struct ObjFieldThunk {779 obj: ObjValue,780 key: IStr,781 }782 impl ThunkValue for ObjFieldThunk {783 type Output = Val;784785 fn get(&self) -> Result<Self::Output> {786 self.obj.get_or_bail(self.key.clone())787 }788 }789790 Thunk::new(ObjFieldThunk {791 obj: self.clone(),792 key,793 })794 }795796 #[allow(dead_code, reason = "used in object ...rest destructuring")]797 pub(crate) fn as_standalone(&self) -> StandaloneSuperCore {798 StandaloneSuperCore {799 sup: CoreIdx {800 idx: self.0.cores.len(),801 },802 this: self.clone(),803 }804 }805 pub fn ptr_eq(a: &Self, b: &Self) -> bool {806 Cc::ptr_eq(&a.0, &b.0)807 }808 pub fn downgrade(self) -> WeakObjValue {809 WeakObjValue(self.0.downgrade())810 }811}812813#[derive(Debug)]814struct FieldVisibilityData {815 omitted_until: Saturating<usize>,816 exists_visible: Option<Visibility>,817 #[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]818 key: FieldSortKey,819}820impl FieldVisibilityData {821 fn visible(&self) -> bool {822 self.exists_visible823 .expect("non-existing fields shall be dropped at the end of fn fields_visibility()")824 .is_visible()825 }826 #[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]827 fn sort_key(&self) -> FieldSortKey {828 self.key829 }830}831832impl ObjValue {833 fn fields_visibility(&self) -> FxHashMap<IStr, FieldVisibilityData> {834 let mut out = FxHashMap::default();835836 let mut super_depth = SuperDepth::default();837 let mut omit_index = Saturating(0);838 for core in self.0.cores.iter().rev() {839 core.0840 .enum_fields_core(&mut super_depth, &mut |depth, index, name, visibility| {841 let entry = out.entry(name);842 let data = entry.or_insert_with(|| FieldVisibilityData {843 exists_visible: None,844 key: FieldSortKey::new(depth, index),845 omitted_until: omit_index,846 });847 match visibility {848 EnumFields::Omit(new_skip) => {849 // +1 including this core850 data.omitted_until = data851 .omitted_until852 .max(omit_index + new_skip + Saturating(1));853 }854 EnumFields::Normal(Visibility::Normal) => {855 if data.omitted_until <= omit_index && data.exists_visible.is_none() {856 data.exists_visible = Some(Visibility::Normal);857 }858 }859 EnumFields::Normal(Visibility::Hidden) => {860 if data.omitted_until <= omit_index {861 data.exists_visible = Some(match data.exists_visible {862 // We're iterating in reverse, later unhide is preserved863 Some(Visibility::Unhide) => Visibility::Unhide,864 _ => Visibility::Hidden,865 });866 }867 }868 EnumFields::Normal(Visibility::Unhide) => {869 if data.omitted_until <= omit_index {870 data.exists_visible = Some(match data.exists_visible {871 // We're iterating in reverse, later hide is preserved872 Some(Visibility::Hidden) => Visibility::Hidden,873 _ => Visibility::Unhide,874 });875 }876 }877 }878 ControlFlow::Continue(())879 });880881 super_depth.deepen();882 omit_index += 1;883 }884885 out.retain(|_, v| v.exists_visible.is_some());886887 out888 }889 pub fn fields_ex(890 &self,891 include_hidden: bool,892 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,893 ) -> Vec<IStr> {894 #[cfg(feature = "exp-preserve-order")]895 if preserve_order {896 let (mut fields, mut keys): (Vec<_>, Vec<_>) = self897 .fields_visibility()898 .into_iter()899 .filter(|(_, d)| include_hidden || d.visible())900 .enumerate()901 .map(|(idx, (k, d))| (k, (d.sort_key(), idx)))902 .unzip();903 keys.sort_unstable_by_key(|v| v.0);904 // Reorder in-place by resulting indexes905 for i in 0..fields.len() {906 let x = fields[i].clone();907 let mut j = i;908 loop {909 let k = keys[j].1;910 keys[j].1 = j;911 if k == i {912 break;913 }914 fields[j] = fields[k].clone();915 j = k;916 }917 fields[j] = x;918 }919 return fields;920 }921922 let mut fields: Vec<_> = self923 .fields_visibility()924 .into_iter()925 .filter(|(_, d)| include_hidden || d.visible())926 .map(|(k, _)| k)927 .collect();928 fields.sort_unstable();929 fields930 }931 pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {932 self.fields_ex(933 false,934 #[cfg(feature = "exp-preserve-order")]935 preserve_order,936 )937 }938 pub fn values_ex(939 &self,940 include_hidden: bool,941 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,942 ) -> ArrValue {943 ArrValue::new(PickObjectValues::new(944 self.clone(),945 self.fields_ex(946 include_hidden,947 #[cfg(feature = "exp-preserve-order")]948 preserve_order,949 ),950 ))951 }952 pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {953 self.values_ex(954 false,955 #[cfg(feature = "exp-preserve-order")]956 preserve_order,957 )958 }959 pub fn key_values_ex(960 &self,961 include_hidden: bool,962 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,963 ) -> ArrValue {964 ArrValue::new(PickObjectKeyValues::new(965 self.clone(),966 self.fields_ex(967 include_hidden,968 #[cfg(feature = "exp-preserve-order")]969 preserve_order,970 ),971 ))972 }973 pub fn key_values(974 &self,975 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,976 ) -> ArrValue {977 self.key_values_ex(978 false,979 #[cfg(feature = "exp-preserve-order")]980 preserve_order,981 )982 }983}984985#[allow(clippy::module_name_repetitions)]986#[must_use = "value not added unless binding() was called"]987pub struct ObjMemberBuilder<Kind> {988 kind: Kind,989 name: IStr,990 add: bool,991 visibility: Visibility,992 original_index: FieldIndex,993 location: Option<Span>,994}995996#[allow(clippy::missing_const_for_fn)]997impl<Kind> ObjMemberBuilder<Kind> {998 pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {999 Self {1000 kind,1001 name,1002 original_index,1003 add: false,1004 visibility: Visibility::Normal,1005 location: None,1006 }1007 }10081009 pub const fn with_add(mut self, add: bool) -> Self {1010 self.add = add;1011 self1012 }1013 pub fn add(self) -> Self {1014 self.with_add(true)1015 }1016 pub fn with_visibility(mut self, visibility: Visibility) -> Self {1017 self.visibility = visibility;1018 self1019 }1020 pub fn hide(self) -> Self {1021 self.with_visibility(Visibility::Hidden)1022 }1023 pub fn with_location(mut self, location: Span) -> Self {1024 self.location = Some(location);1025 self1026 }1027 fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {1028 (1029 self.kind,1030 self.name,1031 ObjMember {1032 flags: ObjFieldFlags::new(self.add, self.visibility),1033 original_index: self.original_index,1034 invoke: binding,1035 location: self.location,1036 },1037 )1038 }1039}10401041pub struct ExtendBuilder<'v>(&'v mut ObjValue);1042impl ObjMemberBuilder<ExtendBuilder<'_>> {1043 pub fn value(self, value: impl Into<Val>) {1044 self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));1045 }1046 pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {1047 self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));1048 }1049 pub fn binding(self, binding: MaybeUnbound) {1050 let (receiver, name, member) = self.build_member(binding);1051 let new = receiver.0.clone();1052 *receiver.0 = new.extend_with_raw_member(name, member);1053 }1054}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 im_rc::{Vector, vector};15use jrsonnet_gcmodule::{Acyclic, Cc, Trace, Weak, cc_dyn};16use jrsonnet_interner::IStr;17use jrsonnet_ir::Span;18use rustc_hash::{FxHashMap, FxHashSet};1920mod oop;2122pub use jrsonnet_ir::Visibility;23pub use oop::ObjValueBuilder;2425use crate::{26 CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,27 arr::{PickObjectKeyValues, PickObjectValues},28 bail,29 error::{ErrorKind::*, suggest_object_fields},30 identity_hash,31 operator::evaluate_add_op,32 val::{ArrValue, ThunkValue},33};3435#[cfg(not(feature = "exp-preserve-order"))]36pub mod ordering {37 #![allow(38 // This module works as stub for preserve-order feature39 clippy::unused_self,40 )]4142 use jrsonnet_gcmodule::Trace;4344 #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]45 pub struct FieldIndex(());46 impl FieldIndex {47 pub fn absolute(_v: u32) -> Self {48 Self(())49 }50 #[must_use]51 pub const fn next(self) -> Self {52 Self(())53 }54 }5556 #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]57 pub struct SuperDepth(());58 impl SuperDepth {59 pub(super) fn deepen(self) {}60 }61}6263#[cfg(feature = "exp-preserve-order")]64pub mod ordering {65 use jrsonnet_gcmodule::Trace;6667 #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]68 pub struct FieldIndex(u32);69 impl FieldIndex {70 pub fn absolute(v: u32) -> Self {71 Self(v)72 }73 #[must_use]74 pub fn next(self) -> Self {75 Self(self.0 + 1)76 }77 }7879 #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]80 pub struct SuperDepth(u32);81 impl SuperDepth {82 pub(super) fn deepen(&mut self) {83 self.0 += 1;84 }85 }86}8788use ordering::{FieldIndex, SuperDepth};8990#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]91pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);92impl FieldSortKey {93 pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {94 Self(Reverse(depth), index)95 }96}9798// 0 - add99// 12 - visibility100#[derive(Clone, Copy)]101pub struct ObjFieldFlags(u8);102impl ObjFieldFlags {103 fn new(add: bool, visibility: Visibility) -> Self {104 let mut v = 0;105 if add {106 v |= 1;107 }108 v |= match visibility {109 Visibility::Normal => 0b000,110 Visibility::Hidden => 0b010,111 Visibility::Unhide => 0b100,112 };113 Self(v)114 }115 pub fn add(&self) -> bool {116 self.0 & 1 != 0117 }118 pub fn visibility(&self) -> Visibility {119 match (self.0 & 0b110) >> 1 {120 0b00 => Visibility::Normal,121 0b01 => Visibility::Hidden,122 0b10 => Visibility::Unhide,123 _ => unreachable!(),124 }125 }126}127impl Debug for ObjFieldFlags {128 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {129 f.debug_struct("ObjFieldFlags")130 .field("add", &self.add())131 .field("visibility", &self.visibility())132 .finish()133 }134}135136#[allow(clippy::module_name_repetitions)]137#[derive(Debug, Trace)]138pub struct ObjMember {139 #[trace(skip)]140 flags: ObjFieldFlags,141 original_index: FieldIndex,142 pub invoke: MaybeUnbound,143 pub location: Option<Span>,144}145146cc_dyn!(CcObjectAssertion, ObjectAssertion);147pub trait ObjectAssertion: Trace {148 fn run(&self, sup_this: SupThis) -> Result<()>;149}150151// Field => This152153#[derive(Trace, Debug)]154enum CacheValue {155 Cached(Result<Option<Val>>),156 Pending,157}158159pub type EnumFieldsHandler<'a> =160 dyn FnMut(SuperDepth, FieldIndex, IStr, EnumFields) -> ControlFlow<()> + 'a;161162#[derive(Debug)]163pub enum EnumFields {164 Normal(Visibility),165 Omit(Skip),166}167168#[derive(Trace, Clone)]169pub enum GetFor {170 // Return value171 Final(Val),172 // Continue iterating over cores, add current value to sum stack173 SuperPlus(Val),174 // Ignore the field value, stop at this layer instead175 Omit(#[trace(skip)] Skip),176 NotFound,177}178179#[derive(Acyclic, Clone)]180pub enum FieldVisibility {181 Found(Visibility),182 Omit(Skip),183 NotFound,184}185186#[derive(Acyclic, Clone)]187pub enum HasFieldIncludeHidden {188 Exists,189 NotFound,190 Omit(Skip),191}192193type Skip = Saturating<usize>;194195pub trait ObjectCore: Trace + Any + Debug {196 // If callback returns false, iteration stops, and this call returns false.197 fn enum_fields_core(198 &self,199 super_depth: &mut SuperDepth,200 handler: &mut EnumFieldsHandler<'_>,201 ) -> bool;202203 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden;204205 fn get_for_core(&self, key: IStr, sup_this: SupThis, omit_only: bool) -> Result<GetFor>;206 fn field_visibility_core(&self, field: IStr) -> FieldVisibility;207208 fn run_assertions_core(&self, sup_this: SupThis) -> Result<()>;209}210211#[derive(Clone, Trace)]212pub struct WeakObjValue(#[trace(skip)] Weak<ObjValueInner>);213impl Debug for WeakObjValue {214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {215 f.debug_tuple("WeakObjValue").finish()216 }217}218219impl PartialEq for WeakObjValue {220 fn eq(&self, other: &Self) -> bool {221 Weak::ptr_eq(&self.0, &other.0)222 }223}224225impl Eq for WeakObjValue {}226impl Hash for WeakObjValue {227 fn hash<H: Hasher>(&self, hasher: &mut H) {228 // Safety: usize is POD229 let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };230 hasher.write_usize(addr);231 }232}233234cc_dyn!(235 #[derive(Clone, Debug)]236 CcObjectCore, ObjectCore,237 pub fn new() {...}238);239240#[derive(Trace, Educe)]241#[educe(Debug)]242struct ObjValueInner {243 cores: Vector<CcObjectCore>,244 assertions_ran: Cell<bool>,245 has_assertions: bool,246 value_cache: RefCell<FxHashMap<(IStr, CoreIdx), CacheValue>>,247}248249thread_local! {250 static RUNNING_ASSERTIONS: RefCell<FxHashSet<ObjValue>> = RefCell::default();251}252fn is_asserting(obj: &ObjValue) -> bool {253 RUNNING_ASSERTIONS.with_borrow(|v| v.contains(obj))254}255/// Returns false if already asserting256fn start_asserting(obj: &ObjValue) -> bool {257 RUNNING_ASSERTIONS.with_borrow_mut(|v| v.insert(obj.clone()))258}259fn finish_asserting(obj: &ObjValue) {260 RUNNING_ASSERTIONS.with_borrow_mut(|v| {261 let r = v.remove(obj);262 debug_assert!(263 r,264 "finish_asserting was called before start_asserting or twice"265 );266 });267}268269thread_local! {270 static EMPTY_OBJ: ObjValue = ObjValue(Cc::new(ObjValueInner {271 cores: vector![],272 assertions_ran: Cell::new(true),273 has_assertions: false,274 value_cache: RefCell::default(),275 }))276}277278#[allow(clippy::module_name_repetitions)]279#[derive(Clone, Trace, Debug, Educe)]280#[educe(PartialEq, Hash, Eq)]281pub struct ObjValue(282 #[educe(PartialEq(method(Cc::ptr_eq)), Hash(method(identity_hash)))] Cc<ObjValueInner>,283);284285impl ObjValue {286 pub fn empty() -> Self {287 EMPTY_OBJ.with(Clone::clone)288 }289 pub fn is_empty(&self) -> bool {290 self.0.cores.is_empty() || self.len() == 0291 }292}293294#[derive(Trace, Debug)]295pub(crate) struct StandaloneSuperCore {296 sup: CoreIdx,297 this: ObjValue,298}299impl ObjectCore for StandaloneSuperCore {300 fn enum_fields_core(301 &self,302 super_depth: &mut SuperDepth,303 handler: &mut EnumFieldsHandler<'_>,304 ) -> bool {305 self.this.enum_fields_idx(super_depth, handler, self.sup)306 }307308 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {309 if self.this.has_field_include_hidden_idx(name, self.sup) {310 HasFieldIncludeHidden::Exists311 } else {312 HasFieldIncludeHidden::NotFound313 }314 }315316 fn get_for_core(&self, key: IStr, _sup_this: SupThis, omit_only: bool) -> Result<GetFor> {317 if omit_only {318 return Ok(GetFor::NotFound);319 }320 let v = self.this.get_idx(key, self.sup)?;321 Ok(v.map_or(GetFor::NotFound, GetFor::Final))322 }323324 fn field_visibility_core(&self, field: IStr) -> FieldVisibility {325 self.this326 .field_visibility_idx(field, self.sup)327 .map_or(FieldVisibility::NotFound, FieldVisibility::Found)328 }329330 fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {331 self.this.run_assertions()332 }333}334335#[derive(Debug, Acyclic)]336struct OmitFieldsCore {337 omit: FxHashSet<IStr>,338 prev_layers: usize,339}340impl ObjectCore for OmitFieldsCore {341 fn enum_fields_core(342 &self,343 super_depth: &mut SuperDepth,344 handler: &mut EnumFieldsHandler<'_>,345 ) -> bool {346 let mut fi = FieldIndex::default();347 for f in &self.omit {348 if handler(349 *super_depth,350 fi,351 f.clone(),352 EnumFields::Omit(Saturating(self.prev_layers)),353 ) == ControlFlow::Break(())354 {355 return false;356 }357 fi = fi.next();358 }359 true360 }361362 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {363 if self.omit.contains(&name) {364 return HasFieldIncludeHidden::Omit(Saturating(self.prev_layers));365 }366 HasFieldIncludeHidden::NotFound367 }368369 fn get_for_core(&self, key: IStr, _sup_this: SupThis, _omit_only: bool) -> Result<GetFor> {370 if self.omit.contains(&key) {371 return Ok(GetFor::Omit(Saturating(self.prev_layers)));372 }373 Ok(GetFor::NotFound)374 }375376 fn field_visibility_core(&self, field: IStr) -> FieldVisibility {377 if self.omit.contains(&field) {378 return FieldVisibility::Omit(Saturating(self.prev_layers));379 }380 FieldVisibility::NotFound381 }382383 fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {384 Ok(())385 }386}387388#[derive(Hash, PartialEq, Eq, Trace, Clone, Copy, Debug)]389struct CoreIdx {390 idx: usize,391}392impl CoreIdx {393 fn super_exists(self) -> bool {394 self.idx != 0395 }396}397#[derive(Trace, Clone, PartialEq, Eq, Hash, Debug)]398pub struct SupThis {399 sup: CoreIdx,400 this: ObjValue,401}402impl SupThis {403 pub fn has_super(&self) -> bool {404 self.sup.super_exists()405 }406 /// Implementation of `"field" in super` operation,407 /// works faster than standalone super path.408 ///409 /// In case of no `super` existence, returns false.410 pub fn field_in_super(&self, field: IStr) -> bool {411 self.this.has_field_include_hidden_idx(field, self.sup)412 }413 /// Implementation of `super.field` operation,414 /// works faster than standalone super path.415 ///416 /// In case of no `super` existence, returns `NoSuperFound`417 pub fn get_super(&self, field: IStr) -> Result<Option<Val>> {418 if !self.sup.super_exists() {419 bail!(NoSuperFound);420 }421 self.this.get_idx(field, self.sup)422 }423 /// `super` with `self` overriden for top-level lookups.424 /// Exists when super appears outside of `super.field`/`"field" in super` expressions425 /// Exclusive to jrsonnet.426 ///427 /// Might return `NoSuperFound` error.428 pub fn standalone_super(&self) -> Result<ObjValue> {429 if !self.sup.super_exists() {430 bail!(NoSuperFound)431 }432 let mut out = ObjValue::builder();433 out.extend_with_core(StandaloneSuperCore {434 sup: self.sup,435 this: self.this.clone(),436 });437 Ok(out.build())438 }439 pub fn this(&self) -> &ObjValue {440 &self.this441 }442 pub fn downgrade(self) -> WeakSupThis {443 WeakSupThis {444 sup: self.sup,445 this: self.this.downgrade(),446 }447 }448}449#[derive(Trace, PartialEq, Eq, Hash, Debug)]450pub struct WeakSupThis {451 sup: CoreIdx,452 this: WeakObjValue,453}454455impl ObjValue {456 pub fn builder() -> ObjValueBuilder {457 ObjValueBuilder::new()458 }459 pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {460 ObjValueBuilder::with_capacity(capacity)461 }462 pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {463 let mut out = ObjValueBuilder::with_capacity(1);464 out.with_super(self);465 let mut member = out.field(key);466 if value.flags.add() {467 member = member.add();468 }469 if let Some(loc) = value.location {470 member = member.with_location(loc);471 }472 let _ = member473 .with_visibility(value.flags.visibility())474 .binding(value.invoke);475 out.build()476 }477 pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {478 ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())479 }480481 pub fn extend(&mut self) -> ObjValueBuilder {482 let mut out = ObjValueBuilder::new();483 out.with_super(self.clone());484 out485 }486487 #[must_use]488 pub fn extend_from(&self, sup: Self) -> Self {489 let cores = sup.0.cores.clone() + self.0.cores.clone();490 let has_assertions = sup.0.has_assertions || self.0.has_assertions;491 ObjValue(Cc::new(ObjValueInner {492 cores,493 value_cache: RefCell::default(),494 assertions_ran: Cell::new(!has_assertions),495 has_assertions,496 }))497 }498 // #[must_use]499 // pub fn with_this(&self, this: Self) -> Self {500 // self.0.with_this(self.clone(), this)501 // }502 /// Returns amount of visible object fields503 /// If object only contains hidden fields - may return zero.504 pub fn len(&self) -> usize {505 self.fields_visibility()506 .values()507 .filter(|d| d.visible())508 .count()509 }510 /// For each field, calls callback.511 /// If callback returns false - ends iteration prematurely.512 ///513 /// Returns false if ended prematurely514 pub fn enum_fields(&self, handler: &mut EnumFieldsHandler<'_>) -> bool {515 let mut super_depth = SuperDepth::default();516 self.enum_fields_idx(517 &mut super_depth,518 handler,519 CoreIdx {520 idx: self.0.cores.len(),521 },522 )523 }524525 fn iter_cores(&self, idx: CoreIdx) -> impl Iterator<Item = &CcObjectCore> {526 self.0.cores.iter().take(idx.idx).rev()527 }528 fn iter_cores_enumerate(&self, idx: CoreIdx) -> impl Iterator<Item = (CoreIdx, &CcObjectCore)> {529 self.0530 .cores531 .iter()532 .take(idx.idx)533 .enumerate()534 .rev()535 .map(|(idx, o)| (CoreIdx { idx }, o))536 }537538 fn enum_fields_idx(539 &self,540 super_depth: &mut SuperDepth,541 handler: &mut EnumFieldsHandler<'_>,542 idx: CoreIdx,543 ) -> bool {544 for core in self.iter_cores(idx) {545 if !core.0.enum_fields_core(super_depth, handler) {546 return false;547 }548 super_depth.deepen();549 }550 true551 }552553 pub fn has_field_include_hidden(&self, name: IStr) -> bool {554 self.has_field_include_hidden_idx(555 name,556 CoreIdx {557 idx: self.0.cores.len(),558 },559 )560 }561 fn has_field_include_hidden_idx(&self, name: IStr, core: CoreIdx) -> bool {562 let mut skip = Saturating(0usize);563 for ele in self.iter_cores(core) {564 match ele.0.has_field_include_hidden_core(name.clone()) {565 HasFieldIncludeHidden::Exists => {566 if skip.0 == 0 {567 return true;568 }569 }570 HasFieldIncludeHidden::Omit(new_skip) => {571 // +1 including this core572 skip = skip.max(new_skip + Saturating(1));573 }574 HasFieldIncludeHidden::NotFound => {}575 }576 skip -= 1;577 }578 false579 }580 pub fn has_field(&self, name: IStr) -> bool {581 match self.field_visibility(name) {582 Some(Visibility::Unhide | Visibility::Normal) => true,583 Some(Visibility::Hidden) | None => false,584 }585 }586 pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {587 if include_hidden {588 self.has_field_include_hidden(name)589 } else {590 self.has_field(name)591 }592 }593 pub fn get(&self, key: IStr) -> Result<Option<Val>> {594 self.get_idx(595 key,596 CoreIdx {597 idx: self.0.cores.len(),598 },599 )600 }601602 fn get_idx(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {603 let cache_key = (key.clone(), core);604 {605 let mut cache = self.0.value_cache.borrow_mut();606 // entry_ref candidate?607 match cache.entry(cache_key.clone()) {608 Entry::Occupied(v) => match v.get() {609 CacheValue::Cached(v) => return v.clone(),610 CacheValue::Pending => {611 if !is_asserting(self) {612 bail!(InfiniteRecursionDetected);613 }614 }615 },616 Entry::Vacant(v) => {617 v.insert(CacheValue::Pending);618 }619 };620 }621 let result = self.get_idx_uncached(key, core);622 {623 let mut cache = self.0.value_cache.borrow_mut();624 cache.insert(cache_key, CacheValue::Cached(result.clone()));625 }626 result627 }628 fn get_idx_uncached(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {629 self.run_assertions()?;630 let mut first_add = None;631 let mut add_stack: Vec<Val> = Vec::new();632 let mut skip = Saturating(0);633 for (sup, core) in self.iter_cores_enumerate(core) {634 let sup_this = SupThis {635 sup,636 this: self.clone(),637 };638 match core.0.get_for_core(key.clone(), sup_this, skip.0 != 0)? {639 GetFor::Final(val) if first_add.is_none() => {640 if skip.0 == 0 {641 return Ok(Some(val));642 }643 }644 GetFor::Final(val) => {645 if skip.0 == 0 {646 add_stack.push(val);647 break;648 }649 }650 GetFor::SuperPlus(val) => {651 if skip.0 == 0 {652 if first_add.is_none() {653 first_add = Some(val);654 } else {655 add_stack.push(val);656 }657 }658 }659 GetFor::Omit(new_skip) => {660 skip = skip.max(new_skip + Saturating(1));661 }662 GetFor::NotFound => {}663 }664 skip -= 1;665 }666 let Some(first) = first_add else {667 if add_stack.is_empty() {668 return Ok(None);669 }670 return Ok(Some(add_stack.pop().expect("single element on stack")));671 };672 if add_stack.is_empty() {673 return Ok(Some(first));674 }675 add_stack.insert(0, first);676 let mut values = add_stack.into_iter().rev();677 let init = values.next().expect("at least 2 elements");678679 values680 .try_fold(init, |a, b| evaluate_add_op(&a, &b))681 .map(Some)682 }683684 pub fn get_or_bail(&self, key: IStr) -> Result<Val> {685 let Some(value) = self.get(key.clone())? else {686 let suggestions = suggest_object_fields(self, key.clone());687 bail!(NoSuchField(key, suggestions))688 };689 Ok(value)690 }691692 fn field_visibility(&self, field: IStr) -> Option<Visibility> {693 self.field_visibility_idx(694 field,695 CoreIdx {696 idx: self.0.cores.len(),697 },698 )699 }700 fn field_visibility_idx(&self, field: IStr, core: CoreIdx) -> Option<Visibility> {701 let mut exists = false;702 let mut skip = Saturating(0usize);703 for ele in self.iter_cores(core) {704 let vis = ele.0.field_visibility_core(field.clone());705 match vis {706 FieldVisibility::Found(vis @ (Visibility::Unhide | Visibility::Hidden)) => {707 if skip.0 == 0 {708 return Some(vis);709 }710 }711 FieldVisibility::Found(Visibility::Normal) => {712 if skip.0 == 0 {713 exists = true;714 }715 }716 FieldVisibility::NotFound => {}717 FieldVisibility::Omit(new_skip) => {718 // +1 including this core719 skip = skip.max(new_skip + Saturating(1));720 }721 }722 skip -= 1;723 }724 exists.then_some(Visibility::Normal)725 }726727 pub fn run_assertions(&self) -> Result<()> {728 if self.0.assertions_ran.get() {729 return Ok(());730 }731 if !start_asserting(self) {732 return Ok(());733 }734 for (idx, ele) in self.0.cores.iter().enumerate() {735 let sup_this = SupThis {736 sup: CoreIdx { idx },737 this: self.clone(),738 };739 ele.0.run_assertions_core(sup_this).inspect_err(|_e| {740 finish_asserting(self);741 })?;742 }743 finish_asserting(self);744 self.0.assertions_ran.set(true);745 Ok(())746 }747748 pub fn iter(749 &self,750 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,751 ) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {752 let fields = self.fields(753 #[cfg(feature = "exp-preserve-order")]754 preserve_order,755 );756 fields.into_iter().map(|field| {757 (758 field.clone(),759 self.get(field)760 .map(|opt| opt.expect("iterating over keys, field exists")),761 )762 })763 }764 pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {765 #[derive(Trace)]766 struct ObjFieldThunk {767 obj: ObjValue,768 key: IStr,769 }770 impl ThunkValue for ObjFieldThunk {771 type Output = Val;772773 fn get(&self) -> Result<Self::Output> {774 self.obj775 .get(self.key.clone())776 .transpose()777 .expect("field existence checked")778 }779 }780781 if !self.has_field_ex(key.clone(), true) {782 return None;783 }784785 Some(Thunk::new(ObjFieldThunk {786 obj: self.clone(),787 key,788 }))789 }790 pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {791 #[derive(Trace)]792 struct ObjFieldThunk {793 obj: ObjValue,794 key: IStr,795 }796 impl ThunkValue for ObjFieldThunk {797 type Output = Val;798799 fn get(&self) -> Result<Self::Output> {800 self.obj.get_or_bail(self.key.clone())801 }802 }803804 Thunk::new(ObjFieldThunk {805 obj: self.clone(),806 key,807 })808 }809810 #[allow(dead_code, reason = "used in object ...rest destructuring")]811 pub(crate) fn as_standalone(&self) -> StandaloneSuperCore {812 StandaloneSuperCore {813 sup: CoreIdx {814 idx: self.0.cores.len(),815 },816 this: self.clone(),817 }818 }819 pub fn ptr_eq(a: &Self, b: &Self) -> bool {820 Cc::ptr_eq(&a.0, &b.0)821 }822 pub fn downgrade(self) -> WeakObjValue {823 WeakObjValue(self.0.downgrade())824 }825}826827#[derive(Debug)]828struct FieldVisibilityData {829 omitted_until: Saturating<usize>,830 exists_visible: Option<Visibility>,831 #[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]832 key: FieldSortKey,833}834impl FieldVisibilityData {835 fn visible(&self) -> bool {836 self.exists_visible837 .expect("non-existing fields shall be dropped at the end of fn fields_visibility()")838 .is_visible()839 }840 #[allow(dead_code, reason = "used for exp-object-ordering, ZST otherwise")]841 fn sort_key(&self) -> FieldSortKey {842 self.key843 }844}845846impl ObjValue {847 fn fields_visibility(&self) -> FxHashMap<IStr, FieldVisibilityData> {848 let mut out = FxHashMap::default();849850 let mut super_depth = SuperDepth::default();851 let mut omit_index = Saturating(0);852 for core in self.0.cores.iter().rev() {853 core.0854 .enum_fields_core(&mut super_depth, &mut |depth, index, name, visibility| {855 let entry = out.entry(name);856 let data = entry.or_insert_with(|| FieldVisibilityData {857 exists_visible: None,858 key: FieldSortKey::new(depth, index),859 omitted_until: omit_index,860 });861 match visibility {862 EnumFields::Omit(new_skip) => {863 // +1 including this core864 data.omitted_until = data865 .omitted_until866 .max(omit_index + new_skip + Saturating(1));867 }868 EnumFields::Normal(Visibility::Normal) => {869 if data.omitted_until <= omit_index && data.exists_visible.is_none() {870 data.exists_visible = Some(Visibility::Normal);871 }872 }873 EnumFields::Normal(Visibility::Hidden) => {874 if data.omitted_until <= omit_index {875 data.exists_visible = Some(match data.exists_visible {876 // We're iterating in reverse, later unhide is preserved877 Some(Visibility::Unhide) => Visibility::Unhide,878 _ => Visibility::Hidden,879 });880 }881 }882 EnumFields::Normal(Visibility::Unhide) => {883 if data.omitted_until <= omit_index {884 data.exists_visible = Some(match data.exists_visible {885 // We're iterating in reverse, later hide is preserved886 Some(Visibility::Hidden) => Visibility::Hidden,887 _ => Visibility::Unhide,888 });889 }890 }891 }892 ControlFlow::Continue(())893 });894895 super_depth.deepen();896 omit_index += 1;897 }898899 out.retain(|_, v| v.exists_visible.is_some());900901 out902 }903 pub fn fields_ex(904 &self,905 include_hidden: bool,906 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,907 ) -> Vec<IStr> {908 #[cfg(feature = "exp-preserve-order")]909 if preserve_order {910 let (mut fields, mut keys): (Vec<_>, Vec<_>) = self911 .fields_visibility()912 .into_iter()913 .filter(|(_, d)| include_hidden || d.visible())914 .enumerate()915 .map(|(idx, (k, d))| (k, (d.sort_key(), idx)))916 .unzip();917 keys.sort_unstable_by_key(|v| v.0);918 // Reorder in-place by resulting indexes919 for i in 0..fields.len() {920 let x = fields[i].clone();921 let mut j = i;922 loop {923 let k = keys[j].1;924 keys[j].1 = j;925 if k == i {926 break;927 }928 fields[j] = fields[k].clone();929 j = k;930 }931 fields[j] = x;932 }933 return fields;934 }935936 let mut fields: Vec<_> = self937 .fields_visibility()938 .into_iter()939 .filter(|(_, d)| include_hidden || d.visible())940 .map(|(k, _)| k)941 .collect();942 fields.sort_unstable();943 fields944 }945 pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {946 self.fields_ex(947 false,948 #[cfg(feature = "exp-preserve-order")]949 preserve_order,950 )951 }952 pub fn values_ex(953 &self,954 include_hidden: bool,955 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,956 ) -> ArrValue {957 ArrValue::new(PickObjectValues::new(958 self.clone(),959 self.fields_ex(960 include_hidden,961 #[cfg(feature = "exp-preserve-order")]962 preserve_order,963 ),964 ))965 }966 pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {967 self.values_ex(968 false,969 #[cfg(feature = "exp-preserve-order")]970 preserve_order,971 )972 }973 pub fn key_values_ex(974 &self,975 include_hidden: bool,976 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,977 ) -> ArrValue {978 ArrValue::new(PickObjectKeyValues::new(979 self.clone(),980 self.fields_ex(981 include_hidden,982 #[cfg(feature = "exp-preserve-order")]983 preserve_order,984 ),985 ))986 }987 pub fn key_values(988 &self,989 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,990 ) -> ArrValue {991 self.key_values_ex(992 false,993 #[cfg(feature = "exp-preserve-order")]994 preserve_order,995 )996 }997}998999#[allow(clippy::module_name_repetitions)]1000#[must_use = "value not added unless binding() was called"]1001pub struct ObjMemberBuilder<Kind> {1002 kind: Kind,1003 name: IStr,1004 add: bool,1005 visibility: Visibility,1006 original_index: FieldIndex,1007 location: Option<Span>,1008}10091010#[allow(clippy::missing_const_for_fn)]1011impl<Kind> ObjMemberBuilder<Kind> {1012 pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {1013 Self {1014 kind,1015 name,1016 original_index,1017 add: false,1018 visibility: Visibility::Normal,1019 location: None,1020 }1021 }10221023 pub const fn with_add(mut self, add: bool) -> Self {1024 self.add = add;1025 self1026 }1027 pub fn add(self) -> Self {1028 self.with_add(true)1029 }1030 pub fn with_visibility(mut self, visibility: Visibility) -> Self {1031 self.visibility = visibility;1032 self1033 }1034 pub fn hide(self) -> Self {1035 self.with_visibility(Visibility::Hidden)1036 }1037 pub fn with_location(mut self, location: Span) -> Self {1038 self.location = Some(location);1039 self1040 }1041 fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {1042 (1043 self.kind,1044 self.name,1045 ObjMember {1046 flags: ObjFieldFlags::new(self.add, self.visibility),1047 original_index: self.original_index,1048 invoke: binding,1049 location: self.location,1050 },1051 )1052 }1053}10541055pub struct ExtendBuilder<'v>(&'v mut ObjValue);1056impl ObjMemberBuilder<ExtendBuilder<'_>> {1057 pub fn value(self, value: impl Into<Val>) {1058 self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));1059 }1060 pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {1061 self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));1062 }1063 pub fn binding(self, binding: MaybeUnbound) {1064 let (receiver, name, member) = self.build_member(binding);1065 let new = receiver.0.clone();1066 *receiver.0 = new.extend_with_raw_member(name, member);1067 }1068}crates/jrsonnet-evaluator/src/obj/oop.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj/oop.rs
+++ b/crates/jrsonnet-evaluator/src/obj/oop.rs
@@ -4,6 +4,7 @@
ops::ControlFlow,
};
+use im_rc::Vector;
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_ir::IStr;
use rustc_hash::{FxHashMap, FxHashSet};
@@ -117,7 +118,7 @@
#[allow(clippy::module_name_repetitions)]
pub struct ObjValueBuilder {
- sup: Vec<CcObjectCore>,
+ sup: Vector<CcObjectCore>,
has_assertions: bool,
new: OopObject,
@@ -129,7 +130,7 @@
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
- sup: vec![],
+ sup: Vector::new(),
has_assertions: false,
new: OopObject::new(FxHashMap::with_capacity(capacity), None),
next_field_index: FieldIndex::default(),
@@ -137,14 +138,10 @@
}
pub fn reserve_fields(&mut self, capacity: usize) {
self.new.this_entries.reserve(capacity);
- }
- pub fn reserve_cores(&mut self, capacity: usize) -> &mut Self {
- self.sup.reserve_exact(capacity);
- self
}
pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {
self.has_assertions |= super_obj.0.has_assertions;
- self.sup.clone_from(&super_obj.0.cores);
+ self.sup = super_obj.0.cores.clone();
self
}
@@ -181,19 +178,20 @@
pub fn extend_with_core(&mut self, core: impl ObjectCore) {
self.commit();
- self.sup.push(CcObjectCore::new(core));
+ self.sup.push_back(CcObjectCore::new(core));
}
fn commit(&mut self) {
if !self.new.is_empty() {
- self.sup.push(CcObjectCore::new(mem::take(&mut self.new)));
+ self.sup
+ .push_back(CcObjectCore::new(mem::take(&mut self.new)));
}
self.next_field_index = FieldIndex::default();
}
pub fn with_fields_omitted(&mut self, omit: FxHashSet<IStr>) {
self.commit();
- self.sup.push(CcObjectCore::new(OmitFieldsCore {
+ self.sup.push_back(CcObjectCore::new(OmitFieldsCore {
omit,
prev_layers: self.sup.len(),
}));