difftreelog
feat(jrsonnet-evaluator) implement gc
in: master
Some manual Trace/Finalize implementations can be replaced with derives with https://github.com/Manishearth/rust-gc/pull/116 getting merged
17 files changed
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -30,13 +30,12 @@
jrsonnet-types = { path = "../jrsonnet-types", version = "0.3.7" }
pathdiff = "0.2.0"
-closure = "0.3.0"
-
md5 = "0.7.0"
base64 = "0.13.0"
rustc-hash = "1.1.0"
thiserror = "1.0"
+gc = { version = "0.4.1", features = ["derive"] }
[dependencies.anyhow]
version = "1.0"
crates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/format.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/format.rs
@@ -2,11 +2,12 @@
#![allow(clippy::too_many_arguments)]
use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};
+use gc::{Finalize, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_types::ValType;
use thiserror::Error;
-#[derive(Debug, Clone, Error)]
+#[derive(Debug, Clone, Error, Trace, Finalize)]
pub enum FormatError {
#[error("truncated format code")]
TruncatedFormatCode,
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -126,6 +126,7 @@
buf.push('}');
}
Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
+ Val::DebugGcTraceValue(v) => manifest_json_ex_buf(&v.value, buf, cur_padding, options)?,
};
Ok(())
}
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,10 +1,11 @@
use crate::{
equals,
error::{Error::*, Result},
- parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,
- FuncVal, LazyVal, Val,
+ parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, DebugGcTraceValue,
+ EvaluationState, FuncVal, LazyVal, Val,
};
use format::{format_arr, format_obj};
+use gc::Gc;
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ArgsDesc, BinaryOpType, ExprLocation};
use jrsonnet_types::ty;
@@ -68,6 +69,8 @@
("md5".into(), builtin_md5),
("base64".into(), builtin_base64),
("trace".into(), builtin_trace),
+ ("gc".into(), builtin_gc),
+ ("gcTrace".into(), builtin_gc_trace),
("join".into(), builtin_join),
("escapeStringJson".into(), builtin_escape_string_json),
("manifestJsonEx".into(), builtin_manifest_json_ex),
@@ -301,7 +304,7 @@
parse_args!(context, "native", args, 1, [
0, x: ty!(string) => Val::Str;
], {
- Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Rc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)
+ Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Gc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)
})
}
@@ -446,6 +449,28 @@
})
}
+fn builtin_gc(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+ parse_args!(context, "gc", args, 1, [
+ 0, rest: ty!(any);
+ ], {
+ println!("GC start");
+ gc::force_collect();
+ println!("GC done");
+
+ Ok(rest)
+ })
+}
+
+fn builtin_gc_trace(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+ parse_args!(context, "gcTrace", args, 2, [
+ 0, name: ty!(string) => Val::Str;
+ 1, rest: ty!(any);
+ ], {
+
+ Ok(DebugGcTraceValue::new(name, rest))
+ })
+}
+
fn builtin_base64(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
parse_args!(context, "base64", args, 1, [
0, input: ty!((string | (Array<number>)));
crates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -2,9 +2,9 @@
error::{Error, LocError, Result},
throw, Context, FuncVal, Val,
};
-use std::rc::Rc;
+use gc::{Finalize, Gc, Trace};
-#[derive(Debug, Clone, thiserror::Error)]
+#[derive(Debug, Clone, thiserror::Error, Trace, Finalize)]
pub enum SortError {
#[error("sort key should be string or number")]
SortKeyShouldBeStringOrNumber,
@@ -59,13 +59,13 @@
Ok(sort_type)
}
-pub fn sort(ctx: Context, mut values: Rc<Vec<Val>>, key_getter: &FuncVal) -> Result<Rc<Vec<Val>>> {
+pub fn sort(ctx: Context, values: Gc<Vec<Val>>, key_getter: &FuncVal) -> Result<Gc<Vec<Val>>> {
if values.len() <= 1 {
return Ok(values);
}
if key_getter.is_ident() {
- let mvalues = Rc::make_mut(&mut values);
- let sort_type = get_sort_type(mvalues, |k| k)?;
+ let mut mvalues = (*values).clone();
+ let sort_type = get_sort_type(&mut mvalues, |k| k)?;
match sort_type {
SortKeyType::Number => mvalues.sort_by_key(|v| match v {
Val::Num(n) => NonNaNf64(*n),
@@ -77,7 +77,7 @@
}),
SortKeyType::Unknown => unreachable!(),
};
- Ok(values)
+ Ok(Gc::new(mvalues))
} else {
let mut vk = Vec::with_capacity(values.len());
for value in values.iter() {
@@ -98,6 +98,6 @@
}),
SortKeyType::Unknown => unreachable!(),
};
- Ok(Rc::new(vk.into_iter().map(|v| v.0).collect()))
+ Ok(Gc::new(vk.into_iter().map(|v| v.0).collect()))
}
}
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -1,13 +1,14 @@
use crate::{
- error::Error::*, map::LayeredHashMap, resolved_lazy_val, FutureWrapper, LazyBinding, LazyVal,
- ObjValue, Result, Val,
+ error::Error::*, map::LayeredHashMap, FutureWrapper, LazyBinding, LazyVal, ObjValue, Result,
+ Val,
};
+use gc::{Finalize, Gc, Trace};
use jrsonnet_interner::IStr;
use rustc_hash::FxHashMap;
+use std::fmt::Debug;
use std::hash::BuildHasherDefault;
-use std::{fmt::Debug, rc::Rc};
-#[derive(Clone)]
+#[derive(Clone, Trace, Finalize)]
pub struct ContextCreator(pub Context, pub FutureWrapper<FxHashMap<IStr, LazyBinding>>);
impl ContextCreator {
pub fn create(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<Context> {
@@ -20,6 +21,7 @@
}
}
+#[derive(Trace, Finalize)]
struct ContextInternals {
dollar: Option<ObjValue>,
this: Option<ObjValue>,
@@ -28,15 +30,12 @@
}
impl Debug for ContextInternals {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- f.debug_struct("Context")
- .field("this", &self.this.as_ref().map(|e| Rc::as_ptr(&e.0)))
- .field("bindings", &self.bindings)
- .finish()
+ f.debug_struct("Context").finish()
}
}
-#[derive(Debug, Clone)]
-pub struct Context(Rc<ContextInternals>);
+#[derive(Debug, Clone, Trace, Finalize)]
+pub struct Context(Gc<ContextInternals>);
impl Context {
pub fn new_future() -> FutureWrapper<Self> {
FutureWrapper::new()
@@ -55,7 +54,7 @@
}
pub fn new() -> Self {
- Self(Rc::new(ContextInternals {
+ Self(Gc::new(ContextInternals {
dollar: None,
this: None,
super_obj: None,
@@ -81,7 +80,7 @@
pub fn with_var(self, name: IStr, value: Val) -> Self {
let mut new_bindings =
FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
- new_bindings.insert(name, resolved_lazy_val!(value));
+ new_bindings.insert(name, LazyVal::new_resolved(value));
self.extend(new_bindings, None, None, None)
}
@@ -96,40 +95,21 @@
new_this: Option<ObjValue>,
new_super_obj: Option<ObjValue>,
) -> Self {
- match Rc::try_unwrap(self.0) {
- Ok(mut ctx) => {
- // Extended context aren't used by anything else, we can freely mutate it without cloning
- if let Some(dollar) = new_dollar {
- ctx.dollar = Some(dollar);
- }
- if let Some(this) = new_this {
- ctx.this = Some(this);
- }
- if let Some(super_obj) = new_super_obj {
- ctx.super_obj = Some(super_obj);
- }
- if !new_bindings.is_empty() {
- ctx.bindings = ctx.bindings.extend(new_bindings);
- }
- Self(Rc::new(ctx))
- }
- Err(ctx) => {
- let dollar = new_dollar.or_else(|| ctx.dollar.clone());
- let this = new_this.or_else(|| ctx.this.clone());
- let super_obj = new_super_obj.or_else(|| ctx.super_obj.clone());
- let bindings = if new_bindings.is_empty() {
- ctx.bindings.clone()
- } else {
- ctx.bindings.clone().extend(new_bindings)
- };
- Self(Rc::new(ContextInternals {
- dollar,
- this,
- super_obj,
- bindings,
- }))
- }
- }
+ let ctx = &self.0;
+ let dollar = new_dollar.or_else(|| ctx.dollar.clone());
+ let this = new_this.or_else(|| ctx.this.clone());
+ let super_obj = new_super_obj.or_else(|| ctx.super_obj.clone());
+ let bindings = if new_bindings.is_empty() {
+ ctx.bindings.clone()
+ } else {
+ ctx.bindings.clone().extend(new_bindings)
+ };
+ Self(Gc::new(ContextInternals {
+ dollar,
+ this,
+ super_obj,
+ bindings,
+ }))
}
pub fn extend_bound(self, new_bindings: FxHashMap<IStr, LazyVal>) -> Self {
let new_this = self.0.this.clone();
@@ -166,22 +146,6 @@
impl PartialEq for Context {
fn eq(&self, other: &Self) -> bool {
- Rc::ptr_eq(&self.0, &other.0)
- }
-}
-
-#[cfg(feature = "unstable")]
-#[derive(Debug, Clone)]
-pub struct WeakContext(std::rc::Weak<ContextInternals>);
-#[cfg(feature = "unstable")]
-impl WeakContext {
- pub fn upgrade(&self) -> Context {
- Context(self.0.upgrade().expect("context is removed"))
- }
-}
-#[cfg(feature = "unstable")]
-impl PartialEq for WeakContext {
- fn eq(&self, other: &Self) -> bool {
- self.0.ptr_eq(&other.0)
+ Gc::ptr_eq(&self.0, &other.0)
}
}
crates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -1,23 +1,23 @@
-use std::{cell::RefCell, rc::Rc};
+use gc::{Finalize, Gc, GcCell, Trace};
-#[derive(Clone)]
-pub struct FutureWrapper<V>(pub Rc<RefCell<Option<V>>>);
-impl<T> FutureWrapper<T> {
+#[derive(Clone, Trace, Finalize)]
+pub struct FutureWrapper<V: Trace + 'static>(pub Gc<GcCell<Option<V>>>);
+impl<T: Trace + 'static> FutureWrapper<T> {
pub fn new() -> Self {
- Self(Rc::new(RefCell::new(None)))
+ Self(Gc::new(GcCell::new(None)))
}
pub fn fill(self, value: T) {
assert!(self.0.borrow().is_none(), "wrapper is filled already");
self.0.borrow_mut().replace(value);
}
}
-impl<T: Clone> FutureWrapper<T> {
+impl<T: Clone + Trace + 'static> FutureWrapper<T> {
pub fn unwrap(&self) -> T {
self.0.borrow().as_ref().cloned().unwrap()
}
}
-impl<T> Default for FutureWrapper<T> {
+impl<T: Trace + 'static> Default for FutureWrapper<T> {
fn default() -> Self {
Self::new()
}
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,13 +2,14 @@
builtin::{format::FormatError, sort::SortError},
typed::TypeLocError,
};
+use gc::{Finalize, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
use jrsonnet_types::ValType;
use std::{path::PathBuf, rc::Rc};
use thiserror::Error;
-#[derive(Error, Debug, Clone)]
+#[derive(Error, Debug, Clone, Trace, Finalize)]
pub enum Error {
#[error("intrinsic not found: {0}")]
IntrinsicNotFound(IStr),
@@ -88,6 +89,7 @@
ImportSyntaxError {
path: Rc<PathBuf>,
source_code: IStr,
+ #[unsafe_ignore_trace]
error: Box<jrsonnet_parser::ParseError>,
},
@@ -95,6 +97,8 @@
RuntimeError(IStr),
#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]
StackOverflow,
+ #[error("infinite recursion detected")]
+ RecursiveLazyValueEvaluation,
#[error("tried to index by fractional value")]
FractionalIndex,
#[error("attempted to divide by zero")]
@@ -142,15 +146,15 @@
}
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Trace, Finalize)]
pub struct StackTraceElement {
pub location: Option<ExprLocation>,
pub desc: String,
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
pub struct StackTrace(pub Vec<StackTraceElement>);
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
pub struct LocError(Box<(Error, StackTrace)>);
impl LocError {
pub fn new(e: Error) -> Self {
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -1,8 +1,9 @@
use crate::{
- equals, error::Error::*, lazy_val, push, throw, with_state, ArrValue, Context, ContextCreator,
- FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
+ equals, error::Error::*, push, throw, with_state, ArrValue, Bindable, Context, ContextCreator,
+ FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjMember, ObjValue,
+ ObjectAssertion, Result, Val,
};
-use closure::closure;
+use gc::{custom_trace, Finalize, Gc, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{
ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprLocation, FieldMember,
@@ -20,17 +21,62 @@
let b = b.clone();
if let Some(params) = &b.params {
let params = params.clone();
- LazyVal::new(Box::new(move || {
- Ok(evaluate_method(
- context_creator.unwrap(),
- b.name.clone(),
- params.clone(),
- b.value.clone(),
- ))
+
+ struct LazyMethodBinding {
+ context_creator: FutureWrapper<Context>,
+ name: IStr,
+ params: ParamsDesc,
+ value: LocExpr,
+ }
+ impl Finalize for LazyMethodBinding {}
+ unsafe impl Trace for LazyMethodBinding {
+ custom_trace!(this, {
+ mark(&this.context_creator);
+ mark(&this.name);
+ mark(&this.params);
+ mark(&this.value);
+ });
+ }
+ impl LazyValValue for LazyMethodBinding {
+ fn get(self: Box<Self>) -> Result<Val> {
+ Ok(evaluate_method(
+ self.context_creator.unwrap(),
+ self.name,
+ self.params,
+ self.value,
+ ))
+ }
+ }
+
+ LazyVal::new(Box::new(LazyMethodBinding {
+ context_creator,
+ name: b.name.clone(),
+ params,
+ value: b.value.clone(),
}))
} else {
- LazyVal::new(Box::new(move || {
- evaluate_named(context_creator.unwrap(), &b.value, b.name.clone())
+ struct LazyNamedBinding {
+ context_creator: FutureWrapper<Context>,
+ name: IStr,
+ value: LocExpr,
+ }
+ impl Finalize for LazyNamedBinding {}
+ unsafe impl Trace for LazyNamedBinding {
+ custom_trace!(this, {
+ mark(&this.context_creator);
+ mark(&this.name);
+ mark(&this.value);
+ });
+ }
+ impl LazyValValue for LazyNamedBinding {
+ fn get(self: Box<Self>) -> Result<Val> {
+ evaluate_named(self.context_creator.unwrap(), &self.value, self.name)
+ }
+ }
+ LazyVal::new(Box::new(LazyNamedBinding {
+ context_creator,
+ name: b.name.clone(),
+ value: b.value,
}))
}
}
@@ -39,37 +85,129 @@
let b = b.clone();
if let Some(params) = &b.params {
let params = params.clone();
+
+ struct BindableMethodLazyVal {
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+
+ context_creator: ContextCreator,
+ name: IStr,
+ params: ParamsDesc,
+ value: LocExpr,
+ }
+ impl Finalize for BindableMethodLazyVal {}
+ unsafe impl Trace for BindableMethodLazyVal {
+ custom_trace!(this, {
+ mark(&this.this);
+ mark(&this.super_obj);
+ mark(&this.context_creator);
+ mark(&this.name);
+ mark(&this.params);
+ mark(&this.value);
+ });
+ }
+ impl LazyValValue for BindableMethodLazyVal {
+ fn get(self: Box<Self>) -> Result<Val> {
+ Ok(evaluate_method(
+ self.context_creator.create(self.this, self.super_obj)?,
+ self.name,
+ self.params,
+ self.value,
+ ))
+ }
+ }
+
+ #[derive(Trace, Finalize)]
+ struct BindableMethod {
+ context_creator: ContextCreator,
+ name: IStr,
+ params: ParamsDesc,
+ value: LocExpr,
+ }
+ impl Bindable for BindableMethod {
+ fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
+ Ok(LazyVal::new(Box::new(BindableMethodLazyVal {
+ this: this.clone(),
+ super_obj: super_obj.clone(),
+
+ context_creator: self.context_creator.clone(),
+ name: self.name.clone(),
+ params: self.params.clone(),
+ value: self.value.clone(),
+ })))
+ }
+ }
+
(
b.name.clone(),
- LazyBinding::Bindable(Rc::new(move |this, super_obj| {
- Ok(lazy_val!(
- closure!(clone b, clone params, clone context_creator, || Ok(evaluate_method(
- context_creator.create(this.clone(), super_obj.clone())?,
- b.name.clone(),
- params.clone(),
- b.value.clone(),
- )))
- ))
- })),
+ LazyBinding::Bindable(Gc::new(Box::new(BindableMethod {
+ context_creator,
+ name: b.name.clone(),
+ params,
+ value: b.value.clone(),
+ }))),
)
} else {
+ struct BindableNamedLazyVal {
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+
+ context_creator: ContextCreator,
+ name: IStr,
+ value: LocExpr,
+ }
+ impl Finalize for BindableNamedLazyVal {}
+ unsafe impl Trace for BindableNamedLazyVal {
+ custom_trace!(this, {
+ mark(&this.this);
+ mark(&this.super_obj);
+ mark(&this.context_creator);
+ mark(&this.name);
+ mark(&this.value);
+ });
+ }
+ impl LazyValValue for BindableNamedLazyVal {
+ fn get(self: Box<Self>) -> Result<Val> {
+ evaluate_named(
+ self.context_creator.create(self.this, self.super_obj)?,
+ &self.value,
+ self.name,
+ )
+ }
+ }
+
+ #[derive(Trace, Finalize)]
+ struct BindableNamed {
+ context_creator: ContextCreator,
+ name: IStr,
+ value: LocExpr,
+ }
+ impl Bindable for BindableNamed {
+ fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
+ Ok(LazyVal::new(Box::new(BindableNamedLazyVal {
+ this,
+ super_obj,
+
+ context_creator: self.context_creator.clone(),
+ name: self.name.clone(),
+ value: self.value.clone(),
+ })))
+ }
+ }
+
(
b.name.clone(),
- LazyBinding::Bindable(Rc::new(move |this, super_obj| {
- Ok(lazy_val!(closure!(clone context_creator, clone b, ||
- evaluate_named(
- context_creator.create(this.clone(), super_obj.clone())?,
- &b.value,
- b.name.clone()
- )
- )))
- })),
+ LazyBinding::Bindable(Gc::new(Box::new(BindableNamed {
+ context_creator,
+ name: b.name.clone(),
+ value: b.value.clone(),
+ }))),
)
}
}
pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {
- Val::Func(Rc::new(FuncVal::Normal(FuncDesc {
+ Val::Func(Gc::new(FuncVal::Normal(FuncDesc {
name,
ctx,
params,
@@ -105,6 +243,9 @@
pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {
Ok(match (a, b) {
+ (Val::DebugGcTraceValue(v1), Val::DebugGcTraceValue(v2)) => {
+ evaluate_add_op(&v1.value, &v2.value)?
+ }
(Val::Str(v1), Val::Str(v2)) => Val::Str(((**v1).to_owned() + v2).into()),
// Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)
@@ -257,7 +398,7 @@
}
let mut new_members = FxHashMap::default();
- let mut assertions = Vec::new();
+ let mut assertions: Vec<Box<dyn ObjectAssertion>> = Vec::new();
for member in members.iter() {
match member {
Member::Field(FieldMember {
@@ -272,20 +413,36 @@
continue;
}
let name = name.unwrap();
+
+ #[derive(Trace, Finalize)]
+ struct ObjMemberBinding {
+ context_creator: ContextCreator,
+ value: LocExpr,
+ name: IStr,
+ }
+ impl Bindable for ObjMemberBinding {
+ fn bind(
+ &self,
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+ ) -> Result<LazyVal> {
+ Ok(LazyVal::new_resolved(evaluate_named(
+ self.context_creator.create(this, super_obj)?,
+ &self.value,
+ self.name.clone(),
+ )?))
+ }
+ }
new_members.insert(
name.clone(),
ObjMember {
add: *plus,
visibility: *visibility,
- invoke: LazyBinding::Bindable(Rc::new(
- closure!(clone name, clone value, clone context_creator, |this, super_obj| {
- Ok(LazyVal::new_resolved(evaluate_named(
- context_creator.create(this, super_obj)?,
- &value,
- name.clone(),
- )?))
- }),
- )),
+ invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
+ context_creator: context_creator.clone(),
+ value: value.clone(),
+ name,
+ }))),
location: value.1.clone(),
},
);
@@ -301,33 +458,73 @@
continue;
}
let name = name.unwrap();
+ #[derive(Trace, Finalize)]
+ struct ObjMemberBinding {
+ context_creator: ContextCreator,
+ value: LocExpr,
+ params: ParamsDesc,
+ name: IStr,
+ }
+ impl Bindable for ObjMemberBinding {
+ fn bind(
+ &self,
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+ ) -> Result<LazyVal> {
+ Ok(LazyVal::new_resolved(evaluate_method(
+ self.context_creator.create(this, super_obj)?,
+ self.name.clone(),
+ self.params.clone(),
+ self.value.clone(),
+ )))
+ }
+ }
new_members.insert(
name.clone(),
ObjMember {
add: false,
visibility: Visibility::Hidden,
- invoke: LazyBinding::Bindable(Rc::new(
- closure!(clone value, clone context_creator, clone params, clone name, |this, super_obj| {
- // TODO: Assert
- Ok(LazyVal::new_resolved(evaluate_method(
- context_creator.create(this, super_obj)?,
- name.clone(),
- params.clone(),
- value.clone(),
- )))
- }),
- )),
+ invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
+ context_creator: context_creator.clone(),
+ value: value.clone(),
+ params: params.clone(),
+ name,
+ }))),
location: value.1.clone(),
},
);
}
Member::BindStmt(_) => {}
Member::AssertStmt(stmt) => {
- assertions.push(stmt.clone());
+ struct ObjectAssert {
+ context_creator: ContextCreator,
+ assert: AssertStmt,
+ }
+ impl Finalize for ObjectAssert {}
+ unsafe impl Trace for ObjectAssert {
+ custom_trace!(this, {
+ mark(&this.context_creator);
+ mark(&this.assert);
+ });
+ }
+ impl ObjectAssertion for ObjectAssert {
+ fn run(
+ &self,
+ this: Option<ObjValue>,
+ super_obj: Option<ObjValue>,
+ ) -> Result<()> {
+ let ctx = self.context_creator.create(this, super_obj)?;
+ evaluate_assert(ctx, &self.assert)
+ }
+ }
+ assertions.push(Box::new(ObjectAssert {
+ context_creator: context_creator.clone(),
+ assert: stmt.clone(),
+ }));
}
}
}
- let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(assertions));
+ let this = ObjValue::new(None, Gc::new(new_members), Gc::new(assertions));
future_this.fill(this.clone());
Ok(this)
}
@@ -361,16 +558,37 @@
match key {
Val::Null => {}
Val::Str(n) => {
+ #[derive(Trace, Finalize)]
+ struct ObjCompBinding {
+ context: Context,
+ value: LocExpr,
+ }
+ impl Bindable for ObjCompBinding {
+ fn bind(
+ &self,
+ this: Option<ObjValue>,
+ _super_obj: Option<ObjValue>,
+ ) -> Result<LazyVal> {
+ Ok(LazyVal::new_resolved(evaluate(
+ self.context.clone().extend(
+ FxHashMap::default(),
+ None,
+ this,
+ None,
+ ),
+ &self.value,
+ )?))
+ }
+ }
new_members.insert(
n,
ObjMember {
add: false,
visibility: Visibility::Normal,
- invoke: LazyBinding::Bindable(Rc::new(
- closure!(clone ctx, clone obj.value, |this, _super_obj| {
- Ok(LazyVal::new_resolved(evaluate(ctx.clone().extend(FxHashMap::default(), None, this, None), &value)?))
- }),
- )),
+ invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjCompBinding {
+ context: ctx.clone(),
+ value: obj.value.clone(),
+ }))),
location: obj.value.1.clone(),
},
);
@@ -381,7 +599,7 @@
Ok(())
})?;
- let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(Vec::new()));
+ let this = ObjValue::new(None, Gc::new(new_members), Gc::new(Vec::new()));
future_this.fill(this.clone());
this
}
@@ -486,7 +704,7 @@
if let Some(v) = v.get(s.clone())? {
Ok(v)
} else if v.get("__intrinsic_namespace__".into())?.is_some() {
- Ok(Val::Func(Rc::new(FuncVal::Intrinsic(s))))
+ Ok(Val::Func(Gc::new(FuncVal::Intrinsic(s))))
} else {
throw!(NoSuchField(s))
}
@@ -549,11 +767,27 @@
Arr(items) => {
let mut out = Vec::with_capacity(items.len());
for item in items {
- out.push(LazyVal::new(Box::new(
- closure!(clone context, clone item, || {
- evaluate(context.clone(), &item)
- }),
- )));
+ // TODO: Implement ArrValue::Lazy with same context for every element?
+ struct ArrayElement {
+ context: Context,
+ item: LocExpr,
+ }
+ impl Finalize for ArrayElement {}
+ unsafe impl Trace for ArrayElement {
+ custom_trace!(this, {
+ mark(&this.context);
+ mark(&this.item);
+ });
+ }
+ impl LazyValValue for ArrayElement {
+ fn get(self: Box<Self>) -> Result<Val> {
+ evaluate(self.context, &self.item)
+ }
+ }
+ out.push(LazyVal::new(Box::new(ArrayElement {
+ context: context.clone(),
+ item: item.clone(),
+ })));
}
Val::Arr(out.into())
}
@@ -563,7 +797,7 @@
out.push(evaluate(ctx, expr)?);
Ok(())
})?;
- Val::Arr(ArrValue::Eager(Rc::new(out)))
+ Val::Arr(ArrValue::Eager(Gc::new(out)))
}
Obj(body) => Val::Obj(evaluate_object(context, body)?),
ObjExtend(s, t) => evaluate_add_op(
@@ -576,7 +810,7 @@
Function(params, body) => {
evaluate_method(context, "anonymous".into(), params.clone(), body.clone())
}
- Intrinsic(name) => Val::Func(Rc::new(FuncVal::Intrinsic(name.clone()))),
+ Intrinsic(name) => Val::Func(Gc::new(FuncVal::Intrinsic(name.clone()))),
AssertExpr(assert, returned) => {
evaluate_assert(context.clone(), assert)?;
evaluate(context, returned)?
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,7 +1,7 @@
-use crate::{error::Error::*, evaluate, lazy_val, resolved_lazy_val, throw, Context, Result, Val};
-use closure::closure;
+use crate::{error::Error::*, evaluate, throw, Context, LazyVal, LazyValValue, Result, Val};
+use gc::{custom_trace, Finalize, Trace};
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, ParamsDesc};
+use jrsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};
use rustc_hash::FxHashMap;
use std::{collections::HashMap, hash::BuildHasherDefault};
@@ -53,9 +53,29 @@
throw!(FunctionParameterNotBoundInCall(p.0.clone()));
};
let val = if tailstrict {
- resolved_lazy_val!(evaluate(ctx, expr)?)
+ LazyVal::new_resolved(evaluate(ctx, expr)?)
} else {
- lazy_val!(closure!(clone ctx, clone expr, ||evaluate(ctx.clone(), &expr)))
+ struct EvaluateLazyVal {
+ context: Context,
+ expr: LocExpr,
+ }
+ impl Finalize for EvaluateLazyVal {}
+ unsafe impl Trace for EvaluateLazyVal {
+ custom_trace!(this, {
+ mark(&this.context);
+ mark(&this.expr);
+ });
+ }
+ impl LazyValValue for EvaluateLazyVal {
+ fn get(self: Box<Self>) -> Result<Val> {
+ evaluate(self.context, &self.expr)
+ }
+ }
+
+ LazyVal::new(Box::new(EvaluateLazyVal {
+ context: ctx.clone(),
+ expr: expr.clone(),
+ }))
};
out.insert(p.0.clone(), val);
}
@@ -89,19 +109,30 @@
// Fill defaults
for (id, p) in params.iter().enumerate() {
let val = if let Some(arg) = positioned_args[id].take() {
- resolved_lazy_val!(arg)
+ LazyVal::new_resolved(arg)
} else if let Some(default) = &p.1 {
if tailstrict {
- resolved_lazy_val!(evaluate(
+ LazyVal::new_resolved(evaluate(
body_ctx.clone().expect(NO_DEFAULT_CONTEXT),
- default
+ default,
)?)
} else {
let body_ctx = body_ctx.clone();
let default = default.clone();
- lazy_val!(move || {
- evaluate(body_ctx.clone().expect(NO_DEFAULT_CONTEXT), &default)
- })
+ #[derive(Trace, Finalize)]
+ struct EvaluateLazyVal {
+ body_ctx: Option<Context>,
+ default: LocExpr,
+ }
+ impl LazyValValue for EvaluateLazyVal {
+ fn get(self: Box<Self>) -> Result<Val> {
+ evaluate(
+ self.body_ctx.clone().expect(NO_DEFAULT_CONTEXT),
+ &self.default,
+ )
+ }
+ }
+ LazyVal::new(Box::new(EvaluateLazyVal { body_ctx, default }))
}
} else {
throw!(FunctionParameterNotBoundInCall(p.0.clone()));
@@ -135,7 +166,7 @@
} else {
throw!(FunctionParameterNotBoundInCall(p.0.clone()));
};
- out.insert(p.0.clone(), resolved_lazy_val!(val));
+ out.insert(p.0.clone(), LazyVal::new_resolved(val));
}
Ok(body_ctx.unwrap_or(ctx).extend(out, None, None, None))
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -2,6 +2,7 @@
error::{Error::*, LocError, Result},
throw, Context, LazyBinding, LazyVal, ObjMember, ObjValue, Val,
};
+use gc::Gc;
use jrsonnet_parser::Visibility;
use rustc_hash::FxHasher;
use serde_json::{Map, Number, Value};
@@ -9,7 +10,6 @@
collections::HashMap,
convert::{TryFrom, TryInto},
hash::BuildHasherDefault,
- rc::Rc,
};
impl TryFrom<&Val> for Value {
@@ -42,6 +42,7 @@
Self::Object(out)
}
Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),
+ Val::DebugGcTraceValue(v) => Value::try_from(&*v.value as &Val)?,
})
}
}
@@ -76,12 +77,7 @@
},
);
}
- Self::Obj(ObjValue::new(
- Context::new(),
- None,
- Rc::new(entries),
- Rc::new(Vec::new()),
- ))
+ Self::Obj(ObjValue::new(None, Gc::new(entries), Gc::new(Vec::new())))
}
}
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -25,6 +25,7 @@
use error::{Error::*, LocError, Result, StackTraceElement};
pub use evaluate::*;
pub use function::parse_function_call;
+use gc::{Finalize, Gc, Trace};
pub use import::*;
use jrsonnet_interner::IStr;
use jrsonnet_parser::*;
@@ -42,10 +43,12 @@
use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};
pub use val::*;
-type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;
-#[derive(Clone)]
+pub trait Bindable: Trace {
+ fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;
+}
+#[derive(Trace, Finalize, Clone)]
pub enum LazyBinding {
- Bindable(Rc<BindableFn>),
+ Bindable(Gc<Box<dyn Bindable>>),
Bound(LazyVal),
}
@@ -57,7 +60,7 @@
impl LazyBinding {
pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {
match self {
- Self::Bindable(v) => v(this, super_obj),
+ Self::Bindable(v) => v.bind(this, super_obj),
Self::Bound(v) => Ok(v.clone()),
}
}
@@ -71,7 +74,7 @@
/// Used for s`td.extVar`
pub ext_vars: HashMap<IStr, Val>,
/// Used for ext.native
- pub ext_natives: HashMap<IStr, Rc<NativeCallback>>,
+ pub ext_natives: HashMap<IStr, Gc<NativeCallback>>,
/// TLA vars
pub tla_vars: HashMap<IStr, Val>,
/// Global variables are inserted in default context
@@ -270,7 +273,7 @@
let mut new_bindings: FxHashMap<IStr, LazyVal> =
FxHashMap::with_capacity_and_hasher(globals.len(), BuildHasherDefault::default());
for (name, value) in globals.iter() {
- new_bindings.insert(name.clone(), resolved_lazy_val!(value.clone()));
+ new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));
}
Context::new().extend_bound(new_bindings)
}
@@ -449,7 +452,7 @@
self.settings_mut().import_resolver = resolver;
}
- pub fn add_native(&self, name: IStr, cb: Rc<NativeCallback>) {
+ pub fn add_native(&self, name: IStr, cb: Gc<NativeCallback>) {
self.settings_mut().ext_natives.insert(name, cb);
}
crates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/map.rs
+++ b/crates/jrsonnet-evaluator/src/map.rs
@@ -1,28 +1,29 @@
+use gc::{Finalize, Gc, Trace};
use jrsonnet_interner::IStr;
use rustc_hash::FxHashMap;
-use std::rc::Rc;
-#[derive(Default, Debug)]
-struct LayeredHashMapInternals<V> {
+pub struct LayeredHashMapInternals<V: Trace + Finalize + 'static> {
parent: Option<LayeredHashMap<V>>,
current: FxHashMap<IStr, V>,
}
-#[derive(Debug)]
-pub struct LayeredHashMap<V>(Rc<LayeredHashMapInternals<V>>);
+unsafe impl<V: Trace + Finalize + 'static> Trace for LayeredHashMapInternals<V> {
+ gc::custom_trace!(this, {
+ mark(&this.parent);
+ mark(&this.current);
+ });
+}
+impl<V: Trace + Finalize + 'static> Finalize for LayeredHashMapInternals<V> {}
+
+#[derive(Trace, Finalize)]
+pub struct LayeredHashMap<V: Trace + Finalize + 'static>(Gc<LayeredHashMapInternals<V>>);
-impl<V> LayeredHashMap<V> {
+impl<V: Trace + 'static> LayeredHashMap<V> {
pub fn extend(self, new_layer: FxHashMap<IStr, V>) -> Self {
- match Rc::try_unwrap(self.0) {
- Ok(mut map) => {
- map.current.extend(new_layer);
- Self(Rc::new(map))
- }
- Err(this) => Self(Rc::new(LayeredHashMapInternals {
- parent: Some(Self(this)),
- current: new_layer,
- })),
- }
+ Self(Gc::new(LayeredHashMapInternals {
+ parent: Some(self),
+ current: new_layer,
+ }))
}
pub fn get(&self, key: &IStr) -> Option<&V> {
@@ -33,15 +34,15 @@
}
}
-impl<V> Clone for LayeredHashMap<V> {
+impl<V: Trace + 'static> Clone for LayeredHashMap<V> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
-impl<V> Default for LayeredHashMap<V> {
+impl<V: Trace + 'static> Default for LayeredHashMap<V> {
fn default() -> Self {
- Self(Rc::new(LayeredHashMapInternals {
+ Self(Gc::new(LayeredHashMapInternals {
parent: None,
current: FxHashMap::default(),
}))
crates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -1,27 +1,27 @@
#![allow(clippy::type_complexity)]
use crate::{error::Result, Val};
+use gc::{Finalize, Trace};
use jrsonnet_parser::ParamsDesc;
use std::fmt::Debug;
use std::path::PathBuf;
use std::rc::Rc;
+pub trait NativeCallbackHandler: Trace {
+ fn call(&self, from: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val>;
+}
+
+#[derive(Trace, Finalize)]
pub struct NativeCallback {
pub params: ParamsDesc,
- handler: Box<dyn Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val>>,
+ handler: Box<dyn NativeCallbackHandler>,
}
impl NativeCallback {
- pub fn new(
- params: ParamsDesc,
- handler: impl Fn(Option<Rc<PathBuf>>, &[Val]) -> Result<Val> + 'static,
- ) -> Self {
- Self {
- params,
- handler: Box::new(handler),
- }
+ pub fn new(params: ParamsDesc, handler: Box<dyn NativeCallbackHandler>) -> Self {
+ Self { params, handler }
}
pub fn call(&self, caller: Option<Rc<PathBuf>>, args: &[Val]) -> Result<Val> {
- (self.handler)(caller, args)
+ self.handler.call(caller, args)
}
}
impl Debug for NativeCallback {
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,11 +1,12 @@
use crate::{evaluate_add_op, evaluate_assert, Context, LazyBinding, Result, Val};
+use gc::{Finalize, Gc, GcCell, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{AssertStmt, ExprLocation, Visibility};
use rustc_hash::{FxHashMap, FxHashSet};
use std::hash::{Hash, Hasher};
-use std::{cell::RefCell, fmt::Debug, hash::BuildHasherDefault, rc::Rc};
+use std::{fmt::Debug, hash::BuildHasherDefault};
-#[derive(Debug)]
+#[derive(Debug, Trace, Finalize)]
pub struct ObjMember {
pub add: bool,
pub visibility: Visibility,
@@ -13,21 +14,24 @@
pub location: Option<ExprLocation>,
}
+pub trait ObjectAssertion: Trace {
+ fn run(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<()>;
+}
+
// Field => This
type CacheKey = (IStr, ObjValue);
-#[derive(Debug)]
+#[derive(Trace, Finalize)]
pub struct ObjValueInternals {
- context: Context,
super_obj: Option<ObjValue>,
- assertions: Rc<Vec<AssertStmt>>,
- assertions_ran: RefCell<FxHashSet<ObjValue>>,
+ assertions: Gc<Vec<Box<dyn ObjectAssertion>>>,
+ assertions_ran: GcCell<FxHashSet<ObjValue>>,
this_obj: Option<ObjValue>,
- this_entries: Rc<FxHashMap<IStr, ObjMember>>,
- value_cache: RefCell<FxHashMap<CacheKey, Option<Val>>>,
+ this_entries: Gc<FxHashMap<IStr, ObjMember>>,
+ value_cache: GcCell<FxHashMap<CacheKey, Option<Val>>>,
}
-#[derive(Clone)]
-pub struct ObjValue(pub(crate) Rc<ObjValueInternals>);
+#[derive(Clone, Trace, Finalize)]
+pub struct ObjValue(pub(crate) Gc<ObjValueInternals>);
impl Debug for ObjValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(super_obj) = self.0.super_obj.as_ref() {
@@ -55,39 +59,30 @@
impl ObjValue {
pub fn new(
- context: Context,
super_obj: Option<Self>,
- this_entries: Rc<FxHashMap<IStr, ObjMember>>,
- assertions: Rc<Vec<AssertStmt>>,
+ this_entries: Gc<FxHashMap<IStr, ObjMember>>,
+ assertions: Gc<Vec<Box<dyn ObjectAssertion>>>,
) -> Self {
- Self(Rc::new(ObjValueInternals {
- context,
+ Self(Gc::new(ObjValueInternals {
super_obj,
assertions,
- assertions_ran: RefCell::new(FxHashSet::default()),
+ assertions_ran: GcCell::new(FxHashSet::default()),
this_obj: None,
this_entries,
- value_cache: RefCell::new(FxHashMap::default()),
+ value_cache: GcCell::new(FxHashMap::default()),
}))
}
pub fn new_empty() -> Self {
- Self::new(
- Context::new(),
- None,
- Rc::new(FxHashMap::default()),
- Rc::new(Vec::new()),
- )
+ Self::new(None, Gc::new(FxHashMap::default()), Gc::new(Vec::new()))
}
pub fn extend_from(&self, super_obj: Self) -> Self {
match &self.0.super_obj {
None => Self::new(
- self.0.context.clone(),
Some(super_obj),
self.0.this_entries.clone(),
self.0.assertions.clone(),
),
Some(v) => Self::new(
- self.0.context.clone(),
Some(v.extend_from(super_obj)),
self.0.this_entries.clone(),
self.0.assertions.clone(),
@@ -95,14 +90,13 @@
}
}
pub fn with_this(&self, this_obj: Self) -> Self {
- Self(Rc::new(ObjValueInternals {
- context: self.0.context.clone(),
+ Self(Gc::new(ObjValueInternals {
super_obj: self.0.super_obj.clone(),
assertions: self.0.assertions.clone(),
- assertions_ran: RefCell::new(FxHashSet::default()),
+ assertions_ran: GcCell::new(FxHashSet::default()),
this_obj: Some(this_obj),
this_entries: self.0.this_entries.clone(),
- value_cache: RefCell::new(FxHashMap::default()),
+ value_cache: GcCell::new(FxHashMap::default()),
}))
}
@@ -203,12 +197,7 @@
pub fn extend_with_field(self, key: IStr, value: ObjMember) -> Self {
let mut new = FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
new.insert(key, value);
- Self::new(
- Context::new(),
- Some(self),
- Rc::new(new),
- Rc::new(Vec::new()),
- )
+ Self::new(Some(self), Gc::new(new), Gc::new(Vec::new()))
}
fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
@@ -249,13 +238,7 @@
fn run_assertions_raw(&self, real_this: &Self) -> Result<()> {
if self.0.assertions_ran.borrow_mut().insert(real_this.clone()) {
for assertion in self.0.assertions.iter() {
- if let Err(e) = evaluate_assert(
- self.0
- .context
- .clone()
- .with_this_super(real_this.clone(), self.0.super_obj.clone()),
- assertion,
- ) {
+ if let Err(e) = assertion.run(Some(real_this.clone()), self.0.super_obj.clone()) {
self.0.assertions_ran.borrow_mut().remove(real_this);
return Err(e);
}
@@ -271,19 +254,19 @@
}
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
- Rc::ptr_eq(&a.0, &b.0)
+ Gc::ptr_eq(&a.0, &b.0)
}
}
impl PartialEq for ObjValue {
fn eq(&self, other: &Self) -> bool {
- Rc::ptr_eq(&self.0, &other.0)
+ Gc::ptr_eq(&self.0, &other.0)
}
}
impl Eq for ObjValue {}
impl Hash for ObjValue {
- fn hash<H: Hasher>(&self, state: &mut H) {
- state.write_usize(Rc::as_ptr(&self.0) as usize)
+ fn hash<H: Hasher>(&self, hasher: &mut H) {
+ hasher.write_usize(&*self.0 as *const _ as usize)
}
}
crates/jrsonnet-evaluator/src/typed.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed.rs
+++ b/crates/jrsonnet-evaluator/src/typed.rs
@@ -4,6 +4,7 @@
error::{Error, LocError, Result},
push, Val,
};
+use gc::{Finalize, Trace};
use jrsonnet_parser::ExprLocation;
use jrsonnet_types::{ComplexValType, ValType};
use thiserror::Error;
@@ -20,7 +21,7 @@
}};
}
-#[derive(Debug, Error, Clone)]
+#[derive(Debug, Error, Clone, Trace, Finalize)]
pub enum TypeError {
#[error("expected {0}, got {1}")]
ExpectedGot(ComplexValType, ValType),
@@ -37,7 +38,7 @@
}
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
pub struct TypeLocError(Box<TypeError>, ValuePathStack);
impl From<TypeError> for TypeLocError {
fn from(e: TypeError) -> Self {
@@ -59,7 +60,7 @@
}
}
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Trace, Finalize)]
pub struct TypeLocErrorList(Vec<TypeLocError>);
impl Display for TypeLocErrorList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -122,7 +123,7 @@
}
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Trace, Finalize)]
enum ValuePathItem {
Field(Rc<str>),
Index(u64),
@@ -137,7 +138,7 @@
}
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Trace, Finalize)]
struct ValuePathStack(Vec<ValuePathItem>);
impl Display for ValuePathStack {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
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.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 map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {287 let mut out = Vec::with_capacity(self.len());288289 for value in self.iter() {290 out.push(mapper(value?)?);291 }292293 Ok(Self::Eager(Rc::new(out)))294 }295296 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {297 let mut out = Vec::with_capacity(self.len());298299 for value in self.iter() {300 let value = value?;301 if filter(&value)? {302 out.push(value);303 }304 }305306 Ok(Self::Eager(Rc::new(out)))307 }308309 pub fn ptr_eq(a: &Self, b: &Self) -> bool {310 match (a, b) {311 (Self::Lazy(a), Self::Lazy(b)) => Rc::ptr_eq(a, b),312 (Self::Eager(a), Self::Eager(b)) => Rc::ptr_eq(a, b),313 _ => false,314 }315 }316}317318impl From<Vec<LazyVal>> for ArrValue {319 fn from(v: Vec<LazyVal>) -> Self {320 Self::Lazy(Rc::new(v))321 }322}323324impl From<Vec<Val>> for ArrValue {325 fn from(v: Vec<Val>) -> Self {326 Self::Eager(Rc::new(v))327 }328}329330#[derive(Debug, Clone)]331pub enum Val {332 Bool(bool),333 Null,334 Str(IStr),335 Num(f64),336 Arr(ArrValue),337 Obj(ObjValue),338 Func(Rc<FuncVal>),339}340341macro_rules! matches_unwrap {342 ($e: expr, $p: pat, $r: expr) => {343 match $e {344 $p => $r,345 _ => panic!("no match"),346 }347 };348}349impl Val {350 /// Creates `Val::Num` after checking for numeric overflow.351 /// As numbers are `f64`, we can just check for their finity.352 pub fn new_checked_num(num: f64) -> Result<Self> {353 if num.is_finite() {354 Ok(Self::Num(num))355 } else {356 throw!(RuntimeError("overflow".into()))357 }358 }359360 pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {361 let this_type = self.value_type();362 if this_type != val_type {363 throw!(TypeMismatch(context, vec![val_type], this_type))364 } else {365 Ok(())366 }367 }368 pub fn unwrap_num(self) -> Result<f64> {369 Ok(matches_unwrap!(self, Self::Num(v), v))370 }371 pub fn unwrap_func(self) -> Result<Rc<FuncVal>> {372 Ok(matches_unwrap!(self, Self::Func(v), v))373 }374 pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {375 self.assert_type(context, ValType::Bool)?;376 Ok(matches_unwrap!(self, Self::Bool(v), v))377 }378 pub fn try_cast_str(self, context: &'static str) -> Result<IStr> {379 self.assert_type(context, ValType::Str)?;380 Ok(matches_unwrap!(self, Self::Str(v), v))381 }382 pub fn try_cast_num(self, context: &'static str) -> Result<f64> {383 self.assert_type(context, ValType::Num)?;384 self.unwrap_num()385 }386 pub const fn value_type(&self) -> ValType {387 match self {388 Self::Str(..) => ValType::Str,389 Self::Num(..) => ValType::Num,390 Self::Arr(..) => ValType::Arr,391 Self::Obj(..) => ValType::Obj,392 Self::Bool(_) => ValType::Bool,393 Self::Null => ValType::Null,394 Self::Func(..) => ValType::Func,395 }396 }397398 pub fn to_string(&self) -> Result<IStr> {399 Ok(match self {400 Self::Bool(true) => "true".into(),401 Self::Bool(false) => "false".into(),402 Self::Null => "null".into(),403 Self::Str(s) => s.clone(),404 v => manifest_json_ex(405 v,406 &ManifestJsonOptions {407 padding: "",408 mtype: ManifestType::ToString,409 },410 )?411 .into(),412 })413 }414415 /// Expects value to be object, outputs (key, manifested value) pairs416 pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {417 let obj = match self {418 Self::Obj(obj) => obj,419 _ => throw!(MultiManifestOutputIsNotAObject),420 };421 let keys = obj.fields();422 let mut out = Vec::with_capacity(keys.len());423 for key in keys {424 let value = obj425 .get(key.clone())?426 .expect("item in object")427 .manifest(ty)?;428 out.push((key, value));429 }430 Ok(out)431 }432433 /// Expects value to be array, outputs manifested values434 pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {435 let arr = match self {436 Self::Arr(a) => a,437 _ => throw!(StreamManifestOutputIsNotAArray),438 };439 let mut out = Vec::with_capacity(arr.len());440 for i in arr.iter() {441 out.push(i?.manifest(ty)?);442 }443 Ok(out)444 }445446 pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {447 Ok(match ty {448 ManifestFormat::YamlStream(format) => {449 let arr = match self {450 Self::Arr(a) => a,451 _ => throw!(StreamManifestOutputIsNotAArray),452 };453 let mut out = String::new();454455 match format as &ManifestFormat {456 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),457 ManifestFormat::String => throw!(StreamManifestCannotNestString),458 _ => {}459 };460461 if !arr.is_empty() {462 for v in arr.iter() {463 out.push_str("---\n");464 out.push_str(&v?.manifest(format)?);465 out.push('\n');466 }467 out.push_str("...");468 }469470 out.into()471 }472 ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,473 ManifestFormat::Json(padding) => self.to_json(*padding)?,474 ManifestFormat::ToString => self.to_string()?,475 ManifestFormat::String => match self {476 Self::Str(s) => s.clone(),477 _ => throw!(StringManifestOutputIsNotAString),478 },479 })480 }481482 /// For manifestification483 pub fn to_json(&self, padding: usize) -> Result<IStr> {484 manifest_json_ex(485 self,486 &ManifestJsonOptions {487 padding: &" ".repeat(padding),488 mtype: if padding == 0 {489 ManifestType::Minify490 } else {491 ManifestType::Manifest492 },493 },494 )495 .map(|s| s.into())496 }497498 /// Calls `std.manifestJson`499 #[cfg(feature = "faster")]500 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {501 manifest_json_ex(502 self,503 &ManifestJsonOptions {504 padding: &" ".repeat(padding),505 mtype: ManifestType::Std,506 },507 )508 .map(|s| s.into())509 }510511 /// Calls `std.manifestJson`512 #[cfg(not(feature = "faster"))]513 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {514 with_state(|s| {515 let ctx = s516 .create_default_context()?517 .with_var("__tmp__to_json__".into(), self.clone())?;518 Ok(evaluate(519 ctx,520 &el!(Expr::Apply(521 el!(Expr::Index(522 el!(Expr::Var("std".into())),523 el!(Expr::Str("manifestJsonEx".into()))524 )),525 ArgsDesc(vec![526 Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),527 Arg(None, el!(Expr::Str(" ".repeat(padding).into())))528 ]),529 false530 )),531 )?532 .try_cast_str("to json")?)533 })534 }535 pub fn to_yaml(&self, padding: usize) -> Result<IStr> {536 with_state(|s| {537 let ctx = s538 .create_default_context()539 .with_var("__tmp__to_json__".into(), self.clone());540 evaluate(541 ctx,542 &el!(Expr::Apply(543 el!(Expr::Index(544 el!(Expr::Var("std".into())),545 el!(Expr::Str("manifestYamlDoc".into()))546 )),547 ArgsDesc(vec![548 Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),549 Arg(550 None,551 el!(Expr::Literal(if padding != 0 {552 LiteralType::True553 } else {554 LiteralType::False555 }))556 )557 ]),558 false559 )),560 )?561 .try_cast_str("to json")562 })563 }564}565566const fn is_function_like(val: &Val) -> bool {567 matches!(val, Val::Func(_))568}569570/// Native implementation of `std.primitiveEquals`571pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {572 Ok(match (val_a, val_b) {573 (Val::Bool(a), Val::Bool(b)) => a == b,574 (Val::Null, Val::Null) => true,575 (Val::Str(a), Val::Str(b)) => a == b,576 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,577 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(578 "primitiveEquals operates on primitive types, got array".into(),579 )),580 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(581 "primitiveEquals operates on primitive types, got object".into(),582 )),583 (a, b) if is_function_like(a) && is_function_like(b) => {584 throw!(RuntimeError("cannot test equality of functions".into()))585 }586 (_, _) => false,587 })588}589590/// Native implementation of `std.equals`591pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {592 if val_a.value_type() != val_b.value_type() {593 return Ok(false);594 }595 match (val_a, val_b) {596 (Val::Arr(a), Val::Arr(b)) => {597 if ArrValue::ptr_eq(a, b) {598 return Ok(true);599 }600 if a.len() != b.len() {601 return Ok(false);602 }603 for (a, b) in a.iter().zip(b.iter()) {604 if !equals(&a?, &b?)? {605 return Ok(false);606 }607 }608 Ok(true)609 }610 (Val::Obj(a), Val::Obj(b)) => {611 if ObjValue::ptr_eq(a, b) {612 return Ok(true);613 }614 let fields = a.fields();615 if fields != b.fields() {616 return Ok(false);617 }618 for field in fields {619 if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {620 return Ok(false);621 }622 }623 Ok(true)624 }625 (a, b) => Ok(primitive_equals(a, b)?),626 }627}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 gc::{custom_trace, Finalize, 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}2122enum LazyValInternals {23 Computed(Val),24 Errored(LocError),25 Waiting(Box<dyn LazyValValue>),26 Pending,27}28impl Finalize for LazyValInternals {}29unsafe impl Trace for LazyValInternals {30 custom_trace!(this, {31 match &this {32 LazyValInternals::Computed(v) => mark(v),33 LazyValInternals::Errored(e) => mark(e),34 LazyValInternals::Waiting(w) => mark(w),35 LazyValInternals::Pending => {}36 }37 });38}3940#[derive(Clone, Trace, Finalize)]41pub struct LazyVal(Gc<GcCell<LazyValInternals>>);42impl LazyVal {43 pub fn new(f: Box<dyn LazyValValue>) -> Self {44 Self(Gc::new(GcCell::new(LazyValInternals::Waiting(f))))45 }46 pub fn new_resolved(val: Val) -> Self {47 Self(Gc::new(GcCell::new(LazyValInternals::Computed(val))))48 }49 pub fn evaluate(&self) -> Result<Val> {50 match &*self.0.borrow() {51 LazyValInternals::Computed(v) => return Ok(v.clone()),52 LazyValInternals::Errored(e) => return Err(e.clone().into()),53 LazyValInternals::Pending => return Err(RecursiveLazyValueEvaluation.into()),54 _ => (),55 };56 let value = if let LazyValInternals::Waiting(value) =57 std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)58 {59 value60 } else {61 unreachable!()62 };63 let new_value = match value.get() {64 Ok(v) => v,65 Err(e) => {66 *self.0.borrow_mut() = LazyValInternals::Errored(e.clone());67 return Err(e);68 }69 };70 *self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());71 Ok(new_value)72 }73}7475impl Debug for LazyVal {76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {77 write!(f, "Lazy")78 }79}80impl PartialEq for LazyVal {81 fn eq(&self, other: &Self) -> bool {82 Gc::ptr_eq(&self.0, &other.0)83 }84}8586#[derive(Debug, PartialEq, Trace, Finalize)]87pub struct FuncDesc {88 pub name: IStr,89 pub ctx: Context,90 pub params: ParamsDesc,91 pub body: LocExpr,92}9394#[derive(Debug, Trace, Finalize)]95pub enum FuncVal {96 /// Plain function implemented in jsonnet97 Normal(FuncDesc),98 /// Standard library function99 Intrinsic(IStr),100 /// Library functions implemented in native101 NativeExt(IStr, Gc<NativeCallback>),102}103104impl PartialEq for FuncVal {105 fn eq(&self, other: &Self) -> bool {106 match (self, other) {107 (Self::Normal(a), Self::Normal(b)) => a == b,108 (Self::Intrinsic(an), Self::Intrinsic(bn)) => an == bn,109 (Self::NativeExt(an, _), Self::NativeExt(bn, _)) => an == bn,110 (..) => false,111 }112 }113}114impl FuncVal {115 pub fn is_ident(&self) -> bool {116 matches!(&self, Self::Intrinsic(n) if n as &str == "id")117 }118 pub fn name(&self) -> IStr {119 match self {120 Self::Normal(normal) => normal.name.clone(),121 Self::Intrinsic(name) => format!("std.{}", name).into(),122 Self::NativeExt(n, _) => format!("native.{}", n).into(),123 }124 }125 pub fn evaluate(126 &self,127 call_ctx: Context,128 loc: Option<&ExprLocation>,129 args: &ArgsDesc,130 tailstrict: bool,131 ) -> Result<Val> {132 match self {133 Self::Normal(func) => {134 let ctx = parse_function_call(135 call_ctx,136 Some(func.ctx.clone()),137 &func.params,138 args,139 tailstrict,140 )?;141 evaluate(ctx, &func.body)142 }143 Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),144 Self::NativeExt(_name, handler) => {145 let args = parse_function_call(call_ctx, None, &handler.params, args, true)?;146 let mut out_args = Vec::with_capacity(handler.params.len());147 for p in handler.params.0.iter() {148 out_args.push(args.binding(p.0.clone())?.evaluate()?);149 }150 Ok(handler.call(loc.map(|l| l.0.clone()), &out_args)?)151 }152 }153 }154155 pub fn evaluate_map(156 &self,157 call_ctx: Context,158 args: &HashMap<IStr, Val>,159 tailstrict: bool,160 ) -> Result<Val> {161 match self {162 Self::Normal(func) => {163 let ctx = parse_function_call_map(164 call_ctx,165 Some(func.ctx.clone()),166 &func.params,167 args,168 tailstrict,169 )?;170 evaluate(ctx, &func.body)171 }172 Self::Intrinsic(_) => todo!(),173 Self::NativeExt(_, _) => todo!(),174 }175 }176177 pub fn evaluate_values(&self, call_ctx: Context, args: &[Val]) -> Result<Val> {178 match self {179 Self::Normal(func) => {180 let ctx = place_args(call_ctx, Some(func.ctx.clone()), &func.params, args)?;181 evaluate(ctx, &func.body)182 }183 Self::Intrinsic(_) => todo!(),184 Self::NativeExt(_, _) => todo!(),185 }186 }187}188189#[derive(Clone)]190pub enum ManifestFormat {191 YamlStream(Box<ManifestFormat>),192 Yaml(usize),193 Json(usize),194 ToString,195 String,196}197198#[derive(Debug, Clone)]199pub enum ArrValue {200 Lazy(Gc<Vec<LazyVal>>),201 Eager(Gc<Vec<Val>>),202 Extended(Box<(Self, Self)>),203}204impl Finalize for ArrValue {}205unsafe impl Trace for ArrValue {206 custom_trace!(this, {207 match &this {208 ArrValue::Lazy(l) => mark(l),209 ArrValue::Eager(e) => mark(e),210 ArrValue::Extended(x) => mark(x),211 }212 });213}214impl ArrValue {215 pub fn new_eager() -> Self {216 Self::Eager(Gc::new(Vec::new()))217 }218219 pub fn len(&self) -> usize {220 match self {221 Self::Lazy(l) => l.len(),222 Self::Eager(e) => e.len(),223 Self::Extended(v) => v.0.len() + v.1.len(),224 }225 }226227 pub fn is_empty(&self) -> bool {228 self.len() == 0229 }230231 pub fn get(&self, index: usize) -> Result<Option<Val>> {232 match self {233 Self::Lazy(vec) => {234 if let Some(v) = vec.get(index) {235 Ok(Some(v.evaluate()?))236 } else {237 Ok(None)238 }239 }240 Self::Eager(vec) => Ok(vec.get(index).cloned()),241 Self::Extended(v) => {242 let a_len = v.0.len();243 if a_len > index {244 v.0.get(index)245 } else {246 v.1.get(index - a_len)247 }248 }249 }250 }251252 pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {253 match self {254 Self::Lazy(vec) => vec.get(index).cloned(),255 Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),256 Self::Extended(v) => {257 let a_len = v.0.len();258 if a_len > index {259 v.0.get_lazy(index)260 } else {261 v.1.get_lazy(index - a_len)262 }263 }264 }265 }266267 pub fn evaluated(&self) -> Result<Gc<Vec<Val>>> {268 Ok(match self {269 Self::Lazy(vec) => {270 let mut out = Vec::with_capacity(vec.len());271 for item in vec.iter() {272 out.push(item.evaluate()?);273 }274 Gc::new(out)275 }276 Self::Eager(vec) => vec.clone(),277 Self::Extended(_v) => {278 let mut out = Vec::with_capacity(self.len());279 for item in self.iter() {280 out.push(item?);281 }282 Gc::new(out)283 }284 })285 }286287 pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {288 (0..self.len()).map(move |idx| match self {289 Self::Lazy(l) => l[idx].evaluate(),290 Self::Eager(e) => Ok(e[idx].clone()),291 Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),292 })293 }294295 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {296 (0..self.len()).map(move |idx| match self {297 Self::Lazy(l) => l[idx].clone(),298 Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),299 Self::Extended(_) => self.get_lazy(idx).unwrap(),300 })301 }302303 pub fn reversed(self) -> Self {304 match self {305 Self::Lazy(vec) => {306 let mut out = (&vec as &Vec<_>).clone();307 out.reverse();308 Self::Lazy(Gc::new(out))309 }310 Self::Eager(vec) => {311 let mut out = (&vec as &Vec<_>).clone();312 out.reverse();313 Self::Eager(Gc::new(out))314 }315 Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),316 }317 }318319 pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {320 let mut out = Vec::with_capacity(self.len());321322 for value in self.iter() {323 out.push(mapper(value?)?);324 }325326 Ok(Self::Eager(Gc::new(out)))327 }328329 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {330 let mut out = Vec::with_capacity(self.len());331332 for value in self.iter() {333 let value = value?;334 if filter(&value)? {335 out.push(value);336 }337 }338339 Ok(Self::Eager(Gc::new(out)))340 }341342 pub fn ptr_eq(a: &Self, b: &Self) -> bool {343 match (a, b) {344 (Self::Lazy(a), Self::Lazy(b)) => Gc::ptr_eq(a, b),345 (Self::Eager(a), Self::Eager(b)) => Gc::ptr_eq(a, b),346 _ => false,347 }348 }349}350351impl From<Vec<LazyVal>> for ArrValue {352 fn from(v: Vec<LazyVal>) -> Self {353 Self::Lazy(Gc::new(v))354 }355}356357impl From<Vec<Val>> for ArrValue {358 fn from(v: Vec<Val>) -> Self {359 Self::Eager(Gc::new(v))360 }361}362363#[derive(Debug)]364pub struct DebugGcTraceValue {365 name: IStr,366 pub value: Box<Val>,367}368impl DebugGcTraceValue {369 fn print(&self, action: &str) {370 println!("{} {}#{:?}", action, self.name, &*self.value as *const _)371 }372}373impl Finalize for DebugGcTraceValue {374 fn finalize(&self) {375 self.print("Garbage-collecting")376 }377}378impl Drop for DebugGcTraceValue {379 fn drop(&mut self) {380 self.print("Garbage-collected")381 }382}383unsafe impl Trace for DebugGcTraceValue {384 unsafe fn trace(&self) {385 self.print("Traced");386 self.value.trace()387 }388 unsafe fn root(&self) {389 self.print("Rooted");390 self.value.root()391 }392 unsafe fn unroot(&self) {393 self.print("Unrooted");394 self.value.unroot()395 }396 fn finalize_glue(&self) {397 Finalize::finalize(self)398 }399}400impl Clone for DebugGcTraceValue {401 fn clone(&self) -> Self {402 self.print("Cloned");403 let value = DebugGcTraceValue {404 name: self.name.clone(),405 value: self.value.clone(),406 };407 value.print("I'm clone");408 value409 }410}411impl DebugGcTraceValue {412 pub fn new(name: IStr, value: Val) -> Val {413 let value = Self {414 name,415 value: Box::new(value),416 };417 value.print("Constructed");418 Val::DebugGcTraceValue(value)419 }420}421422#[derive(Debug, Clone)]423pub enum Val {424 Bool(bool),425 Null,426 Str(IStr),427 Num(f64),428 Arr(ArrValue),429 Obj(ObjValue),430 Func(Gc<FuncVal>),431 DebugGcTraceValue(DebugGcTraceValue),432}433impl Finalize for Val {}434unsafe impl Trace for Val {435 custom_trace!(this, {436 match &this {437 Val::Bool(_) => {}438 Val::Null => {}439 Val::Str(_) => {}440 Val::Num(_) => {}441 Val::Arr(a) => mark(a),442 Val::Obj(o) => mark(o),443 Val::Func(f) => mark(f),444 Val::DebugGcTraceValue(v) => mark(v),445 }446 });447}448449macro_rules! matches_unwrap {450 ($e: expr, $p: pat, $r: expr) => {451 match $e {452 $p => $r,453 _ => panic!("no match"),454 }455 };456}457impl Val {458 /// Creates `Val::Num` after checking for numeric overflow.459 /// As numbers are `f64`, we can just check for their finity.460 pub fn new_checked_num(num: f64) -> Result<Self> {461 if num.is_finite() {462 Ok(Self::Num(num))463 } else {464 throw!(RuntimeError("overflow".into()))465 }466 }467468 pub fn assert_type(&self, context: &'static str, val_type: ValType) -> Result<()> {469 let this_type = self.value_type();470 if this_type != val_type {471 throw!(TypeMismatch(context, vec![val_type], this_type))472 } else {473 Ok(())474 }475 }476 pub fn unwrap_num(self) -> Result<f64> {477 Ok(matches_unwrap!(self, Self::Num(v), v))478 }479 pub fn unwrap_func(self) -> Result<Gc<FuncVal>> {480 Ok(matches_unwrap!(self, Self::Func(v), v))481 }482 pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {483 self.assert_type(context, ValType::Bool)?;484 Ok(matches_unwrap!(self, Self::Bool(v), v))485 }486 pub fn try_cast_str(self, context: &'static str) -> Result<IStr> {487 self.assert_type(context, ValType::Str)?;488 Ok(matches_unwrap!(self, Self::Str(v), v))489 }490 pub fn try_cast_num(self, context: &'static str) -> Result<f64> {491 self.assert_type(context, ValType::Num)?;492 self.unwrap_num()493 }494 pub const fn value_type(&self) -> ValType {495 match self {496 Self::Str(..) => ValType::Str,497 Self::Num(..) => ValType::Num,498 Self::Arr(..) => ValType::Arr,499 Self::Obj(..) => ValType::Obj,500 Self::Bool(_) => ValType::Bool,501 Self::Null => ValType::Null,502 Self::Func(..) => ValType::Func,503 Self::DebugGcTraceValue(v) => v.value.value_type(),504 }505 }506507 pub fn to_string(&self) -> Result<IStr> {508 Ok(match self {509 Self::Bool(true) => "true".into(),510 Self::Bool(false) => "false".into(),511 Self::Null => "null".into(),512 Self::Str(s) => s.clone(),513 v => manifest_json_ex(514 v,515 &ManifestJsonOptions {516 padding: "",517 mtype: ManifestType::ToString,518 },519 )?520 .into(),521 })522 }523524 /// Expects value to be object, outputs (key, manifested value) pairs525 pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {526 let obj = match self {527 Self::Obj(obj) => obj,528 _ => throw!(MultiManifestOutputIsNotAObject),529 };530 let keys = obj.fields();531 let mut out = Vec::with_capacity(keys.len());532 for key in keys {533 let value = obj534 .get(key.clone())?535 .expect("item in object")536 .manifest(ty)?;537 out.push((key, value));538 }539 Ok(out)540 }541542 /// Expects value to be array, outputs manifested values543 pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {544 let arr = match self {545 Self::Arr(a) => a,546 _ => throw!(StreamManifestOutputIsNotAArray),547 };548 let mut out = Vec::with_capacity(arr.len());549 for i in arr.iter() {550 out.push(i?.manifest(ty)?);551 }552 Ok(out)553 }554555 pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {556 Ok(match ty {557 ManifestFormat::YamlStream(format) => {558 let arr = match self {559 Self::Arr(a) => a,560 _ => throw!(StreamManifestOutputIsNotAArray),561 };562 let mut out = String::new();563564 match format as &ManifestFormat {565 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),566 ManifestFormat::String => throw!(StreamManifestCannotNestString),567 _ => {}568 };569570 if !arr.is_empty() {571 for v in arr.iter() {572 out.push_str("---\n");573 out.push_str(&v?.manifest(format)?);574 out.push('\n');575 }576 out.push_str("...");577 }578579 out.into()580 }581 ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,582 ManifestFormat::Json(padding) => self.to_json(*padding)?,583 ManifestFormat::ToString => self.to_string()?,584 ManifestFormat::String => match self {585 Self::Str(s) => s.clone(),586 _ => throw!(StringManifestOutputIsNotAString),587 },588 })589 }590591 /// For manifestification592 pub fn to_json(&self, padding: usize) -> Result<IStr> {593 manifest_json_ex(594 self,595 &ManifestJsonOptions {596 padding: &" ".repeat(padding),597 mtype: if padding == 0 {598 ManifestType::Minify599 } else {600 ManifestType::Manifest601 },602 },603 )604 .map(|s| s.into())605 }606607 /// Calls `std.manifestJson`608 #[cfg(feature = "faster")]609 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {610 manifest_json_ex(611 self,612 &ManifestJsonOptions {613 padding: &" ".repeat(padding),614 mtype: ManifestType::Std,615 },616 )617 .map(|s| s.into())618 }619620 /// Calls `std.manifestJson`621 #[cfg(not(feature = "faster"))]622 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {623 with_state(|s| {624 let ctx = s625 .create_default_context()?626 .with_var("__tmp__to_json__".into(), self.clone())?;627 Ok(evaluate(628 ctx,629 &el!(Expr::Apply(630 el!(Expr::Index(631 el!(Expr::Var("std".into())),632 el!(Expr::Str("manifestJsonEx".into()))633 )),634 ArgsDesc(vec![635 Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),636 Arg(None, el!(Expr::Str(" ".repeat(padding).into())))637 ]),638 false639 )),640 )?641 .try_cast_str("to json")?)642 })643 }644 pub fn to_yaml(&self, padding: usize) -> Result<IStr> {645 with_state(|s| {646 let ctx = s647 .create_default_context()648 .with_var("__tmp__to_json__".into(), self.clone());649 evaluate(650 ctx,651 &el!(Expr::Apply(652 el!(Expr::Index(653 el!(Expr::Var("std".into())),654 el!(Expr::Str("manifestYamlDoc".into()))655 )),656 ArgsDesc(vec![657 Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),658 Arg(659 None,660 el!(Expr::Literal(if padding != 0 {661 LiteralType::True662 } else {663 LiteralType::False664 }))665 )666 ]),667 false668 )),669 )?670 .try_cast_str("to json")671 })672 }673}674675const fn is_function_like(val: &Val) -> bool {676 matches!(val, Val::Func(_))677}678679/// Native implementation of `std.primitiveEquals`680pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {681 Ok(match (val_a, val_b) {682 (Val::Bool(a), Val::Bool(b)) => a == b,683 (Val::Null, Val::Null) => true,684 (Val::Str(a), Val::Str(b)) => a == b,685 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,686 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(687 "primitiveEquals operates on primitive types, got array".into(),688 )),689 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(690 "primitiveEquals operates on primitive types, got object".into(),691 )),692 (a, b) if is_function_like(a) && is_function_like(b) => {693 throw!(RuntimeError("cannot test equality of functions".into()))694 }695 (_, _) => false,696 })697}698699/// Native implementation of `std.equals`700pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {701 if val_a.value_type() != val_b.value_type() {702 return Ok(false);703 }704 match (val_a, val_b) {705 (Val::Arr(a), Val::Arr(b)) => {706 if ArrValue::ptr_eq(a, b) {707 return Ok(true);708 }709 if a.len() != b.len() {710 return Ok(false);711 }712 for (a, b) in a.iter().zip(b.iter()) {713 if !equals(&a?, &b?)? {714 return Ok(false);715 }716 }717 Ok(true)718 }719 (Val::Obj(a), Val::Obj(b)) => {720 if ObjValue::ptr_eq(a, b) {721 return Ok(true);722 }723 let fields = a.fields();724 if fields != b.fields() {725 return Ok(false);726 }727 for field in fields {728 if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {729 return Ok(false);730 }731 }732 Ok(true)733 }734 (a, b) => Ok(primitive_equals(a, b)?),735 }736}