difftreelog
perf lazy slice
in: master
5 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
@@ -43,33 +43,45 @@
pub fn std_slice(
indexable: IndexableVal,
- index: Option<usize>,
- end: Option<usize>,
- step: Option<usize>,
+ index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
+ end: Option<BoundedUsize<0, { i32::MAX as usize }>>,
+ step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
) -> Result<Val> {
- let index = index.unwrap_or(0);
- let end = end.unwrap_or_else(|| match &indexable {
- IndexableVal::Str(_) => usize::MAX,
- IndexableVal::Arr(v) => v.len(),
- });
- let step = step.unwrap_or(1);
match &indexable {
- IndexableVal::Str(s) => Ok(Val::Str(
+ IndexableVal::Str(s) => {
+ let index = index.as_deref().copied().unwrap_or(0);
+ let end = end.as_deref().copied().unwrap_or(usize::MAX);
+ let step = step.as_deref().copied().unwrap_or(1);
+
+ if index >= end {
+ return Ok(Val::Str("".into()));
+ }
+
+ Ok(Val::Str(
(s.chars()
.skip(index)
.take(end - index)
.step_by(step)
.collect::<String>())
.into(),
- )),
- IndexableVal::Arr(arr) => Ok(Val::Arr(
- (arr.iter()
- .skip(index)
- .take(end - index)
- .step_by(step)
- .collect::<Result<Vec<Val>>>()?)
- .into(),
- )),
+ ))
+ }
+ IndexableVal::Arr(arr) => {
+ let index = index.as_deref().copied().unwrap_or(0);
+ let end = end.as_deref().copied().unwrap_or(usize::MAX).min(arr.len());
+ let step = step.as_deref().copied().unwrap_or(1);
+
+ if index >= end {
+ return Ok(Val::Arr(ArrValue::new_eager()));
+ }
+
+ Ok(Val::Arr(ArrValue::Slice(Box::new(Slice {
+ inner: arr.clone(),
+ from: index as u32,
+ to: end as u32,
+ step: step as u32,
+ }))))
+ }
}
}
@@ -221,9 +233,9 @@
#[jrsonnet_macros::builtin]
fn builtin_slice(
indexable: IndexableVal,
- index: Option<usize>,
- end: Option<usize>,
- step: Option<usize>,
+ index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
+ end: Option<BoundedUsize<0, { i32::MAX as usize }>>,
+ step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
) -> Result<Any> {
std_slice(indexable, index, end, step).map(Any)
}
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -191,3 +191,10 @@
return Err($e.into())
};
}
+
+#[macro_export]
+macro_rules! throw_runtime {
+ ($($tt:tt)*) => {
+ return Err($crate::error::Error::RuntimeError(format!($($tt)*).into()).into())
+ };
+}
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -665,23 +665,28 @@
}
Slice(value, desc) => {
let indexable = evaluate(context.clone(), value)?;
+ let loc = CallLocation::new(loc);
- fn parse_num(
+ fn parse_idx<const MIN: usize>(
+ loc: CallLocation,
context: &Context,
- expr: Option<&LocExpr>,
+ expr: &Option<LocExpr>,
desc: &'static str,
- ) -> Result<Option<usize>> {
- Ok(match expr {
- Some(s) => evaluate(context.clone(), s)?
- .try_cast_nullable_num(desc)?
- .map(|v| v as usize),
- None => None,
- })
+ ) -> Result<Option<BoundedUsize<MIN, { i32::MAX as usize }>>> {
+ if let Some(value) = expr {
+ Ok(Some(push_frame(
+ loc,
+ || format!("slice {}", desc),
+ || Ok(evaluate(context.clone(), value)?.try_into()?),
+ )?))
+ } else {
+ Ok(None)
+ }
}
- let start = parse_num(&context, desc.start.as_ref(), "start")?;
- let end = parse_num(&context, desc.end.as_ref(), "end")?;
- let step = parse_num(&context, desc.step.as_ref(), "step")?;
+ let start = parse_idx(loc, &context, &desc.start, "start")?;
+ let end = parse_idx(loc, &context, &desc.end, "end")?;
+ let step = parse_idx(loc, &context, &desc.step, "step")?;
std_slice(indexable.into_indexable()?, start, end, step)?
}
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -1,5 +1,6 @@
use std::{
convert::{TryFrom, TryInto},
+ ops::Deref,
rc::Rc,
};
@@ -12,7 +13,8 @@
error::{Error::*, LocError, Result},
throw,
typed::CheckType,
- ArrValue, FuncDesc, FuncVal, IndexableVal, ObjValue, ObjValueBuilder, Val,
+ val::{ArrValue, FuncDesc, FuncVal, IndexableVal},
+ ObjValue, ObjValueBuilder, Val,
};
pub trait TypedObj: Typed {
@@ -69,6 +71,76 @@
impl_int!(i8 u8 i16 u16 i32 u32);
+macro_rules! impl_bounded_int {
+ ($($name:ident = $ty:ty)*) => {$(
+ #[derive(Clone, Copy)]
+ pub struct $name<const MIN: $ty, const MAX: $ty>($ty);
+ impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {
+ pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {
+ if value >= MIN && value <= MAX {
+ Some(Self(value))
+ } else {
+ None
+ }
+ }
+ pub const fn value(self) -> $ty {
+ self.0
+ }
+ }
+ impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {
+ type Target = $ty;
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+ }
+
+ impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {
+ const TYPE: &'static ComplexValType =
+ &ComplexValType::BoundedNumber(
+ Some(MIN as f64),
+ Some(MAX as f64),
+ );
+ }
+ impl<const MIN: $ty, const MAX: $ty> TryFrom<Val> for $name<MIN, MAX> {
+ type Error = LocError;
+
+ fn try_from(value: Val) -> Result<Self> {
+ <Self as Typed>::TYPE.check(&value)?;
+ match value {
+ Val::Num(n) => {
+ if n.trunc() != n {
+ throw!(RuntimeError(
+ format!(
+ "cannot convert number with fractional part to {}",
+ stringify!($ty)
+ )
+ .into()
+ ))
+ }
+ Ok(Self(n as $ty))
+ }
+ _ => unreachable!(),
+ }
+ }
+ }
+ impl<const MIN: $ty, const MAX: $ty> TryFrom<$name<MIN, MAX>> for Val {
+ type Error = LocError;
+
+ fn try_from(value: $name<MIN, MAX>) -> Result<Self> {
+ Ok(Self::Num(value.0 as f64))
+ }
+ }
+ )*};
+}
+
+impl_bounded_int!(
+ BoundedI8 = i8
+ BoundedI16 = i16
+ BoundedI32 = i32
+ BoundedI64 = i64
+ BoundedUsize = usize
+);
+
impl Typed for f64 {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth1use crate::{2 builtin::manifest::{3 manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,4 },5 cc_ptr_eq,6 error::{Error::*, LocError},7 evaluate,8 function::{9 parse_default_function_call, parse_function_call, ArgsLike, Builtin, CallLocation,10 StaticBuiltin,11 },12 gc::TraceBox,13 throw, Context, ObjValue, Result,14};15use gcmodule::{Cc, Trace};16use jrsonnet_interner::IStr;17use jrsonnet_parser::{LocExpr, ParamsDesc};18use jrsonnet_types::ValType;19use std::{cell::RefCell, fmt::Debug, rc::Rc};2021pub trait LazyValValue: Trace {22 fn get(self: Box<Self>) -> Result<Val>;23}2425#[derive(Trace)]26enum LazyValInternals {27 Computed(Val),28 Errored(LocError),29 Waiting(TraceBox<dyn LazyValValue>),30 Pending,31}3233#[derive(Clone, Trace)]34pub struct LazyVal(Cc<RefCell<LazyValInternals>>);35impl LazyVal {36 pub fn new(f: TraceBox<dyn LazyValValue>) -> Self {37 Self(Cc::new(RefCell::new(LazyValInternals::Waiting(f))))38 }39 pub fn new_resolved(val: Val) -> Self {40 Self(Cc::new(RefCell::new(LazyValInternals::Computed(val))))41 }42 pub fn force(&self) -> Result<()> {43 self.evaluate()?;44 Ok(())45 }46 pub fn evaluate(&self) -> Result<Val> {47 match &*self.0.borrow() {48 LazyValInternals::Computed(v) => return Ok(v.clone()),49 LazyValInternals::Errored(e) => return Err(e.clone()),50 LazyValInternals::Pending => return Err(InfiniteRecursionDetected.into()),51 _ => (),52 };53 let value = if let LazyValInternals::Waiting(value) =54 std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)55 {56 value57 } else {58 unreachable!()59 };60 let new_value = match value.0.get() {61 Ok(v) => v,62 Err(e) => {63 *self.0.borrow_mut() = LazyValInternals::Errored(e.clone());64 return Err(e);65 }66 };67 *self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());68 Ok(new_value)69 }70}7172impl Debug for LazyVal {73 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {74 write!(f, "Lazy")75 }76}77impl PartialEq for LazyVal {78 fn eq(&self, other: &Self) -> bool {79 cc_ptr_eq(&self.0, &other.0)80 }81}8283#[derive(Debug, PartialEq, Trace)]84pub struct FuncDesc {85 pub name: IStr,86 pub ctx: Context,87 pub params: ParamsDesc,88 pub body: LocExpr,89}90impl FuncDesc {91 /// Create body context, but fill arguments without defaults with lazy error92 pub fn default_body_context(&self) -> Context {93 parse_default_function_call(self.ctx.clone(), &self.params)94 }9596 /// Create context, with which body code will run97 pub fn call_body_context(98 &self,99 call_ctx: Context,100 args: &dyn ArgsLike,101 tailstrict: bool,102 ) -> Result<Context> {103 parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)104 }105}106107#[derive(Trace, Clone)]108pub enum FuncVal {109 /// Plain function implemented in jsonnet110 Normal(Cc<FuncDesc>),111 /// Standard library function112 StaticBuiltin(#[skip_trace] &'static dyn StaticBuiltin),113 /// User-provided function114 Builtin(Cc<TraceBox<dyn Builtin>>),115}116117impl Debug for FuncVal {118 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {119 match self {120 Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),121 Self::StaticBuiltin(arg0) => {122 f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()123 }124 Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),125 }126 }127}128129impl FuncVal {130 pub fn args_len(&self) -> usize {131 match self {132 Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),133 Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),134 Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),135 }136 }137 pub fn name(&self) -> IStr {138 match self {139 Self::Normal(normal) => normal.name.clone(),140 Self::StaticBuiltin(builtin) => builtin.name().into(),141 Self::Builtin(builtin) => builtin.name().into(),142 }143 }144 pub fn evaluate(145 &self,146 call_ctx: Context,147 loc: CallLocation,148 args: &dyn ArgsLike,149 tailstrict: bool,150 ) -> Result<Val> {151 match self {152 Self::Normal(func) => {153 let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;154 evaluate(body_ctx, &func.body)155 }156 Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),157 Self::Builtin(b) => b.call(call_ctx, loc, args),158 }159 }160 pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {161 self.evaluate(Context::default(), CallLocation::native(), args, true)162 }163}164165#[derive(Clone)]166pub enum ManifestFormat {167 YamlStream(Box<ManifestFormat>),168 Yaml(usize),169 Json(usize),170 ToString,171 String,172}173174#[derive(Debug, Clone, Trace)]175#[force_tracking]176pub enum ArrValue {177 Bytes(#[skip_trace] Rc<[u8]>),178 Lazy(Cc<Vec<LazyVal>>),179 Eager(Cc<Vec<Val>>),180 Extended(Box<(Self, Self)>),181 Range(i32, i32),182 Reversed(Box<Self>),183}184impl ArrValue {185 pub fn new_eager() -> Self {186 Self::Eager(Cc::new(Vec::new()))187 }188 pub fn new_range(a: i32, b: i32) -> Self {189 assert!(a <= b);190 Self::Range(a, b)191 }192193 pub fn len(&self) -> usize {194 match self {195 Self::Bytes(i) => i.len(),196 Self::Lazy(l) => l.len(),197 Self::Eager(e) => e.len(),198 Self::Extended(v) => v.0.len() + v.1.len(),199 Self::Range(a, b) => a.abs_diff(*b) as usize,200 Self::Reversed(i) => i.len(),201 }202 }203204 pub fn is_empty(&self) -> bool {205 self.len() == 0206 }207208 pub fn get(&self, index: usize) -> Result<Option<Val>> {209 match self {210 Self::Bytes(i) => i211 .get(index)212 .map_or(Ok(None), |v| Ok(Some(Val::Num(*v as f64)))),213 Self::Lazy(vec) => {214 if let Some(v) = vec.get(index) {215 Ok(Some(v.evaluate()?))216 } else {217 Ok(None)218 }219 }220 Self::Eager(vec) => Ok(vec.get(index).cloned()),221 Self::Extended(v) => {222 let a_len = v.0.len();223 if a_len > index {224 v.0.get(index)225 } else {226 v.1.get(index - a_len)227 }228 }229 Self::Range(a, _) => {230 if index >= self.len() {231 return Ok(None);232 }233 Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))234 }235 Self::Reversed(v) => {236 let len = v.len();237 if index >= len {238 return Ok(None);239 }240 v.get(len - index - 1)241 }242 }243 }244245 pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {246 match self {247 Self::Bytes(i) => i248 .get(index)249 .map(|b| LazyVal::new_resolved(Val::Num(*b as f64))),250 Self::Lazy(vec) => vec.get(index).cloned(),251 Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),252 Self::Extended(v) => {253 let a_len = v.0.len();254 if a_len > index {255 v.0.get_lazy(index)256 } else {257 v.1.get_lazy(index - a_len)258 }259 }260 Self::Range(a, _) => {261 if index >= self.len() {262 return None;263 }264 Some(LazyVal::new_resolved(Val::Num(265 ((*a as isize) + index as isize) as f64,266 )))267 }268 Self::Reversed(v) => {269 let len = v.len();270 if index >= len {271 return None;272 }273 v.get_lazy(len - index - 1)274 }275 }276 }277278 pub fn evaluated(&self) -> Result<Cc<Vec<Val>>> {279 Ok(match self {280 Self::Bytes(i) => {281 let mut out = Vec::with_capacity(i.len());282 for v in i.iter() {283 out.push(Val::Num(*v as f64));284 }285 Cc::new(out)286 }287 Self::Lazy(vec) => {288 let mut out = Vec::with_capacity(vec.len());289 for item in vec.iter() {290 out.push(item.evaluate()?);291 }292 Cc::new(out)293 }294 Self::Eager(vec) => vec.clone(),295 Self::Extended(_v) => {296 let mut out = Vec::with_capacity(self.len());297 for item in self.iter() {298 out.push(item?);299 }300 Cc::new(out)301 }302 Self::Range(a, b) => {303 let mut out = Vec::with_capacity(self.len());304 for i in *a..*b {305 out.push(Val::Num(i as f64));306 }307 Cc::new(out)308 }309 Self::Reversed(r) => {310 let mut r = r.evaluated()?;311 Cc::update_with(&mut r, |v| v.reverse());312 r313 }314 })315 }316317 pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {318 // if let Self::Reversed(v) = self {319 // return v.iter().rev();320 // }321 let len = self.len();322 (0..len).map(move |idx| match self {323 Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),324 Self::Lazy(l) => l[idx].evaluate(),325 Self::Eager(e) => Ok(e[idx].clone()),326 Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),327 Self::Range(..) => self.get(idx).map(|e| e.unwrap()),328 Self::Reversed(..) => self.get(len - idx - 1).map(|e| e.unwrap()),329 })330 }331332 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {333 let len = self.len();334 (0..len).map(move |idx| match self {335 Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),336 Self::Lazy(l) => l[idx].clone(),337 Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),338 Self::Extended(_) => self.get_lazy(idx).unwrap(),339 Self::Range(..) => self.get_lazy(idx).unwrap(),340 Self::Reversed(..) => self.get_lazy(len - idx - 1).unwrap(),341 })342 }343344 pub fn reversed(self) -> Self {345 Self::Reversed(Box::new(self))346 }347348 pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {349 let mut out = Vec::with_capacity(self.len());350351 for value in self.iter() {352 out.push(mapper(value?)?);353 }354355 Ok(Self::Eager(Cc::new(out)))356 }357358 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {359 let mut out = Vec::with_capacity(self.len());360361 for value in self.iter() {362 let value = value?;363 if filter(&value)? {364 out.push(value);365 }366 }367368 Ok(Self::Eager(Cc::new(out)))369 }370371 pub fn ptr_eq(a: &Self, b: &Self) -> bool {372 match (a, b) {373 (Self::Lazy(a), Self::Lazy(b)) => cc_ptr_eq(a, b),374 (Self::Eager(a), Self::Eager(b)) => cc_ptr_eq(a, b),375 _ => false,376 }377 }378}379380impl From<Vec<LazyVal>> for ArrValue {381 fn from(v: Vec<LazyVal>) -> Self {382 Self::Lazy(Cc::new(v))383 }384}385386impl From<Vec<Val>> for ArrValue {387 fn from(v: Vec<Val>) -> Self {388 Self::Eager(Cc::new(v))389 }390}391392pub enum IndexableVal {393 Str(IStr),394 Arr(ArrValue),395}396397#[derive(Debug, Clone, Trace)]398pub enum Val {399 Bool(bool),400 Null,401 Str(IStr),402 Num(f64),403 Arr(ArrValue),404 Obj(ObjValue),405 Func(FuncVal),406}407408impl Val {409 pub const fn as_bool(&self) -> Option<bool> {410 match self {411 Val::Bool(v) => Some(*v),412 _ => None,413 }414 }415 pub const fn as_null(&self) -> Option<()> {416 match self {417 Val::Null => Some(()),418 _ => None,419 }420 }421 pub fn as_str(&self) -> Option<IStr> {422 match self {423 Val::Str(s) => Some(s.clone()),424 _ => None,425 }426 }427 pub const fn as_num(&self) -> Option<f64> {428 match self {429 Val::Num(n) => Some(*n),430 _ => None,431 }432 }433 pub fn as_arr(&self) -> Option<ArrValue> {434 match self {435 Val::Arr(a) => Some(a.clone()),436 _ => None,437 }438 }439 pub fn as_obj(&self) -> Option<ObjValue> {440 match self {441 Val::Obj(o) => Some(o.clone()),442 _ => None,443 }444 }445 pub fn as_func(&self) -> Option<FuncVal> {446 match self {447 Val::Func(f) => Some(f.clone()),448 _ => None,449 }450 }451452 /// Creates `Val::Num` after checking for numeric overflow.453 /// As numbers are `f64`, we can just check for their finity.454 pub fn new_checked_num(num: f64) -> Result<Self> {455 if num.is_finite() {456 Ok(Self::Num(num))457 } else {458 throw!(RuntimeError("overflow".into()))459 }460 }461462 pub fn try_cast_nullable_num(self, context: &'static str) -> Result<Option<f64>> {463 Ok(match self {464 Val::Null => None,465 Val::Num(num) => Some(num),466 _ => throw!(TypeMismatch(467 context,468 vec![ValType::Null, ValType::Num],469 self.value_type()470 )),471 })472 }473 pub const fn value_type(&self) -> ValType {474 match self {475 Self::Str(..) => ValType::Str,476 Self::Num(..) => ValType::Num,477 Self::Arr(..) => ValType::Arr,478 Self::Obj(..) => ValType::Obj,479 Self::Bool(_) => ValType::Bool,480 Self::Null => ValType::Null,481 Self::Func(..) => ValType::Func,482 }483 }484485 pub fn to_string(&self) -> Result<IStr> {486 Ok(match self {487 Self::Bool(true) => "true".into(),488 Self::Bool(false) => "false".into(),489 Self::Null => "null".into(),490 Self::Str(s) => s.clone(),491 v => manifest_json_ex(492 v,493 &ManifestJsonOptions {494 padding: "",495 mtype: ManifestType::ToString,496 newline: "\n",497 key_val_sep: ": ",498 },499 )?500 .into(),501 })502 }503504 /// Expects value to be object, outputs (key, manifested value) pairs505 pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {506 let obj = match self {507 Self::Obj(obj) => obj,508 _ => throw!(MultiManifestOutputIsNotAObject),509 };510 let keys = obj.fields();511 let mut out = Vec::with_capacity(keys.len());512 for key in keys {513 let value = obj514 .get(key.clone())?515 .expect("item in object")516 .manifest(ty)?;517 out.push((key, value));518 }519 Ok(out)520 }521522 /// Expects value to be array, outputs manifested values523 pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {524 let arr = match self {525 Self::Arr(a) => a,526 _ => throw!(StreamManifestOutputIsNotAArray),527 };528 let mut out = Vec::with_capacity(arr.len());529 for i in arr.iter() {530 out.push(i?.manifest(ty)?);531 }532 Ok(out)533 }534535 pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {536 Ok(match ty {537 ManifestFormat::YamlStream(format) => {538 let arr = match self {539 Self::Arr(a) => a,540 _ => throw!(StreamManifestOutputIsNotAArray),541 };542 let mut out = String::new();543544 match format as &ManifestFormat {545 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),546 ManifestFormat::String => throw!(StreamManifestCannotNestString),547 _ => {}548 };549550 if !arr.is_empty() {551 for v in arr.iter() {552 out.push_str("---\n");553 out.push_str(&v?.manifest(format)?);554 out.push('\n');555 }556 out.push_str("...");557 }558559 out.into()560 }561 ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,562 ManifestFormat::Json(padding) => self.to_json(*padding)?,563 ManifestFormat::ToString => self.to_string()?,564 ManifestFormat::String => match self {565 Self::Str(s) => s.clone(),566 _ => throw!(StringManifestOutputIsNotAString),567 },568 })569 }570571 /// For manifestification572 pub fn to_json(&self, padding: usize) -> Result<IStr> {573 manifest_json_ex(574 self,575 &ManifestJsonOptions {576 padding: &" ".repeat(padding),577 mtype: if padding == 0 {578 ManifestType::Minify579 } else {580 ManifestType::Manifest581 },582 newline: "\n",583 key_val_sep: ": ",584 },585 )586 .map(|s| s.into())587 }588589 /// Calls `std.manifestJson`590 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {591 manifest_json_ex(592 self,593 &ManifestJsonOptions {594 padding: &" ".repeat(padding),595 mtype: ManifestType::Std,596 newline: "\n",597 key_val_sep: ": ",598 },599 )600 .map(|s| s.into())601 }602603 pub fn to_yaml(&self, padding: usize) -> Result<IStr> {604 let padding = &" ".repeat(padding);605 manifest_yaml_ex(606 self,607 &ManifestYamlOptions {608 padding,609 arr_element_padding: padding,610 quote_keys: false,611 },612 )613 .map(|s| s.into())614 }615 pub fn into_indexable(self) -> Result<IndexableVal> {616 Ok(match self {617 Val::Str(s) => IndexableVal::Str(s),618 Val::Arr(arr) => IndexableVal::Arr(arr),619 _ => throw!(ValueIsNotIndexable(self.value_type())),620 })621 }622}623624const fn is_function_like(val: &Val) -> bool {625 matches!(val, Val::Func(_))626}627628/// Native implementation of `std.primitiveEquals`629pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {630 Ok(match (val_a, val_b) {631 (Val::Bool(a), Val::Bool(b)) => a == b,632 (Val::Null, Val::Null) => true,633 (Val::Str(a), Val::Str(b)) => a == b,634 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,635 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(636 "primitiveEquals operates on primitive types, got array".into(),637 )),638 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(639 "primitiveEquals operates on primitive types, got object".into(),640 )),641 (a, b) if is_function_like(a) && is_function_like(b) => {642 throw!(RuntimeError("cannot test equality of functions".into()))643 }644 (_, _) => false,645 })646}647648/// Native implementation of `std.equals`649pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {650 if val_a.value_type() != val_b.value_type() {651 return Ok(false);652 }653 match (val_a, val_b) {654 (Val::Arr(a), Val::Arr(b)) => {655 if ArrValue::ptr_eq(a, b) {656 return Ok(true);657 }658 if a.len() != b.len() {659 return Ok(false);660 }661 for (a, b) in a.iter().zip(b.iter()) {662 if !equals(&a?, &b?)? {663 return Ok(false);664 }665 }666 Ok(true)667 }668 (Val::Obj(a), Val::Obj(b)) => {669 if ObjValue::ptr_eq(a, b) {670 return Ok(true);671 }672 let fields = a.fields();673 if fields != b.fields() {674 return Ok(false);675 }676 for field in fields {677 if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {678 return Ok(false);679 }680 }681 Ok(true)682 }683 (a, b) => Ok(primitive_equals(a, b)?),684 }685}1use crate::{2 builtin::manifest::{3 manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,4 },5 cc_ptr_eq,6 error::{Error::*, LocError},7 evaluate,8 function::{9 parse_default_function_call, parse_function_call, ArgsLike, Builtin, CallLocation,10 StaticBuiltin,11 },12 gc::TraceBox,13 throw, Context, ObjValue, Result,14};15use gcmodule::{Cc, Trace};16use jrsonnet_interner::IStr;17use jrsonnet_parser::{LocExpr, ParamsDesc};18use jrsonnet_types::ValType;19use std::{cell::RefCell, fmt::Debug, rc::Rc};2021pub trait LazyValValue: Trace {22 fn get(self: Box<Self>) -> Result<Val>;23}2425#[derive(Trace)]26enum LazyValInternals {27 Computed(Val),28 Errored(LocError),29 Waiting(TraceBox<dyn LazyValValue>),30 Pending,31}3233#[derive(Clone, Trace)]34pub struct LazyVal(Cc<RefCell<LazyValInternals>>);35impl LazyVal {36 pub fn new(f: TraceBox<dyn LazyValValue>) -> Self {37 Self(Cc::new(RefCell::new(LazyValInternals::Waiting(f))))38 }39 pub fn new_resolved(val: Val) -> Self {40 Self(Cc::new(RefCell::new(LazyValInternals::Computed(val))))41 }42 pub fn force(&self) -> Result<()> {43 self.evaluate()?;44 Ok(())45 }46 pub fn evaluate(&self) -> Result<Val> {47 match &*self.0.borrow() {48 LazyValInternals::Computed(v) => return Ok(v.clone()),49 LazyValInternals::Errored(e) => return Err(e.clone()),50 LazyValInternals::Pending => return Err(InfiniteRecursionDetected.into()),51 _ => (),52 };53 let value = if let LazyValInternals::Waiting(value) =54 std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)55 {56 value57 } else {58 unreachable!()59 };60 let new_value = match value.0.get() {61 Ok(v) => v,62 Err(e) => {63 *self.0.borrow_mut() = LazyValInternals::Errored(e.clone());64 return Err(e);65 }66 };67 *self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());68 Ok(new_value)69 }70}7172impl Debug for LazyVal {73 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {74 write!(f, "Lazy")75 }76}77impl PartialEq for LazyVal {78 fn eq(&self, other: &Self) -> bool {79 cc_ptr_eq(&self.0, &other.0)80 }81}8283#[derive(Debug, PartialEq, Trace)]84pub struct FuncDesc {85 pub name: IStr,86 pub ctx: Context,87 pub params: ParamsDesc,88 pub body: LocExpr,89}90impl FuncDesc {91 /// Create body context, but fill arguments without defaults with lazy error92 pub fn default_body_context(&self) -> Context {93 parse_default_function_call(self.ctx.clone(), &self.params)94 }9596 /// Create context, with which body code will run97 pub fn call_body_context(98 &self,99 call_ctx: Context,100 args: &dyn ArgsLike,101 tailstrict: bool,102 ) -> Result<Context> {103 parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)104 }105}106107#[derive(Trace, Clone)]108pub enum FuncVal {109 /// Plain function implemented in jsonnet110 Normal(Cc<FuncDesc>),111 /// Standard library function112 StaticBuiltin(#[skip_trace] &'static dyn StaticBuiltin),113 /// User-provided function114 Builtin(Cc<TraceBox<dyn Builtin>>),115}116117impl Debug for FuncVal {118 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {119 match self {120 Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),121 Self::StaticBuiltin(arg0) => {122 f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()123 }124 Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),125 }126 }127}128129impl FuncVal {130 pub fn args_len(&self) -> usize {131 match self {132 Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),133 Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),134 Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),135 }136 }137 pub fn name(&self) -> IStr {138 match self {139 Self::Normal(normal) => normal.name.clone(),140 Self::StaticBuiltin(builtin) => builtin.name().into(),141 Self::Builtin(builtin) => builtin.name().into(),142 }143 }144 pub fn evaluate(145 &self,146 call_ctx: Context,147 loc: CallLocation,148 args: &dyn ArgsLike,149 tailstrict: bool,150 ) -> Result<Val> {151 match self {152 Self::Normal(func) => {153 let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;154 evaluate(body_ctx, &func.body)155 }156 Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),157 Self::Builtin(b) => b.call(call_ctx, loc, args),158 }159 }160 pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {161 self.evaluate(Context::default(), CallLocation::native(), args, true)162 }163}164165#[derive(Clone)]166pub enum ManifestFormat {167 YamlStream(Box<ManifestFormat>),168 Yaml(usize),169 Json(usize),170 ToString,171 String,172}173174#[derive(Debug, Clone, Trace)]175pub struct Slice {176 pub(crate) inner: ArrValue,177 pub(crate) from: u32,178 pub(crate) to: u32,179 pub(crate) step: u32,180}181impl Slice {182 fn from(&self) -> usize {183 self.from as usize184 }185 fn to(&self) -> usize {186 self.to as usize187 }188 fn step(&self) -> usize {189 self.step as usize190 }191 fn len(&self) -> usize {192 // TODO: use div_ceil193 let diff = self.to() - self.from();194 let rem = diff % self.step();195 let div = diff / self.step();196197 if rem != 0 {198 div + 1199 } else {200 div201 }202 }203}204205#[derive(Debug, Clone, Trace)]206#[force_tracking]207pub enum ArrValue {208 Bytes(#[skip_trace] Rc<[u8]>),209 Lazy(Cc<Vec<LazyVal>>),210 Eager(Cc<Vec<Val>>),211 Extended(Box<(Self, Self)>),212 Range(i32, i32),213 Slice(Box<Slice>),214 Reversed(Box<Self>),215}216impl ArrValue {217 pub fn new_eager() -> Self {218 Self::Eager(Cc::new(Vec::new()))219 }220 pub fn new_range(a: i32, b: i32) -> Self {221 assert!(a <= b);222 Self::Range(a, b)223 }224225 pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {226 let len = self.len();227 let from = from.unwrap_or(0);228 let to = to.unwrap_or(len).min(len);229 let step = step.unwrap_or(1);230 assert!(from < to);231 assert!(step > 0);232233 Self::Slice(Box::new(Slice {234 inner: self,235 from: from as u32,236 to: to as u32,237 step: step as u32,238 }))239 }240241 pub fn len(&self) -> usize {242 match self {243 Self::Bytes(i) => i.len(),244 Self::Lazy(l) => l.len(),245 Self::Eager(e) => e.len(),246 Self::Extended(v) => v.0.len() + v.1.len(),247 Self::Range(a, b) => a.abs_diff(*b) as usize,248 Self::Reversed(i) => i.len(),249 Self::Slice(s) => s.len(),250 }251 }252253 pub fn is_empty(&self) -> bool {254 self.len() == 0255 }256257 pub fn get(&self, index: usize) -> Result<Option<Val>> {258 match self {259 Self::Bytes(i) => i260 .get(index)261 .map_or(Ok(None), |v| Ok(Some(Val::Num(*v as f64)))),262 Self::Lazy(vec) => {263 if let Some(v) = vec.get(index) {264 Ok(Some(v.evaluate()?))265 } else {266 Ok(None)267 }268 }269 Self::Eager(vec) => Ok(vec.get(index).cloned()),270 Self::Extended(v) => {271 let a_len = v.0.len();272 if a_len > index {273 v.0.get(index)274 } else {275 v.1.get(index - a_len)276 }277 }278 Self::Range(a, _) => {279 if index >= self.len() {280 return Ok(None);281 }282 Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))283 }284 Self::Reversed(v) => {285 let len = v.len();286 if index >= len {287 return Ok(None);288 }289 v.get(len - index - 1)290 }291 Self::Slice(s) => {292 let index = s.from() + index * s.step();293 if index >= s.to() {294 return Ok(None);295 }296 s.inner.get(index as usize)297 }298 }299 }300301 pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {302 match self {303 Self::Bytes(i) => i304 .get(index)305 .map(|b| LazyVal::new_resolved(Val::Num(*b as f64))),306 Self::Lazy(vec) => vec.get(index).cloned(),307 Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),308 Self::Extended(v) => {309 let a_len = v.0.len();310 if a_len > index {311 v.0.get_lazy(index)312 } else {313 v.1.get_lazy(index - a_len)314 }315 }316 Self::Range(a, _) => {317 if index >= self.len() {318 return None;319 }320 Some(LazyVal::new_resolved(Val::Num(321 ((*a as isize) + index as isize) as f64,322 )))323 }324 Self::Reversed(v) => {325 let len = v.len();326 if index >= len {327 return None;328 }329 v.get_lazy(len - index - 1)330 }331 Self::Slice(s) => {332 let index = s.from() + index * s.step();333 if index >= s.to() {334 return None;335 }336 s.inner.get_lazy(index as usize)337 }338 }339 }340341 pub fn evaluated(&self) -> Result<Cc<Vec<Val>>> {342 Ok(match self {343 Self::Bytes(i) => {344 let mut out = Vec::with_capacity(i.len());345 for v in i.iter() {346 out.push(Val::Num(*v as f64));347 }348 Cc::new(out)349 }350 Self::Lazy(vec) => {351 let mut out = Vec::with_capacity(vec.len());352 for item in vec.iter() {353 out.push(item.evaluate()?);354 }355 Cc::new(out)356 }357 Self::Eager(vec) => vec.clone(),358 Self::Extended(_v) => {359 let mut out = Vec::with_capacity(self.len());360 for item in self.iter() {361 out.push(item?);362 }363 Cc::new(out)364 }365 Self::Range(a, b) => {366 let mut out = Vec::with_capacity(self.len());367 for i in *a..*b {368 out.push(Val::Num(i as f64));369 }370 Cc::new(out)371 }372 Self::Reversed(r) => {373 let mut r = r.evaluated()?;374 Cc::update_with(&mut r, |v| v.reverse());375 r376 }377 Self::Slice(v) => {378 let mut out = Vec::with_capacity(v.inner.len());379 for v in v380 .inner381 .iter_lazy()382 .skip(v.from())383 .take(v.to() - v.from())384 .step_by(v.step())385 {386 out.push(v.evaluate()?)387 }388 Cc::new(out)389 }390 })391 }392393 pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {394 (0..self.len()).map(move |idx| match self {395 Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),396 Self::Lazy(l) => l[idx].evaluate(),397 Self::Eager(e) => Ok(e[idx].clone()),398 Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),399 Self::Range(..) => self.get(idx).map(|e| e.unwrap()),400 Self::Reversed(..) => self.get(idx).map(|e| e.unwrap()),401 Self::Slice(..) => self.get(idx).map(|e| e.unwrap()),402 })403 }404405 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {406 (0..self.len()).map(move |idx| match self {407 Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),408 Self::Lazy(l) => l[idx].clone(),409 Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),410 Self::Extended(_) => self.get_lazy(idx).unwrap(),411 Self::Range(..) => self.get_lazy(idx).unwrap(),412 Self::Reversed(..) => self.get_lazy(idx).unwrap(),413 Self::Slice(..) => self.get_lazy(idx).unwrap(),414 })415 }416417 pub fn reversed(self) -> Self {418 Self::Reversed(Box::new(self))419 }420421 pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {422 let mut out = Vec::with_capacity(self.len());423424 for value in self.iter() {425 out.push(mapper(value?)?);426 }427428 Ok(Self::Eager(Cc::new(out)))429 }430431 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {432 let mut out = Vec::with_capacity(self.len());433434 for value in self.iter() {435 let value = value?;436 if filter(&value)? {437 out.push(value);438 }439 }440441 Ok(Self::Eager(Cc::new(out)))442 }443444 pub fn ptr_eq(a: &Self, b: &Self) -> bool {445 match (a, b) {446 (Self::Lazy(a), Self::Lazy(b)) => cc_ptr_eq(a, b),447 (Self::Eager(a), Self::Eager(b)) => cc_ptr_eq(a, b),448 _ => false,449 }450 }451}452453impl From<Vec<LazyVal>> for ArrValue {454 fn from(v: Vec<LazyVal>) -> Self {455 Self::Lazy(Cc::new(v))456 }457}458459impl From<Vec<Val>> for ArrValue {460 fn from(v: Vec<Val>) -> Self {461 Self::Eager(Cc::new(v))462 }463}464465pub enum IndexableVal {466 Str(IStr),467 Arr(ArrValue),468}469470#[derive(Debug, Clone, Trace)]471pub enum Val {472 Bool(bool),473 Null,474 Str(IStr),475 Num(f64),476 Arr(ArrValue),477 Obj(ObjValue),478 Func(FuncVal),479}480481impl Val {482 pub const fn as_bool(&self) -> Option<bool> {483 match self {484 Val::Bool(v) => Some(*v),485 _ => None,486 }487 }488 pub const fn as_null(&self) -> Option<()> {489 match self {490 Val::Null => Some(()),491 _ => None,492 }493 }494 pub fn as_str(&self) -> Option<IStr> {495 match self {496 Val::Str(s) => Some(s.clone()),497 _ => None,498 }499 }500 pub const fn as_num(&self) -> Option<f64> {501 match self {502 Val::Num(n) => Some(*n),503 _ => None,504 }505 }506 pub fn as_arr(&self) -> Option<ArrValue> {507 match self {508 Val::Arr(a) => Some(a.clone()),509 _ => None,510 }511 }512 pub fn as_obj(&self) -> Option<ObjValue> {513 match self {514 Val::Obj(o) => Some(o.clone()),515 _ => None,516 }517 }518 pub fn as_func(&self) -> Option<FuncVal> {519 match self {520 Val::Func(f) => Some(f.clone()),521 _ => None,522 }523 }524525 /// Creates `Val::Num` after checking for numeric overflow.526 /// As numbers are `f64`, we can just check for their finity.527 pub fn new_checked_num(num: f64) -> Result<Self> {528 if num.is_finite() {529 Ok(Self::Num(num))530 } else {531 throw!(RuntimeError("overflow".into()))532 }533 }534535 pub const fn value_type(&self) -> ValType {536 match self {537 Self::Str(..) => ValType::Str,538 Self::Num(..) => ValType::Num,539 Self::Arr(..) => ValType::Arr,540 Self::Obj(..) => ValType::Obj,541 Self::Bool(_) => ValType::Bool,542 Self::Null => ValType::Null,543 Self::Func(..) => ValType::Func,544 }545 }546547 pub fn to_string(&self) -> Result<IStr> {548 Ok(match self {549 Self::Bool(true) => "true".into(),550 Self::Bool(false) => "false".into(),551 Self::Null => "null".into(),552 Self::Str(s) => s.clone(),553 v => manifest_json_ex(554 v,555 &ManifestJsonOptions {556 padding: "",557 mtype: ManifestType::ToString,558 newline: "\n",559 key_val_sep: ": ",560 },561 )?562 .into(),563 })564 }565566 /// Expects value to be object, outputs (key, manifested value) pairs567 pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {568 let obj = match self {569 Self::Obj(obj) => obj,570 _ => throw!(MultiManifestOutputIsNotAObject),571 };572 let keys = obj.fields();573 let mut out = Vec::with_capacity(keys.len());574 for key in keys {575 let value = obj576 .get(key.clone())?577 .expect("item in object")578 .manifest(ty)?;579 out.push((key, value));580 }581 Ok(out)582 }583584 /// Expects value to be array, outputs manifested values585 pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {586 let arr = match self {587 Self::Arr(a) => a,588 _ => throw!(StreamManifestOutputIsNotAArray),589 };590 let mut out = Vec::with_capacity(arr.len());591 for i in arr.iter() {592 out.push(i?.manifest(ty)?);593 }594 Ok(out)595 }596597 pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {598 Ok(match ty {599 ManifestFormat::YamlStream(format) => {600 let arr = match self {601 Self::Arr(a) => a,602 _ => throw!(StreamManifestOutputIsNotAArray),603 };604 let mut out = String::new();605606 match format as &ManifestFormat {607 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),608 ManifestFormat::String => throw!(StreamManifestCannotNestString),609 _ => {}610 };611612 if !arr.is_empty() {613 for v in arr.iter() {614 out.push_str("---\n");615 out.push_str(&v?.manifest(format)?);616 out.push('\n');617 }618 out.push_str("...");619 }620621 out.into()622 }623 ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,624 ManifestFormat::Json(padding) => self.to_json(*padding)?,625 ManifestFormat::ToString => self.to_string()?,626 ManifestFormat::String => match self {627 Self::Str(s) => s.clone(),628 _ => throw!(StringManifestOutputIsNotAString),629 },630 })631 }632633 /// For manifestification634 pub fn to_json(&self, padding: usize) -> Result<IStr> {635 manifest_json_ex(636 self,637 &ManifestJsonOptions {638 padding: &" ".repeat(padding),639 mtype: if padding == 0 {640 ManifestType::Minify641 } else {642 ManifestType::Manifest643 },644 newline: "\n",645 key_val_sep: ": ",646 },647 )648 .map(|s| s.into())649 }650651 /// Calls `std.manifestJson`652 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {653 manifest_json_ex(654 self,655 &ManifestJsonOptions {656 padding: &" ".repeat(padding),657 mtype: ManifestType::Std,658 newline: "\n",659 key_val_sep: ": ",660 },661 )662 .map(|s| s.into())663 }664665 pub fn to_yaml(&self, padding: usize) -> Result<IStr> {666 let padding = &" ".repeat(padding);667 manifest_yaml_ex(668 self,669 &ManifestYamlOptions {670 padding,671 arr_element_padding: padding,672 quote_keys: false,673 },674 )675 .map(|s| s.into())676 }677 pub fn into_indexable(self) -> Result<IndexableVal> {678 Ok(match self {679 Val::Str(s) => IndexableVal::Str(s),680 Val::Arr(arr) => IndexableVal::Arr(arr),681 _ => throw!(ValueIsNotIndexable(self.value_type())),682 })683 }684}685686const fn is_function_like(val: &Val) -> bool {687 matches!(val, Val::Func(_))688}689690/// Native implementation of `std.primitiveEquals`691pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {692 Ok(match (val_a, val_b) {693 (Val::Bool(a), Val::Bool(b)) => a == b,694 (Val::Null, Val::Null) => true,695 (Val::Str(a), Val::Str(b)) => a == b,696 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,697 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(698 "primitiveEquals operates on primitive types, got array".into(),699 )),700 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(701 "primitiveEquals operates on primitive types, got object".into(),702 )),703 (a, b) if is_function_like(a) && is_function_like(b) => {704 throw!(RuntimeError("cannot test equality of functions".into()))705 }706 (_, _) => false,707 })708}709710/// Native implementation of `std.equals`711pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {712 if val_a.value_type() != val_b.value_type() {713 return Ok(false);714 }715 match (val_a, val_b) {716 (Val::Arr(a), Val::Arr(b)) => {717 if ArrValue::ptr_eq(a, b) {718 return Ok(true);719 }720 if a.len() != b.len() {721 return Ok(false);722 }723 for (a, b) in a.iter().zip(b.iter()) {724 if !equals(&a?, &b?)? {725 return Ok(false);726 }727 }728 Ok(true)729 }730 (Val::Obj(a), Val::Obj(b)) => {731 if ObjValue::ptr_eq(a, b) {732 return Ok(true);733 }734 let fields = a.fields();735 if fields != b.fields() {736 return Ok(false);737 }738 for field in fields {739 if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {740 return Ok(false);741 }742 }743 Ok(true)744 }745 (a, b) => Ok(primitive_equals(a, b)?),746 }747}