difftreelog
refactor use new object methods
in: master
4 files changed
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -144,12 +144,7 @@
0, obj: ty!(object) => Val::Obj;
1, inc_hidden: ty!(boolean) => Val::Bool;
], {
- let mut out = obj.fields_visibility()
- .into_iter()
- .filter(|(_k, v)| *v || inc_hidden)
- .map(|(k, _v)|k)
- .collect::<Vec<_>>();
- out.sort();
+ let out = obj.fields_ex(inc_hidden);
Ok(Val::Arr(out.into_iter().map(Val::Str).collect::<Vec<_>>().into()))
})
}
@@ -164,12 +159,7 @@
1, f: ty!(string) => Val::Str;
2, inc_hidden: ty!(boolean) => Val::Bool;
], {
- Ok(Val::Bool(
- obj.fields_visibility()
- .into_iter()
- .filter(|(_k, v)| *v || inc_hidden)
- .any(|(k, _v)| *k == *f),
- ))
+ Ok(Val::Bool(obj.has_field_ex(f, inc_hidden)))
})
}
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -245,7 +245,7 @@
new_bindings.fill(bindings);
}
- let mut new_members = HashMap::new();
+ let mut new_members = FxHashMap::default();
for member in members.iter() {
match member {
Member::Field(FieldMember {
@@ -320,7 +320,7 @@
ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,
ObjBody::ObjComp(obj) => {
let future_this = FutureObjValue::new();
- let mut new_members = HashMap::new();
+ let mut new_members = FxHashMap::default();
for (k, v) in evaluate_comp(
context.clone(),
&|ctx| {
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -3,10 +3,12 @@
throw, LazyBinding, LazyVal, ObjMember, ObjValue, Val,
};
use jrsonnet_parser::Visibility;
+use rustc_hash::FxHasher;
use serde_json::{Map, Number, Value};
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
+ hash::BuildHasherDefault,
rc::Rc,
};
@@ -31,7 +33,7 @@
}
Val::Obj(o) => {
let mut out = Map::new();
- for key in o.visible_fields() {
+ for key in o.fields() {
out.insert(
(&key as &str).into(),
(&o.get(key)?.expect("field exists")).try_into()?,
@@ -59,7 +61,10 @@
Self::Arr(out.into())
}
Value::Object(o) => {
- let mut entries = HashMap::with_capacity(o.len());
+ let mut entries = HashMap::with_capacity_and_hasher(
+ o.len(),
+ BuildHasherDefault::<FxHasher>::default(),
+ );
for (k, v) in o {
entries.insert(
(k as &str).into(),
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth1use crate::{2 builtin::{3 call_builtin,4 manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},5 },6 error::Error::*,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_interner::IStr;13use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};14use jrsonnet_types::ValType;15use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};1617enum LazyValInternals {18 Computed(Val),19 Waiting(Box<dyn Fn() -> Result<Val>>),20}21#[derive(Clone)]22pub struct LazyVal(Rc<RefCell<LazyValInternals>>);23impl LazyVal {24 pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {25 Self(Rc::new(RefCell::new(LazyValInternals::Waiting(f))))26 }27 pub fn new_resolved(val: Val) -> Self {28 Self(Rc::new(RefCell::new(LazyValInternals::Computed(val))))29 }30 pub fn evaluate(&self) -> Result<Val> {31 let new_value = match &*self.0.borrow() {32 LazyValInternals::Computed(v) => return Ok(v.clone()),33 LazyValInternals::Waiting(f) => f()?,34 };35 *self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());36 Ok(new_value)37 }38}3940#[macro_export]41macro_rules! lazy_val {42 ($f: expr) => {43 $crate::LazyVal::new(Box::new($f))44 };45}46#[macro_export]47macro_rules! resolved_lazy_val {48 ($f: expr) => {49 $crate::LazyVal::new_resolved($f)50 };51}52impl Debug for LazyVal {53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {54 write!(f, "Lazy")55 }56}57impl PartialEq for LazyVal {58 fn eq(&self, other: &Self) -> bool {59 Rc::ptr_eq(&self.0, &other.0)60 }61}6263#[derive(Debug, PartialEq)]64pub struct FuncDesc {65 pub name: IStr,66 pub ctx: Context,67 pub params: ParamsDesc,68 pub body: LocExpr,69}7071#[derive(Debug)]72pub enum FuncVal {73 /// Plain function implemented in jsonnet74 Normal(FuncDesc),75 /// Standard library function76 Intrinsic(IStr),77 /// Library functions implemented in native78 NativeExt(IStr, Rc<NativeCallback>),79}8081impl PartialEq for FuncVal {82 fn eq(&self, other: &Self) -> bool {83 match (self, other) {84 (Self::Normal(a), Self::Normal(b)) => a == b,85 (Self::Intrinsic(an), Self::Intrinsic(bn)) => an == bn,86 (Self::NativeExt(an, _), Self::NativeExt(bn, _)) => an == bn,87 (..) => false,88 }89 }90}91impl FuncVal {92 pub fn is_ident(&self) -> bool {93 matches!(&self, Self::Intrinsic(n) if n as &str == "id")94 }95 pub fn name(&self) -> IStr {96 match self {97 Self::Normal(normal) => normal.name.clone(),98 Self::Intrinsic(name) => format!("std.{}", name).into(),99 Self::NativeExt(n, _) => format!("native.{}", n).into(),100 }101 }102 pub fn evaluate(103 &self,104 call_ctx: Context,105 loc: Option<&ExprLocation>,106 args: &ArgsDesc,107 tailstrict: bool,108 ) -> Result<Val> {109 match self {110 Self::Normal(func) => {111 let ctx = parse_function_call(112 call_ctx,113 Some(func.ctx.clone()),114 &func.params,115 args,116 tailstrict,117 )?;118 evaluate(ctx, &func.body)119 }120 Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),121 Self::NativeExt(_name, handler) => {122 let args = parse_function_call(call_ctx, None, &handler.params, args, true)?;123 let mut out_args = Vec::with_capacity(handler.params.len());124 for p in handler.params.0.iter() {125 out_args.push(args.binding(p.0.clone())?.evaluate()?);126 }127 Ok(handler.call(loc.clone().map(|l| l.0.clone()), &out_args)?)128 }129 }130 }131132 pub fn evaluate_map(133 &self,134 call_ctx: Context,135 args: &HashMap<IStr, Val>,136 tailstrict: bool,137 ) -> Result<Val> {138 match self {139 Self::Normal(func) => {140 let ctx = parse_function_call_map(141 call_ctx,142 Some(func.ctx.clone()),143 &func.params,144 args,145 tailstrict,146 )?;147 evaluate(ctx, &func.body)148 }149 Self::Intrinsic(_) => todo!(),150 Self::NativeExt(_, _) => todo!(),151 }152 }153154 pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {155 match self {156 Self::Normal(func) => {157 let ctx = place_args(call_ctx, Some(func.ctx.clone()), &func.params, args)?;158 evaluate(ctx, &func.body)159 }160 Self::Intrinsic(_) => todo!(),161 Self::NativeExt(_, _) => todo!(),162 }163 }164}165166#[derive(Clone)]167pub enum ManifestFormat {168 YamlStream(Box<ManifestFormat>),169 Yaml(usize),170 Json(usize),171 ToString,172 String,173}174175#[derive(Debug, Clone)]176pub enum ArrValue {177 Lazy(Rc<Vec<LazyVal>>),178 Eager(Rc<Vec<Val>>),179 Extended(Box<(Self, Self)>),180}181impl ArrValue {182 pub fn new_eager() -> Self {183 Self::Eager(Rc::new(Vec::new()))184 }185186 pub fn len(&self) -> usize {187 match self {188 Self::Lazy(l) => l.len(),189 Self::Eager(e) => e.len(),190 Self::Extended(v) => v.0.len() + v.1.len(),191 }192 }193194 pub fn is_empty(&self) -> bool {195 self.len() == 0196 }197198 pub fn get(&self, index: usize) -> Result<Option<Val>> {199 match self {200 Self::Lazy(vec) => {201 if let Some(v) = vec.get(index) {202 Ok(Some(v.evaluate()?))203 } else {204 Ok(None)205 }206 }207 Self::Eager(vec) => Ok(vec.get(index).cloned()),208 Self::Extended(v) => {209 let a_len = v.0.len();210 if a_len > index {211 v.0.get(index)212 } else {213 v.1.get(index - a_len)214 }215 }216 }217 }218219 pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {220 match self {221 Self::Lazy(vec) => vec.get(index).cloned(),222 Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),223 Self::Extended(v) => {224 let a_len = v.0.len();225 if a_len > index {226 v.0.get_lazy(index)227 } else {228 v.1.get_lazy(index - a_len)229 }230 }231 }232 }233234 pub fn evaluated(&self) -> Result<Rc<Vec<Val>>> {235 Ok(match self {236 Self::Lazy(vec) => {237 let mut out = Vec::with_capacity(vec.len());238 for item in vec.iter() {239 out.push(item.evaluate()?);240 }241 Rc::new(out)242 }243 Self::Eager(vec) => vec.clone(),244 Self::Extended(_v) => {245 let mut out = Vec::with_capacity(self.len());246 for item in self.iter() {247 out.push(item?);248 }249 Rc::new(out)250 }251 })252 }253254 pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {255 (0..self.len()).map(move |idx| match self {256 Self::Lazy(l) => l[idx].evaluate(),257 Self::Eager(e) => Ok(e[idx].clone()),258 Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),259 })260 }261262 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {263 (0..self.len()).map(move |idx| match self {264 Self::Lazy(l) => l[idx].clone(),265 Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),266 Self::Extended(_) => self.get_lazy(idx).unwrap(),267 })268 }269270 pub fn reversed(self) -> Self {271 match self {272 Self::Lazy(vec) => {273 let mut out = (&vec as &Vec<_>).clone();274 out.reverse();275 Self::Lazy(Rc::new(out))276 }277 Self::Eager(vec) => {278 let mut out = (&vec as &Vec<_>).clone();279 out.reverse();280 Self::Eager(Rc::new(out))281 }282 Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),283 }284 }285286 pub fn ptr_eq(a: &Self, b: &Self) -> bool {287 match (a, b) {288 (Self::Lazy(a), Self::Lazy(b)) => Rc::ptr_eq(a, b),289 (Self::Eager(a), Self::Eager(b)) => Rc::ptr_eq(a, b),290 _ => false,291 }292 }293}294295impl From<Vec<LazyVal>> for ArrValue {296 fn from(v: Vec<LazyVal>) -> Self {297 Self::Lazy(Rc::new(v))298 }299}300301impl From<Vec<Val>> for ArrValue {302 fn from(v: Vec<Val>) -> Self {303 Self::Eager(Rc::new(v))304 }305}306307#[derive(Debug, Clone)]308pub enum Val {309 Bool(bool),310 Null,311 Str(IStr),312 Num(f64),313 Arr(ArrValue),314 Obj(ObjValue),315 Func(Rc<FuncVal>),316}317318macro_rules! matches_unwrap {319 ($e: expr, $p: pat, $r: expr) => {320 match $e {321 $p => $r,322 _ => panic!("no match"),323 }324 };325}326impl Val {327 /// Creates `Val::Num` after checking for numeric overflow.328 /// As numbers are `f64`, we can just check for their finity.329 pub fn new_checked_num(num: f64) -> Result<Self> {330 if num.is_finite() {331 Ok(Self::Num(num))332 } else {333 throw!(RuntimeError("overflow".into()))334 }335 }336337 pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {338 let this_type = self.value_type();339 if this_type != val_type {340 throw!(TypeMismatch(context, vec![val_type], this_type))341 } else {342 Ok(())343 }344 }345 pub fn unwrap_num(self) -> Result<f64> {346 Ok(matches_unwrap!(self, Self::Num(v), v))347 }348 pub fn unwrap_func(self) -> Result<Rc<FuncVal>> {349 Ok(matches_unwrap!(self, Self::Func(v), v))350 }351 pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {352 self.assert_type(context, ValType::Bool)?;353 Ok(matches_unwrap!(self, Self::Bool(v), v))354 }355 pub fn try_cast_str(self, context: &'static str) -> Result<IStr> {356 self.assert_type(context, ValType::Str)?;357 Ok(matches_unwrap!(self, Self::Str(v), v))358 }359 pub fn try_cast_num(self, context: &'static str) -> Result<f64> {360 self.assert_type(context, ValType::Num)?;361 self.unwrap_num()362 }363 pub const fn value_type(&self) -> ValType {364 match self {365 Self::Str(..) => ValType::Str,366 Self::Num(..) => ValType::Num,367 Self::Arr(..) => ValType::Arr,368 Self::Obj(..) => ValType::Obj,369 Self::Bool(_) => ValType::Bool,370 Self::Null => ValType::Null,371 Self::Func(..) => ValType::Func,372 }373 }374375 pub fn to_string(&self) -> Result<IStr> {376 Ok(match self {377 Self::Bool(true) => "true".into(),378 Self::Bool(false) => "false".into(),379 Self::Null => "null".into(),380 Self::Str(s) => s.clone(),381 v => manifest_json_ex(382 v,383 &ManifestJsonOptions {384 padding: "",385 mtype: ManifestType::ToString,386 },387 )?388 .into(),389 })390 }391392 /// Expects value to be object, outputs (key, manifested value) pairs393 pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {394 let obj = match self {395 Self::Obj(obj) => obj,396 _ => throw!(MultiManifestOutputIsNotAObject),397 };398 let keys = obj.visible_fields();399 let mut out = Vec::with_capacity(keys.len());400 for key in keys {401 let value = obj402 .get(key.clone())?403 .expect("item in object")404 .manifest(ty)?;405 out.push((key, value));406 }407 Ok(out)408 }409410 /// Expects value to be array, outputs manifested values411 pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {412 let arr = match self {413 Self::Arr(a) => a,414 _ => throw!(StreamManifestOutputIsNotAArray),415 };416 let mut out = Vec::with_capacity(arr.len());417 for i in arr.iter() {418 out.push(i?.manifest(ty)?);419 }420 Ok(out)421 }422423 pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {424 Ok(match ty {425 ManifestFormat::YamlStream(format) => {426 let arr = match self {427 Self::Arr(a) => a,428 _ => throw!(StreamManifestOutputIsNotAArray),429 };430 let mut out = String::new();431432 match format as &ManifestFormat {433 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),434 ManifestFormat::String => throw!(StreamManifestCannotNestString),435 _ => {}436 };437438 if !arr.is_empty() {439 for v in arr.iter() {440 out.push_str("---\n");441 out.push_str(&v?.manifest(format)?);442 out.push('\n');443 }444 out.push_str("...");445 }446447 out.into()448 }449 ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,450 ManifestFormat::Json(padding) => self.to_json(*padding)?,451 ManifestFormat::ToString => self.to_string()?,452 ManifestFormat::String => match self {453 Self::Str(s) => s.clone(),454 _ => throw!(StringManifestOutputIsNotAString),455 },456 })457 }458459 /// For manifestification460 pub fn to_json(&self, padding: usize) -> Result<IStr> {461 manifest_json_ex(462 self,463 &ManifestJsonOptions {464 padding: &" ".repeat(padding),465 mtype: if padding == 0 {466 ManifestType::Minify467 } else {468 ManifestType::Manifest469 },470 },471 )472 .map(|s| s.into())473 }474475 /// Calls `std.manifestJson`476 #[cfg(feature = "faster")]477 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {478 manifest_json_ex(479 self,480 &ManifestJsonOptions {481 padding: &" ".repeat(padding),482 mtype: ManifestType::Std,483 },484 )485 .map(|s| s.into())486 }487488 /// Calls `std.manifestJson`489 #[cfg(not(feature = "faster"))]490 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {491 with_state(|s| {492 let ctx = s493 .create_default_context()?494 .with_var("__tmp__to_json__".into(), self.clone())?;495 Ok(evaluate(496 ctx,497 &el!(Expr::Apply(498 el!(Expr::Index(499 el!(Expr::Var("std".into())),500 el!(Expr::Str("manifestJsonEx".into()))501 )),502 ArgsDesc(vec![503 Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),504 Arg(None, el!(Expr::Str(" ".repeat(padding).into())))505 ]),506 false507 )),508 )?509 .try_cast_str("to json")?)510 })511 }512 pub fn to_yaml(&self, padding: usize) -> Result<IStr> {513 with_state(|s| {514 let ctx = s515 .create_default_context()?516 .with_var("__tmp__to_json__".into(), self.clone());517 Ok(evaluate(518 ctx,519 &el!(Expr::Apply(520 el!(Expr::Index(521 el!(Expr::Var("std".into())),522 el!(Expr::Str("manifestYamlDoc".into()))523 )),524 ArgsDesc(vec![525 Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),526 Arg(527 None,528 el!(Expr::Literal(if padding != 0 {529 LiteralType::True530 } else {531 LiteralType::False532 }))533 )534 ]),535 false536 )),537 )?538 .try_cast_str("to json")?)539 })540 }541}542543const fn is_function_like(val: &Val) -> bool {544 matches!(val, Val::Func(_))545}546547/// Native implementation of `std.primitiveEquals`548pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {549 Ok(match (val_a, val_b) {550 (Val::Bool(a), Val::Bool(b)) => a == b,551 (Val::Null, Val::Null) => true,552 (Val::Str(a), Val::Str(b)) => a == b,553 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,554 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(555 "primitiveEquals operates on primitive types, got array".into(),556 )),557 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(558 "primitiveEquals operates on primitive types, got object".into(),559 )),560 (a, b) if is_function_like(a) && is_function_like(b) => {561 throw!(RuntimeError("cannot test equality of functions".into()))562 }563 (_, _) => false,564 })565}566567/// Native implementation of `std.equals`568pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {569 if val_a.value_type() != val_b.value_type() {570 return Ok(false);571 }572 match (val_a, val_b) {573 (Val::Arr(a), Val::Arr(b)) => {574 if ArrValue::ptr_eq(a, b) {575 return Ok(true);576 }577 if a.len() != b.len() {578 return Ok(false);579 }580 for (a, b) in a.iter().zip(b.iter()) {581 if !equals(&a?, &b?)? {582 return Ok(false);583 }584 }585 Ok(true)586 }587 (Val::Obj(a), Val::Obj(b)) => {588 if ObjValue::ptr_eq(a, b) {589 return Ok(true);590 }591 let fields = a.visible_fields();592 if fields != b.visible_fields() {593 return Ok(false);594 }595 for field in fields {596 if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {597 return Ok(false);598 }599 }600 Ok(true)601 }602 (a, b) => Ok(primitive_equals(a, b)?),603 }604}1use crate::{2 builtin::{3 call_builtin,4 manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},5 },6 error::Error::*,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_interner::IStr;13use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};14use jrsonnet_types::ValType;15use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};1617enum LazyValInternals {18 Computed(Val),19 Waiting(Box<dyn Fn() -> Result<Val>>),20}21#[derive(Clone)]22pub struct LazyVal(Rc<RefCell<LazyValInternals>>);23impl LazyVal {24 pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {25 Self(Rc::new(RefCell::new(LazyValInternals::Waiting(f))))26 }27 pub fn new_resolved(val: Val) -> Self {28 Self(Rc::new(RefCell::new(LazyValInternals::Computed(val))))29 }30 pub fn evaluate(&self) -> Result<Val> {31 let new_value = match &*self.0.borrow() {32 LazyValInternals::Computed(v) => return Ok(v.clone()),33 LazyValInternals::Waiting(f) => f()?,34 };35 *self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());36 Ok(new_value)37 }38}3940#[macro_export]41macro_rules! lazy_val {42 ($f: expr) => {43 $crate::LazyVal::new(Box::new($f))44 };45}46#[macro_export]47macro_rules! resolved_lazy_val {48 ($f: expr) => {49 $crate::LazyVal::new_resolved($f)50 };51}52impl Debug for LazyVal {53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {54 write!(f, "Lazy")55 }56}57impl PartialEq for LazyVal {58 fn eq(&self, other: &Self) -> bool {59 Rc::ptr_eq(&self.0, &other.0)60 }61}6263#[derive(Debug, PartialEq)]64pub struct FuncDesc {65 pub name: IStr,66 pub ctx: Context,67 pub params: ParamsDesc,68 pub body: LocExpr,69}7071#[derive(Debug)]72pub enum FuncVal {73 /// Plain function implemented in jsonnet74 Normal(FuncDesc),75 /// Standard library function76 Intrinsic(IStr),77 /// Library functions implemented in native78 NativeExt(IStr, Rc<NativeCallback>),79}8081impl PartialEq for FuncVal {82 fn eq(&self, other: &Self) -> bool {83 match (self, other) {84 (Self::Normal(a), Self::Normal(b)) => a == b,85 (Self::Intrinsic(an), Self::Intrinsic(bn)) => an == bn,86 (Self::NativeExt(an, _), Self::NativeExt(bn, _)) => an == bn,87 (..) => false,88 }89 }90}91impl FuncVal {92 pub fn is_ident(&self) -> bool {93 matches!(&self, Self::Intrinsic(n) if n as &str == "id")94 }95 pub fn name(&self) -> IStr {96 match self {97 Self::Normal(normal) => normal.name.clone(),98 Self::Intrinsic(name) => format!("std.{}", name).into(),99 Self::NativeExt(n, _) => format!("native.{}", n).into(),100 }101 }102 pub fn evaluate(103 &self,104 call_ctx: Context,105 loc: Option<&ExprLocation>,106 args: &ArgsDesc,107 tailstrict: bool,108 ) -> Result<Val> {109 match self {110 Self::Normal(func) => {111 let ctx = parse_function_call(112 call_ctx,113 Some(func.ctx.clone()),114 &func.params,115 args,116 tailstrict,117 )?;118 evaluate(ctx, &func.body)119 }120 Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),121 Self::NativeExt(_name, handler) => {122 let args = parse_function_call(call_ctx, None, &handler.params, args, true)?;123 let mut out_args = Vec::with_capacity(handler.params.len());124 for p in handler.params.0.iter() {125 out_args.push(args.binding(p.0.clone())?.evaluate()?);126 }127 Ok(handler.call(loc.clone().map(|l| l.0.clone()), &out_args)?)128 }129 }130 }131132 pub fn evaluate_map(133 &self,134 call_ctx: Context,135 args: &HashMap<IStr, Val>,136 tailstrict: bool,137 ) -> Result<Val> {138 match self {139 Self::Normal(func) => {140 let ctx = parse_function_call_map(141 call_ctx,142 Some(func.ctx.clone()),143 &func.params,144 args,145 tailstrict,146 )?;147 evaluate(ctx, &func.body)148 }149 Self::Intrinsic(_) => todo!(),150 Self::NativeExt(_, _) => todo!(),151 }152 }153154 pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {155 match self {156 Self::Normal(func) => {157 let ctx = place_args(call_ctx, Some(func.ctx.clone()), &func.params, args)?;158 evaluate(ctx, &func.body)159 }160 Self::Intrinsic(_) => todo!(),161 Self::NativeExt(_, _) => todo!(),162 }163 }164}165166#[derive(Clone)]167pub enum ManifestFormat {168 YamlStream(Box<ManifestFormat>),169 Yaml(usize),170 Json(usize),171 ToString,172 String,173}174175#[derive(Debug, Clone)]176pub enum ArrValue {177 Lazy(Rc<Vec<LazyVal>>),178 Eager(Rc<Vec<Val>>),179 Extended(Box<(Self, Self)>),180}181impl ArrValue {182 pub fn new_eager() -> Self {183 Self::Eager(Rc::new(Vec::new()))184 }185186 pub fn len(&self) -> usize {187 match self {188 Self::Lazy(l) => l.len(),189 Self::Eager(e) => e.len(),190 Self::Extended(v) => v.0.len() + v.1.len(),191 }192 }193194 pub fn is_empty(&self) -> bool {195 self.len() == 0196 }197198 pub fn get(&self, index: usize) -> Result<Option<Val>> {199 match self {200 Self::Lazy(vec) => {201 if let Some(v) = vec.get(index) {202 Ok(Some(v.evaluate()?))203 } else {204 Ok(None)205 }206 }207 Self::Eager(vec) => Ok(vec.get(index).cloned()),208 Self::Extended(v) => {209 let a_len = v.0.len();210 if a_len > index {211 v.0.get(index)212 } else {213 v.1.get(index - a_len)214 }215 }216 }217 }218219 pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {220 match self {221 Self::Lazy(vec) => vec.get(index).cloned(),222 Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),223 Self::Extended(v) => {224 let a_len = v.0.len();225 if a_len > index {226 v.0.get_lazy(index)227 } else {228 v.1.get_lazy(index - a_len)229 }230 }231 }232 }233234 pub fn evaluated(&self) -> Result<Rc<Vec<Val>>> {235 Ok(match self {236 Self::Lazy(vec) => {237 let mut out = Vec::with_capacity(vec.len());238 for item in vec.iter() {239 out.push(item.evaluate()?);240 }241 Rc::new(out)242 }243 Self::Eager(vec) => vec.clone(),244 Self::Extended(_v) => {245 let mut out = Vec::with_capacity(self.len());246 for item in self.iter() {247 out.push(item?);248 }249 Rc::new(out)250 }251 })252 }253254 pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {255 (0..self.len()).map(move |idx| match self {256 Self::Lazy(l) => l[idx].evaluate(),257 Self::Eager(e) => Ok(e[idx].clone()),258 Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),259 })260 }261262 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {263 (0..self.len()).map(move |idx| match self {264 Self::Lazy(l) => l[idx].clone(),265 Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),266 Self::Extended(_) => self.get_lazy(idx).unwrap(),267 })268 }269270 pub fn reversed(self) -> Self {271 match self {272 Self::Lazy(vec) => {273 let mut out = (&vec as &Vec<_>).clone();274 out.reverse();275 Self::Lazy(Rc::new(out))276 }277 Self::Eager(vec) => {278 let mut out = (&vec as &Vec<_>).clone();279 out.reverse();280 Self::Eager(Rc::new(out))281 }282 Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),283 }284 }285286 pub fn ptr_eq(a: &Self, b: &Self) -> bool {287 match (a, b) {288 (Self::Lazy(a), Self::Lazy(b)) => Rc::ptr_eq(a, b),289 (Self::Eager(a), Self::Eager(b)) => Rc::ptr_eq(a, b),290 _ => false,291 }292 }293}294295impl From<Vec<LazyVal>> for ArrValue {296 fn from(v: Vec<LazyVal>) -> Self {297 Self::Lazy(Rc::new(v))298 }299}300301impl From<Vec<Val>> for ArrValue {302 fn from(v: Vec<Val>) -> Self {303 Self::Eager(Rc::new(v))304 }305}306307#[derive(Debug, Clone)]308pub enum Val {309 Bool(bool),310 Null,311 Str(IStr),312 Num(f64),313 Arr(ArrValue),314 Obj(ObjValue),315 Func(Rc<FuncVal>),316}317318macro_rules! matches_unwrap {319 ($e: expr, $p: pat, $r: expr) => {320 match $e {321 $p => $r,322 _ => panic!("no match"),323 }324 };325}326impl Val {327 /// Creates `Val::Num` after checking for numeric overflow.328 /// As numbers are `f64`, we can just check for their finity.329 pub fn new_checked_num(num: f64) -> Result<Self> {330 if num.is_finite() {331 Ok(Self::Num(num))332 } else {333 throw!(RuntimeError("overflow".into()))334 }335 }336337 pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {338 let this_type = self.value_type();339 if this_type != val_type {340 throw!(TypeMismatch(context, vec![val_type], this_type))341 } else {342 Ok(())343 }344 }345 pub fn unwrap_num(self) -> Result<f64> {346 Ok(matches_unwrap!(self, Self::Num(v), v))347 }348 pub fn unwrap_func(self) -> Result<Rc<FuncVal>> {349 Ok(matches_unwrap!(self, Self::Func(v), v))350 }351 pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {352 self.assert_type(context, ValType::Bool)?;353 Ok(matches_unwrap!(self, Self::Bool(v), v))354 }355 pub fn try_cast_str(self, context: &'static str) -> Result<IStr> {356 self.assert_type(context, ValType::Str)?;357 Ok(matches_unwrap!(self, Self::Str(v), v))358 }359 pub fn try_cast_num(self, context: &'static str) -> Result<f64> {360 self.assert_type(context, ValType::Num)?;361 self.unwrap_num()362 }363 pub const fn value_type(&self) -> ValType {364 match self {365 Self::Str(..) => ValType::Str,366 Self::Num(..) => ValType::Num,367 Self::Arr(..) => ValType::Arr,368 Self::Obj(..) => ValType::Obj,369 Self::Bool(_) => ValType::Bool,370 Self::Null => ValType::Null,371 Self::Func(..) => ValType::Func,372 }373 }374375 pub fn to_string(&self) -> Result<IStr> {376 Ok(match self {377 Self::Bool(true) => "true".into(),378 Self::Bool(false) => "false".into(),379 Self::Null => "null".into(),380 Self::Str(s) => s.clone(),381 v => manifest_json_ex(382 v,383 &ManifestJsonOptions {384 padding: "",385 mtype: ManifestType::ToString,386 },387 )?388 .into(),389 })390 }391392 /// Expects value to be object, outputs (key, manifested value) pairs393 pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {394 let obj = match self {395 Self::Obj(obj) => obj,396 _ => throw!(MultiManifestOutputIsNotAObject),397 };398 let keys = obj.fields();399 let mut out = Vec::with_capacity(keys.len());400 for key in keys {401 let value = obj402 .get(key.clone())?403 .expect("item in object")404 .manifest(ty)?;405 out.push((key, value));406 }407 Ok(out)408 }409410 /// Expects value to be array, outputs manifested values411 pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {412 let arr = match self {413 Self::Arr(a) => a,414 _ => throw!(StreamManifestOutputIsNotAArray),415 };416 let mut out = Vec::with_capacity(arr.len());417 for i in arr.iter() {418 out.push(i?.manifest(ty)?);419 }420 Ok(out)421 }422423 pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {424 Ok(match ty {425 ManifestFormat::YamlStream(format) => {426 let arr = match self {427 Self::Arr(a) => a,428 _ => throw!(StreamManifestOutputIsNotAArray),429 };430 let mut out = String::new();431432 match format as &ManifestFormat {433 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),434 ManifestFormat::String => throw!(StreamManifestCannotNestString),435 _ => {}436 };437438 if !arr.is_empty() {439 for v in arr.iter() {440 out.push_str("---\n");441 out.push_str(&v?.manifest(format)?);442 out.push('\n');443 }444 out.push_str("...");445 }446447 out.into()448 }449 ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,450 ManifestFormat::Json(padding) => self.to_json(*padding)?,451 ManifestFormat::ToString => self.to_string()?,452 ManifestFormat::String => match self {453 Self::Str(s) => s.clone(),454 _ => throw!(StringManifestOutputIsNotAString),455 },456 })457 }458459 /// For manifestification460 pub fn to_json(&self, padding: usize) -> Result<IStr> {461 manifest_json_ex(462 self,463 &ManifestJsonOptions {464 padding: &" ".repeat(padding),465 mtype: if padding == 0 {466 ManifestType::Minify467 } else {468 ManifestType::Manifest469 },470 },471 )472 .map(|s| s.into())473 }474475 /// Calls `std.manifestJson`476 #[cfg(feature = "faster")]477 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {478 manifest_json_ex(479 self,480 &ManifestJsonOptions {481 padding: &" ".repeat(padding),482 mtype: ManifestType::Std,483 },484 )485 .map(|s| s.into())486 }487488 /// Calls `std.manifestJson`489 #[cfg(not(feature = "faster"))]490 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {491 with_state(|s| {492 let ctx = s493 .create_default_context()?494 .with_var("__tmp__to_json__".into(), self.clone())?;495 Ok(evaluate(496 ctx,497 &el!(Expr::Apply(498 el!(Expr::Index(499 el!(Expr::Var("std".into())),500 el!(Expr::Str("manifestJsonEx".into()))501 )),502 ArgsDesc(vec![503 Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),504 Arg(None, el!(Expr::Str(" ".repeat(padding).into())))505 ]),506 false507 )),508 )?509 .try_cast_str("to json")?)510 })511 }512 pub fn to_yaml(&self, padding: usize) -> Result<IStr> {513 with_state(|s| {514 let ctx = s515 .create_default_context()?516 .with_var("__tmp__to_json__".into(), self.clone());517 Ok(evaluate(518 ctx,519 &el!(Expr::Apply(520 el!(Expr::Index(521 el!(Expr::Var("std".into())),522 el!(Expr::Str("manifestYamlDoc".into()))523 )),524 ArgsDesc(vec![525 Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),526 Arg(527 None,528 el!(Expr::Literal(if padding != 0 {529 LiteralType::True530 } else {531 LiteralType::False532 }))533 )534 ]),535 false536 )),537 )?538 .try_cast_str("to json")?)539 })540 }541}542543const fn is_function_like(val: &Val) -> bool {544 matches!(val, Val::Func(_))545}546547/// Native implementation of `std.primitiveEquals`548pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {549 Ok(match (val_a, val_b) {550 (Val::Bool(a), Val::Bool(b)) => a == b,551 (Val::Null, Val::Null) => true,552 (Val::Str(a), Val::Str(b)) => a == b,553 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,554 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(555 "primitiveEquals operates on primitive types, got array".into(),556 )),557 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(558 "primitiveEquals operates on primitive types, got object".into(),559 )),560 (a, b) if is_function_like(a) && is_function_like(b) => {561 throw!(RuntimeError("cannot test equality of functions".into()))562 }563 (_, _) => false,564 })565}566567/// Native implementation of `std.equals`568pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {569 if val_a.value_type() != val_b.value_type() {570 return Ok(false);571 }572 match (val_a, val_b) {573 (Val::Arr(a), Val::Arr(b)) => {574 if ArrValue::ptr_eq(a, b) {575 return Ok(true);576 }577 if a.len() != b.len() {578 return Ok(false);579 }580 for (a, b) in a.iter().zip(b.iter()) {581 if !equals(&a?, &b?)? {582 return Ok(false);583 }584 }585 Ok(true)586 }587 (Val::Obj(a), Val::Obj(b)) => {588 if ObjValue::ptr_eq(a, b) {589 return Ok(true);590 }591 let fields = a.fields();592 if fields != b.fields() {593 return Ok(false);594 }595 for field in fields {596 if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {597 return Ok(false);598 }599 }600 Ok(true)601 }602 (a, b) => Ok(primitive_equals(a, b)?),603 }604}