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.rsdiffbeforeafterboth1use crate::{2 equals, error::Error::*, lazy_val, push, throw, with_state, ArrValue, Context, ContextCreator,3 FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,4};5use closure::closure;6use jrsonnet_interner::IStr;7use jrsonnet_parser::{8 ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprLocation, FieldMember,9 ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,10 Visibility,11};12use jrsonnet_types::ValType;13use rustc_hash::{FxHashMap, FxHasher};14use std::{collections::HashMap, hash::BuildHasherDefault, rc::Rc};1516pub fn evaluate_binding_in_future(17 b: &BindSpec,18 context_creator: FutureWrapper<Context>,19) -> LazyVal {20 let b = b.clone();21 if let Some(params) = &b.params {22 let params = params.clone();23 LazyVal::new(Box::new(move || {24 Ok(evaluate_method(25 context_creator.unwrap(),26 b.name.clone(),27 params.clone(),28 b.value.clone(),29 ))30 }))31 } else {32 LazyVal::new(Box::new(move || {33 evaluate_named(context_creator.unwrap(), &b.value, b.name.clone())34 }))35 }36}3738pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {39 let b = b.clone();40 if let Some(params) = &b.params {41 let params = params.clone();42 (43 b.name.clone(),44 LazyBinding::Bindable(Rc::new(move |this, super_obj| {45 Ok(lazy_val!(46 closure!(clone b, clone params, clone context_creator, || Ok(evaluate_method(47 context_creator.create(this.clone(), super_obj.clone())?,48 b.name.clone(),49 params.clone(),50 b.value.clone(),51 )))52 ))53 })),54 )55 } else {56 (57 b.name.clone(),58 LazyBinding::Bindable(Rc::new(move |this, super_obj| {59 Ok(lazy_val!(closure!(clone context_creator, clone b, ||60 evaluate_named(61 context_creator.create(this.clone(), super_obj.clone())?,62 &b.value,63 b.name.clone()64 )65 )))66 })),67 )68 }69}7071pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {72 Val::Func(Rc::new(FuncVal::Normal(FuncDesc {73 name,74 ctx,75 params,76 body,77 })))78}7980pub fn evaluate_field_name(81 context: Context,82 field_name: &jrsonnet_parser::FieldName,83) -> Result<Option<IStr>> {84 Ok(match field_name {85 jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),86 jrsonnet_parser::FieldName::Dyn(expr) => {87 let value = evaluate(context, expr)?;88 if matches!(value, Val::Null) {89 None90 } else {91 Some(value.try_cast_str("dynamic field name")?)92 }93 }94 })95}9697pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {98 Ok(match (op, b) {99 (UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),100 (UnaryOpType::Minus, Val::Num(n)) => Val::Num(-*n),101 (UnaryOpType::BitNot, Val::Num(n)) => Val::Num(!(*n as i32) as f64),102 (op, o) => throw!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),103 })104}105106pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {107 Ok(match (a, b) {108 (Val::Str(v1), Val::Str(v2)) => Val::Str(((**v1).to_owned() + v2).into()),109110 // Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)111 (Val::Num(n), Val::Str(o)) => Val::Str(format!("{}{}", n, o).into()),112 (Val::Str(o), Val::Num(n)) => Val::Str(format!("{}{}", o, n).into()),113114 (Val::Str(s), o) => Val::Str(format!("{}{}", s, o.clone().to_string()?).into()),115 (o, Val::Str(s)) => Val::Str(format!("{}{}", o.clone().to_string()?, s).into()),116117 (Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.extend_from(v1.clone())),118 (Val::Arr(a), Val::Arr(b)) => {119 let mut out = Vec::with_capacity(a.len() + b.len());120 out.extend(a.iter_lazy());121 out.extend(b.iter_lazy());122 Val::Arr(out.into())123 }124 (Val::Num(v1), Val::Num(v2)) => Val::new_checked_num(v1 + v2)?,125 _ => throw!(BinaryOperatorDoesNotOperateOnValues(126 BinaryOpType::Add,127 a.value_type(),128 b.value_type(),129 )),130 })131}132133pub fn evaluate_binary_op_special(134 context: Context,135 a: &LocExpr,136 op: BinaryOpType,137 b: &LocExpr,138) -> Result<Val> {139 Ok(match (evaluate(context.clone(), a)?, op, b) {140 (Val::Bool(true), BinaryOpType::Or, _o) => Val::Bool(true),141 (Val::Bool(false), BinaryOpType::And, _o) => Val::Bool(false),142 (a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?)?,143 })144}145146pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {147 Ok(match (a, op, b) {148 (a, BinaryOpType::Add, b) => evaluate_add_op(a, b)?,149150 (a, BinaryOpType::Eq, b) => Val::Bool(equals(a, b)?),151 (a, BinaryOpType::Neq, b) => Val::Bool(!equals(a, b)?),152153 (Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize).into()),154155 // Bool X Bool156 (Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),157 (Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),158159 // Str X Str160 (Val::Str(v1), BinaryOpType::Lt, Val::Str(v2)) => Val::Bool(v1 < v2),161 (Val::Str(v1), BinaryOpType::Gt, Val::Str(v2)) => Val::Bool(v1 > v2),162 (Val::Str(v1), BinaryOpType::Lte, Val::Str(v2)) => Val::Bool(v1 <= v2),163 (Val::Str(v1), BinaryOpType::Gte, Val::Str(v2)) => Val::Bool(v1 >= v2),164165 // Num X Num166 (Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::new_checked_num(v1 * v2)?,167 (Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => {168 if *v2 <= f64::EPSILON {169 throw!(DivisionByZero)170 }171 Val::new_checked_num(v1 / v2)?172 }173174 (Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::new_checked_num(v1 - v2)?,175176 (Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => Val::Bool(v1 < v2),177 (Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => Val::Bool(v1 > v2),178 (Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => Val::Bool(v1 <= v2),179 (Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => Val::Bool(v1 >= v2),180181 (Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {182 Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)183 }184 (Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {185 Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)186 }187 (Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {188 Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)189 }190 (Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {191 if *v2 < 0.0 {192 throw!(RuntimeError("shift by negative exponent".into()))193 }194 Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)195 }196 (Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {197 if *v2 < 0.0 {198 throw!(RuntimeError("shift by negative exponent".into()))199 }200 Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)201 }202203 _ => throw!(BinaryOperatorDoesNotOperateOnValues(204 op,205 a.value_type(),206 b.value_type(),207 )),208 })209}210211pub fn evaluate_comp(212 context: Context,213 specs: &[CompSpec],214 callback: &mut impl FnMut(Context) -> Result<()>,215) -> Result<()> {216 match specs.get(0) {217 None => callback(context)?,218 Some(CompSpec::IfSpec(IfSpecData(cond))) => {219 if evaluate(context.clone(), cond)?.try_cast_bool("if spec")? {220 evaluate_comp(context, &specs[1..], callback)?221 }222 }223 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {224 Val::Arr(list) => {225 for item in list.iter() {226 evaluate_comp(227 context.clone().with_var(var.clone(), item?.clone()),228 &specs[1..],229 callback,230 )?231 }232 }233 _ => throw!(InComprehensionCanOnlyIterateOverArray),234 },235 }236 Ok(())237}238239pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {240 let new_bindings = FutureWrapper::new();241 let future_this = FutureWrapper::new();242 let context_creator = ContextCreator(context.clone(), new_bindings.clone());243 {244 let mut bindings: FxHashMap<IStr, LazyBinding> =245 FxHashMap::with_capacity_and_hasher(members.len(), BuildHasherDefault::default());246 for (n, b) in members247 .iter()248 .filter_map(|m| match m {249 Member::BindStmt(b) => Some(b.clone()),250 _ => None,251 })252 .map(|b| evaluate_binding(&b, context_creator.clone()))253 {254 bindings.insert(n, b);255 }256 new_bindings.fill(bindings);257 }258259 let mut new_members = FxHashMap::default();260 let mut assertions = Vec::new();261 for member in members.iter() {262 match member {263 Member::Field(FieldMember {264 name,265 plus,266 params: None,267 visibility,268 value,269 }) => {270 let name = evaluate_field_name(context.clone(), name)?;271 if name.is_none() {272 continue;273 }274 let name = name.unwrap();275 new_members.insert(276 name.clone(),277 ObjMember {278 add: *plus,279 visibility: *visibility,280 invoke: LazyBinding::Bindable(Rc::new(281 closure!(clone name, clone value, clone context_creator, |this, super_obj| {282 Ok(LazyVal::new_resolved(evaluate_named(283 context_creator.create(this, super_obj)?,284 &value,285 name.clone(),286 )?))287 }),288 )),289 location: value.1.clone(),290 },291 );292 }293 Member::Field(FieldMember {294 name,295 params: Some(params),296 value,297 ..298 }) => {299 let name = evaluate_field_name(context.clone(), name)?;300 if name.is_none() {301 continue;302 }303 let name = name.unwrap();304 new_members.insert(305 name.clone(),306 ObjMember {307 add: false,308 visibility: Visibility::Hidden,309 invoke: LazyBinding::Bindable(Rc::new(310 closure!(clone value, clone context_creator, clone params, clone name, |this, super_obj| {311 // TODO: Assert312 Ok(LazyVal::new_resolved(evaluate_method(313 context_creator.create(this, super_obj)?,314 name.clone(),315 params.clone(),316 value.clone(),317 )))318 }),319 )),320 location: value.1.clone(),321 },322 );323 }324 Member::BindStmt(_) => {}325 Member::AssertStmt(stmt) => {326 assertions.push(stmt.clone());327 }328 }329 }330 let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(assertions));331 future_this.fill(this.clone());332 Ok(this)333}334335pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {336 Ok(match object {337 ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,338 ObjBody::ObjComp(obj) => {339 let future_this = FutureWrapper::new();340 let mut new_members = FxHashMap::default();341 evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {342 let new_bindings = FutureWrapper::new();343 let context_creator = ContextCreator(context.clone(), new_bindings.clone());344 let mut bindings: FxHashMap<IStr, LazyBinding> =345 FxHashMap::with_capacity_and_hasher(346 obj.pre_locals.len() + obj.post_locals.len(),347 BuildHasherDefault::default(),348 );349 for (n, b) in obj350 .pre_locals351 .iter()352 .chain(obj.post_locals.iter())353 .map(|b| evaluate_binding(b, context_creator.clone()))354 {355 bindings.insert(n, b);356 }357 new_bindings.fill(bindings.clone());358 let ctx = ctx.extend_unbound(bindings, None, None, None)?;359 let key = evaluate(ctx.clone(), &obj.key)?;360361 match key {362 Val::Null => {}363 Val::Str(n) => {364 new_members.insert(365 n,366 ObjMember {367 add: false,368 visibility: Visibility::Normal,369 invoke: LazyBinding::Bindable(Rc::new(370 closure!(clone ctx, clone obj.value, |this, _super_obj| {371 Ok(LazyVal::new_resolved(evaluate(ctx.clone().extend(FxHashMap::default(), None, this, None), &value)?))372 }),373 )),374 location: obj.value.1.clone(),375 },376 );377 }378 v => throw!(FieldMustBeStringGot(v.value_type())),379 }380381 Ok(())382 })?;383384 let this = ObjValue::new(context, None, Rc::new(new_members), Rc::new(Vec::new()));385 future_this.fill(this.clone());386 this387 }388 })389}390391pub fn evaluate_apply(392 context: Context,393 value: &LocExpr,394 args: &ArgsDesc,395 loc: Option<&ExprLocation>,396 tailstrict: bool,397) -> Result<Val> {398 let value = evaluate(context.clone(), value)?;399 Ok(match value {400 Val::Func(f) => {401 let body = || f.evaluate(context, loc, args, tailstrict);402 if tailstrict {403 body()?404 } else {405 push(loc, || format!("function <{}> call", f.name()), body)?406 }407 }408 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),409 })410}411412pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {413 let value = &assertion.0;414 let msg = &assertion.1;415 let assertion_result = push(416 value.1.as_ref(),417 || "assertion condition".to_owned(),418 || {419 evaluate(context.clone(), value)?420 .try_cast_bool("assertion condition should be of type `boolean`")421 },422 )?;423 if !assertion_result {424 push(425 value.1.as_ref(),426 || "assertion failure".to_owned(),427 || {428 if let Some(msg) = msg {429 throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));430 } else {431 throw!(AssertionFailed(Val::Null.to_string()?));432 }433 },434 )?435 }436 Ok(())437}438439pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {440 use Expr::*;441 let LocExpr(expr, _loc) = lexpr;442 Ok(match &**expr {443 Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),444 _ => evaluate(context, lexpr)?,445 })446}447448pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {449 use Expr::*;450 let LocExpr(expr, loc) = expr;451 Ok(match &**expr {452 Literal(LiteralType::This) => {453 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)454 }455 Literal(LiteralType::Super) => Val::Obj(456 context457 .super_obj()458 .clone()459 .ok_or(NoSuperFound)?460 .with_this(context.this().clone().unwrap()),461 ),462 Literal(LiteralType::Dollar) => {463 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)464 }465 Literal(LiteralType::True) => Val::Bool(true),466 Literal(LiteralType::False) => Val::Bool(false),467 Literal(LiteralType::Null) => Val::Null,468 Parened(e) => evaluate(context, e)?,469 Str(v) => Val::Str(v.clone()),470 Num(v) => Val::new_checked_num(*v)?,471 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,472 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,473 Var(name) => push(474 loc.as_ref(),475 || format!("variable <{}>", name),476 || context.binding(name.clone())?.evaluate(),477 )?,478 Index(value, index) => {479 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {480 (Val::Obj(v), Val::Str(s)) => {481 let sn = s.clone();482 push(483 loc.as_ref(),484 || format!("field <{}> access", sn),485 || {486 if let Some(v) = v.get(s.clone())? {487 Ok(v)488 } else if v.get("__intrinsic_namespace__".into())?.is_some() {489 Ok(Val::Func(Rc::new(FuncVal::Intrinsic(s))))490 } else {491 throw!(NoSuchField(s))492 }493 },494 )?495 }496 (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(497 ValType::Obj,498 ValType::Str,499 n.value_type(),500 )),501502 (Val::Arr(v), Val::Num(n)) => {503 if n.fract() > f64::EPSILON {504 throw!(FractionalIndex)505 }506 v.get(n as usize)?507 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?508 }509 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),510 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(511 ValType::Arr,512 ValType::Num,513 n.value_type(),514 )),515516 (Val::Str(s), Val::Num(n)) => Val::Str(517 s.chars()518 .skip(n as usize)519 .take(1)520 .collect::<String>()521 .into(),522 ),523 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(524 ValType::Str,525 ValType::Num,526 n.value_type(),527 )),528529 (v, _) => throw!(CantIndexInto(v.value_type())),530 }531 }532 LocalExpr(bindings, returned) => {533 let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(534 bindings.len(),535 BuildHasherDefault::<FxHasher>::default(),536 );537 let future_context = Context::new_future();538 for b in bindings {539 new_bindings.insert(540 b.name.clone(),541 evaluate_binding_in_future(b, future_context.clone()),542 );543 }544 let context = context545 .extend_bound(new_bindings)546 .into_future(future_context);547 evaluate(context, &returned.clone())?548 }549 Arr(items) => {550 let mut out = Vec::with_capacity(items.len());551 for item in items {552 out.push(LazyVal::new(Box::new(553 closure!(clone context, clone item, || {554 evaluate(context.clone(), &item)555 }),556 )));557 }558 Val::Arr(out.into())559 }560 ArrComp(expr, comp_specs) => {561 let mut out = Vec::new();562 evaluate_comp(context, comp_specs, &mut |ctx| {563 out.push(evaluate(ctx, expr)?);564 Ok(())565 })?;566 Val::Arr(ArrValue::Eager(Rc::new(out)))567 }568 Obj(body) => Val::Obj(evaluate_object(context, body)?),569 ObjExtend(s, t) => evaluate_add_op(570 &evaluate(context.clone(), s)?,571 &Val::Obj(evaluate_object(context, t)?),572 )?,573 Apply(value, args, tailstrict) => {574 evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?575 }576 Function(params, body) => {577 evaluate_method(context, "anonymous".into(), params.clone(), body.clone())578 }579 Intrinsic(name) => Val::Func(Rc::new(FuncVal::Intrinsic(name.clone()))),580 AssertExpr(assert, returned) => {581 evaluate_assert(context.clone(), assert)?;582 evaluate(context, returned)?583 }584 ErrorStmt(e) => push(585 loc.as_ref(),586 || "error statement".to_owned(),587 || {588 throw!(RuntimeError(589 evaluate(context, e)?.try_cast_str("error text should be of type `string`")?,590 ))591 },592 )?,593 IfElse {594 cond,595 cond_then,596 cond_else,597 } => {598 if push(599 loc.as_ref(),600 || "if condition".to_owned(),601 || evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),602 )? {603 evaluate(context, cond_then)?604 } else {605 match cond_else {606 Some(v) => evaluate(context, v)?,607 None => Val::Null,608 }609 }610 }611 Import(path) => {612 let mut tmp = loc613 .clone()614 .expect("imports cannot be used without loc_data")615 .0;616 let import_location = Rc::make_mut(&mut tmp);617 import_location.pop();618 push(619 loc.as_ref(),620 || format!("import {:?}", path),621 || with_state(|s| s.import_file(import_location, path)),622 )?623 }624 ImportStr(path) => {625 let mut tmp = loc626 .clone()627 .expect("imports cannot be used without loc_data")628 .0;629 let import_location = Rc::make_mut(&mut tmp);630 import_location.pop();631 Val::Str(with_state(|s| s.import_file_str(import_location, path))?)632 }633 })634}1use crate::{2 equals, error::Error::*, push, throw, with_state, ArrValue, Bindable, Context, ContextCreator,3 FuncDesc, FuncVal, FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjMember, ObjValue,4 ObjectAssertion, Result, Val,5};6use gc::{custom_trace, Finalize, Gc, Trace};7use jrsonnet_interner::IStr;8use jrsonnet_parser::{9 ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprLocation, FieldMember,10 ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,11 Visibility,12};13use jrsonnet_types::ValType;14use rustc_hash::{FxHashMap, FxHasher};15use std::{collections::HashMap, hash::BuildHasherDefault, rc::Rc};1617pub fn evaluate_binding_in_future(18 b: &BindSpec,19 context_creator: FutureWrapper<Context>,20) -> LazyVal {21 let b = b.clone();22 if let Some(params) = &b.params {23 let params = params.clone();2425 struct LazyMethodBinding {26 context_creator: FutureWrapper<Context>,27 name: IStr,28 params: ParamsDesc,29 value: LocExpr,30 }31 impl Finalize for LazyMethodBinding {}32 unsafe impl Trace for LazyMethodBinding {33 custom_trace!(this, {34 mark(&this.context_creator);35 mark(&this.name);36 mark(&this.params);37 mark(&this.value);38 });39 }40 impl LazyValValue for LazyMethodBinding {41 fn get(self: Box<Self>) -> Result<Val> {42 Ok(evaluate_method(43 self.context_creator.unwrap(),44 self.name,45 self.params,46 self.value,47 ))48 }49 }5051 LazyVal::new(Box::new(LazyMethodBinding {52 context_creator,53 name: b.name.clone(),54 params,55 value: b.value.clone(),56 }))57 } else {58 struct LazyNamedBinding {59 context_creator: FutureWrapper<Context>,60 name: IStr,61 value: LocExpr,62 }63 impl Finalize for LazyNamedBinding {}64 unsafe impl Trace for LazyNamedBinding {65 custom_trace!(this, {66 mark(&this.context_creator);67 mark(&this.name);68 mark(&this.value);69 });70 }71 impl LazyValValue for LazyNamedBinding {72 fn get(self: Box<Self>) -> Result<Val> {73 evaluate_named(self.context_creator.unwrap(), &self.value, self.name)74 }75 }76 LazyVal::new(Box::new(LazyNamedBinding {77 context_creator,78 name: b.name.clone(),79 value: b.value,80 }))81 }82}8384pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {85 let b = b.clone();86 if let Some(params) = &b.params {87 let params = params.clone();8889 struct BindableMethodLazyVal {90 this: Option<ObjValue>,91 super_obj: Option<ObjValue>,9293 context_creator: ContextCreator,94 name: IStr,95 params: ParamsDesc,96 value: LocExpr,97 }98 impl Finalize for BindableMethodLazyVal {}99 unsafe impl Trace for BindableMethodLazyVal {100 custom_trace!(this, {101 mark(&this.this);102 mark(&this.super_obj);103 mark(&this.context_creator);104 mark(&this.name);105 mark(&this.params);106 mark(&this.value);107 });108 }109 impl LazyValValue for BindableMethodLazyVal {110 fn get(self: Box<Self>) -> Result<Val> {111 Ok(evaluate_method(112 self.context_creator.create(self.this, self.super_obj)?,113 self.name,114 self.params,115 self.value,116 ))117 }118 }119120 #[derive(Trace, Finalize)]121 struct BindableMethod {122 context_creator: ContextCreator,123 name: IStr,124 params: ParamsDesc,125 value: LocExpr,126 }127 impl Bindable for BindableMethod {128 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {129 Ok(LazyVal::new(Box::new(BindableMethodLazyVal {130 this: this.clone(),131 super_obj: super_obj.clone(),132133 context_creator: self.context_creator.clone(),134 name: self.name.clone(),135 params: self.params.clone(),136 value: self.value.clone(),137 })))138 }139 }140141 (142 b.name.clone(),143 LazyBinding::Bindable(Gc::new(Box::new(BindableMethod {144 context_creator,145 name: b.name.clone(),146 params,147 value: b.value.clone(),148 }))),149 )150 } else {151 struct BindableNamedLazyVal {152 this: Option<ObjValue>,153 super_obj: Option<ObjValue>,154155 context_creator: ContextCreator,156 name: IStr,157 value: LocExpr,158 }159 impl Finalize for BindableNamedLazyVal {}160 unsafe impl Trace for BindableNamedLazyVal {161 custom_trace!(this, {162 mark(&this.this);163 mark(&this.super_obj);164 mark(&this.context_creator);165 mark(&this.name);166 mark(&this.value);167 });168 }169 impl LazyValValue for BindableNamedLazyVal {170 fn get(self: Box<Self>) -> Result<Val> {171 evaluate_named(172 self.context_creator.create(self.this, self.super_obj)?,173 &self.value,174 self.name,175 )176 }177 }178179 #[derive(Trace, Finalize)]180 struct BindableNamed {181 context_creator: ContextCreator,182 name: IStr,183 value: LocExpr,184 }185 impl Bindable for BindableNamed {186 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {187 Ok(LazyVal::new(Box::new(BindableNamedLazyVal {188 this,189 super_obj,190191 context_creator: self.context_creator.clone(),192 name: self.name.clone(),193 value: self.value.clone(),194 })))195 }196 }197198 (199 b.name.clone(),200 LazyBinding::Bindable(Gc::new(Box::new(BindableNamed {201 context_creator,202 name: b.name.clone(),203 value: b.value.clone(),204 }))),205 )206 }207}208209pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {210 Val::Func(Gc::new(FuncVal::Normal(FuncDesc {211 name,212 ctx,213 params,214 body,215 })))216}217218pub fn evaluate_field_name(219 context: Context,220 field_name: &jrsonnet_parser::FieldName,221) -> Result<Option<IStr>> {222 Ok(match field_name {223 jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),224 jrsonnet_parser::FieldName::Dyn(expr) => {225 let value = evaluate(context, expr)?;226 if matches!(value, Val::Null) {227 None228 } else {229 Some(value.try_cast_str("dynamic field name")?)230 }231 }232 })233}234235pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {236 Ok(match (op, b) {237 (UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),238 (UnaryOpType::Minus, Val::Num(n)) => Val::Num(-*n),239 (UnaryOpType::BitNot, Val::Num(n)) => Val::Num(!(*n as i32) as f64),240 (op, o) => throw!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),241 })242}243244pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {245 Ok(match (a, b) {246 (Val::DebugGcTraceValue(v1), Val::DebugGcTraceValue(v2)) => {247 evaluate_add_op(&v1.value, &v2.value)?248 }249 (Val::Str(v1), Val::Str(v2)) => Val::Str(((**v1).to_owned() + v2).into()),250251 // Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)252 (Val::Num(n), Val::Str(o)) => Val::Str(format!("{}{}", n, o).into()),253 (Val::Str(o), Val::Num(n)) => Val::Str(format!("{}{}", o, n).into()),254255 (Val::Str(s), o) => Val::Str(format!("{}{}", s, o.clone().to_string()?).into()),256 (o, Val::Str(s)) => Val::Str(format!("{}{}", o.clone().to_string()?, s).into()),257258 (Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.extend_from(v1.clone())),259 (Val::Arr(a), Val::Arr(b)) => {260 let mut out = Vec::with_capacity(a.len() + b.len());261 out.extend(a.iter_lazy());262 out.extend(b.iter_lazy());263 Val::Arr(out.into())264 }265 (Val::Num(v1), Val::Num(v2)) => Val::new_checked_num(v1 + v2)?,266 _ => throw!(BinaryOperatorDoesNotOperateOnValues(267 BinaryOpType::Add,268 a.value_type(),269 b.value_type(),270 )),271 })272}273274pub fn evaluate_binary_op_special(275 context: Context,276 a: &LocExpr,277 op: BinaryOpType,278 b: &LocExpr,279) -> Result<Val> {280 Ok(match (evaluate(context.clone(), a)?, op, b) {281 (Val::Bool(true), BinaryOpType::Or, _o) => Val::Bool(true),282 (Val::Bool(false), BinaryOpType::And, _o) => Val::Bool(false),283 (a, op, eb) => evaluate_binary_op_normal(&a, op, &evaluate(context, eb)?)?,284 })285}286287pub fn evaluate_binary_op_normal(a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {288 Ok(match (a, op, b) {289 (a, BinaryOpType::Add, b) => evaluate_add_op(a, b)?,290291 (a, BinaryOpType::Eq, b) => Val::Bool(equals(a, b)?),292 (a, BinaryOpType::Neq, b) => Val::Bool(!equals(a, b)?),293294 (Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize).into()),295296 // Bool X Bool297 (Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),298 (Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),299300 // Str X Str301 (Val::Str(v1), BinaryOpType::Lt, Val::Str(v2)) => Val::Bool(v1 < v2),302 (Val::Str(v1), BinaryOpType::Gt, Val::Str(v2)) => Val::Bool(v1 > v2),303 (Val::Str(v1), BinaryOpType::Lte, Val::Str(v2)) => Val::Bool(v1 <= v2),304 (Val::Str(v1), BinaryOpType::Gte, Val::Str(v2)) => Val::Bool(v1 >= v2),305306 // Num X Num307 (Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::new_checked_num(v1 * v2)?,308 (Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => {309 if *v2 <= f64::EPSILON {310 throw!(DivisionByZero)311 }312 Val::new_checked_num(v1 / v2)?313 }314315 (Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::new_checked_num(v1 - v2)?,316317 (Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => Val::Bool(v1 < v2),318 (Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => Val::Bool(v1 > v2),319 (Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => Val::Bool(v1 <= v2),320 (Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => Val::Bool(v1 >= v2),321322 (Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {323 Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)324 }325 (Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {326 Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)327 }328 (Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {329 Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)330 }331 (Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {332 if *v2 < 0.0 {333 throw!(RuntimeError("shift by negative exponent".into()))334 }335 Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)336 }337 (Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {338 if *v2 < 0.0 {339 throw!(RuntimeError("shift by negative exponent".into()))340 }341 Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)342 }343344 _ => throw!(BinaryOperatorDoesNotOperateOnValues(345 op,346 a.value_type(),347 b.value_type(),348 )),349 })350}351352pub fn evaluate_comp(353 context: Context,354 specs: &[CompSpec],355 callback: &mut impl FnMut(Context) -> Result<()>,356) -> Result<()> {357 match specs.get(0) {358 None => callback(context)?,359 Some(CompSpec::IfSpec(IfSpecData(cond))) => {360 if evaluate(context.clone(), cond)?.try_cast_bool("if spec")? {361 evaluate_comp(context, &specs[1..], callback)?362 }363 }364 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {365 Val::Arr(list) => {366 for item in list.iter() {367 evaluate_comp(368 context.clone().with_var(var.clone(), item?.clone()),369 &specs[1..],370 callback,371 )?372 }373 }374 _ => throw!(InComprehensionCanOnlyIterateOverArray),375 },376 }377 Ok(())378}379380pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {381 let new_bindings = FutureWrapper::new();382 let future_this = FutureWrapper::new();383 let context_creator = ContextCreator(context.clone(), new_bindings.clone());384 {385 let mut bindings: FxHashMap<IStr, LazyBinding> =386 FxHashMap::with_capacity_and_hasher(members.len(), BuildHasherDefault::default());387 for (n, b) in members388 .iter()389 .filter_map(|m| match m {390 Member::BindStmt(b) => Some(b.clone()),391 _ => None,392 })393 .map(|b| evaluate_binding(&b, context_creator.clone()))394 {395 bindings.insert(n, b);396 }397 new_bindings.fill(bindings);398 }399400 let mut new_members = FxHashMap::default();401 let mut assertions: Vec<Box<dyn ObjectAssertion>> = Vec::new();402 for member in members.iter() {403 match member {404 Member::Field(FieldMember {405 name,406 plus,407 params: None,408 visibility,409 value,410 }) => {411 let name = evaluate_field_name(context.clone(), name)?;412 if name.is_none() {413 continue;414 }415 let name = name.unwrap();416417 #[derive(Trace, Finalize)]418 struct ObjMemberBinding {419 context_creator: ContextCreator,420 value: LocExpr,421 name: IStr,422 }423 impl Bindable for ObjMemberBinding {424 fn bind(425 &self,426 this: Option<ObjValue>,427 super_obj: Option<ObjValue>,428 ) -> Result<LazyVal> {429 Ok(LazyVal::new_resolved(evaluate_named(430 self.context_creator.create(this, super_obj)?,431 &self.value,432 self.name.clone(),433 )?))434 }435 }436 new_members.insert(437 name.clone(),438 ObjMember {439 add: *plus,440 visibility: *visibility,441 invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {442 context_creator: context_creator.clone(),443 value: value.clone(),444 name,445 }))),446 location: value.1.clone(),447 },448 );449 }450 Member::Field(FieldMember {451 name,452 params: Some(params),453 value,454 ..455 }) => {456 let name = evaluate_field_name(context.clone(), name)?;457 if name.is_none() {458 continue;459 }460 let name = name.unwrap();461 #[derive(Trace, Finalize)]462 struct ObjMemberBinding {463 context_creator: ContextCreator,464 value: LocExpr,465 params: ParamsDesc,466 name: IStr,467 }468 impl Bindable for ObjMemberBinding {469 fn bind(470 &self,471 this: Option<ObjValue>,472 super_obj: Option<ObjValue>,473 ) -> Result<LazyVal> {474 Ok(LazyVal::new_resolved(evaluate_method(475 self.context_creator.create(this, super_obj)?,476 self.name.clone(),477 self.params.clone(),478 self.value.clone(),479 )))480 }481 }482 new_members.insert(483 name.clone(),484 ObjMember {485 add: false,486 visibility: Visibility::Hidden,487 invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {488 context_creator: context_creator.clone(),489 value: value.clone(),490 params: params.clone(),491 name,492 }))),493 location: value.1.clone(),494 },495 );496 }497 Member::BindStmt(_) => {}498 Member::AssertStmt(stmt) => {499 struct ObjectAssert {500 context_creator: ContextCreator,501 assert: AssertStmt,502 }503 impl Finalize for ObjectAssert {}504 unsafe impl Trace for ObjectAssert {505 custom_trace!(this, {506 mark(&this.context_creator);507 mark(&this.assert);508 });509 }510 impl ObjectAssertion for ObjectAssert {511 fn run(512 &self,513 this: Option<ObjValue>,514 super_obj: Option<ObjValue>,515 ) -> Result<()> {516 let ctx = self.context_creator.create(this, super_obj)?;517 evaluate_assert(ctx, &self.assert)518 }519 }520 assertions.push(Box::new(ObjectAssert {521 context_creator: context_creator.clone(),522 assert: stmt.clone(),523 }));524 }525 }526 }527 let this = ObjValue::new(None, Gc::new(new_members), Gc::new(assertions));528 future_this.fill(this.clone());529 Ok(this)530}531532pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {533 Ok(match object {534 ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,535 ObjBody::ObjComp(obj) => {536 let future_this = FutureWrapper::new();537 let mut new_members = FxHashMap::default();538 evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {539 let new_bindings = FutureWrapper::new();540 let context_creator = ContextCreator(context.clone(), new_bindings.clone());541 let mut bindings: FxHashMap<IStr, LazyBinding> =542 FxHashMap::with_capacity_and_hasher(543 obj.pre_locals.len() + obj.post_locals.len(),544 BuildHasherDefault::default(),545 );546 for (n, b) in obj547 .pre_locals548 .iter()549 .chain(obj.post_locals.iter())550 .map(|b| evaluate_binding(b, context_creator.clone()))551 {552 bindings.insert(n, b);553 }554 new_bindings.fill(bindings.clone());555 let ctx = ctx.extend_unbound(bindings, None, None, None)?;556 let key = evaluate(ctx.clone(), &obj.key)?;557558 match key {559 Val::Null => {}560 Val::Str(n) => {561 #[derive(Trace, Finalize)]562 struct ObjCompBinding {563 context: Context,564 value: LocExpr,565 }566 impl Bindable for ObjCompBinding {567 fn bind(568 &self,569 this: Option<ObjValue>,570 _super_obj: Option<ObjValue>,571 ) -> Result<LazyVal> {572 Ok(LazyVal::new_resolved(evaluate(573 self.context.clone().extend(574 FxHashMap::default(),575 None,576 this,577 None,578 ),579 &self.value,580 )?))581 }582 }583 new_members.insert(584 n,585 ObjMember {586 add: false,587 visibility: Visibility::Normal,588 invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjCompBinding {589 context: ctx.clone(),590 value: obj.value.clone(),591 }))),592 location: obj.value.1.clone(),593 },594 );595 }596 v => throw!(FieldMustBeStringGot(v.value_type())),597 }598599 Ok(())600 })?;601602 let this = ObjValue::new(None, Gc::new(new_members), Gc::new(Vec::new()));603 future_this.fill(this.clone());604 this605 }606 })607}608609pub fn evaluate_apply(610 context: Context,611 value: &LocExpr,612 args: &ArgsDesc,613 loc: Option<&ExprLocation>,614 tailstrict: bool,615) -> Result<Val> {616 let value = evaluate(context.clone(), value)?;617 Ok(match value {618 Val::Func(f) => {619 let body = || f.evaluate(context, loc, args, tailstrict);620 if tailstrict {621 body()?622 } else {623 push(loc, || format!("function <{}> call", f.name()), body)?624 }625 }626 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),627 })628}629630pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {631 let value = &assertion.0;632 let msg = &assertion.1;633 let assertion_result = push(634 value.1.as_ref(),635 || "assertion condition".to_owned(),636 || {637 evaluate(context.clone(), value)?638 .try_cast_bool("assertion condition should be of type `boolean`")639 },640 )?;641 if !assertion_result {642 push(643 value.1.as_ref(),644 || "assertion failure".to_owned(),645 || {646 if let Some(msg) = msg {647 throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));648 } else {649 throw!(AssertionFailed(Val::Null.to_string()?));650 }651 },652 )?653 }654 Ok(())655}656657pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {658 use Expr::*;659 let LocExpr(expr, _loc) = lexpr;660 Ok(match &**expr {661 Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),662 _ => evaluate(context, lexpr)?,663 })664}665666pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {667 use Expr::*;668 let LocExpr(expr, loc) = expr;669 Ok(match &**expr {670 Literal(LiteralType::This) => {671 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)672 }673 Literal(LiteralType::Super) => Val::Obj(674 context675 .super_obj()676 .clone()677 .ok_or(NoSuperFound)?678 .with_this(context.this().clone().unwrap()),679 ),680 Literal(LiteralType::Dollar) => {681 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)682 }683 Literal(LiteralType::True) => Val::Bool(true),684 Literal(LiteralType::False) => Val::Bool(false),685 Literal(LiteralType::Null) => Val::Null,686 Parened(e) => evaluate(context, e)?,687 Str(v) => Val::Str(v.clone()),688 Num(v) => Val::new_checked_num(*v)?,689 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,690 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,691 Var(name) => push(692 loc.as_ref(),693 || format!("variable <{}>", name),694 || context.binding(name.clone())?.evaluate(),695 )?,696 Index(value, index) => {697 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {698 (Val::Obj(v), Val::Str(s)) => {699 let sn = s.clone();700 push(701 loc.as_ref(),702 || format!("field <{}> access", sn),703 || {704 if let Some(v) = v.get(s.clone())? {705 Ok(v)706 } else if v.get("__intrinsic_namespace__".into())?.is_some() {707 Ok(Val::Func(Gc::new(FuncVal::Intrinsic(s))))708 } else {709 throw!(NoSuchField(s))710 }711 },712 )?713 }714 (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(715 ValType::Obj,716 ValType::Str,717 n.value_type(),718 )),719720 (Val::Arr(v), Val::Num(n)) => {721 if n.fract() > f64::EPSILON {722 throw!(FractionalIndex)723 }724 v.get(n as usize)?725 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?726 }727 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),728 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(729 ValType::Arr,730 ValType::Num,731 n.value_type(),732 )),733734 (Val::Str(s), Val::Num(n)) => Val::Str(735 s.chars()736 .skip(n as usize)737 .take(1)738 .collect::<String>()739 .into(),740 ),741 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(742 ValType::Str,743 ValType::Num,744 n.value_type(),745 )),746747 (v, _) => throw!(CantIndexInto(v.value_type())),748 }749 }750 LocalExpr(bindings, returned) => {751 let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(752 bindings.len(),753 BuildHasherDefault::<FxHasher>::default(),754 );755 let future_context = Context::new_future();756 for b in bindings {757 new_bindings.insert(758 b.name.clone(),759 evaluate_binding_in_future(b, future_context.clone()),760 );761 }762 let context = context763 .extend_bound(new_bindings)764 .into_future(future_context);765 evaluate(context, &returned.clone())?766 }767 Arr(items) => {768 let mut out = Vec::with_capacity(items.len());769 for item in items {770 // TODO: Implement ArrValue::Lazy with same context for every element?771 struct ArrayElement {772 context: Context,773 item: LocExpr,774 }775 impl Finalize for ArrayElement {}776 unsafe impl Trace for ArrayElement {777 custom_trace!(this, {778 mark(&this.context);779 mark(&this.item);780 });781 }782 impl LazyValValue for ArrayElement {783 fn get(self: Box<Self>) -> Result<Val> {784 evaluate(self.context, &self.item)785 }786 }787 out.push(LazyVal::new(Box::new(ArrayElement {788 context: context.clone(),789 item: item.clone(),790 })));791 }792 Val::Arr(out.into())793 }794 ArrComp(expr, comp_specs) => {795 let mut out = Vec::new();796 evaluate_comp(context, comp_specs, &mut |ctx| {797 out.push(evaluate(ctx, expr)?);798 Ok(())799 })?;800 Val::Arr(ArrValue::Eager(Gc::new(out)))801 }802 Obj(body) => Val::Obj(evaluate_object(context, body)?),803 ObjExtend(s, t) => evaluate_add_op(804 &evaluate(context.clone(), s)?,805 &Val::Obj(evaluate_object(context, t)?),806 )?,807 Apply(value, args, tailstrict) => {808 evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?809 }810 Function(params, body) => {811 evaluate_method(context, "anonymous".into(), params.clone(), body.clone())812 }813 Intrinsic(name) => Val::Func(Gc::new(FuncVal::Intrinsic(name.clone()))),814 AssertExpr(assert, returned) => {815 evaluate_assert(context.clone(), assert)?;816 evaluate(context, returned)?817 }818 ErrorStmt(e) => push(819 loc.as_ref(),820 || "error statement".to_owned(),821 || {822 throw!(RuntimeError(823 evaluate(context, e)?.try_cast_str("error text should be of type `string`")?,824 ))825 },826 )?,827 IfElse {828 cond,829 cond_then,830 cond_else,831 } => {832 if push(833 loc.as_ref(),834 || "if condition".to_owned(),835 || evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),836 )? {837 evaluate(context, cond_then)?838 } else {839 match cond_else {840 Some(v) => evaluate(context, v)?,841 None => Val::Null,842 }843 }844 }845 Import(path) => {846 let mut tmp = loc847 .clone()848 .expect("imports cannot be used without loc_data")849 .0;850 let import_location = Rc::make_mut(&mut tmp);851 import_location.pop();852 push(853 loc.as_ref(),854 || format!("import {:?}", path),855 || with_state(|s| s.import_file(import_location, path)),856 )?857 }858 ImportStr(path) => {859 let mut tmp = loc860 .clone()861 .expect("imports cannot be used without loc_data")862 .0;863 let import_location = Rc::make_mut(&mut tmp);864 import_location.pop();865 Val::Str(with_state(|s| s.import_file_str(import_location, path))?)866 }867 })868}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.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -3,52 +3,75 @@
call_builtin,
manifest::{manifest_json_ex, ManifestJsonOptions, ManifestType},
},
- error::Error::*,
+ error::{Error::*, LocError},
evaluate,
function::{parse_function_call, parse_function_call_map, place_args},
native::NativeCallback,
throw, with_state, Context, ObjValue, Result,
};
+use gc::{custom_trace, Finalize, Gc, GcCell, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
use jrsonnet_types::ValType;
-use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
+use std::{collections::HashMap, fmt::Debug, rc::Rc};
+pub trait LazyValValue: Trace {
+ fn get(self: Box<Self>) -> Result<Val>;
+}
+
enum LazyValInternals {
Computed(Val),
- Waiting(Box<dyn Fn() -> Result<Val>>),
+ Errored(LocError),
+ Waiting(Box<dyn LazyValValue>),
+ Pending,
}
-#[derive(Clone)]
-pub struct LazyVal(Rc<RefCell<LazyValInternals>>);
+impl Finalize for LazyValInternals {}
+unsafe impl Trace for LazyValInternals {
+ custom_trace!(this, {
+ match &this {
+ LazyValInternals::Computed(v) => mark(v),
+ LazyValInternals::Errored(e) => mark(e),
+ LazyValInternals::Waiting(w) => mark(w),
+ LazyValInternals::Pending => {}
+ }
+ });
+}
+
+#[derive(Clone, Trace, Finalize)]
+pub struct LazyVal(Gc<GcCell<LazyValInternals>>);
impl LazyVal {
- pub fn new(f: Box<dyn Fn() -> Result<Val>>) -> Self {
- Self(Rc::new(RefCell::new(LazyValInternals::Waiting(f))))
+ pub fn new(f: Box<dyn LazyValValue>) -> Self {
+ Self(Gc::new(GcCell::new(LazyValInternals::Waiting(f))))
}
pub fn new_resolved(val: Val) -> Self {
- Self(Rc::new(RefCell::new(LazyValInternals::Computed(val))))
+ Self(Gc::new(GcCell::new(LazyValInternals::Computed(val))))
}
pub fn evaluate(&self) -> Result<Val> {
- let new_value = match &*self.0.borrow() {
+ match &*self.0.borrow() {
LazyValInternals::Computed(v) => return Ok(v.clone()),
- LazyValInternals::Waiting(f) => f()?,
+ LazyValInternals::Errored(e) => return Err(e.clone().into()),
+ LazyValInternals::Pending => return Err(RecursiveLazyValueEvaluation.into()),
+ _ => (),
+ };
+ let value = if let LazyValInternals::Waiting(value) =
+ std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)
+ {
+ value
+ } else {
+ unreachable!()
};
+ let new_value = match value.get() {
+ Ok(v) => v,
+ Err(e) => {
+ *self.0.borrow_mut() = LazyValInternals::Errored(e.clone());
+ return Err(e);
+ }
+ };
*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());
Ok(new_value)
}
}
-#[macro_export]
-macro_rules! lazy_val {
- ($f: expr) => {
- $crate::LazyVal::new(Box::new($f))
- };
-}
-#[macro_export]
-macro_rules! resolved_lazy_val {
- ($f: expr) => {
- $crate::LazyVal::new_resolved($f)
- };
-}
impl Debug for LazyVal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Lazy")
@@ -56,11 +79,11 @@
}
impl PartialEq for LazyVal {
fn eq(&self, other: &Self) -> bool {
- Rc::ptr_eq(&self.0, &other.0)
+ Gc::ptr_eq(&self.0, &other.0)
}
}
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Trace, Finalize)]
pub struct FuncDesc {
pub name: IStr,
pub ctx: Context,
@@ -68,14 +91,14 @@
pub body: LocExpr,
}
-#[derive(Debug)]
+#[derive(Debug, Trace, Finalize)]
pub enum FuncVal {
/// Plain function implemented in jsonnet
Normal(FuncDesc),
/// Standard library function
Intrinsic(IStr),
/// Library functions implemented in native
- NativeExt(IStr, Rc<NativeCallback>),
+ NativeExt(IStr, Gc<NativeCallback>),
}
impl PartialEq for FuncVal {
@@ -174,13 +197,23 @@
#[derive(Debug, Clone)]
pub enum ArrValue {
- Lazy(Rc<Vec<LazyVal>>),
- Eager(Rc<Vec<Val>>),
+ Lazy(Gc<Vec<LazyVal>>),
+ Eager(Gc<Vec<Val>>),
Extended(Box<(Self, Self)>),
}
+impl Finalize for ArrValue {}
+unsafe impl Trace for ArrValue {
+ custom_trace!(this, {
+ match &this {
+ ArrValue::Lazy(l) => mark(l),
+ ArrValue::Eager(e) => mark(e),
+ ArrValue::Extended(x) => mark(x),
+ }
+ });
+}
impl ArrValue {
pub fn new_eager() -> Self {
- Self::Eager(Rc::new(Vec::new()))
+ Self::Eager(Gc::new(Vec::new()))
}
pub fn len(&self) -> usize {
@@ -231,14 +264,14 @@
}
}
- pub fn evaluated(&self) -> Result<Rc<Vec<Val>>> {
+ pub fn evaluated(&self) -> Result<Gc<Vec<Val>>> {
Ok(match self {
Self::Lazy(vec) => {
let mut out = Vec::with_capacity(vec.len());
for item in vec.iter() {
out.push(item.evaluate()?);
}
- Rc::new(out)
+ Gc::new(out)
}
Self::Eager(vec) => vec.clone(),
Self::Extended(_v) => {
@@ -246,7 +279,7 @@
for item in self.iter() {
out.push(item?);
}
- Rc::new(out)
+ Gc::new(out)
}
})
}
@@ -272,12 +305,12 @@
Self::Lazy(vec) => {
let mut out = (&vec as &Vec<_>).clone();
out.reverse();
- Self::Lazy(Rc::new(out))
+ Self::Lazy(Gc::new(out))
}
Self::Eager(vec) => {
let mut out = (&vec as &Vec<_>).clone();
out.reverse();
- Self::Eager(Rc::new(out))
+ Self::Eager(Gc::new(out))
}
Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),
}
@@ -290,7 +323,7 @@
out.push(mapper(value?)?);
}
- Ok(Self::Eager(Rc::new(out)))
+ Ok(Self::Eager(Gc::new(out)))
}
pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
@@ -303,13 +336,13 @@
}
}
- Ok(Self::Eager(Rc::new(out)))
+ Ok(Self::Eager(Gc::new(out)))
}
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
match (a, b) {
- (Self::Lazy(a), Self::Lazy(b)) => Rc::ptr_eq(a, b),
- (Self::Eager(a), Self::Eager(b)) => Rc::ptr_eq(a, b),
+ (Self::Lazy(a), Self::Lazy(b)) => Gc::ptr_eq(a, b),
+ (Self::Eager(a), Self::Eager(b)) => Gc::ptr_eq(a, b),
_ => false,
}
}
@@ -317,13 +350,72 @@
impl From<Vec<LazyVal>> for ArrValue {
fn from(v: Vec<LazyVal>) -> Self {
- Self::Lazy(Rc::new(v))
+ Self::Lazy(Gc::new(v))
}
}
impl From<Vec<Val>> for ArrValue {
fn from(v: Vec<Val>) -> Self {
- Self::Eager(Rc::new(v))
+ Self::Eager(Gc::new(v))
+ }
+}
+
+#[derive(Debug)]
+pub struct DebugGcTraceValue {
+ name: IStr,
+ pub value: Box<Val>,
+}
+impl DebugGcTraceValue {
+ fn print(&self, action: &str) {
+ println!("{} {}#{:?}", action, self.name, &*self.value as *const _)
+ }
+}
+impl Finalize for DebugGcTraceValue {
+ fn finalize(&self) {
+ self.print("Garbage-collecting")
+ }
+}
+impl Drop for DebugGcTraceValue {
+ fn drop(&mut self) {
+ self.print("Garbage-collected")
+ }
+}
+unsafe impl Trace for DebugGcTraceValue {
+ unsafe fn trace(&self) {
+ self.print("Traced");
+ self.value.trace()
+ }
+ unsafe fn root(&self) {
+ self.print("Rooted");
+ self.value.root()
+ }
+ unsafe fn unroot(&self) {
+ self.print("Unrooted");
+ self.value.unroot()
+ }
+ fn finalize_glue(&self) {
+ Finalize::finalize(self)
+ }
+}
+impl Clone for DebugGcTraceValue {
+ fn clone(&self) -> Self {
+ self.print("Cloned");
+ let value = DebugGcTraceValue {
+ name: self.name.clone(),
+ value: self.value.clone(),
+ };
+ value.print("I'm clone");
+ value
+ }
+}
+impl DebugGcTraceValue {
+ pub fn new(name: IStr, value: Val) -> Val {
+ let value = Self {
+ name,
+ value: Box::new(value),
+ };
+ value.print("Constructed");
+ Val::DebugGcTraceValue(value)
}
}
@@ -335,7 +427,23 @@
Num(f64),
Arr(ArrValue),
Obj(ObjValue),
- Func(Rc<FuncVal>),
+ Func(Gc<FuncVal>),
+ DebugGcTraceValue(DebugGcTraceValue),
+}
+impl Finalize for Val {}
+unsafe impl Trace for Val {
+ custom_trace!(this, {
+ match &this {
+ Val::Bool(_) => {}
+ Val::Null => {}
+ Val::Str(_) => {}
+ Val::Num(_) => {}
+ Val::Arr(a) => mark(a),
+ Val::Obj(o) => mark(o),
+ Val::Func(f) => mark(f),
+ Val::DebugGcTraceValue(v) => mark(v),
+ }
+ });
}
macro_rules! matches_unwrap {
@@ -368,7 +476,7 @@
pub fn unwrap_num(self) -> Result<f64> {
Ok(matches_unwrap!(self, Self::Num(v), v))
}
- pub fn unwrap_func(self) -> Result<Rc<FuncVal>> {
+ pub fn unwrap_func(self) -> Result<Gc<FuncVal>> {
Ok(matches_unwrap!(self, Self::Func(v), v))
}
pub fn try_cast_bool(self, context: &'static str) -> Result<bool> {
@@ -392,6 +500,7 @@
Self::Bool(_) => ValType::Bool,
Self::Null => ValType::Null,
Self::Func(..) => ValType::Func,
+ Self::DebugGcTraceValue(v) => v.value.value_type(),
}
}