difftreelog
feat macro name interning
in: master
9 files changed
cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -11,6 +11,10 @@
workspace = true
[features]
+default = [
+ "exp-regex",
+]
+
experimental = [
"exp-preserve-order",
"exp-destruct",
@@ -18,7 +22,6 @@
"exp-object-iteration",
"exp-bigint",
"exp-apply",
- "exp-regex",
]
# Use mimalloc as allocator
mimalloc = ["mimallocator"]
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -5,11 +5,11 @@
rc::Rc,
};
-use jrsonnet_gcmodule::{cc_dyn, Cc};
+use jrsonnet_gcmodule::{cc_dyn, Cc, Trace};
use jrsonnet_interner::IBytes;
use jrsonnet_parser::{Expr, Spanned};
-use crate::{function::NativeFn, Context, Result, Thunk, Val};
+use crate::{function::NativeFn, typed::Typed, Context, Result, Thunk, Val};
mod spec;
pub use spec::{ArrayLike, *};
@@ -241,3 +241,4 @@
self.0.is_cheap()
}
}
+
crates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth1use std::{2 any::Any,3 cell::{Cell, RefCell},4 clone::Clone,5 collections::hash_map::Entry,6 fmt::{self, Debug},7 hash::{Hash, Hasher},8 num::Saturating,9 ops::ControlFlow,10};1112use educe::Educe;13use jrsonnet_gcmodule::{cc_dyn, Acyclic, Cc, Trace, Weak};14use jrsonnet_interner::IStr;15use jrsonnet_parser::{Span, Visibility};16use rustc_hash::{FxHashMap, FxHashSet};1718mod oop;1920pub use oop::ObjValueBuilder;2122use crate::{23 arr::{PickObjectKeyValues, PickObjectValues},24 bail,25 error::{suggest_object_fields, ErrorKind::*},26 identity_hash,27 operator::evaluate_add_op,28 val::{ArrValue, ThunkValue},29 CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,30};3132#[cfg(not(feature = "exp-preserve-order"))]33mod ordering {34 #![allow(35 // This module works as stub for preserve-order feature36 clippy::unused_self,37 )]3839 use jrsonnet_gcmodule::Trace;4041 #[derive(Clone, Copy, Default, Debug, Trace)]42 pub struct FieldIndex(());43 impl FieldIndex {44 pub const fn next(self) -> Self {45 Self(())46 }47 }4849 #[derive(Clone, Copy, Default, Debug, Trace)]50 pub struct SuperDepth(());51 impl SuperDepth {52 pub(super) fn deepen(self) {}53 }54}5556#[cfg(feature = "exp-preserve-order")]57mod ordering {58 use std::cmp::Reverse;5960 use jrsonnet_gcmodule::Trace;6162 #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]63 pub struct FieldIndex(u32);64 impl FieldIndex {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 += 175 }76 }7778 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]79 pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);80 impl FieldSortKey {81 pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {82 Self(Reverse(depth), index)83 }84 }85}8687#[cfg(feature = "exp-preserve-order")]88use ordering::FieldSortKey;89use ordering::{FieldIndex, SuperDepth};9091// 0 - add92// 12 - visibility93#[derive(Clone, Copy)]94pub struct ObjFieldFlags(u8);95impl ObjFieldFlags {96 fn new(add: bool, visibility: Visibility) -> Self {97 let mut v = 0;98 if add {99 v |= 1;100 }101 v |= match visibility {102 Visibility::Normal => 0b000,103 Visibility::Hidden => 0b010,104 Visibility::Unhide => 0b100,105 };106 Self(v)107 }108 pub fn add(&self) -> bool {109 self.0 & 1 != 0110 }111 pub fn visibility(&self) -> Visibility {112 match (self.0 & 0b110) >> 1 {113 0b00 => Visibility::Normal,114 0b01 => Visibility::Hidden,115 0b10 => Visibility::Unhide,116 _ => unreachable!(),117 }118 }119}120impl Debug for ObjFieldFlags {121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {122 f.debug_struct("ObjFieldFlags")123 .field("add", &self.add())124 .field("visibility", &self.visibility())125 .finish()126 }127}128129#[allow(clippy::module_name_repetitions)]130#[derive(Debug, Trace)]131pub struct ObjMember {132 #[trace(skip)]133 flags: ObjFieldFlags,134 original_index: FieldIndex,135 pub invoke: MaybeUnbound,136 pub location: Option<Span>,137}138139cc_dyn!(CcObjectAssertion, ObjectAssertion);140pub trait ObjectAssertion: Trace {141 fn run(&self, sup_this: SupThis) -> Result<()>;142}143144// Field => This145146#[derive(Trace, Debug)]147enum CacheValue {148 Cached(Result<Option<Val>>),149 Pending,150}151152type EnumFieldsHandler<'a> =153 dyn FnMut(SuperDepth, FieldIndex, IStr, EnumFields) -> ControlFlow<()> + 'a;154155pub enum EnumFields {156 Normal(Visibility),157 Omit(Skip),158}159160#[derive(Trace, Clone)]161pub enum GetFor {162 // Return value163 Final(Val),164 // Continue iterating over cores, add current value to sum stack165 SuperPlus(Val),166 // Ignore the field value, stop at this layer instead167 Omit(#[trace(skip)] Skip),168 NotFound,169}170171#[derive(Acyclic, Clone)]172pub enum FieldVisibility {173 Found(Visibility),174 Omit(Skip),175 NotFound,176}177178#[derive(Acyclic, Clone)]179pub enum HasFieldIncludeHidden {180 Exists,181 NotFound,182 Omit(Skip),183}184185type Skip = Saturating<usize>;186187pub trait ObjectCore: Trace + Any + Debug {188 // If callback returns false, iteration stops, and this call returns false.189 fn enum_fields_core(190 &self,191 super_depth: &mut SuperDepth,192 handler: &mut EnumFieldsHandler<'_>,193 ) -> bool;194195 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden;196197 fn get_for_core(&self, key: IStr, sup_this: SupThis, omit_only: bool) -> Result<GetFor>;198 fn field_visibility_core(&self, field: IStr) -> FieldVisibility;199200 fn run_assertions_core(&self, sup_this: SupThis) -> Result<()>;201}202203#[derive(Clone, Trace)]204pub struct WeakObjValue(#[trace(skip)] Weak<ObjValueInner>);205impl Debug for WeakObjValue {206 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {207 f.debug_tuple("WeakObjValue").finish()208 }209}210211impl PartialEq for WeakObjValue {212 fn eq(&self, other: &Self) -> bool {213 Weak::ptr_eq(&self.0, &other.0)214 }215}216217impl Eq for WeakObjValue {}218impl Hash for WeakObjValue {219 fn hash<H: Hasher>(&self, hasher: &mut H) {220 // Safety: usize is POD221 let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };222 hasher.write_usize(addr);223 }224}225226cc_dyn!(227 #[derive(Clone, Debug)]228 CcObjectCore, ObjectCore,229 pub fn new() {...}230);231#[derive(Trace, Educe)]232#[educe(Debug)]233struct ObjValueInner {234 cores: Vec<CcObjectCore>,235 assertions_ran: Cell<bool>,236 value_cache: RefCell<FxHashMap<(IStr, CoreIdx), CacheValue>>,237}238239thread_local! {240 static RUNNING_ASSERTIONS: RefCell<FxHashSet<ObjValue>> = RefCell::default();241}242fn is_asserting(obj: &ObjValue) -> bool {243 RUNNING_ASSERTIONS.with_borrow(|v| v.contains(obj))244}245/// Returns false if already asserting246fn start_asserting(obj: &ObjValue) -> bool {247 RUNNING_ASSERTIONS.with_borrow_mut(|v| v.insert(obj.clone()))248}249fn finish_asserting(obj: &ObjValue) {250 RUNNING_ASSERTIONS.with_borrow_mut(|v| {251 let r = v.remove(obj);252 debug_assert!(253 r,254 "finish_asserting was called before start_asserting or twice"255 );256 });257}258259thread_local! {260 static EMPTY_OBJ: ObjValue = ObjValue(Cc::new(ObjValueInner {261 cores: vec![],262 assertions_ran: Cell::new(true),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 ObjValue(Cc::new(ObjValueInner {481 cores,482 value_cache: RefCell::default(),483 assertions_ran: Cell::new(false),484 }))485 }486 // #[must_use]487 // pub fn with_this(&self, this: Self) -> Self {488 // self.0.with_this(self.clone(), this)489 // }490 /// Returns amount of visible object fields491 /// If object only contains hidden fields - may return zero.492 pub fn len(&self) -> usize {493 self.fields_visibility()494 .values()495 .filter(|d| d.visible())496 .count()497 }498 /// For each field, calls callback.499 /// If callback returns false - ends iteration prematurely.500 ///501 /// Returns false if ended prematurely502 pub fn enum_fields(&self, handler: &mut EnumFieldsHandler<'_>) -> bool {503 let mut super_depth = SuperDepth::default();504 self.enum_fields_idx(505 &mut super_depth,506 handler,507 CoreIdx {508 idx: self.0.cores.len(),509 },510 )511 }512 fn enum_fields_idx(513 &self,514 super_depth: &mut SuperDepth,515 handler: &mut EnumFieldsHandler<'_>,516 idx: CoreIdx,517 ) -> bool {518 for core in self.0.cores[..idx.idx].iter().rev() {519 if !core.0.enum_fields_core(super_depth, handler) {520 return false;521 }522 super_depth.deepen();523 }524 true525 }526527 pub fn has_field_include_hidden(&self, name: IStr) -> bool {528 self.has_field_include_hidden_idx(529 name,530 CoreIdx {531 idx: self.0.cores.len(),532 },533 )534 }535 fn has_field_include_hidden_idx(&self, name: IStr, core: CoreIdx) -> bool {536 let mut skip = Saturating(0usize);537 for ele in self.0.cores[..core.idx].iter().rev() {538 match ele.0.has_field_include_hidden_core(name.clone()) {539 HasFieldIncludeHidden::Exists => {540 if skip.0 == 0 {541 return true;542 }543 }544 HasFieldIncludeHidden::Omit(new_skip) => {545 // +1 including this core546 skip = skip.max(new_skip + Saturating(1));547 }548 HasFieldIncludeHidden::NotFound => {}549 }550 skip -= 1;551 }552 false553 }554 pub fn has_field(&self, name: IStr) -> bool {555 match self.field_visibility(name) {556 Some(Visibility::Unhide | Visibility::Normal) => true,557 Some(Visibility::Hidden) | None => false,558 }559 }560 pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {561 if include_hidden {562 self.has_field_include_hidden(name)563 } else {564 self.has_field(name)565 }566 }567 pub fn get(&self, key: IStr) -> Result<Option<Val>> {568 self.get_idx(569 key,570 CoreIdx {571 idx: self.0.cores.len(),572 },573 )574 }575576 fn get_idx(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {577 let cache_key = (key.clone(), core);578 {579 let mut cache = self.0.value_cache.borrow_mut();580 // entry_ref candidate?581 match cache.entry(cache_key.clone()) {582 Entry::Occupied(v) => match v.get() {583 CacheValue::Cached(v) => return v.clone(),584 CacheValue::Pending => {585 if !is_asserting(self) {586 bail!(InfiniteRecursionDetected);587 }588 }589 },590 Entry::Vacant(v) => {591 v.insert(CacheValue::Pending);592 }593 };594 }595 let result = self.get_idx_uncached(key, core);596 {597 let mut cache = self.0.value_cache.borrow_mut();598 cache.insert(cache_key, CacheValue::Cached(result.clone()));599 }600 result601 }602 fn get_idx_uncached(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {603 self.run_assertions()?;604 let mut add_stack = Vec::with_capacity(2);605 let mut skip = Saturating(0);606 for (sup, core) in self.0.cores[..core.idx].iter().enumerate().rev() {607 let sup_this = SupThis {608 sup: CoreIdx { idx: sup },609 this: self.clone(),610 };611 match core.0.get_for_core(key.clone(), sup_this, skip.0 != 0)? {612 GetFor::Final(val) if add_stack.is_empty() => {613 if skip.0 == 0 {614 return Ok(Some(val));615 }616 }617 GetFor::Final(val) => {618 if skip.0 == 0 {619 add_stack.push(val);620 break;621 }622 }623 GetFor::SuperPlus(val) => {624 if skip.0 == 0 {625 add_stack.push(val);626 }627 }628 GetFor::Omit(new_skip) => {629 // +1 including this core630 skip = skip.max(new_skip + Saturating(1));631 }632 GetFor::NotFound => {}633 }634 skip -= 1;635 }636 if add_stack.is_empty() {637 // None of layers had this field638 return Ok(None);639 } else if add_stack.len() == 1 {640 // A layer had this field, but it wanted this field to be added with super.641 // However, no super had this field, fail-safe642 return Ok(Some(add_stack.pop().expect("single element on stack")));643 }644 let mut values = add_stack.into_iter().rev();645 let init = values.next().expect("at least 2 elements");646647 values648 .try_fold(init, |a, b| evaluate_add_op(&a, &b))649 .map(Some)650651 // self.0.get_raw(key, this)652 }653654 pub fn get_or_bail(&self, key: IStr) -> Result<Val> {655 let Some(value) = self.get(key.clone())? else {656 let suggestions = suggest_object_fields(self, key.clone());657 bail!(NoSuchField(key, suggestions))658 };659 Ok(value)660 }661662 fn field_visibility(&self, field: IStr) -> Option<Visibility> {663 self.field_visibility_idx(664 field,665 CoreIdx {666 idx: self.0.cores.len(),667 },668 )669 }670 fn field_visibility_idx(&self, field: IStr, core: CoreIdx) -> Option<Visibility> {671 let mut exists = false;672 let mut skip = Saturating(0usize);673 for ele in self.0.cores[..core.idx].iter().rev() {674 let vis = ele.0.field_visibility_core(field.clone());675 match vis {676 FieldVisibility::Found(vis @ (Visibility::Unhide | Visibility::Hidden)) => {677 if skip.0 == 0 {678 return Some(vis);679 }680 }681 FieldVisibility::Found(Visibility::Normal) => {682 if skip.0 == 0 {683 exists = true;684 }685 }686 FieldVisibility::NotFound => {}687 FieldVisibility::Omit(new_skip) => {688 // +1 including this core689 skip = skip.max(new_skip + Saturating(1));690 }691 }692 skip -= 1;693 }694 exists.then_some(Visibility::Normal)695 }696697 pub fn run_assertions(&self) -> Result<()> {698 if self.0.assertions_ran.get() {699 return Ok(());700 }701 if !start_asserting(self) {702 return Ok(());703 }704 for (idx, ele) in self.0.cores.iter().enumerate() {705 let sup_this = SupThis {706 sup: CoreIdx { idx },707 this: self.clone(),708 };709 ele.0.run_assertions_core(sup_this).inspect_err(|_e| {710 finish_asserting(self);711 })?;712 }713 finish_asserting(self);714 self.0.assertions_ran.set(true);715 Ok(())716 }717718 pub fn iter(719 &self,720 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,721 ) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {722 let fields = self.fields(723 #[cfg(feature = "exp-preserve-order")]724 preserve_order,725 );726 fields.into_iter().map(|field| {727 (728 field.clone(),729 self.get(field)730 .map(|opt| opt.expect("iterating over keys, field exists")),731 )732 })733 }734 pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {735 #[derive(Trace)]736 struct ObjFieldThunk {737 obj: ObjValue,738 key: IStr,739 }740 impl ThunkValue for ObjFieldThunk {741 type Output = Val;742743 fn get(&self) -> Result<Self::Output> {744 self.obj745 .get(self.key.clone())746 .transpose()747 .expect("field existence checked")748 }749 }750751 if !self.has_field_ex(key.clone(), true) {752 return None;753 }754755 Some(Thunk::new(ObjFieldThunk {756 obj: self.clone(),757 key,758 }))759 }760 pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {761 #[derive(Trace)]762 struct ObjFieldThunk {763 obj: ObjValue,764 key: IStr,765 }766 impl ThunkValue for ObjFieldThunk {767 type Output = Val;768769 fn get(&self) -> Result<Self::Output> {770 self.obj.get_or_bail(self.key.clone())771 }772 }773774 Thunk::new(ObjFieldThunk {775 obj: self.clone(),776 key,777 })778 }779 pub fn ptr_eq(a: &Self, b: &Self) -> bool {780 Cc::ptr_eq(&a.0, &b.0)781 }782 pub fn downgrade(self) -> WeakObjValue {783 WeakObjValue(self.0.downgrade())784 }785}786787#[derive(Debug)]788struct FieldVisibilityData {789 omitted_until: Saturating<usize>,790 exists_visible: Option<Visibility>,791 #[cfg(feature = "exp-preserve-order")]792 key: FieldSortKey,793}794impl FieldVisibilityData {795 fn visible(&self) -> bool {796 self.exists_visible797 .expect("non-existing fields shall be dropped at the end of fn fields_visibility()")798 .is_visible()799 }800 #[cfg(feature = "exp-preserve-order")]801 fn sort_key(&self) -> FieldSortKey {802 self.key803 }804}805806impl ObjValue {807 fn fields_visibility(&self) -> FxHashMap<IStr, FieldVisibilityData> {808 let mut out = FxHashMap::default();809810 let mut super_depth = SuperDepth::default();811 let mut omit_index = Saturating(0);812 for core in self.0.cores.iter().rev() {813 core.0814 .enum_fields_core(&mut super_depth, &mut |_depth, _index, name, visibility| {815 let entry = out.entry(name);816 let data = entry.or_insert(FieldVisibilityData {817 exists_visible: None,818 #[cfg(feature = "exp-preserve-order")]819 key: FieldSortKey::new(_depth, _index),820 omitted_until: omit_index,821 });822 match visibility {823 EnumFields::Omit(new_skip) => {824 // +1 including this core825 data.omitted_until = data826 .omitted_until827 .max(omit_index + new_skip + Saturating(1));828 }829 EnumFields::Normal(Visibility::Normal) => {830 if data.omitted_until <= omit_index && data.exists_visible.is_none() {831 data.exists_visible = Some(Visibility::Normal);832 }833 }834 EnumFields::Normal(Visibility::Hidden) => {835 if data.omitted_until <= omit_index {836 data.exists_visible = Some(match data.exists_visible {837 // We're iterating in reverse, later unhide is preserved838 Some(Visibility::Unhide) => Visibility::Unhide,839 _ => Visibility::Hidden,840 });841 }842 }843 EnumFields::Normal(Visibility::Unhide) => {844 if data.omitted_until <= omit_index {845 data.exists_visible = Some(match data.exists_visible {846 // We're iterating in reverse, later hide is preserved847 Some(Visibility::Hidden) => Visibility::Hidden,848 _ => Visibility::Unhide,849 });850 }851 }852 }853 ControlFlow::Continue(())854 });855856 super_depth.deepen();857 omit_index += 1;858 }859860 out.retain(|_, v| v.exists_visible.is_some());861862 out863 }864 pub fn fields_ex(865 &self,866 include_hidden: bool,867 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,868 ) -> Vec<IStr> {869 #[cfg(feature = "exp-preserve-order")]870 if preserve_order {871 let (mut fields, mut keys): (Vec<_>, Vec<_>) = self872 .fields_visibility()873 .into_iter()874 .filter(|(_, d)| include_hidden || d.visible())875 .enumerate()876 .map(|(idx, (k, d))| (k, (d.sort_key(), idx)))877 .unzip();878 keys.sort_unstable_by_key(|v| v.0);879 // Reorder in-place by resulting indexes880 for i in 0..fields.len() {881 let x = fields[i].clone();882 let mut j = i;883 loop {884 let k = keys[j].1;885 keys[j].1 = j;886 if k == i {887 break;888 }889 fields[j] = fields[k].clone();890 j = k;891 }892 fields[j] = x;893 }894 return fields;895 }896897 let mut fields: Vec<_> = self898 .fields_visibility()899 .into_iter()900 .filter(|(_, d)| include_hidden || d.visible())901 .map(|(k, _)| k)902 .collect();903 fields.sort_unstable();904 fields905 }906 pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {907 self.fields_ex(908 false,909 #[cfg(feature = "exp-preserve-order")]910 preserve_order,911 )912 }913 pub fn values_ex(914 &self,915 include_hidden: bool,916 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,917 ) -> ArrValue {918 ArrValue::new(PickObjectValues::new(919 self.clone(),920 self.fields_ex(921 include_hidden,922 #[cfg(feature = "exp-preserve-order")]923 preserve_order,924 ),925 ))926 }927 pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {928 self.values_ex(929 false,930 #[cfg(feature = "exp-preserve-order")]931 preserve_order,932 )933 }934 pub fn key_values_ex(935 &self,936 include_hidden: bool,937 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,938 ) -> ArrValue {939 ArrValue::new(PickObjectKeyValues::new(940 self.clone(),941 self.fields_ex(942 include_hidden,943 #[cfg(feature = "exp-preserve-order")]944 preserve_order,945 ),946 ))947 }948 pub fn key_values(949 &self,950 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,951 ) -> ArrValue {952 self.key_values_ex(953 false,954 #[cfg(feature = "exp-preserve-order")]955 preserve_order,956 )957 }958}959960#[allow(clippy::module_name_repetitions)]961#[must_use = "value not added unless binding() was called"]962pub struct ObjMemberBuilder<Kind> {963 kind: Kind,964 name: IStr,965 add: bool,966 visibility: Visibility,967 original_index: FieldIndex,968 location: Option<Span>,969}970971#[allow(clippy::missing_const_for_fn)]972impl<Kind> ObjMemberBuilder<Kind> {973 pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {974 Self {975 kind,976 name,977 original_index,978 add: false,979 visibility: Visibility::Normal,980 location: None,981 }982 }983984 pub const fn with_add(mut self, add: bool) -> Self {985 self.add = add;986 self987 }988 pub fn add(self) -> Self {989 self.with_add(true)990 }991 pub fn with_visibility(mut self, visibility: Visibility) -> Self {992 self.visibility = visibility;993 self994 }995 pub fn hide(self) -> Self {996 self.with_visibility(Visibility::Hidden)997 }998 pub fn with_location(mut self, location: Span) -> Self {999 self.location = Some(location);1000 self1001 }1002 fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {1003 (1004 self.kind,1005 self.name,1006 ObjMember {1007 flags: ObjFieldFlags::new(self.add, self.visibility),1008 original_index: self.original_index,1009 invoke: binding,1010 location: self.location,1011 },1012 )1013 }1014}10151016pub struct ExtendBuilder<'v>(&'v mut ObjValue);1017impl ObjMemberBuilder<ExtendBuilder<'_>> {1018 pub fn value(self, value: impl Into<Val>) {1019 self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));1020 }1021 pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {1022 self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));1023 }1024 pub fn binding(self, binding: MaybeUnbound) {1025 let (receiver, name, member) = self.build_member(binding);1026 let new = receiver.0.clone();1027 *receiver.0 = new.extend_with_raw_member(name, member);1028 }1029}1use std::{2 any::Any,3 cell::{Cell, RefCell},4 clone::Clone,5 collections::hash_map::Entry,6 fmt::{self, Debug},7 hash::{Hash, Hasher},8 num::Saturating,9 ops::ControlFlow,10};1112use educe::Educe;13use jrsonnet_gcmodule::{cc_dyn, Acyclic, Cc, Trace, Weak};14use jrsonnet_interner::IStr;15use jrsonnet_parser::Span;16use rustc_hash::{FxHashMap, FxHashSet};1718mod oop;1920pub use jrsonnet_parser::Visibility;21pub use oop::ObjValueBuilder;2223use crate::{24 arr::{PickObjectKeyValues, PickObjectValues},25 bail,26 error::{suggest_object_fields, ErrorKind::*},27 identity_hash,28 operator::evaluate_add_op,29 val::{ArrValue, ThunkValue},30 CcUnbound, MaybeUnbound, Result, Thunk, Unbound, Val,31};3233#[cfg(not(feature = "exp-preserve-order"))]34pub mod ordering {35 #![allow(36 // This module works as stub for preserve-order feature37 clippy::unused_self,38 )]3940 use jrsonnet_gcmodule::Trace;4142 #[derive(Clone, Copy, Default, Debug, Trace)]43 pub struct FieldIndex(());44 impl FieldIndex {45 pub fn absolute(_v: u32) -> Self {46 Self(())47 }48 pub const fn next(self) -> Self {49 Self(())50 }51 }5253 #[derive(Clone, Copy, Default, Debug, Trace)]54 pub struct SuperDepth(());55 impl SuperDepth {56 pub(super) fn deepen(self) {}57 }58}5960#[cfg(feature = "exp-preserve-order")]61pub mod ordering {62 use std::cmp::Reverse;6364 use jrsonnet_gcmodule::Trace;6566 #[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]67 pub struct FieldIndex(u32);68 impl FieldIndex {69 pub fn absolute(v: u32) -> Self {70 Self(v)71 }72 pub fn next(self) -> Self {73 Self(self.0 + 1)74 }75 }7677 #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]78 pub struct SuperDepth(u32);79 impl SuperDepth {80 pub(super) fn deepen(&mut self) {81 self.0 += 182 }83 }8485 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]86 pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);87 impl FieldSortKey {88 pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {89 Self(Reverse(depth), index)90 }91 }92}9394#[cfg(feature = "exp-preserve-order")]95use ordering::FieldSortKey;96use ordering::{FieldIndex, SuperDepth};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;161162pub 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 value_cache: RefCell<FxHashMap<(IStr, CoreIdx), CacheValue>>,244}245246thread_local! {247 static RUNNING_ASSERTIONS: RefCell<FxHashSet<ObjValue>> = RefCell::default();248}249fn is_asserting(obj: &ObjValue) -> bool {250 RUNNING_ASSERTIONS.with_borrow(|v| v.contains(obj))251}252/// Returns false if already asserting253fn start_asserting(obj: &ObjValue) -> bool {254 RUNNING_ASSERTIONS.with_borrow_mut(|v| v.insert(obj.clone()))255}256fn finish_asserting(obj: &ObjValue) {257 RUNNING_ASSERTIONS.with_borrow_mut(|v| {258 let r = v.remove(obj);259 debug_assert!(260 r,261 "finish_asserting was called before start_asserting or twice"262 );263 });264}265266thread_local! {267 static EMPTY_OBJ: ObjValue = ObjValue(Cc::new(ObjValueInner {268 cores: vec![],269 assertions_ran: Cell::new(true),270 value_cache: RefCell::default(),271 }))272}273274#[allow(clippy::module_name_repetitions)]275#[derive(Clone, Trace, Debug, Educe)]276#[educe(PartialEq, Hash, Eq)]277pub struct ObjValue(278 #[educe(PartialEq(method(Cc::ptr_eq)), Hash(method(identity_hash)))] Cc<ObjValueInner>,279);280281impl ObjValue {282 pub fn empty() -> Self {283 EMPTY_OBJ.with(Clone::clone)284 }285 pub fn is_empty(&self) -> bool {286 self.0.cores.is_empty() || self.len() == 0287 }288}289290#[derive(Trace, Debug)]291struct StandaloneSuperCore {292 sup: CoreIdx,293 this: ObjValue,294}295impl ObjectCore for StandaloneSuperCore {296 fn enum_fields_core(297 &self,298 super_depth: &mut SuperDepth,299 handler: &mut EnumFieldsHandler<'_>,300 ) -> bool {301 self.this.enum_fields_idx(super_depth, handler, self.sup)302 }303304 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {305 if self.this.has_field_include_hidden_idx(name, self.sup) {306 HasFieldIncludeHidden::Exists307 } else {308 HasFieldIncludeHidden::NotFound309 }310 }311312 fn get_for_core(&self, key: IStr, _sup_this: SupThis, omit_only: bool) -> Result<GetFor> {313 if omit_only {314 return Ok(GetFor::NotFound);315 }316 let v = self.this.get_idx(key, self.sup)?;317 Ok(v.map_or(GetFor::NotFound, GetFor::Final))318 }319320 fn field_visibility_core(&self, field: IStr) -> FieldVisibility {321 self.this322 .field_visibility_idx(field, self.sup)323 .map_or(FieldVisibility::NotFound, FieldVisibility::Found)324 }325326 fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {327 self.this.run_assertions()328 }329}330331#[derive(Debug, Acyclic)]332struct OmitFieldsCore {333 omit: FxHashSet<IStr>,334 prev_layers: usize,335}336impl ObjectCore for OmitFieldsCore {337 fn enum_fields_core(338 &self,339 super_depth: &mut SuperDepth,340 handler: &mut EnumFieldsHandler<'_>,341 ) -> bool {342 let mut fi = FieldIndex::default();343 for f in &self.omit {344 if handler(345 *super_depth,346 fi,347 f.clone(),348 EnumFields::Omit(Saturating(self.prev_layers)),349 ) == ControlFlow::Break(())350 {351 return false;352 }353 fi = fi.next();354 }355 true356 }357358 fn has_field_include_hidden_core(&self, name: IStr) -> HasFieldIncludeHidden {359 if self.omit.contains(&name) {360 return HasFieldIncludeHidden::Omit(Saturating(self.prev_layers));361 }362 HasFieldIncludeHidden::NotFound363 }364365 fn get_for_core(&self, key: IStr, _sup_this: SupThis, _omit_only: bool) -> Result<GetFor> {366 if self.omit.contains(&key) {367 return Ok(GetFor::Omit(Saturating(self.prev_layers)));368 }369 Ok(GetFor::NotFound)370 }371372 fn field_visibility_core(&self, field: IStr) -> FieldVisibility {373 if self.omit.contains(&field) {374 return FieldVisibility::Omit(Saturating(self.prev_layers));375 }376 FieldVisibility::NotFound377 }378379 fn run_assertions_core(&self, _sup_this: SupThis) -> Result<()> {380 Ok(())381 }382}383384#[derive(Hash, PartialEq, Eq, Trace, Clone, Copy, Debug)]385struct CoreIdx {386 idx: usize,387}388impl CoreIdx {389 fn super_exists(self) -> bool {390 self.idx != 0391 }392}393#[derive(Trace, Clone, PartialEq, Eq, Hash, Debug)]394pub struct SupThis {395 sup: CoreIdx,396 this: ObjValue,397}398impl SupThis {399 pub fn has_super(&self) -> bool {400 self.sup.super_exists()401 }402 /// Implementation of `"field" in super` operation,403 /// works faster than standalone super path.404 ///405 /// In case of no `super` existence, returns false.406 pub fn field_in_super(&self, field: IStr) -> bool {407 self.this.has_field_include_hidden_idx(field, self.sup)408 }409 /// Implementation of `super.field` operation,410 /// works faster than standalone super path.411 ///412 /// In case of no `super` existence, returns `NoSuperFound`413 pub fn get_super(&self, field: IStr) -> Result<Option<Val>> {414 if !self.sup.super_exists() {415 bail!(NoSuperFound);416 }417 self.this.get_idx(field, self.sup)418 }419 /// `super` with `self` overriden for top-level lookups.420 /// Exists when super appears outside of `super.field`/`"field" in super` expressions421 /// Exclusive to jrsonnet.422 ///423 /// Might return `NoSuperFound` error.424 pub fn standalone_super(&self) -> Result<ObjValue> {425 if !self.sup.super_exists() {426 bail!(NoSuperFound)427 }428 let mut out = ObjValue::builder();429 out.reserve_cores(1).extend_with_core(StandaloneSuperCore {430 sup: self.sup,431 this: self.this.clone(),432 });433 Ok(out.build())434 }435 pub fn this(&self) -> &ObjValue {436 &self.this437 }438 pub fn downgrade(self) -> WeakSupThis {439 WeakSupThis {440 sup: self.sup,441 this: self.this.downgrade(),442 }443 }444}445#[derive(Trace, PartialEq, Eq, Hash, Debug)]446pub struct WeakSupThis {447 sup: CoreIdx,448 this: WeakObjValue,449}450451impl ObjValue {452 pub fn builder() -> ObjValueBuilder {453 ObjValueBuilder::new()454 }455 pub fn builder_with_capacity(capacity: usize) -> ObjValueBuilder {456 ObjValueBuilder::with_capacity(capacity)457 }458 pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {459 let mut out = ObjValueBuilder::with_capacity(1);460 out.with_super(self);461 let mut member = out.field(key);462 if value.flags.add() {463 member = member.add();464 }465 if let Some(loc) = value.location {466 member = member.with_location(loc);467 }468 let _ = member469 .with_visibility(value.flags.visibility())470 .binding(value.invoke);471 out.build()472 }473 pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {474 ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())475 }476477 pub fn extend(&mut self) -> ObjValueBuilder {478 let mut out = ObjValueBuilder::new();479 out.with_super(self.clone());480 out481 }482483 #[must_use]484 pub fn extend_from(&self, sup: Self) -> Self {485 let mut cores = sup.0.cores.clone();486 cores.extend(self.0.cores.iter().cloned());487 ObjValue(Cc::new(ObjValueInner {488 cores,489 value_cache: RefCell::default(),490 assertions_ran: Cell::new(false),491 }))492 }493 // #[must_use]494 // pub fn with_this(&self, this: Self) -> Self {495 // self.0.with_this(self.clone(), this)496 // }497 /// Returns amount of visible object fields498 /// If object only contains hidden fields - may return zero.499 pub fn len(&self) -> usize {500 self.fields_visibility()501 .values()502 .filter(|d| d.visible())503 .count()504 }505 /// For each field, calls callback.506 /// If callback returns false - ends iteration prematurely.507 ///508 /// Returns false if ended prematurely509 pub fn enum_fields(&self, handler: &mut EnumFieldsHandler<'_>) -> bool {510 let mut super_depth = SuperDepth::default();511 self.enum_fields_idx(512 &mut super_depth,513 handler,514 CoreIdx {515 idx: self.0.cores.len(),516 },517 )518 }519 fn enum_fields_idx(520 &self,521 super_depth: &mut SuperDepth,522 handler: &mut EnumFieldsHandler<'_>,523 idx: CoreIdx,524 ) -> bool {525 for core in self.0.cores[..idx.idx].iter().rev() {526 if !core.0.enum_fields_core(super_depth, handler) {527 return false;528 }529 super_depth.deepen();530 }531 true532 }533534 pub fn has_field_include_hidden(&self, name: IStr) -> bool {535 self.has_field_include_hidden_idx(536 name,537 CoreIdx {538 idx: self.0.cores.len(),539 },540 )541 }542 fn has_field_include_hidden_idx(&self, name: IStr, core: CoreIdx) -> bool {543 let mut skip = Saturating(0usize);544 for ele in self.0.cores[..core.idx].iter().rev() {545 match ele.0.has_field_include_hidden_core(name.clone()) {546 HasFieldIncludeHidden::Exists => {547 if skip.0 == 0 {548 return true;549 }550 }551 HasFieldIncludeHidden::Omit(new_skip) => {552 // +1 including this core553 skip = skip.max(new_skip + Saturating(1));554 }555 HasFieldIncludeHidden::NotFound => {}556 }557 skip -= 1;558 }559 false560 }561 pub fn has_field(&self, name: IStr) -> bool {562 match self.field_visibility(name) {563 Some(Visibility::Unhide | Visibility::Normal) => true,564 Some(Visibility::Hidden) | None => false,565 }566 }567 pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {568 if include_hidden {569 self.has_field_include_hidden(name)570 } else {571 self.has_field(name)572 }573 }574 pub fn get(&self, key: IStr) -> Result<Option<Val>> {575 self.get_idx(576 key,577 CoreIdx {578 idx: self.0.cores.len(),579 },580 )581 }582583 fn get_idx(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {584 let cache_key = (key.clone(), core);585 {586 let mut cache = self.0.value_cache.borrow_mut();587 // entry_ref candidate?588 match cache.entry(cache_key.clone()) {589 Entry::Occupied(v) => match v.get() {590 CacheValue::Cached(v) => return v.clone(),591 CacheValue::Pending => {592 if !is_asserting(self) {593 bail!(InfiniteRecursionDetected);594 }595 }596 },597 Entry::Vacant(v) => {598 v.insert(CacheValue::Pending);599 }600 };601 }602 let result = self.get_idx_uncached(key, core);603 {604 let mut cache = self.0.value_cache.borrow_mut();605 cache.insert(cache_key, CacheValue::Cached(result.clone()));606 }607 result608 }609 fn get_idx_uncached(&self, key: IStr, core: CoreIdx) -> Result<Option<Val>> {610 self.run_assertions()?;611 let mut add_stack = Vec::with_capacity(2);612 let mut skip = Saturating(0);613 for (sup, core) in self.0.cores[..core.idx].iter().enumerate().rev() {614 let sup_this = SupThis {615 sup: CoreIdx { idx: sup },616 this: self.clone(),617 };618 match core.0.get_for_core(key.clone(), sup_this, skip.0 != 0)? {619 GetFor::Final(val) if add_stack.is_empty() => {620 if skip.0 == 0 {621 return Ok(Some(val));622 }623 }624 GetFor::Final(val) => {625 if skip.0 == 0 {626 add_stack.push(val);627 break;628 }629 }630 GetFor::SuperPlus(val) => {631 if skip.0 == 0 {632 add_stack.push(val);633 }634 }635 GetFor::Omit(new_skip) => {636 // +1 including this core637 skip = skip.max(new_skip + Saturating(1));638 }639 GetFor::NotFound => {}640 }641 skip -= 1;642 }643 if add_stack.is_empty() {644 // None of layers had this field645 return Ok(None);646 } else if add_stack.len() == 1 {647 // A layer had this field, but it wanted this field to be added with super.648 // However, no super had this field, fail-safe649 return Ok(Some(add_stack.pop().expect("single element on stack")));650 }651 let mut values = add_stack.into_iter().rev();652 let init = values.next().expect("at least 2 elements");653654 values655 .try_fold(init, |a, b| evaluate_add_op(&a, &b))656 .map(Some)657658 // self.0.get_raw(key, this)659 }660661 pub fn get_or_bail(&self, key: IStr) -> Result<Val> {662 let Some(value) = self.get(key.clone())? else {663 let suggestions = suggest_object_fields(self, key.clone());664 bail!(NoSuchField(key, suggestions))665 };666 Ok(value)667 }668669 fn field_visibility(&self, field: IStr) -> Option<Visibility> {670 self.field_visibility_idx(671 field,672 CoreIdx {673 idx: self.0.cores.len(),674 },675 )676 }677 fn field_visibility_idx(&self, field: IStr, core: CoreIdx) -> Option<Visibility> {678 let mut exists = false;679 let mut skip = Saturating(0usize);680 for ele in self.0.cores[..core.idx].iter().rev() {681 let vis = ele.0.field_visibility_core(field.clone());682 match vis {683 FieldVisibility::Found(vis @ (Visibility::Unhide | Visibility::Hidden)) => {684 if skip.0 == 0 {685 return Some(vis);686 }687 }688 FieldVisibility::Found(Visibility::Normal) => {689 if skip.0 == 0 {690 exists = true;691 }692 }693 FieldVisibility::NotFound => {}694 FieldVisibility::Omit(new_skip) => {695 // +1 including this core696 skip = skip.max(new_skip + Saturating(1));697 }698 }699 skip -= 1;700 }701 exists.then_some(Visibility::Normal)702 }703704 pub fn run_assertions(&self) -> Result<()> {705 if self.0.assertions_ran.get() {706 return Ok(());707 }708 if !start_asserting(self) {709 return Ok(());710 }711 for (idx, ele) in self.0.cores.iter().enumerate() {712 let sup_this = SupThis {713 sup: CoreIdx { idx },714 this: self.clone(),715 };716 ele.0.run_assertions_core(sup_this).inspect_err(|_e| {717 finish_asserting(self);718 })?;719 }720 finish_asserting(self);721 self.0.assertions_ran.set(true);722 Ok(())723 }724725 pub fn iter(726 &self,727 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,728 ) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {729 let fields = self.fields(730 #[cfg(feature = "exp-preserve-order")]731 preserve_order,732 );733 fields.into_iter().map(|field| {734 (735 field.clone(),736 self.get(field)737 .map(|opt| opt.expect("iterating over keys, field exists")),738 )739 })740 }741 pub fn get_lazy(&self, key: IStr) -> Option<Thunk<Val>> {742 #[derive(Trace)]743 struct ObjFieldThunk {744 obj: ObjValue,745 key: IStr,746 }747 impl ThunkValue for ObjFieldThunk {748 type Output = Val;749750 fn get(&self) -> Result<Self::Output> {751 self.obj752 .get(self.key.clone())753 .transpose()754 .expect("field existence checked")755 }756 }757758 if !self.has_field_ex(key.clone(), true) {759 return None;760 }761762 Some(Thunk::new(ObjFieldThunk {763 obj: self.clone(),764 key,765 }))766 }767 pub fn get_lazy_or_bail(&self, key: IStr) -> Thunk<Val> {768 #[derive(Trace)]769 struct ObjFieldThunk {770 obj: ObjValue,771 key: IStr,772 }773 impl ThunkValue for ObjFieldThunk {774 type Output = Val;775776 fn get(&self) -> Result<Self::Output> {777 self.obj.get_or_bail(self.key.clone())778 }779 }780781 Thunk::new(ObjFieldThunk {782 obj: self.clone(),783 key,784 })785 }786 pub fn ptr_eq(a: &Self, b: &Self) -> bool {787 Cc::ptr_eq(&a.0, &b.0)788 }789 pub fn downgrade(self) -> WeakObjValue {790 WeakObjValue(self.0.downgrade())791 }792}793794#[derive(Debug)]795struct FieldVisibilityData {796 omitted_until: Saturating<usize>,797 exists_visible: Option<Visibility>,798 #[cfg(feature = "exp-preserve-order")]799 key: FieldSortKey,800}801impl FieldVisibilityData {802 fn visible(&self) -> bool {803 self.exists_visible804 .expect("non-existing fields shall be dropped at the end of fn fields_visibility()")805 .is_visible()806 }807 #[cfg(feature = "exp-preserve-order")]808 fn sort_key(&self) -> FieldSortKey {809 self.key810 }811}812813impl ObjValue {814 fn fields_visibility(&self) -> FxHashMap<IStr, FieldVisibilityData> {815 let mut out = FxHashMap::default();816817 let mut super_depth = SuperDepth::default();818 let mut omit_index = Saturating(0);819 for core in self.0.cores.iter().rev() {820 core.0821 .enum_fields_core(&mut super_depth, &mut |_depth, _index, name, visibility| {822 let entry = out.entry(name);823 let data = entry.or_insert(FieldVisibilityData {824 exists_visible: None,825 #[cfg(feature = "exp-preserve-order")]826 key: FieldSortKey::new(_depth, _index),827 omitted_until: omit_index,828 });829 match visibility {830 EnumFields::Omit(new_skip) => {831 // +1 including this core832 data.omitted_until = data833 .omitted_until834 .max(omit_index + new_skip + Saturating(1));835 }836 EnumFields::Normal(Visibility::Normal) => {837 if data.omitted_until <= omit_index && data.exists_visible.is_none() {838 data.exists_visible = Some(Visibility::Normal);839 }840 }841 EnumFields::Normal(Visibility::Hidden) => {842 if data.omitted_until <= omit_index {843 data.exists_visible = Some(match data.exists_visible {844 // We're iterating in reverse, later unhide is preserved845 Some(Visibility::Unhide) => Visibility::Unhide,846 _ => Visibility::Hidden,847 });848 }849 }850 EnumFields::Normal(Visibility::Unhide) => {851 if data.omitted_until <= omit_index {852 data.exists_visible = Some(match data.exists_visible {853 // We're iterating in reverse, later hide is preserved854 Some(Visibility::Hidden) => Visibility::Hidden,855 _ => Visibility::Unhide,856 });857 }858 }859 }860 ControlFlow::Continue(())861 });862863 super_depth.deepen();864 omit_index += 1;865 }866867 out.retain(|_, v| v.exists_visible.is_some());868869 out870 }871 pub fn fields_ex(872 &self,873 include_hidden: bool,874 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,875 ) -> Vec<IStr> {876 #[cfg(feature = "exp-preserve-order")]877 if preserve_order {878 let (mut fields, mut keys): (Vec<_>, Vec<_>) = self879 .fields_visibility()880 .into_iter()881 .filter(|(_, d)| include_hidden || d.visible())882 .enumerate()883 .map(|(idx, (k, d))| (k, (d.sort_key(), idx)))884 .unzip();885 keys.sort_unstable_by_key(|v| v.0);886 // Reorder in-place by resulting indexes887 for i in 0..fields.len() {888 let x = fields[i].clone();889 let mut j = i;890 loop {891 let k = keys[j].1;892 keys[j].1 = j;893 if k == i {894 break;895 }896 fields[j] = fields[k].clone();897 j = k;898 }899 fields[j] = x;900 }901 return fields;902 }903904 let mut fields: Vec<_> = self905 .fields_visibility()906 .into_iter()907 .filter(|(_, d)| include_hidden || d.visible())908 .map(|(k, _)| k)909 .collect();910 fields.sort_unstable();911 fields912 }913 pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {914 self.fields_ex(915 false,916 #[cfg(feature = "exp-preserve-order")]917 preserve_order,918 )919 }920 pub fn values_ex(921 &self,922 include_hidden: bool,923 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,924 ) -> ArrValue {925 ArrValue::new(PickObjectValues::new(926 self.clone(),927 self.fields_ex(928 include_hidden,929 #[cfg(feature = "exp-preserve-order")]930 preserve_order,931 ),932 ))933 }934 pub fn values(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> ArrValue {935 self.values_ex(936 false,937 #[cfg(feature = "exp-preserve-order")]938 preserve_order,939 )940 }941 pub fn key_values_ex(942 &self,943 include_hidden: bool,944 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,945 ) -> ArrValue {946 ArrValue::new(PickObjectKeyValues::new(947 self.clone(),948 self.fields_ex(949 include_hidden,950 #[cfg(feature = "exp-preserve-order")]951 preserve_order,952 ),953 ))954 }955 pub fn key_values(956 &self,957 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,958 ) -> ArrValue {959 self.key_values_ex(960 false,961 #[cfg(feature = "exp-preserve-order")]962 preserve_order,963 )964 }965}966967#[allow(clippy::module_name_repetitions)]968#[must_use = "value not added unless binding() was called"]969pub struct ObjMemberBuilder<Kind> {970 kind: Kind,971 name: IStr,972 add: bool,973 visibility: Visibility,974 original_index: FieldIndex,975 location: Option<Span>,976}977978#[allow(clippy::missing_const_for_fn)]979impl<Kind> ObjMemberBuilder<Kind> {980 pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {981 Self {982 kind,983 name,984 original_index,985 add: false,986 visibility: Visibility::Normal,987 location: None,988 }989 }990991 pub const fn with_add(mut self, add: bool) -> Self {992 self.add = add;993 self994 }995 pub fn add(self) -> Self {996 self.with_add(true)997 }998 pub fn with_visibility(mut self, visibility: Visibility) -> Self {999 self.visibility = visibility;1000 self1001 }1002 pub fn hide(self) -> Self {1003 self.with_visibility(Visibility::Hidden)1004 }1005 pub fn with_location(mut self, location: Span) -> Self {1006 self.location = Some(location);1007 self1008 }1009 fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {1010 (1011 self.kind,1012 self.name,1013 ObjMember {1014 flags: ObjFieldFlags::new(self.add, self.visibility),1015 original_index: self.original_index,1016 invoke: binding,1017 location: self.location,1018 },1019 )1020 }1021}10221023pub struct ExtendBuilder<'v>(&'v mut ObjValue);1024impl ObjMemberBuilder<ExtendBuilder<'_>> {1025 pub fn value(self, value: impl Into<Val>) {1026 self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));1027 }1028 pub fn bindable(self, bindable: impl Unbound<Bound = Val>) {1029 self.binding(MaybeUnbound::Unbound(CcUnbound::new(bindable)));1030 }1031 pub fn binding(self, binding: MaybeUnbound) {1032 let (receiver, name, member) = self.build_member(binding);1033 let new = receiver.0.clone();1034 *receiver.0 = new.extend_with_raw_member(name, member);1035 }1036}crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -21,6 +21,8 @@
mod inner;
use inner::Inner;
+mod names;
+
/// Interned string
///
/// Provides O(1) comparsions and hashing, cheap copy, and cheap conversion to [`IBytes`]
crates/jrsonnet-interner/src/names.rsdiffbeforeafterbothno changes
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -13,6 +13,11 @@
LitStr, Meta, Pat, Path, PathArguments, Result, ReturnType, Token, Type,
};
+use self::typed::derive_typed_inner;
+
+mod typed;
+mod names;
+
fn try_parse_attr_noargs<I>(attrs: &[Attribute], ident: I) -> Result<bool>
where
Ident: PartialEq<I>,
@@ -435,278 +440,6 @@
}
};
})
-}
-
-#[derive(Default)]
-#[allow(clippy::struct_excessive_bools)]
-struct TypedAttr {
- rename: Option<String>,
- aliases: Vec<String>,
- flatten: bool,
- /// flatten(ok) strategy for flattened optionals
- /// field would be None in case of any parsing error (as in serde)
- flatten_ok: bool,
- // Should it be `field+:` instead of `field:`
- add: bool,
- // Should it be `field::` instead of `field:`
- hide: bool,
-}
-impl Parse for TypedAttr {
- fn parse(input: ParseStream) -> syn::Result<Self> {
- let mut out = Self::default();
- loop {
- let lookahead = input.lookahead1();
- if lookahead.peek(kw::rename) {
- input.parse::<kw::rename>()?;
- input.parse::<Token![=]>()?;
- let name = input.parse::<LitStr>()?;
- if out.rename.is_some() {
- return Err(Error::new(
- name.span(),
- "rename attribute may only be specified once",
- ));
- }
- out.rename = Some(name.value());
- } else if lookahead.peek(kw::alias) {
- input.parse::<kw::alias>()?;
- input.parse::<Token![=]>()?;
- let alias = input.parse::<LitStr>()?;
- out.aliases.push(alias.value());
- } else if lookahead.peek(kw::flatten) {
- input.parse::<kw::flatten>()?;
- out.flatten = true;
- if input.peek(token::Paren) {
- let content;
- parenthesized!(content in input);
- let lookahead = content.lookahead1();
- if lookahead.peek(kw::ok) {
- content.parse::<kw::ok>()?;
- out.flatten_ok = true;
- } else {
- return Err(lookahead.error());
- }
- }
- } else if lookahead.peek(kw::add) {
- input.parse::<kw::add>()?;
- out.add = true;
- } else if lookahead.peek(kw::hide) {
- input.parse::<kw::hide>()?;
- out.hide = true;
- } else if input.is_empty() {
- break;
- } else {
- return Err(lookahead.error());
- }
- if input.peek(Token![,]) {
- input.parse::<Token![,]>()?;
- } else {
- break;
- }
- }
- Ok(out)
- }
-}
-
-struct TypedField {
- attr: TypedAttr,
- ident: Ident,
- ty: Type,
- is_option: bool,
- is_lazy: bool,
-}
-impl TypedField {
- fn parse(field: &syn::Field) -> Result<Self> {
- let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();
- let Some(ident) = field.ident.clone() else {
- return Err(Error::new(
- field.span(),
- "this field should appear in output object, but it has no visible name",
- ));
- };
- let (is_option, ty) = extract_type_from_option(&field.ty)?
- .map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));
- if is_option && attr.flatten {
- if !attr.flatten_ok {
- return Err(Error::new(
- field.span(),
- "strategy should be set when flattening Option",
- ));
- }
- } else if attr.flatten_ok {
- return Err(Error::new(
- field.span(),
- "flatten(ok) is only useable on optional fields",
- ));
- }
-
- let is_lazy = type_is_path(&ty, "Thunk").is_some();
-
- Ok(Self {
- attr,
- ident,
- ty,
- is_option,
- is_lazy,
- })
- }
- /// None if this field is flattened in jsonnet output
- fn name(&self) -> Option<String> {
- if self.attr.flatten {
- return None;
- }
- Some(
- self.attr
- .rename
- .clone()
- .unwrap_or_else(|| self.ident.to_string()),
- )
- }
-
- fn expand_field(&self) -> Option<TokenStream> {
- if self.is_option {
- return None;
- }
- let name = self.name()?;
- let ty = &self.ty;
- Some(quote! {
- (#name, <#ty as Typed>::TYPE)
- })
- }
-
- fn expand_parse(&self) -> TokenStream {
- if self.is_option {
- self.expand_parse_optional()
- } else {
- self.expand_parse_mandatory()
- }
- }
-
- fn expand_parse_optional(&self) -> TokenStream {
- let ident = &self.ident;
- let ty = &self.ty;
-
- // optional flatten is handled in same way as serde
- if self.attr.flatten {
- return quote! {
- #ident: <#ty as TypedObj>::parse(&obj).ok(),
- };
- }
-
- let name = self.name().unwrap();
- let aliases = &self.attr.aliases;
-
- quote! {
- #ident: {
- let __value = if let Some(__v) = obj.get(#name.into())? {
- Some(__v)
- } #(else if let Some(__v) = obj.get(#aliases.into())? {
- Some(__v)
- })* else {
- None
- };
-
- __value.map(<#ty as Typed>::from_untyped).transpose()?
- },
- }
- }
-
- fn expand_parse_mandatory(&self) -> TokenStream {
- let ident = &self.ident;
- let ty = &self.ty;
-
- // optional flatten is handled in same way as serde
- if self.attr.flatten {
- return quote! {
- #ident: <#ty as TypedObj>::parse(&obj)?,
- };
- }
-
- let name = self.name().unwrap();
- let aliases = &self.attr.aliases;
-
- let error_text = if aliases.is_empty() {
- // clippy does not understand name variable usage in quote! macro
- #[allow(clippy::redundant_clone)]
- name.clone()
- } else {
- format!("{name} (alias {})", aliases.join(", "))
- };
-
- quote! {
- #ident: {
- let __value = if let Some(__v) = obj.get(#name.into())? {
- __v
- } #(else if let Some(__v) = obj.get(#aliases.into())? {
- __v
- })* else {
- return Err(ErrorKind::NoSuchField(#error_text.into(), vec![]).into());
- };
-
- <#ty as Typed>::from_untyped(__value)?
- },
- }
- }
-
- fn expand_serialize(&self) -> TokenStream {
- let ident = &self.ident;
- let ty = &self.ty;
- self.name().map_or_else(
- || {
- if self.is_option {
- quote! {
- if let Some(value) = self.#ident {
- <#ty as TypedObj>::serialize(value, out)?;
- }
- }
- } else {
- quote! {
- <#ty as TypedObj>::serialize(self.#ident, out)?;
- }
- }
- },
- |name| {
- let hide = if self.attr.hide {
- quote! {.hide()}
- } else {
- quote! {}
- };
- let add = if self.attr.add {
- quote! {.add()}
- } else {
- quote! {}
- };
- let value = if self.is_lazy {
- quote! {
- out.field(#name)
- #hide
- #add
- .try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;
- }
- } else {
- quote! {
- out.field(#name)
- #hide
- #add
- .try_value(<#ty as Typed>::into_untyped(value)?)?;
- }
- };
- if self.is_option {
- quote! {
- if let Some(value) = self.#ident {
- #value
- }
- }
- } else {
- quote! {
- {
- let value = self.#ident;
- #value
- }
- }
- }
- },
- )
- }
}
#[proc_macro_derive(Typed, attributes(typed))]
@@ -717,79 +450,6 @@
Ok(v) => v.into(),
Err(e) => e.to_compile_error().into(),
}
-}
-
-fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {
- let syn::Data::Struct(data) = &input.data else {
- return Err(Error::new(input.span(), "only structs supported"));
- };
-
- let ident = &input.ident;
- let fields = data
- .fields
- .iter()
- .map(TypedField::parse)
- .collect::<Result<Vec<_>>>()?;
-
- let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
-
- let typed = {
- let fields = fields
- .iter()
- .filter_map(TypedField::expand_field)
- .collect::<Vec<_>>();
- quote! {
- impl #impl_generics Typed for #ident #ty_generics #where_clause {
- const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[
- #(#fields,)*
- ]);
-
- fn from_untyped(value: Val) -> JrResult<Self> {
- let obj = value.as_obj().expect("shape is correct");
- Self::parse(&obj)
- }
-
- fn into_untyped(value: Self) -> JrResult<Val> {
- let mut out = ObjValueBuilder::new();
- value.serialize(&mut out)?;
- Ok(Val::Obj(out.build()))
- }
-
- }
- }
- };
-
- let fields_parse = fields.iter().map(TypedField::expand_parse);
- let fields_serialize = fields
- .iter()
- .map(TypedField::expand_serialize)
- .collect::<Vec<_>>();
-
- Ok(quote! {
- const _: () = {
- use ::jrsonnet_evaluator::{
- typed::{ComplexValType, Typed, TypedObj, CheckType},
- Val, State,
- error::{ErrorKind, Result as JrResult},
- ObjValueBuilder, ObjValue,
- };
-
- #typed
-
- impl #impl_generics TypedObj for #ident #ty_generics #where_clause {
- fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {
- #(#fields_serialize)*
-
- Ok(())
- }
- fn parse(obj: &ObjValue) -> JrResult<Self> {
- Ok(Self {
- #(#fields_parse)*
- })
- }
- }
- };
- })
}
struct FormatInput {
crates/jrsonnet-macros/src/names.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-macros/src/names.rs
@@ -0,0 +1,32 @@
+use proc_macro2::TokenStream;
+use quote::quote;
+use std::cell::RefCell;
+
+#[derive(Default)]
+pub struct Names {
+ names: Vec<String>,
+}
+
+impl Names {
+ pub fn intern(&mut self, s: impl AsRef<str>) -> usize {
+ let s = s.as_ref();
+ if let Some(pos) = self.names.iter().position(|v| v == s) {
+ return pos;
+ }
+ let pos = self.names.len();
+ self.names.push(s.to_owned());
+ pos
+ }
+
+ pub fn expand(&self) -> TokenStream {
+ let len = self.names.len();
+ let name = self.names.iter();
+ quote! {
+ thread_local! {
+ static NAMES: [::jrsonnet_evaluator::IStr; #len] = [
+ #(::jrsonnet_evaluator::IStr::from(#name),)*
+ ];
+ }
+ }
+ }
+}
crates/jrsonnet-macros/src/typed.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-macros/src/typed.rs
@@ -0,0 +1,373 @@
+use crate::names::Names;
+use crate::{extract_type_from_option, kw, parse_attr, type_is_path};
+use proc_macro2::TokenStream;
+use quote::quote;
+use syn::parse::{Parse, ParseStream};
+use syn::spanned::Spanned as _;
+use syn::{parenthesized, token, DeriveInput, Error, Ident, LitStr, Result, Token, Type};
+
+#[derive(Default)]
+#[allow(clippy::struct_excessive_bools)]
+struct TypedAttr {
+ rename: Option<String>,
+ aliases: Vec<String>,
+ flatten: bool,
+ /// flatten(ok) strategy for flattened optionals
+ /// field would be None in case of any parsing error (as in serde)
+ flatten_ok: bool,
+ // Should it be `field+:` instead of `field:`
+ add: bool,
+ // Should it be `field::` instead of `field:`
+ hide: bool,
+}
+impl Parse for TypedAttr {
+ fn parse(input: ParseStream) -> syn::Result<Self> {
+ let mut out = Self::default();
+ loop {
+ let lookahead = input.lookahead1();
+ if lookahead.peek(kw::rename) {
+ input.parse::<kw::rename>()?;
+ input.parse::<Token![=]>()?;
+ let name = input.parse::<LitStr>()?;
+ if out.rename.is_some() {
+ return Err(Error::new(
+ name.span(),
+ "rename attribute may only be specified once",
+ ));
+ }
+ out.rename = Some(name.value());
+ } else if lookahead.peek(kw::alias) {
+ input.parse::<kw::alias>()?;
+ input.parse::<Token![=]>()?;
+ let alias = input.parse::<LitStr>()?;
+ out.aliases.push(alias.value());
+ } else if lookahead.peek(kw::flatten) {
+ input.parse::<kw::flatten>()?;
+ out.flatten = true;
+ if input.peek(token::Paren) {
+ let content;
+ parenthesized!(content in input);
+ let lookahead = content.lookahead1();
+ if lookahead.peek(kw::ok) {
+ content.parse::<kw::ok>()?;
+ out.flatten_ok = true;
+ } else {
+ return Err(lookahead.error());
+ }
+ }
+ } else if lookahead.peek(kw::add) {
+ input.parse::<kw::add>()?;
+ out.add = true;
+ } else if lookahead.peek(kw::hide) {
+ input.parse::<kw::hide>()?;
+ out.hide = true;
+ } else if input.is_empty() {
+ break;
+ } else {
+ return Err(lookahead.error());
+ }
+ if input.peek(Token![,]) {
+ input.parse::<Token![,]>()?;
+ } else {
+ break;
+ }
+ }
+ Ok(out)
+ }
+}
+struct TypedField {
+ attr: TypedAttr,
+ ident: Ident,
+ ty: Type,
+ is_option: bool,
+ is_lazy: bool,
+}
+impl TypedField {
+ fn parse(field: &syn::Field) -> Result<Self> {
+ let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();
+ let Some(ident) = field.ident.clone() else {
+ return Err(Error::new(
+ field.span(),
+ "this field should appear in output object, but it has no visible name",
+ ));
+ };
+ let (is_option, ty) = extract_type_from_option(&field.ty)?
+ .map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));
+ if is_option && attr.flatten {
+ if !attr.flatten_ok {
+ return Err(Error::new(
+ field.span(),
+ "strategy should be set when flattening Option",
+ ));
+ }
+ } else if attr.flatten_ok {
+ return Err(Error::new(
+ field.span(),
+ "flatten(ok) is only useable on optional fields",
+ ));
+ }
+
+ let is_lazy = type_is_path(&ty, "Thunk").is_some();
+
+ Ok(Self {
+ attr,
+ ident,
+ ty,
+ is_option,
+ is_lazy,
+ })
+ }
+ /// None if this field is flattened in jsonnet output
+ fn name(&self) -> Option<String> {
+ if self.attr.flatten {
+ return None;
+ }
+ Some(
+ self.attr
+ .rename
+ .clone()
+ .unwrap_or_else(|| self.ident.to_string()),
+ )
+ }
+
+ fn expand_field(&self) -> Option<TokenStream> {
+ if self.is_option {
+ return None;
+ }
+ let name = self.name()?;
+ let ty = &self.ty;
+ Some(quote! {
+ (#name, <#ty as Typed>::TYPE)
+ })
+ }
+
+ fn expand_parse(&self, names: &mut Names) -> TokenStream {
+ if self.is_option {
+ self.expand_parse_optional(names)
+ } else {
+ self.expand_parse_mandatory(names)
+ }
+ }
+
+ fn expand_parse_optional(&self, names: &mut Names) -> TokenStream {
+ let ident = &self.ident;
+ let ty = &self.ty;
+
+ // optional flatten is handled in same way as serde
+ if self.attr.flatten {
+ return quote! {
+ #ident: <#ty as TypedObj>::parse(&obj).ok(),
+ };
+ }
+
+ let name = names.intern(self.name().unwrap());
+ let aliases = self
+ .attr
+ .aliases
+ .iter()
+ .map(|name| names.intern(name))
+ .collect::<Vec<_>>();
+
+ quote! {
+ #ident: {
+ let __value = if let Some(__v) = obj.get(__names[#name].clone())? {
+ Some(__v)
+ } #(else if let Some(__v) = obj.get(__names[#aliases].clone())? {
+ Some(__v)
+ })* else {
+ None
+ };
+
+ __value.map(<#ty as Typed>::from_untyped).transpose()?
+ },
+ }
+ }
+
+ fn expand_parse_mandatory(&self, names: &mut Names) -> TokenStream {
+ let ident = &self.ident;
+ let ty = &self.ty;
+
+ // optional flatten is handled in same way as serde
+ if self.attr.flatten {
+ return quote! {
+ #ident: <#ty as TypedObj>::parse(&obj)?,
+ };
+ }
+
+ let name = self.name().unwrap();
+ let aliases = &self.attr.aliases;
+
+ let error_text = if aliases.is_empty() {
+ // clippy does not understand name variable usage in quote! macro
+ #[allow(clippy::redundant_clone)]
+ name.clone()
+ } else {
+ format!("{name} (alias {})", aliases.join(", "))
+ };
+
+ let error_text = names.intern(error_text);
+ let name = names.intern(name);
+ let aliases = aliases.iter().map(|alias| names.intern(alias));
+
+ quote! {
+ #ident: {
+ let __value = if let Some(__v) = obj.get(__names[#name].clone())? {
+ __v
+ } #(else if let Some(__v) = obj.get(__names[#aliases].clone())? {
+ __v
+ })* else {
+ return Err(ErrorKind::NoSuchField(__names[#error_text].clone(), vec![]).into());
+ };
+
+ <#ty as Typed>::from_untyped(__value)?
+ },
+ }
+ }
+
+ fn expand_serialize(&self, names: &mut Names) -> TokenStream {
+ let ident = &self.ident;
+ let ty = &self.ty;
+ self.name().map_or_else(
+ || {
+ if self.is_option {
+ quote! {
+ if let Some(value) = self.#ident {
+ <#ty as TypedObj>::serialize(value, out)?;
+ }
+ }
+ } else {
+ quote! {
+ <#ty as TypedObj>::serialize(self.#ident, out)?;
+ }
+ }
+ },
+ |name| {
+ let name = names.intern(name);
+ let hide = if self.attr.hide {
+ quote! {.hide()}
+ } else {
+ quote! {}
+ };
+ let add = if self.attr.add {
+ quote! {.add()}
+ } else {
+ quote! {}
+ };
+ let value = if self.is_lazy {
+ quote! {
+ out.field(__names[#name].clone())
+ #hide
+ #add
+ .try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;
+ }
+ } else {
+ quote! {
+ out.field(__names[#name].clone())
+ #hide
+ #add
+ .try_value(<#ty as Typed>::into_untyped(value)?)?;
+ }
+ };
+ if self.is_option {
+ quote! {
+ if let Some(value) = self.#ident {
+ #value
+ }
+ }
+ } else {
+ quote! {
+ {
+ let value = self.#ident;
+ #value
+ }
+ }
+ }
+ },
+ )
+ }
+}
+
+pub fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {
+ let syn::Data::Struct(data) = &input.data else {
+ return Err(Error::new(input.span(), "only structs supported"));
+ };
+
+ let ident = &input.ident;
+ let fields = data
+ .fields
+ .iter()
+ .map(TypedField::parse)
+ .collect::<Result<Vec<_>>>()?;
+
+ let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
+
+ let capacity = fields.len();
+
+ let typed = {
+ let fields = fields
+ .iter()
+ .filter_map(TypedField::expand_field)
+ .collect::<Vec<_>>();
+ quote! {
+ impl #impl_generics Typed for #ident #ty_generics #where_clause {
+ const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[
+ #(#fields,)*
+ ]);
+
+ fn from_untyped(value: Val) -> JrResult<Self> {
+ let obj = value.as_obj().expect("shape is correct");
+ Self::parse(&obj)
+ }
+
+ fn into_untyped(value: Self) -> JrResult<Val> {
+ let mut out = ObjValueBuilder::with_capacity(#capacity);
+ value.serialize(&mut out)?;
+ Ok(Val::Obj(out.build()))
+ }
+
+ }
+ }
+ };
+
+ let mut names = Names::default();
+
+ let fields_parse = fields
+ .iter()
+ .map(|f| f.expand_parse(&mut names))
+ .collect::<Vec<_>>();
+ let fields_serialize = fields
+ .iter()
+ .map(|f| f.expand_serialize(&mut names))
+ .collect::<Vec<_>>();
+
+ let names_expanded = names.expand();
+ Ok(quote! {
+ const _: () = {
+ use ::jrsonnet_evaluator::{
+ typed::{ComplexValType, Typed, TypedObj, CheckType},
+ Val, State,
+ error::{ErrorKind, Result as JrResult},
+ ObjValueBuilder, ObjValue, IStr,
+ };
+
+ #typed
+
+ #names_expanded
+
+ impl #impl_generics TypedObj for #ident #ty_generics #where_clause {
+ fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {
+ NAMES.with(|__names| {
+ #(#fields_serialize)*
+
+ Ok(())
+ })
+ }
+ fn parse(obj: &ObjValue) -> JrResult<Self> {
+ NAMES.with(|__names| Ok(Self {
+ #(#fields_parse)*
+ }))
+ }
+ }
+ };
+ })
+}
crates/jrsonnet-stdlib/src/regex.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/regex.rs
+++ b/crates/jrsonnet-stdlib/src/regex.rs
@@ -4,8 +4,9 @@
use jrsonnet_evaluator::{
error::{ErrorKind::*, Result},
rustc_hash::FxBuildHasher,
+ typed::Typed,
val::StrValue,
- IStr, ObjValueBuilder, Val,
+ IStr, ObjValue, ObjValueBuilder,
};
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_macros::builtin;
@@ -20,7 +21,7 @@
Self {
cache: RefCell::new(LruCache::with_hasher(
NonZeroUsize::new(20).unwrap(),
- FxBuildHasher::default(),
+ FxBuildHasher,
)),
}
}
@@ -40,21 +41,27 @@
}
}
-pub fn regex_match_inner(regex: &Regex, str: String) -> Result<Val> {
- let mut out = ObjValueBuilder::with_capacity(3);
+#[derive(Typed)]
+pub struct RegexMatch {
+ string: IStr,
+ captures: Vec<IStr>,
+ #[typed(rename = "namedCaptures")]
+ named_captures: ObjValue,
+}
+fn regex_match_inner(regex: &Regex, str: String) -> Result<Option<RegexMatch>> {
let mut captures = Vec::with_capacity(regex.captures_len());
let mut named_captures = ObjValueBuilder::with_capacity(regex.capture_names().len());
let Some(captured) = regex.captures(&str) else {
- return Ok(Val::Null);
+ return Ok(None);
};
for ele in captured.iter().skip(1) {
if let Some(ele) = ele {
- captures.push(Val::Str(StrValue::Flat(ele.as_str().into())));
+ captures.push(ele.as_str().into());
} else {
- captures.push(Val::Str(StrValue::Flat(IStr::empty())));
+ captures.push(IStr::empty());
}
}
for (i, name) in regex
@@ -67,13 +74,11 @@
named_captures.field(name).try_value(capture)?;
}
- out.field("string")
- .value(Val::Str(captured.get(0).unwrap().as_str().into()));
- out.field("captures").value(Val::Arr(captures.into()));
- out.field("namedCaptures")
- .value(Val::Obj(named_captures.build()));
-
- Ok(Val::Obj(out.build()))
+ Ok(Some(RegexMatch {
+ string: captured.get(0).expect("regex matched").as_str().into(),
+ named_captures: named_captures.build(),
+ captures,
+ }))
}
#[builtin(fields(
@@ -83,7 +88,7 @@
this: &builtin_regex_partial_match,
pattern: IStr,
str: String,
-) -> Result<Val> {
+) -> Result<Option<RegexMatch>> {
let regex = this.cache.parse(pattern)?;
regex_match_inner(®ex, str)
}
@@ -95,7 +100,7 @@
this: &builtin_regex_full_match,
pattern: StrValue,
str: String,
-) -> Result<Val> {
+) -> Result<Option<RegexMatch>> {
let pattern = format!("^{pattern}$").into();
let regex = this.cache.parse(pattern)?;
regex_match_inner(®ex, str)