1use crate::{2 builtin::{3 call_builtin,4 manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},5 },6 error::{Error::*, LocError},7 evaluate,8 function::{parse_function_call, parse_function_call_map, place_args},9 native::NativeCallback,10 throw, with_state, Context, ObjValue, Result,11};12use jrsonnet_gc::{Gc, GcCell, Trace};13use jrsonnet_interner::IStr;14use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};15use jrsonnet_types::ValType;16use std::{collections::HashMap, fmt::Debug, rc::Rc};1718pub trait LazyValValue: Trace {19 fn get(self: Box<Self>) -> Result<Val>;20}2122#[derive(Trace)]23#[trivially_drop]24enum LazyValInternals {25 Computed(Val),26 Errored(LocError),27 Waiting(Box<dyn LazyValValue>),28 Pending,29}3031#[derive(Clone, Trace)]32#[trivially_drop]33pub struct LazyVal(Gc<GcCell<LazyValInternals>>);34impl LazyVal {35 pub fn new(f: Box<dyn LazyValValue>) -> Self {36 Self(Gc::new(GcCell::new(LazyValInternals::Waiting(f))))37 }38 pub fn new_resolved(val: Val) -> Self {39 Self(Gc::new(GcCell::new(LazyValInternals::Computed(val))))40 }41 pub fn evaluate(&self) -> Result<Val> {42 match &*self.0.borrow() {43 LazyValInternals::Computed(v) => return Ok(v.clone()),44 LazyValInternals::Errored(e) => return Err(e.clone()),45 LazyValInternals::Pending => return Err(RecursiveLazyValueEvaluation.into()),46 _ => (),47 };48 let value = if let LazyValInternals::Waiting(value) =49 std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)50 {51 value52 } else {53 unreachable!()54 };55 let new_value = match value.get() {56 Ok(v) => v,57 Err(e) => {58 *self.0.borrow_mut() = LazyValInternals::Errored(e.clone());59 return Err(e);60 }61 };62 *self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());63 Ok(new_value)64 }65}6667impl Debug for LazyVal {68 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {69 write!(f, "Lazy")70 }71}72impl PartialEq for LazyVal {73 fn eq(&self, other: &Self) -> bool {74 Gc::ptr_eq(&self.0, &other.0)75 }76}7778#[derive(Debug, PartialEq, Trace)]79#[trivially_drop]80pub struct FuncDesc {81 pub name: IStr,82 pub ctx: Context,83 pub params: ParamsDesc,84 pub body: LocExpr,85}8687#[derive(Debug, Trace)]88#[trivially_drop]89pub enum FuncVal {90 91 Normal(FuncDesc),92 93 Intrinsic(IStr),94 95 NativeExt(IStr, Gc<NativeCallback>),96}9798impl PartialEq for FuncVal {99 fn eq(&self, other: &Self) -> bool {100 match (self, other) {101 (Self::Normal(a), Self::Normal(b)) => a == b,102 (Self::Intrinsic(an), Self::Intrinsic(bn)) => an == bn,103 (Self::NativeExt(an, _), Self::NativeExt(bn, _)) => an == bn,104 (..) => false,105 }106 }107}108impl FuncVal {109 pub fn is_ident(&self) -> bool {110 matches!(&self, Self::Intrinsic(n) if n as &str == "id")111 }112 pub fn name(&self) -> IStr {113 match self {114 Self::Normal(normal) => normal.name.clone(),115 Self::Intrinsic(name) => format!("std.{}", name).into(),116 Self::NativeExt(n, _) => format!("native.{}", n).into(),117 }118 }119 pub fn evaluate(120 &self,121 call_ctx: Context,122 loc: Option<&ExprLocation>,123 args: &ArgsDesc,124 tailstrict: bool,125 ) -> Result<Val> {126 match self {127 Self::Normal(func) => {128 let ctx = parse_function_call(129 call_ctx,130 Some(func.ctx.clone()),131 &func.params,132 args,133 tailstrict,134 )?;135 evaluate(ctx, &func.body)136 }137 Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),138 Self::NativeExt(_name, handler) => {139 let args = parse_function_call(call_ctx, None, &handler.params, args, true)?;140 let mut out_args = Vec::with_capacity(handler.params.len());141 for p in handler.params.0.iter() {142 out_args.push(args.binding(p.0.clone())?.evaluate()?);143 }144 Ok(handler.call(loc.map(|l| l.0.clone()), &out_args)?)145 }146 }147 }148149 pub fn evaluate_map(150 &self,151 call_ctx: Context,152 args: &HashMap<IStr, Val>,153 tailstrict: bool,154 ) -> Result<Val> {155 match self {156 Self::Normal(func) => {157 let ctx = parse_function_call_map(158 call_ctx,159 Some(func.ctx.clone()),160 &func.params,161 args,162 tailstrict,163 )?;164 evaluate(ctx, &func.body)165 }166 Self::Intrinsic(_) => todo!(),167 Self::NativeExt(_, _) => todo!(),168 }169 }170171 pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {172 match self {173 Self::Normal(func) => {174 let ctx = place_args(call_ctx, Some(func.ctx.clone()), &func.params, args)?;175 evaluate(ctx, &func.body)176 }177 Self::Intrinsic(_) => todo!(),178 Self::NativeExt(_, _) => todo!(),179 }180 }181}182183#[derive(Clone)]184pub enum ManifestFormat {185 YamlStream(Box<ManifestFormat>),186 Yaml(usize),187 Json(usize),188 ToString,189 String,190}191192#[derive(Debug, Clone, Trace)]193#[trivially_drop]194pub enum ArrValue {195 Lazy(Gc<Vec<LazyVal>>),196 Eager(Gc<Vec<Val>>),197 Extended(Box<(Self, Self)>),198}199impl ArrValue {200 pub fn new_eager() -> Self {201 Self::Eager(Gc::new(Vec::new()))202 }203204 pub fn len(&self) -> usize {205 match self {206 Self::Lazy(l) => l.len(),207 Self::Eager(e) => e.len(),208 Self::Extended(v) => v.0.len() + v.1.len(),209 }210 }211212 pub fn is_empty(&self) -> bool {213 self.len() == 0214 }215216 pub fn get(&self, index: usize) -> Result<Option<Val>> {217 match self {218 Self::Lazy(vec) => {219 if let Some(v) = vec.get(index) {220 Ok(Some(v.evaluate()?))221 } else {222 Ok(None)223 }224 }225 Self::Eager(vec) => Ok(vec.get(index).cloned()),226 Self::Extended(v) => {227 let a_len = v.0.len();228 if a_len > index {229 v.0.get(index)230 } else {231 v.1.get(index - a_len)232 }233 }234 }235 }236237 pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {238 match self {239 Self::Lazy(vec) => vec.get(index).cloned(),240 Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),241 Self::Extended(v) => {242 let a_len = v.0.len();243 if a_len > index {244 v.0.get_lazy(index)245 } else {246 v.1.get_lazy(index - a_len)247 }248 }249 }250 }251252 pub fn evaluated(&self) -> Result<Gc<Vec<Val>>> {253 Ok(match self {254 Self::Lazy(vec) => {255 let mut out = Vec::with_capacity(vec.len());256 for item in vec.iter() {257 out.push(item.evaluate()?);258 }259 Gc::new(out)260 }261 Self::Eager(vec) => vec.clone(),262 Self::Extended(_v) => {263 let mut out = Vec::with_capacity(self.len());264 for item in self.iter() {265 out.push(item?);266 }267 Gc::new(out)268 }269 })270 }271272 pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {273 (0..self.len()).map(move |idx| match self {274 Self::Lazy(l) => l[idx].evaluate(),275 Self::Eager(e) => Ok(e[idx].clone()),276 Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),277 })278 }279280 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {281 (0..self.len()).map(move |idx| match self {282 Self::Lazy(l) => l[idx].clone(),283 Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),284 Self::Extended(_) => self.get_lazy(idx).unwrap(),285 })286 }287288 pub fn reversed(self) -> Self {289 match self {290 Self::Lazy(vec) => {291 let mut out = (&vec as &Vec<_>).clone();292 out.reverse();293 Self::Lazy(Gc::new(out))294 }295 Self::Eager(vec) => {296 let mut out = (&vec as &Vec<_>).clone();297 out.reverse();298 Self::Eager(Gc::new(out))299 }300 Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),301 }302 }303304 pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {305 let mut out = Vec::with_capacity(self.len());306307 for value in self.iter() {308 out.push(mapper(value?)?);309 }310311 Ok(Self::Eager(Gc::new(out)))312 }313314 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {315 let mut out = Vec::with_capacity(self.len());316317 for value in self.iter() {318 let value = value?;319 if filter(&value)? {320 out.push(value);321 }322 }323324 Ok(Self::Eager(Gc::new(out)))325 }326327 pub fn ptr_eq(a: &Self, b: &Self) -> bool {328 match (a, b) {329 (Self::Lazy(a), Self::Lazy(b)) => Gc::ptr_eq(a, b),330 (Self::Eager(a), Self::Eager(b)) => Gc::ptr_eq(a, b),331 _ => false,332 }333 }334}335336impl From<Vec<LazyVal>> for ArrValue {337 fn from(v: Vec<LazyVal>) -> Self {338 Self::Lazy(Gc::new(v))339 }340}341342impl From<Vec<Val>> for ArrValue {343 fn from(v: Vec<Val>) -> Self {344 Self::Eager(Gc::new(v))345 }346}347348#[derive(Debug, Clone, Trace)]349#[trivially_drop]350pub enum Val {351 Bool(bool),352 Null,353 Str(IStr),354 Num(f64),355 Arr(ArrValue),356 Obj(ObjValue),357 Func(Gc<FuncVal>),358}359360macro_rules! matches_unwrap {361 ($e: expr, $p: pat, $r: expr) => {362 match $e {363 $p => $r,364 _ => panic!("no match"),365 }366 };367}368impl Val {369 370 371 pub fn new_checked_num(num: f64) -> Result<Self> {372 if num.is_finite() {373 Ok(Self::Num(num))374 } else {375 throw!(RuntimeError("overflow".into()))376 }377 }378379 pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {380 let this_type = self.value_type();381 if this_type != val_type {382 throw!(TypeMismatch(context, vec![val_type], this_type))383 } else {384 Ok(())385 }386 }387 pub fn unwrap_num(self) -> Result<f64> {388 Ok(matches_unwrap!(self, Self::Num(v), v))389 }390 pub fn unwrap_func(self) -> Result<Gc<FuncVal>> {391 Ok(matches_unwrap!(self, Self::Func(v), v))392 }393 pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {394 self.assert_type(context, ValType::Bool)?;395 Ok(matches_unwrap!(self, Self::Bool(v), v))396 }397 pub fn try_cast_str(self, context: &'static str) -> Result<IStr> {398 self.assert_type(context, ValType::Str)?;399 Ok(matches_unwrap!(self, Self::Str(v), v))400 }401 pub fn try_cast_num(self, context: &'static str) -> Result<f64> {402 self.assert_type(context, ValType::Num)?;403 self.unwrap_num()404 }405 pub const fn value_type(&self) -> ValType {406 match self {407 Self::Str(..) => ValType::Str,408 Self::Num(..) => ValType::Num,409 Self::Arr(..) => ValType::Arr,410 Self::Obj(..) => ValType::Obj,411 Self::Bool(_) => ValType::Bool,412 Self::Null => ValType::Null,413 Self::Func(..) => ValType::Func,414 }415 }416417 pub fn to_string(&self) -> Result<IStr> {418 Ok(match self {419 Self::Bool(true) => "true".into(),420 Self::Bool(false) => "false".into(),421 Self::Null => "null".into(),422 Self::Str(s) => s.clone(),423 v => manifest_json_ex(424 v,425 &ManifestJsonOptions {426 padding: "",427 mtype: ManifestType::ToString,428 },429 )?430 .into(),431 })432 }433434 435 pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {436 let obj = match self {437 Self::Obj(obj) => obj,438 _ => throw!(MultiManifestOutputIsNotAObject),439 };440 let keys = obj.fields();441 let mut out = Vec::with_capacity(keys.len());442 for key in keys {443 let value = obj444 .get(key.clone())?445 .expect("item in object")446 .manifest(ty)?;447 out.push((key, value));448 }449 Ok(out)450 }451452 453 pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {454 let arr = match self {455 Self::Arr(a) => a,456 _ => throw!(StreamManifestOutputIsNotAArray),457 };458 let mut out = Vec::with_capacity(arr.len());459 for i in arr.iter() {460 out.push(i?.manifest(ty)?);461 }462 Ok(out)463 }464465 pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {466 Ok(match ty {467 ManifestFormat::YamlStream(format) => {468 let arr = match self {469 Self::Arr(a) => a,470 _ => throw!(StreamManifestOutputIsNotAArray),471 };472 let mut out = String::new();473474 match format as &ManifestFormat {475 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),476 ManifestFormat::String => throw!(StreamManifestCannotNestString),477 _ => {}478 };479480 if !arr.is_empty() {481 for v in arr.iter() {482 out.push_str("---\n");483 out.push_str(&v?.manifest(format)?);484 out.push('\n');485 }486 out.push_str("...");487 }488489 out.into()490 }491 ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,492 ManifestFormat::Json(padding) => self.to_json(*padding)?,493 ManifestFormat::ToString => self.to_string()?,494 ManifestFormat::String => match self {495 Self::Str(s) => s.clone(),496 _ => throw!(StringManifestOutputIsNotAString),497 },498 })499 }500501 502 pub fn to_json(&self, padding: usize) -> Result<IStr> {503 manifest_json_ex(504 self,505 &ManifestJsonOptions {506 padding: &" ".repeat(padding),507 mtype: if padding == 0 {508 ManifestType::Minify509 } else {510 ManifestType::Manifest511 },512 },513 )514 .map(|s| s.into())515 }516517 518 #[cfg(feature = "faster")]519 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {520 manifest_json_ex(521 self,522 &ManifestJsonOptions {523 padding: &" ".repeat(padding),524 mtype: ManifestType::Std,525 },526 )527 .map(|s| s.into())528 }529530 531 #[cfg(not(feature = "faster"))]532 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {533 with_state(|s| {534 let ctx = s535 .create_default_context()?536 .with_var("__tmp__to_json__".into(), self.clone())?;537 Ok(evaluate(538 ctx,539 &el!(Expr::Apply(540 el!(Expr::Index(541 el!(Expr::Var("std".into())),542 el!(Expr::Str("manifestJsonEx".into()))543 )),544 ArgsDesc(vec![545 Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),546 Arg(None, el!(Expr::Str(" ".repeat(padding).into())))547 ]),548 false549 )),550 )?551 .try_cast_str("to json")?)552 })553 }554 pub fn to_yaml(&self, padding: usize) -> Result<IStr> {555 with_state(|s| {556 let ctx = s557 .create_default_context()558 .with_var("__tmp__to_json__".into(), self.clone());559 evaluate(560 ctx,561 &el!(Expr::Apply(562 el!(Expr::Index(563 el!(Expr::Var("std".into())),564 el!(Expr::Str("manifestYamlDoc".into()))565 )),566 ArgsDesc(vec![567 Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),568 Arg(569 None,570 el!(Expr::Literal(if padding != 0 {571 LiteralType::True572 } else {573 LiteralType::False574 }))575 )576 ]),577 false578 )),579 )?580 .try_cast_str("to json")581 })582 }583}584585const fn is_function_like(val: &Val) -> bool {586 matches!(val, Val::Func(_))587}588589590pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {591 Ok(match (val_a, val_b) {592 (Val::Bool(a), Val::Bool(b)) => a == b,593 (Val::Null, Val::Null) => true,594 (Val::Str(a), Val::Str(b)) => a == b,595 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,596 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(597 "primitiveEquals operates on primitive types, got array".into(),598 )),599 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(600 "primitiveEquals operates on primitive types, got object".into(),601 )),602 (a, b) if is_function_like(a) && is_function_like(b) => {603 throw!(RuntimeError("cannot test equality of functions".into()))604 }605 (_, _) => false,606 })607}608609610pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {611 if val_a.value_type() != val_b.value_type() {612 return Ok(false);613 }614 match (val_a, val_b) {615 (Val::Arr(a), Val::Arr(b)) => {616 if ArrValue::ptr_eq(a, b) {617 return Ok(true);618 }619 if a.len() != b.len() {620 return Ok(false);621 }622 for (a, b) in a.iter().zip(b.iter()) {623 if !equals(&a?, &b?)? {624 return Ok(false);625 }626 }627 Ok(true)628 }629 (Val::Obj(a), Val::Obj(b)) => {630 if ObjValue::ptr_eq(a, b) {631 return Ok(true);632 }633 let fields = a.fields();634 if fields != b.fields() {635 return Ok(false);636 }637 for field in fields {638 if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {639 return Ok(false);640 }641 }642 Ok(true)643 }644 (a, b) => Ok(primitive_equals(a, b)?),645 }646}