1use crate::{evaluate_add_op, LazyBinding, Result, Val};2use indexmap::IndexMap;3use jrsonnet_parser::{ExprLocation, Visibility};4use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};56#[derive(Debug)]7pub struct ObjMember {8 pub add: bool,9 pub visibility: Visibility,10 pub invoke: LazyBinding,11}1213#[derive(Debug)]14pub struct ObjValueInternals {15 super_obj: Option<ObjValue>,16 this_entries: Rc<HashMap<Rc<str>, ObjMember>>,17 value_cache: RefCell<HashMap<Rc<str>, Val>>,18}19#[derive(Clone)]20pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);21impl Debug for ObjValue {22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {23 if let Some(super_obj) = self.0.super_obj.as_ref() {24 if f.alternate() {25 write!(f, "{:#?}", super_obj)?;26 } else {27 write!(f, "{:?}", super_obj)?;28 }29 write!(f, " + ")?;30 }31 let mut debug = f.debug_struct("ObjValue");32 for (name, member) in self.0.this_entries.iter() {33 debug.field(name, member);34 }35 debug.finish_non_exhaustive()36 }37}3839impl ObjValue {40 pub fn new(41 super_obj: Option<ObjValue>,42 this_entries: Rc<HashMap<Rc<str>, ObjMember>>,43 ) -> ObjValue {44 ObjValue(Rc::new(ObjValueInternals {45 super_obj,46 this_entries,47 value_cache: RefCell::new(HashMap::new()),48 }))49 }50 pub fn new_empty() -> ObjValue {51 Self::new(None, Rc::new(HashMap::new()))52 }53 pub fn with_super(&self, super_obj: ObjValue) -> ObjValue {54 match &self.0.super_obj {55 None => ObjValue::new(Some(super_obj), self.0.this_entries.clone()),56 Some(v) => ObjValue::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),57 }58 }59 pub fn enum_fields(&self, handler: &impl Fn(&Rc<str>, &Visibility)) {60 if let Some(s) = &self.0.super_obj {61 s.enum_fields(handler);62 }63 for (name, member) in self.0.this_entries.iter() {64 handler(&name, &member.visibility);65 }66 }67 pub fn fields_visibility(&self) -> IndexMap<Rc<str>, bool> {68 let out = Rc::new(RefCell::new(IndexMap::new()));69 self.enum_fields(&|name, visibility| {70 let mut out = out.borrow_mut();71 match visibility {72 Visibility::Normal => {73 if !out.contains_key(name) {74 out.insert(name.to_owned(), true);75 }76 }77 Visibility::Hidden => {78 out.insert(name.to_owned(), false);79 }80 Visibility::Unhide => {81 out.insert(name.to_owned(), true);82 }83 };84 });85 Rc::try_unwrap(out).unwrap().into_inner()86 }87 pub fn visible_fields(&self) -> Vec<Rc<str>> {88 self.fields_visibility()89 .into_iter()90 .filter(|(_k, v)| *v)91 .map(|(k, _)| k)92 .collect()93 }94 pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {95 if let Some(v) = self.0.value_cache.borrow().get(&key) {96 return Ok(Some(v.clone()));97 }98 if let Some(v) = self.get_raw(&key, self)? {99 let v = v.unwrap_if_lazy()?;100 self.0.value_cache.borrow_mut().insert(key, v.clone());101 Ok(Some(v))102 } else {103 Ok(None)104 }105 }106 pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {107 match (self.0.this_entries.get(key), &self.0.super_obj) {108 (Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),109 (Some(k), Some(s)) => {110 let our = self.evaluate_this(k, real_this)?;111 if k.add {112 s.get_raw(key, real_this)?113 .map_or(Ok(Some(our.clone())), |v| {114 Ok(Some(evaluate_add_op(&v, &our)?))115 })116 } else {117 Ok(Some(our))118 }119 }120 (None, Some(s)) => s.get_raw(key, real_this),121 (None, None) => Ok(None),122 }123 }124 fn evaluate_this(&self, v: &ObjMember, real_this: &ObjValue) -> Result<Val> {125 Ok(v.invoke126 .evaluate(Some(real_this.clone()), self.0.super_obj.clone())?127 .evaluate()?)128 }129}130impl PartialEq for ObjValue {131 fn eq(&self, other: &Self) -> bool {132 Rc::ptr_eq(&self.0, &other.0)133 }134}