difftreelog
refactor reuse code between ObjValue::get and get_for
in: master
1 file changed
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth1use std::{2 cell::RefCell,3 fmt::Debug,4 hash::{Hash, Hasher},5 ptr::addr_of,6};78use jrsonnet_gcmodule::{Cc, Trace, Weak};9use jrsonnet_interner::IStr;10use jrsonnet_parser::{ExprLocation, Visibility};11use rustc_hash::FxHashMap;1213use crate::{14 error::{Error, ErrorKind::*},15 function::CallLocation,16 gc::{GcHashMap, GcHashSet, TraceBox},17 operator::evaluate_add_op,18 tb, throw,19 val::ThunkValue,20 MaybeUnbound, Result, State, Thunk, Unbound, Val,21};2223#[cfg(not(feature = "exp-preserve-order"))]24mod ordering {25 #![allow(26 // This module works as stub for preserve-order feature27 clippy::unused_self,28 )]2930 use jrsonnet_gcmodule::Trace;3132 #[derive(Clone, Copy, Default, Debug, Trace)]33 pub struct FieldIndex(());34 impl FieldIndex {35 pub const fn next(self) -> Self {36 Self(())37 }38 }3940 #[derive(Clone, Copy, Default, Debug, Trace)]41 pub struct SuperDepth(());42 impl SuperDepth {43 pub const fn deeper(self) -> Self {44 Self(())45 }46 }4748 #[derive(Clone, Copy)]49 pub struct FieldSortKey(());50 impl FieldSortKey {51 pub const fn new(_: SuperDepth, _: FieldIndex) -> Self {52 Self(())53 }54 }55}5657#[cfg(feature = "exp-preserve-order")]58mod ordering {59 use std::cmp::{Ordering, Reverse};6061 use jrsonnet_gcmodule::Trace;6263 #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]64 pub struct FieldIndex(u32);65 impl FieldIndex {66 pub fn next(self) -> Self {67 Self(self.0 + 1)68 }69 }7071 #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]72 pub struct SuperDepth(u32);73 impl SuperDepth {74 pub fn deeper(self) -> Self {75 Self(self.0 + 1)76 }77 }7879 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]80 pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);81 impl FieldSortKey {82 pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {83 Self(Reverse(depth), index)84 }85 pub fn collide(self, other: Self) -> Self {86 match self.0 .0.cmp(&other.0 .0) {87 Ordering::Greater => self,88 Ordering::Less => other,89 Ordering::Equal => unreachable!("object can't have two fields with the same name"),90 }91 }92 }93}9495use ordering::*;9697#[allow(clippy::module_name_repetitions)]98#[derive(Debug, Trace)]99pub struct ObjMember {100 pub add: bool,101 pub visibility: Visibility,102 original_index: FieldIndex,103 pub invoke: MaybeUnbound,104 pub location: Option<ExprLocation>,105}106107pub trait ObjectAssertion: Trace {108 fn run(&self, super_obj: Option<ObjValue>, this: Option<ObjValue>) -> Result<()>;109}110111// Field => This112113#[derive(Trace)]114enum CacheValue {115 Cached(Val),116 NotFound,117 Pending,118 Errored(Error),119}120121#[allow(clippy::module_name_repetitions)]122#[derive(Trace)]123#[trace(tracking(force))]124pub struct ObjValueInternals {125 sup: Option<ObjValue>,126 this: Option<ObjValue>,127128 assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,129 assertions_ran: RefCell<GcHashSet<ObjValue>>,130 this_entries: Cc<GcHashMap<IStr, ObjMember>>,131 value_cache: RefCell<GcHashMap<(IStr, Option<WeakObjValue>), CacheValue>>,132}133134#[derive(Clone, Trace)]135pub struct WeakObjValue(#[trace(skip)] pub(crate) Weak<ObjValueInternals>);136137impl PartialEq for WeakObjValue {138 fn eq(&self, other: &Self) -> bool {139 Weak::ptr_eq(&self.0, &other.0)140 }141}142143impl Eq for WeakObjValue {}144impl Hash for WeakObjValue {145 fn hash<H: Hasher>(&self, hasher: &mut H) {146 // Safety: usize is POD147 let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };148 hasher.write_usize(addr);149 }150}151152#[allow(clippy::module_name_repetitions)]153#[derive(Clone, Trace)]154pub struct ObjValue(pub(crate) Cc<ObjValueInternals>);155impl Debug for ObjValue {156 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {157 if let Some(super_obj) = self.0.sup.as_ref() {158 if f.alternate() {159 write!(f, "{super_obj:#?}")?;160 } else {161 write!(f, "{super_obj:?}")?;162 }163 write!(f, " + ")?;164 }165 let mut debug = f.debug_struct("ObjValue");166 for (name, member) in self.0.this_entries.iter() {167 debug.field(name, member);168 }169 debug.finish_non_exhaustive()170 }171}172173impl ObjValue {174 pub fn new(175 sup: Option<Self>,176 this_entries: Cc<GcHashMap<IStr, ObjMember>>,177 assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,178 ) -> Self {179 Self(Cc::new(ObjValueInternals {180 sup,181 this: None,182 assertions,183 assertions_ran: RefCell::new(GcHashSet::new()),184 this_entries,185 value_cache: RefCell::new(GcHashMap::new()),186 }))187 }188 pub fn new_empty() -> Self {189 Self::new(None, Cc::new(GcHashMap::new()), Cc::new(Vec::new()))190 }191 pub fn builder() -> ObjValueBuilder {192 ObjValueBuilder::new()193 }194 pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {195 ObjValueBuilder::with_capacity(capacity)196 }197 #[must_use]198 pub fn extend_from(&self, sup: Self) -> Self {199 match &self.0.sup {200 None => Self::new(201 Some(sup),202 self.0.this_entries.clone(),203 self.0.assertions.clone(),204 ),205 Some(v) => Self::new(206 Some(v.extend_from(sup)),207 self.0.this_entries.clone(),208 self.0.assertions.clone(),209 ),210 }211 }212 pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {213 let mut new = GcHashMap::with_capacity(1);214 new.insert(key, value);215 Self::new(Some(self), Cc::new(new), Cc::new(Vec::new()))216 }217 pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {218 ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())219 }220221 #[must_use]222 pub fn with_this(&self, this: Self) -> Self {223 Self(Cc::new(ObjValueInternals {224 sup: self.0.sup.clone(),225 assertions: self.0.assertions.clone(),226 assertions_ran: RefCell::new(GcHashSet::new()),227 this: Some(this),228 this_entries: self.0.this_entries.clone(),229 value_cache: RefCell::new(GcHashMap::new()),230 }))231 }232233 pub fn len(&self) -> usize {234 self.fields_visibility()235 .into_iter()236 .filter(|(_, (visible, _))| *visible)237 .count()238 }239240 pub fn is_empty(&self) -> bool {241 if !self.0.this_entries.is_empty() {242 return false;243 }244 self.0.sup.as_ref().map_or(true, Self::is_empty)245 }246247 /// Run callback for every field found in object248 ///249 /// Returns true if ended prematurely250 pub(crate) fn enum_fields(251 &self,252 depth: SuperDepth,253 handler: &mut impl FnMut(SuperDepth, &IStr, &ObjMember) -> bool,254 ) -> bool {255 if let Some(s) = &self.0.sup {256 if s.enum_fields(depth.deeper(), handler) {257 return true;258 }259 }260 for (name, member) in self.0.this_entries.iter() {261 if handler(depth, name, member) {262 return true;263 }264 }265 false266 }267268 pub fn fields_visibility(&self) -> FxHashMap<IStr, (bool, FieldSortKey)> {269 let mut out = FxHashMap::default();270 self.enum_fields(SuperDepth::default(), &mut |depth, name, member| {271 let new_sort_key = FieldSortKey::new(depth, member.original_index);272 let entry = out.entry(name.clone());273 let (visible, _) = entry.or_insert((true, new_sort_key));274 match member.visibility {275 Visibility::Normal => {}276 Visibility::Hidden => {277 *visible = false;278 }279 Visibility::Unhide => {280 *visible = true;281 }282 };283 false284 });285 out286 }287 pub fn fields_ex(288 &self,289 include_hidden: bool,290 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,291 ) -> Vec<IStr> {292 #[cfg(feature = "exp-preserve-order")]293 if preserve_order {294 let (mut fields, mut keys): (Vec<_>, Vec<_>) = self295 .fields_visibility()296 .into_iter()297 .filter(|(_, (visible, _))| include_hidden || *visible)298 .enumerate()299 .map(|(idx, (k, (_, sk)))| (k, (sk, idx)))300 .unzip();301 keys.sort_unstable_by_key(|v| v.0);302 // Reorder in-place by resulting indexes303 for i in 0..fields.len() {304 let x = fields[i].clone();305 let mut j = i;306 loop {307 let k = keys[j].1;308 keys[j].1 = j;309 if k == i {310 break;311 }312 fields[j] = fields[k].clone();313 j = k;314 }315 fields[j] = x;316 }317 return fields;318 }319320 let mut fields: Vec<_> = self321 .fields_visibility()322 .into_iter()323 .filter(|(_, (visible, _))| include_hidden || *visible)324 .map(|(k, _)| k)325 .collect();326 fields.sort_unstable();327 fields328 }329 pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {330 self.fields_ex(331 false,332 #[cfg(feature = "exp-preserve-order")]333 preserve_order,334 )335 }336337 pub fn field_visibility(&self, name: IStr) -> Option<Visibility> {338 if let Some(m) = self.0.this_entries.get(&name) {339 Some(match &m.visibility {340 Visibility::Normal => self341 .0342 .sup343 .as_ref()344 .and_then(|super_obj| super_obj.field_visibility(name))345 .unwrap_or(Visibility::Normal),346 v => *v,347 })348 } else if let Some(super_obj) = &self.0.sup {349 super_obj.field_visibility(name)350 } else {351 None352 }353 }354355 fn has_field_include_hidden(&self, name: IStr) -> bool {356 if self.0.this_entries.contains_key(&name) {357 true358 } else if let Some(super_obj) = &self.0.sup {359 super_obj.has_field_include_hidden(name)360 } else {361 false362 }363 }364365 pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {366 if include_hidden {367 self.has_field_include_hidden(name)368 } else {369 self.has_field(name)370 }371 }372 pub fn has_field(&self, name: IStr) -> bool {373 self.field_visibility(name)374 .map_or(false, |v| v.is_visible())375 }376377 pub fn iter(378 &self,379 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,380 ) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {381 let fields = self.fields(382 #[cfg(feature = "exp-preserve-order")]383 preserve_order,384 );385 fields.into_iter().map(|field| {386 (387 field.clone(),388 self.get(field)389 .map(|opt| opt.expect("iterating over keys, field exists")),390 )391 })392 }393 pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {394 #[derive(Trace)]395 struct ThunkGet {396 obj: ObjValue,397 key: IStr,398 }399 impl ThunkValue for ThunkGet {400 type Output = Val;401402 fn get(self: Box<Self>) -> Result<Self::Output> {403 Ok(self.obj.get(self.key)?.expect("field exists"))404 }405 }406407 if !self.has_field_ex(key.clone(), true) {408 return None;409 }410 Some(Thunk::new(ThunkGet {411 obj: self.clone(),412 key,413 }))414 }415 pub fn get(&self, key: IStr) -> Result<Option<Val>> {416 self.run_assertions()?;417 let cache_key = (key.clone(), None);418 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {419 return Ok(match v {420 CacheValue::Cached(v) => Some(v.clone()),421 CacheValue::NotFound => None,422 CacheValue::Pending => throw!(InfiniteRecursionDetected),423 CacheValue::Errored(e) => return Err(e.clone()),424 });425 }426 self.0427 .value_cache428 .borrow_mut()429 .insert(cache_key.clone(), CacheValue::Pending);430 let value = self431 .get_raw(key, self.0.this.clone().unwrap_or_else(|| self.clone()))432 .map_err(|e| {433 self.0434 .value_cache435 .borrow_mut()436 .insert(cache_key.clone(), CacheValue::Errored(e.clone()));437 e438 })?;439 self.0.value_cache.borrow_mut().insert(440 cache_key,441 value442 .as_ref()443 .map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),444 );445 Ok(value)446 }447 pub fn get_for(&self, key: IStr, this: Self) -> Result<Option<Val>> {448 self.run_assertions()?;449 let cache_key = (key.clone(), Some(this.clone().downgrade()));450 if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {451 return Ok(match v {452 CacheValue::Cached(v) => Some(v.clone()),453 CacheValue::NotFound => None,454 CacheValue::Pending => throw!(InfiniteRecursionDetected),455 CacheValue::Errored(e) => return Err(e.clone()),456 });457 }458 self.0459 .value_cache460 .borrow_mut()461 .insert(cache_key.clone(), CacheValue::Pending);462 let value = self.get_raw(key, this).map_err(|e| {463 self.0464 .value_cache465 .borrow_mut()466 .insert(cache_key.clone(), CacheValue::Errored(e.clone()));467 e468 })?;469 self.0.value_cache.borrow_mut().insert(470 cache_key,471 value472 .as_ref()473 .map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),474 );475 Ok(value)476 }477478 fn get_raw(&self, key: IStr, real_this: Self) -> Result<Option<Val>> {479 match (self.0.this_entries.get(&key), &self.0.sup) {480 (Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),481 (Some(k), Some(super_obj)) => {482 let our = self.evaluate_this(k, real_this.clone())?;483 if k.add {484 super_obj485 .get_raw(key, real_this)?486 .map_or(Ok(Some(our.clone())), |v| {487 Ok(Some(evaluate_add_op(&v, &our)?))488 })489 } else {490 Ok(Some(our))491 }492 }493 (None, Some(super_obj)) => super_obj.get_raw(key, real_this),494 (None, None) => Ok(None),495 }496 }497 fn evaluate_this(&self, v: &ObjMember, real_this: Self) -> Result<Val> {498 v.invoke.evaluate(self.0.sup.clone(), Some(real_this))499 }500501 fn run_assertions_raw(&self, real_this: &Self) -> Result<()> {502 if self.0.assertions.is_empty() {503 if let Some(super_obj) = &self.0.sup {504 super_obj.run_assertions_raw(real_this)?;505 }506 return Ok(());507 }508 if self.0.assertions_ran.borrow_mut().insert(real_this.clone()) {509 for assertion in self.0.assertions.iter() {510 if let Err(e) = assertion.run(self.0.sup.clone(), Some(real_this.clone())) {511 self.0.assertions_ran.borrow_mut().remove(real_this);512 return Err(e);513 }514 }515 if let Some(super_obj) = &self.0.sup {516 super_obj.run_assertions_raw(real_this)?;517 }518 }519 Ok(())520 }521 pub fn run_assertions(&self) -> Result<()> {522 self.run_assertions_raw(self)523 }524525 pub fn ptr_eq(a: &Self, b: &Self) -> bool {526 Cc::ptr_eq(&a.0, &b.0)527 }528 pub fn downgrade(self) -> WeakObjValue {529 WeakObjValue(self.0.downgrade())530 }531}532533impl PartialEq for ObjValue {534 fn eq(&self, other: &Self) -> bool {535 Cc::ptr_eq(&self.0, &other.0)536 }537}538539impl Eq for ObjValue {}540impl Hash for ObjValue {541 fn hash<H: Hasher>(&self, hasher: &mut H) {542 hasher.write_usize(addr_of!(*self.0) as usize);543 }544}545546#[allow(clippy::module_name_repetitions)]547pub struct ObjValueBuilder {548 sup: Option<ObjValue>,549 map: GcHashMap<IStr, ObjMember>,550 assertions: Vec<TraceBox<dyn ObjectAssertion>>,551 next_field_index: FieldIndex,552}553impl ObjValueBuilder {554 pub fn new() -> Self {555 Self::with_capacity(0)556 }557 pub fn with_capacity(capacity: usize) -> Self {558 Self {559 sup: None,560 map: GcHashMap::with_capacity(capacity),561 assertions: Vec::new(),562 next_field_index: FieldIndex::default(),563 }564 }565 pub fn reserve_asserts(&mut self, capacity: usize) -> &mut Self {566 self.assertions.reserve_exact(capacity);567 self568 }569 pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {570 self.sup = Some(super_obj);571 self572 }573574 pub fn assert(&mut self, assertion: impl ObjectAssertion + 'static) -> &mut Self {575 self.assertions.push(tb!(assertion));576 self577 }578 pub fn member(&mut self, name: IStr) -> ObjMemberBuilder<ValueBuilder<'_>> {579 let field_index = self.next_field_index;580 self.next_field_index = self.next_field_index.next();581 ObjMemberBuilder::new(ValueBuilder(self), name, field_index)582 }583584 pub fn build(self) -> ObjValue {585 ObjValue::new(self.sup, Cc::new(self.map), Cc::new(self.assertions))586 }587}588impl Default for ObjValueBuilder {589 fn default() -> Self {590 Self::with_capacity(0)591 }592}593594#[allow(clippy::module_name_repetitions)]595#[must_use = "value not added unless binding() was called"]596pub struct ObjMemberBuilder<Kind> {597 kind: Kind,598 name: IStr,599 add: bool,600 visibility: Visibility,601 original_index: FieldIndex,602 location: Option<ExprLocation>,603}604605#[allow(clippy::missing_const_for_fn)]606impl<Kind> ObjMemberBuilder<Kind> {607 pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {608 Self {609 kind,610 name,611 original_index,612 add: false,613 visibility: Visibility::Normal,614 location: None,615 }616 }617618 pub const fn with_add(mut self, add: bool) -> Self {619 self.add = add;620 self621 }622 pub fn add(self) -> Self {623 self.with_add(true)624 }625 pub fn with_visibility(mut self, visibility: Visibility) -> Self {626 self.visibility = visibility;627 self628 }629 pub fn hide(self) -> Self {630 self.with_visibility(Visibility::Hidden)631 }632 pub fn with_location(mut self, location: ExprLocation) -> Self {633 self.location = Some(location);634 self635 }636 fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {637 (638 self.kind,639 self.name,640 ObjMember {641 add: self.add,642 visibility: self.visibility,643 original_index: self.original_index,644 invoke: binding,645 location: self.location,646 },647 )648 }649}650651pub struct ValueBuilder<'v>(&'v mut ObjValueBuilder);652impl ObjMemberBuilder<ValueBuilder<'_>> {653 /// Inserts value, replacing if it is already defined654 pub fn value_unchecked(self, value: Val) {655 let (receiver, name, member) =656 self.build_member(MaybeUnbound::Bound(Thunk::evaluated(value)));657 let entry = receiver.0.map.entry(name);658 entry.insert(member);659 }660661 pub fn value(self, value: Val) -> Result<()> {662 self.thunk(Thunk::evaluated(value))663 }664 pub fn thunk(self, value: Thunk<Val>) -> Result<()> {665 self.binding(MaybeUnbound::Bound(value))666 }667 pub fn bindable(self, bindable: impl Unbound<Bound = Val>) -> Result<()> {668 self.binding(MaybeUnbound::Unbound(Cc::new(tb!(bindable))))669 }670 pub fn binding(self, binding: MaybeUnbound) -> Result<()> {671 let (receiver, name, member) = self.build_member(binding);672 let location = member.location.clone();673 let old = receiver.0.map.insert(name.clone(), member);674 if old.is_some() {675 State::push(676 CallLocation(location.as_ref()),677 || format!("field <{}> initializtion", name.clone()),678 || throw!(DuplicateFieldName(name.clone())),679 )?;680 }681 Ok(())682 }683}684685pub struct ExtendBuilder<'v>(&'v mut ObjValue);686impl ObjMemberBuilder<ExtendBuilder<'_>> {687 pub fn value(self, value: Val) {688 self.binding(MaybeUnbound::Bound(Thunk::evaluated(value)));689 }690 pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Val>>) {691 self.binding(MaybeUnbound::Unbound(Cc::new(bindable)));692 }693 pub fn binding(self, binding: MaybeUnbound) {694 let (receiver, name, member) = self.build_member(binding);695 let new = receiver.0.clone();696 *receiver.0 = new.extend_with_raw_member(name, member);697 }698}