difftreelog
refactor reduce boilerplate by automatic conversions
in: master
29 files changed
bindings/jsonnet/src/native.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -69,10 +69,7 @@
ctx: *const c_void,
mut raw_params: *const *const c_char,
) {
- let name = CStr::from_ptr(name)
- .to_str()
- .expect("name is not utf-8")
- .into();
+ let name = CStr::from_ptr(name).to_str().expect("name is not utf-8");
let mut params = Vec::new();
loop {
if (*raw_params).is_null() {
bindings/jsonnet/src/val_make.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_make.rs
+++ b/bindings/jsonnet/src/val_make.rs
@@ -5,10 +5,7 @@
os::raw::{c_char, c_double, c_int},
};
-use jrsonnet_evaluator::{
- val::{ArrValue, StrValue},
- ObjValue, Val,
-};
+use jrsonnet_evaluator::{val::ArrValue, ObjValue, Val};
use crate::VM;
@@ -21,7 +18,7 @@
pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &VM, val: *const c_char) -> *mut Val {
let val = CStr::from_ptr(val);
let val = val.to_str().expect("string is not utf-8");
- Box::into_raw(Box::new(Val::Str(StrValue::Flat(val.into()))))
+ Box::into_raw(Box::new(Val::string(val)))
}
/// Convert the given double to a `JsonnetJsonValue`.
bindings/jsonnet/src/val_modify.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -5,7 +5,6 @@
use std::{ffi::CStr, os::raw::c_char};
use jrsonnet_evaluator::{val::ArrValue, Thunk, Val};
-use jrsonnet_gcmodule::Cc;
use crate::VM;
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -1,7 +1,7 @@
use std::{fs::read_to_string, str::FromStr};
use clap::Parser;
-use jrsonnet_evaluator::{error::Result, trace::PathResolver, State};
+use jrsonnet_evaluator::{trace::PathResolver, Result, State};
use jrsonnet_stdlib::ContextInitializer;
#[derive(Clone)]
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -6,12 +6,8 @@
use super::ArrValue;
use crate::{
- error::ErrorKind::InfiniteRecursionDetected,
- evaluate,
- function::FuncVal,
- typed::Typed,
- val::{StrValue, ThunkValue},
- Context, Error, ObjValue, Result, Thunk, Val,
+ error::ErrorKind::InfiniteRecursionDetected, evaluate, function::FuncVal, typed::Typed,
+ val::ThunkValue, Context, Error, ObjValue, Result, Thunk, Val,
};
pub trait ArrayLike: Any + Trace + Debug {
@@ -101,9 +97,7 @@
}
fn get_cheap(&self, index: usize) -> Option<Val> {
- self.0
- .get(index)
- .map(|v| Val::Str(StrValue::Flat(IStr::from(*v))))
+ self.0.get(index).map(|v| Val::string(*v))
}
fn is_cheap(&self) -> bool {
true
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -87,9 +87,9 @@
}
#[must_use]
- pub fn with_var(self, name: IStr, value: Val) -> Self {
+ pub fn with_var(self, name: impl Into<IStr>, value: Val) -> Self {
let mut new_bindings = GcHashMap::with_capacity(1);
- new_bindings.insert(name, Thunk::evaluated(value));
+ new_bindings.insert(name.into(), Thunk::evaluated(value));
self.extend(new_bindings, None, None, None)
}
@@ -161,8 +161,8 @@
}
/// # Panics
/// If `name` is already bound
- pub fn bind(&mut self, name: IStr, value: Thunk<Val>) -> &mut Self {
- let old = self.bindings.insert(name, value);
+ pub fn bind(&mut self, name: impl Into<IStr>, value: Thunk<Val>) -> &mut Self {
+ let old = self.bindings.insert(name.into(), value);
assert!(old.is_none(), "variable bound twice in single context call");
self
}
crates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -52,9 +52,3 @@
Self::new()
}
}
-
-impl<T: Trace + Clone> From<Pending<T>> for Thunk<T> {
- fn from(value: Pending<T>) -> Self {
- Self::new(value)
- }
-}
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7 IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13 arr::ArrValue,14 bail,15 destructure::evaluate_dest,16 error::{suggest_object_fields, ErrorKind::*},17 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},18 function::{CallLocation, FuncDesc, FuncVal},19 typed::Typed,20 val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},21 Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, ResultExt,22 State, Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28 fn is_trivial(expr: &LocExpr) -> bool {29 match &*expr.0 {30 Expr::Str(_)31 | Expr::Num(_)32 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33 Expr::Arr(a) => a.iter().all(is_trivial),34 Expr::Parened(e) => is_trivial(e),35 _ => false,36 }37 }38 Some(match &*expr.0 {39 Expr::Str(s) => Val::Str(StrValue::Flat(s.clone())),40 Expr::Num(n) => Val::Num(*n),41 Expr::Literal(LiteralType::False) => Val::Bool(false),42 Expr::Literal(LiteralType::True) => Val::Bool(true),43 Expr::Literal(LiteralType::Null) => Val::Null,44 Expr::Arr(n) => {45 if n.iter().any(|e| !is_trivial(e)) {46 return None;47 }48 Val::Arr(ArrValue::eager(49 n.iter()50 .map(evaluate_trivial)51 .map(|e| e.expect("checked trivial"))52 .collect(),53 ))54 }55 Expr::Parened(e) => evaluate_trivial(e)?,56 _ => return None,57 })58}5960pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {61 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {62 name,63 ctx,64 params,65 body,66 })))67}6869pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {70 Ok(match field_name {71 FieldName::Fixed(n) => Some(n.clone()),72 FieldName::Dyn(expr) => State::push(73 CallLocation::new(&expr.1),74 || "evaluating field name".to_string(),75 || {76 let value = evaluate(ctx, expr)?;77 if matches!(value, Val::Null) {78 Ok(None)79 } else {80 Ok(Some(IStr::from_untyped(value)?))81 }82 },83 )?,84 })85}8687pub fn evaluate_comp(88 ctx: Context,89 specs: &[CompSpec],90 callback: &mut impl FnMut(Context) -> Result<()>,91) -> Result<()> {92 match specs.get(0) {93 None => callback(ctx)?,94 Some(CompSpec::IfSpec(IfSpecData(cond))) => {95 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {96 evaluate_comp(ctx, &specs[1..], callback)?;97 }98 }99 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {100 Val::Arr(list) => {101 for item in list.iter_lazy() {102 let fctx = Pending::new();103 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());104 destruct(var, item, fctx.clone(), &mut new_bindings)?;105 let ctx = ctx106 .clone()107 .extend(new_bindings, None, None, None)108 .into_future(fctx);109110 evaluate_comp(ctx, &specs[1..], callback)?;111 }112 }113 #[cfg(feature = "exp-object-iteration")]114 Val::Obj(obj) => {115 for field in obj.fields(116 // TODO: Should there be ability to preserve iteration order?117 #[cfg(feature = "exp-preserve-order")]118 false,119 ) {120 #[derive(Trace)]121 struct ObjectFieldThunk {122 obj: ObjValue,123 field: IStr,124 }125 impl ThunkValue for ObjectFieldThunk {126 type Output = Val;127128 fn get(self: Box<Self>) -> Result<Self::Output> {129 self.obj.get(self.field).transpose().expect(130 "field exists, as field name was obtained from object.fields()",131 )132 }133 }134135 let fctx = Pending::new();136 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());137 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![138 Thunk::evaluated(Val::Str(StrValue::Flat(field.clone()))),139 Thunk::new(ObjectFieldThunk {140 field: field.clone(),141 obj: obj.clone(),142 }),143 ])));144 destruct(var, value, fctx.clone(), &mut new_bindings)?;145 let ctx = ctx146 .clone()147 .extend(new_bindings, None, None, None)148 .into_future(fctx);149150 evaluate_comp(ctx, &specs[1..], callback)?;151 }152 }153 _ => bail!(InComprehensionCanOnlyIterateOverArray),154 },155 }156 Ok(())157}158159trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}160impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}161162fn evaluate_object_locals(163 fctx: Pending<Context>,164 locals: Rc<Vec<BindSpec>>,165) -> impl CloneableUnbound<Context> {166 #[derive(Trace, Clone)]167 struct UnboundLocals {168 fctx: Pending<Context>,169 locals: Rc<Vec<BindSpec>>,170 }171 impl Unbound for UnboundLocals {172 type Bound = Context;173174 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {175 let fctx = Context::new_future();176 let mut new_bindings =177 GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());178 for b in self.locals.iter() {179 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;180 }181182 let ctx = self.fctx.unwrap();183 let new_dollar = ctx.dollar().cloned().or_else(|| this.clone());184185 let ctx = ctx186 .extend(new_bindings, new_dollar, sup, this)187 .into_future(fctx);188189 Ok(ctx)190 }191 }192193 UnboundLocals { fctx, locals }194}195196pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(197 builder: &mut ObjValueBuilder,198 ctx: Context,199 uctx: B,200 field: &FieldMember,201) -> Result<()> {202 let name = evaluate_field_name(ctx, &field.name)?;203 let Some(name) = name else {204 return Ok(());205 };206207 match field {208 FieldMember {209 plus,210 params: None,211 visibility,212 value,213 ..214 } => {215 #[derive(Trace)]216 struct UnboundValue<B: Trace> {217 uctx: B,218 value: LocExpr,219 name: IStr,220 }221 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {222 type Bound = Val;223 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {224 evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())225 }226 }227228 builder229 .member(name.clone())230 .with_add(*plus)231 .with_visibility(*visibility)232 .with_location(value.1.clone())233 .bindable(UnboundValue {234 uctx,235 value: value.clone(),236 name,237 })?;238 }239 FieldMember {240 params: Some(params),241 visibility,242 value,243 ..244 } => {245 #[derive(Trace)]246 struct UnboundMethod<B: Trace> {247 uctx: B,248 value: LocExpr,249 params: ParamsDesc,250 name: IStr,251 }252 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {253 type Bound = Val;254 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {255 Ok(evaluate_method(256 self.uctx.bind(sup, this)?,257 self.name.clone(),258 self.params.clone(),259 self.value.clone(),260 ))261 }262 }263264 builder265 .member(name.clone())266 .with_visibility(*visibility)267 .with_location(value.1.clone())268 .bindable(UnboundMethod {269 uctx,270 value: value.clone(),271 params: params.clone(),272 name,273 })?;274 }275 }276 Ok(())277}278279#[allow(clippy::too_many_lines)]280pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {281 let mut builder = ObjValueBuilder::new();282 let locals = Rc::new(283 members284 .iter()285 .filter_map(|m| match m {286 Member::BindStmt(bind) => Some(bind.clone()),287 _ => None,288 })289 .collect::<Vec<_>>(),290 );291292 let fctx = Context::new_future();293294 // We have single context for all fields, so we can cache binds295 let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));296297 for member in members {298 match member {299 Member::Field(field) => {300 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;301 }302 Member::AssertStmt(stmt) => {303 #[derive(Trace)]304 struct ObjectAssert<B: Trace> {305 uctx: B,306 assert: AssertStmt,307 }308 impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309 fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {310 let ctx = self.uctx.bind(sup, this)?;311 evaluate_assert(ctx, &self.assert)312 }313 }314 builder.assert(ObjectAssert {315 uctx: uctx.clone(),316 assert: stmt.clone(),317 });318 }319 Member::BindStmt(_) => {320 // Already handled321 }322 }323 }324 let this = builder.build();325 fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));326 Ok(this)327}328329pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {330 Ok(match object {331 ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,332 ObjBody::ObjComp(obj) => {333 let mut builder = ObjValueBuilder::new();334 let locals = Rc::new(335 obj.pre_locals336 .iter()337 .chain(obj.post_locals.iter())338 .cloned()339 .collect::<Vec<_>>(),340 );341 let mut ctxs = vec![];342 evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343 let fctx = Context::new_future();344 ctxs.push((ctx.clone(), fctx.clone()));345 let uctx = evaluate_object_locals(fctx, locals.clone());346347 evaluate_field_member(&mut builder, ctx, uctx, &obj.field)348 })?;349350 let this = builder.build();351 for (ctx, fctx) in ctxs {352 let _ctx = ctx353 .extend(GcHashMap::new(), None, None, Some(this.clone()))354 .into_future(fctx);355 }356 this357 }358 })359}360361pub fn evaluate_apply(362 ctx: Context,363 value: &LocExpr,364 args: &ArgsDesc,365 loc: CallLocation<'_>,366 tailstrict: bool,367) -> Result<Val> {368 let value = evaluate(ctx.clone(), value)?;369 Ok(match value {370 Val::Func(f) => {371 let body = || f.evaluate(ctx, loc, args, tailstrict);372 if tailstrict {373 body()?374 } else {375 State::push(loc, || format!("function <{}> call", f.name()), body)?376 }377 }378 v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),379 })380}381382pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {383 let value = &assertion.0;384 let msg = &assertion.1;385 let assertion_result = State::push(386 CallLocation::new(&value.1),387 || "assertion condition".to_owned(),388 || bool::from_untyped(evaluate(ctx.clone(), value)?),389 )?;390 if !assertion_result {391 State::push(392 CallLocation::new(&value.1),393 || "assertion failure".to_owned(),394 || {395 if let Some(msg) = msg {396 bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));397 }398 bail!(AssertionFailed(Val::Null.to_string()?));399 },400 )?;401 }402 Ok(())403}404405pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {406 use Expr::*;407 let LocExpr(raw_expr, _loc) = expr;408 Ok(match &**raw_expr {409 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410 _ => evaluate(ctx, expr)?,411 })412}413414#[allow(clippy::too_many_lines)]415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {416 use Expr::*;417418 if let Some(trivial) = evaluate_trivial(expr) {419 return Ok(trivial);420 }421 let LocExpr(expr, loc) = expr;422 Ok(match &**expr {423 Literal(LiteralType::This) => {424 Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())425 }426 Literal(LiteralType::Super) => Val::Obj(427 ctx.super_obj().ok_or(NoSuperFound)?.with_this(428 ctx.this()429 .expect("if super exists - then this should too")430 .clone(),431 ),432 ),433 Literal(LiteralType::Dollar) => {434 Val::Obj(ctx.dollar().ok_or(NoTopLevelObjectFound)?.clone())435 }436 Literal(LiteralType::True) => Val::Bool(true),437 Literal(LiteralType::False) => Val::Bool(false),438 Literal(LiteralType::Null) => Val::Null,439 Parened(e) => evaluate(ctx, e)?,440 Str(v) => Val::Str(StrValue::Flat(v.clone())),441 Num(v) => Val::new_checked_num(*v)?,442 BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,443 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,444 Var(name) => State::push(445 CallLocation::new(loc),446 || format!("variable <{name}> access"),447 || ctx.binding(name.clone())?.evaluate(),448 )?,449 Index { indexable, parts } => {450 let mut parts = parts.iter();451 let mut indexable = match &indexable {452 // Cheaper to execute than creating object with overriden `this`453 LocExpr(v, _) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {454 let part = parts.next().expect("at least part should exist");455 let Some(super_obj) = ctx.super_obj() else {456 #[cfg(feature = "exp-null-coaelse")]457 if part.null_coaelse {458 return Ok(Val::Null);459 }460 bail!(NoSuperFound)461 };462 let name = evaluate(ctx.clone(), &part.value)?;463464 let Val::Str(name) = name else {465 bail!(ValueIndexMustBeTypeGot(466 ValType::Obj,467 ValType::Str,468 name.value_type(),469 ))470 };471472 let this = ctx473 .this()474 .expect("no this found, while super present, should not happen");475 let name = name.into_flat();476 match super_obj477 .get_for(name.clone(), this.clone())478 .with_description_src(&part.value, || format!("field <{name}> access"))?479 {480 Some(v) => v,481 #[cfg(feature = "exp-null-coaelse")]482 None if part.null_coaelse => return Ok(Val::Null),483 None => {484 let suggestions = suggest_object_fields(super_obj, name.clone());485486 bail!(NoSuchField(name, suggestions))487 }488 }489 }490 e => evaluate(ctx.clone(), e)?,491 };492493 for part in parts {494 indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {495 (Val::Obj(v), Val::Str(key)) => match v496 .get(key.clone().into_flat())497 .with_description_src(&part.value, || format!("field <{key}> access"))?498 {499 Some(v) => v,500 #[cfg(feature = "exp-null-coaelse")]501 None if part.null_coaelse => return Ok(Val::Null),502 None => {503 let suggestions = suggest_object_fields(&v, key.clone().into_flat());504505 bail!(NoSuchField(key.clone().into_flat(), suggestions))506 }507 },508 (Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(509 ValType::Obj,510 ValType::Str,511 n.value_type(),512 )),513 (Val::Arr(v), Val::Num(n)) => {514 if n.fract() > f64::EPSILON {515 bail!(FractionalIndex)516 }517 v.get(n as usize)?518 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?519 }520 (Val::Arr(_), Val::Str(n)) => {521 bail!(AttemptedIndexAnArrayWithString(n.into_flat()))522 }523 (Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(524 ValType::Arr,525 ValType::Num,526 n.value_type(),527 )),528529 (Val::Str(s), Val::Num(n)) => Val::Str({530 let v: IStr = s531 .clone()532 .into_flat()533 .chars()534 .skip(n as usize)535 .take(1)536 .collect::<String>()537 .into();538 if v.is_empty() {539 let size = s.into_flat().chars().count();540 bail!(StringBoundsError(n as usize, size))541 }542 StrValue::Flat(v)543 }),544 (Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(545 ValType::Str,546 ValType::Num,547 n.value_type(),548 )),549 #[cfg(feature = "exp-null-coaelse")]550 (Val::Null, _) if part.null_coaelse => return Ok(Val::Null),551 (v, _) => bail!(CantIndexInto(v.value_type())),552 };553 }554 indexable555 }556 LocalExpr(bindings, returned) => {557 let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =558 GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());559 let fctx = Context::new_future();560 for b in bindings {561 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;562 }563 let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);564 evaluate(ctx, &returned.clone())?565 }566 Arr(items) => {567 if items.is_empty() {568 Val::Arr(ArrValue::empty())569 } else if items.len() == 1 {570 #[derive(Trace)]571 struct ArrayElement {572 ctx: Context,573 item: LocExpr,574 }575 impl ThunkValue for ArrayElement {576 type Output = Val;577 fn get(self: Box<Self>) -> Result<Val> {578 evaluate(self.ctx, &self.item)579 }580 }581 Val::Arr(ArrValue::lazy(vec![Thunk::new(ArrayElement {582 ctx,583 item: items[0].clone(),584 })]))585 } else {586 Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))587 }588 }589 ArrComp(expr, comp_specs) => {590 let mut out = Vec::new();591 evaluate_comp(ctx, comp_specs, &mut |ctx| {592 out.push(evaluate(ctx, expr)?);593 Ok(())594 })?;595 Val::Arr(ArrValue::eager(out))596 }597 Obj(body) => Val::Obj(evaluate_object(ctx, body)?),598 ObjExtend(a, b) => evaluate_add_op(599 &evaluate(ctx.clone(), a)?,600 &Val::Obj(evaluate_object(ctx, b)?),601 )?,602 Apply(value, args, tailstrict) => {603 evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?604 }605 Function(params, body) => {606 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())607 }608 AssertExpr(assert, returned) => {609 evaluate_assert(ctx.clone(), assert)?;610 evaluate(ctx, returned)?611 }612 ErrorStmt(e) => State::push(613 CallLocation::new(loc),614 || "error statement".to_owned(),615 || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),616 )?,617 IfElse {618 cond,619 cond_then,620 cond_else,621 } => {622 if State::push(623 CallLocation::new(loc),624 || "if condition".to_owned(),625 || bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),626 )? {627 evaluate(ctx, cond_then)?628 } else {629 match cond_else {630 Some(v) => evaluate(ctx, v)?,631 None => Val::Null,632 }633 }634 }635 Slice(value, desc) => {636 fn parse_idx<T: Typed>(637 loc: CallLocation<'_>,638 ctx: &Context,639 expr: Option<&LocExpr>,640 desc: &'static str,641 ) -> Result<Option<T>> {642 if let Some(value) = expr {643 Ok(Some(State::push(644 loc,645 || format!("slice {desc}"),646 || T::from_untyped(evaluate(ctx.clone(), value)?),647 )?))648 } else {649 Ok(None)650 }651 }652653 let indexable = evaluate(ctx.clone(), value)?;654 let loc = CallLocation::new(loc);655656 let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;657 let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;658 let step = parse_idx(loc, &ctx, desc.step.as_ref(), "step")?;659660 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?661 }662 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {663 let Expr::Str(path) = &*path.0 else {664 bail!("computed imports are not supported")665 };666 let tmp = loc.clone().0;667 let s = ctx.state();668 let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;669 match i {670 Import(_) => State::push(671 CallLocation::new(loc),672 || format!("import {:?}", path.clone()),673 || s.import_resolved(resolved_path),674 )?,675 ImportStr(_) => Val::Str(StrValue::Flat(s.import_resolved_str(resolved_path)?)),676 ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),677 _ => unreachable!(),678 }679 }680 })681}1use std::rc::Rc;23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7 IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13 arr::ArrValue,14 bail,15 destructure::evaluate_dest,16 error::{suggest_object_fields, ErrorKind::*},17 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},18 function::{CallLocation, FuncDesc, FuncVal},19 typed::Typed,20 val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},21 Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, ResultExt,22 State, Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28 fn is_trivial(expr: &LocExpr) -> bool {29 match &*expr.0 {30 Expr::Str(_)31 | Expr::Num(_)32 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33 Expr::Arr(a) => a.iter().all(is_trivial),34 Expr::Parened(e) => is_trivial(e),35 _ => false,36 }37 }38 Some(match &*expr.0 {39 Expr::Str(s) => Val::string(s.clone()),40 Expr::Num(n) => Val::Num(*n),41 Expr::Literal(LiteralType::False) => Val::Bool(false),42 Expr::Literal(LiteralType::True) => Val::Bool(true),43 Expr::Literal(LiteralType::Null) => Val::Null,44 Expr::Arr(n) => {45 if n.iter().any(|e| !is_trivial(e)) {46 return None;47 }48 Val::Arr(ArrValue::eager(49 n.iter()50 .map(evaluate_trivial)51 .map(|e| e.expect("checked trivial"))52 .collect(),53 ))54 }55 Expr::Parened(e) => evaluate_trivial(e)?,56 _ => return None,57 })58}5960pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {61 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {62 name,63 ctx,64 params,65 body,66 })))67}6869pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {70 Ok(match field_name {71 FieldName::Fixed(n) => Some(n.clone()),72 FieldName::Dyn(expr) => State::push(73 CallLocation::new(&expr.1),74 || "evaluating field name".to_string(),75 || {76 let value = evaluate(ctx, expr)?;77 if matches!(value, Val::Null) {78 Ok(None)79 } else {80 Ok(Some(IStr::from_untyped(value)?))81 }82 },83 )?,84 })85}8687pub fn evaluate_comp(88 ctx: Context,89 specs: &[CompSpec],90 callback: &mut impl FnMut(Context) -> Result<()>,91) -> Result<()> {92 match specs.get(0) {93 None => callback(ctx)?,94 Some(CompSpec::IfSpec(IfSpecData(cond))) => {95 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {96 evaluate_comp(ctx, &specs[1..], callback)?;97 }98 }99 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {100 Val::Arr(list) => {101 for item in list.iter_lazy() {102 let fctx = Pending::new();103 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());104 destruct(var, item, fctx.clone(), &mut new_bindings)?;105 let ctx = ctx106 .clone()107 .extend(new_bindings, None, None, None)108 .into_future(fctx);109110 evaluate_comp(ctx, &specs[1..], callback)?;111 }112 }113 #[cfg(feature = "exp-object-iteration")]114 Val::Obj(obj) => {115 for field in obj.fields(116 // TODO: Should there be ability to preserve iteration order?117 #[cfg(feature = "exp-preserve-order")]118 false,119 ) {120 #[derive(Trace)]121 struct ObjectFieldThunk {122 obj: ObjValue,123 field: IStr,124 }125 impl ThunkValue for ObjectFieldThunk {126 type Output = Val;127128 fn get(self: Box<Self>) -> Result<Self::Output> {129 self.obj.get(self.field).transpose().expect(130 "field exists, as field name was obtained from object.fields()",131 )132 }133 }134135 let fctx = Pending::new();136 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());137 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(vec![138 Thunk::evaluated(Val::string(field.clone())),139 Thunk::new(ObjectFieldThunk {140 field: field.clone(),141 obj: obj.clone(),142 }),143 ])));144 destruct(var, value, fctx.clone(), &mut new_bindings)?;145 let ctx = ctx146 .clone()147 .extend(new_bindings, None, None, None)148 .into_future(fctx);149150 evaluate_comp(ctx, &specs[1..], callback)?;151 }152 }153 _ => bail!(InComprehensionCanOnlyIterateOverArray),154 },155 }156 Ok(())157}158159trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}160impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}161162fn evaluate_object_locals(163 fctx: Pending<Context>,164 locals: Rc<Vec<BindSpec>>,165) -> impl CloneableUnbound<Context> {166 #[derive(Trace, Clone)]167 struct UnboundLocals {168 fctx: Pending<Context>,169 locals: Rc<Vec<BindSpec>>,170 }171 impl Unbound for UnboundLocals {172 type Bound = Context;173174 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {175 let fctx = Context::new_future();176 let mut new_bindings =177 GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());178 for b in self.locals.iter() {179 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;180 }181182 let ctx = self.fctx.unwrap();183 let new_dollar = ctx.dollar().cloned().or_else(|| this.clone());184185 let ctx = ctx186 .extend(new_bindings, new_dollar, sup, this)187 .into_future(fctx);188189 Ok(ctx)190 }191 }192193 UnboundLocals { fctx, locals }194}195196pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(197 builder: &mut ObjValueBuilder,198 ctx: Context,199 uctx: B,200 field: &FieldMember,201) -> Result<()> {202 let name = evaluate_field_name(ctx, &field.name)?;203 let Some(name) = name else {204 return Ok(());205 };206207 match field {208 FieldMember {209 plus,210 params: None,211 visibility,212 value,213 ..214 } => {215 #[derive(Trace)]216 struct UnboundValue<B: Trace> {217 uctx: B,218 value: LocExpr,219 name: IStr,220 }221 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {222 type Bound = Val;223 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {224 evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())225 }226 }227228 builder229 .field(name.clone())230 .with_add(*plus)231 .with_visibility(*visibility)232 .with_location(value.1.clone())233 .bindable(UnboundValue {234 uctx,235 value: value.clone(),236 name,237 })?;238 }239 FieldMember {240 params: Some(params),241 visibility,242 value,243 ..244 } => {245 #[derive(Trace)]246 struct UnboundMethod<B: Trace> {247 uctx: B,248 value: LocExpr,249 params: ParamsDesc,250 name: IStr,251 }252 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {253 type Bound = Val;254 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {255 Ok(evaluate_method(256 self.uctx.bind(sup, this)?,257 self.name.clone(),258 self.params.clone(),259 self.value.clone(),260 ))261 }262 }263264 builder265 .field(name.clone())266 .with_visibility(*visibility)267 .with_location(value.1.clone())268 .bindable(UnboundMethod {269 uctx,270 value: value.clone(),271 params: params.clone(),272 name,273 })?;274 }275 }276 Ok(())277}278279#[allow(clippy::too_many_lines)]280pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {281 let mut builder = ObjValueBuilder::new();282 let locals = Rc::new(283 members284 .iter()285 .filter_map(|m| match m {286 Member::BindStmt(bind) => Some(bind.clone()),287 _ => None,288 })289 .collect::<Vec<_>>(),290 );291292 let fctx = Context::new_future();293294 // We have single context for all fields, so we can cache binds295 let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));296297 for member in members {298 match member {299 Member::Field(field) => {300 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;301 }302 Member::AssertStmt(stmt) => {303 #[derive(Trace)]304 struct ObjectAssert<B: Trace> {305 uctx: B,306 assert: AssertStmt,307 }308 impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309 fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {310 let ctx = self.uctx.bind(sup, this)?;311 evaluate_assert(ctx, &self.assert)312 }313 }314 builder.assert(ObjectAssert {315 uctx: uctx.clone(),316 assert: stmt.clone(),317 });318 }319 Member::BindStmt(_) => {320 // Already handled321 }322 }323 }324 let this = builder.build();325 fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));326 Ok(this)327}328329pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {330 Ok(match object {331 ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,332 ObjBody::ObjComp(obj) => {333 let mut builder = ObjValueBuilder::new();334 let locals = Rc::new(335 obj.pre_locals336 .iter()337 .chain(obj.post_locals.iter())338 .cloned()339 .collect::<Vec<_>>(),340 );341 let mut ctxs = vec![];342 evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343 let fctx = Context::new_future();344 ctxs.push((ctx.clone(), fctx.clone()));345 let uctx = evaluate_object_locals(fctx, locals.clone());346347 evaluate_field_member(&mut builder, ctx, uctx, &obj.field)348 })?;349350 let this = builder.build();351 for (ctx, fctx) in ctxs {352 let _ctx = ctx353 .extend(GcHashMap::new(), None, None, Some(this.clone()))354 .into_future(fctx);355 }356 this357 }358 })359}360361pub fn evaluate_apply(362 ctx: Context,363 value: &LocExpr,364 args: &ArgsDesc,365 loc: CallLocation<'_>,366 tailstrict: bool,367) -> Result<Val> {368 let value = evaluate(ctx.clone(), value)?;369 Ok(match value {370 Val::Func(f) => {371 let body = || f.evaluate(ctx, loc, args, tailstrict);372 if tailstrict {373 body()?374 } else {375 State::push(loc, || format!("function <{}> call", f.name()), body)?376 }377 }378 v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),379 })380}381382pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {383 let value = &assertion.0;384 let msg = &assertion.1;385 let assertion_result = State::push(386 CallLocation::new(&value.1),387 || "assertion condition".to_owned(),388 || bool::from_untyped(evaluate(ctx.clone(), value)?),389 )?;390 if !assertion_result {391 State::push(392 CallLocation::new(&value.1),393 || "assertion failure".to_owned(),394 || {395 if let Some(msg) = msg {396 bail!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));397 }398 bail!(AssertionFailed(Val::Null.to_string()?));399 },400 )?;401 }402 Ok(())403}404405pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {406 use Expr::*;407 let LocExpr(raw_expr, _loc) = expr;408 Ok(match &**raw_expr {409 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410 _ => evaluate(ctx, expr)?,411 })412}413414#[allow(clippy::too_many_lines)]415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {416 use Expr::*;417418 if let Some(trivial) = evaluate_trivial(expr) {419 return Ok(trivial);420 }421 let LocExpr(expr, loc) = expr;422 Ok(match &**expr {423 Literal(LiteralType::This) => {424 Val::Obj(ctx.this().ok_or(CantUseSelfOutsideOfObject)?.clone())425 }426 Literal(LiteralType::Super) => Val::Obj(427 ctx.super_obj().ok_or(NoSuperFound)?.with_this(428 ctx.this()429 .expect("if super exists - then this should too")430 .clone(),431 ),432 ),433 Literal(LiteralType::Dollar) => {434 Val::Obj(ctx.dollar().ok_or(NoTopLevelObjectFound)?.clone())435 }436 Literal(LiteralType::True) => Val::Bool(true),437 Literal(LiteralType::False) => Val::Bool(false),438 Literal(LiteralType::Null) => Val::Null,439 Parened(e) => evaluate(ctx, e)?,440 Str(v) => Val::string(v.clone()),441 Num(v) => Val::new_checked_num(*v)?,442 BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,443 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,444 Var(name) => State::push(445 CallLocation::new(loc),446 || format!("variable <{name}> access"),447 || ctx.binding(name.clone())?.evaluate(),448 )?,449 Index { indexable, parts } => {450 let mut parts = parts.iter();451 let mut indexable = match &indexable {452 // Cheaper to execute than creating object with overriden `this`453 LocExpr(v, _) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {454 let part = parts.next().expect("at least part should exist");455 let Some(super_obj) = ctx.super_obj() else {456 #[cfg(feature = "exp-null-coaelse")]457 if part.null_coaelse {458 return Ok(Val::Null);459 }460 bail!(NoSuperFound)461 };462 let name = evaluate(ctx.clone(), &part.value)?;463464 let Val::Str(name) = name else {465 bail!(ValueIndexMustBeTypeGot(466 ValType::Obj,467 ValType::Str,468 name.value_type(),469 ))470 };471472 let this = ctx473 .this()474 .expect("no this found, while super present, should not happen");475 let name = name.into_flat();476 match super_obj477 .get_for(name.clone(), this.clone())478 .with_description_src(&part.value, || format!("field <{name}> access"))?479 {480 Some(v) => v,481 #[cfg(feature = "exp-null-coaelse")]482 None if part.null_coaelse => return Ok(Val::Null),483 None => {484 let suggestions = suggest_object_fields(super_obj, name.clone());485486 bail!(NoSuchField(name, suggestions))487 }488 }489 }490 e => evaluate(ctx.clone(), e)?,491 };492493 for part in parts {494 indexable = match (indexable, evaluate(ctx.clone(), &part.value)?) {495 (Val::Obj(v), Val::Str(key)) => match v496 .get(key.clone().into_flat())497 .with_description_src(&part.value, || format!("field <{key}> access"))?498 {499 Some(v) => v,500 #[cfg(feature = "exp-null-coaelse")]501 None if part.null_coaelse => return Ok(Val::Null),502 None => {503 let suggestions = suggest_object_fields(&v, key.clone().into_flat());504505 bail!(NoSuchField(key.clone().into_flat(), suggestions))506 }507 },508 (Val::Obj(_), n) => bail!(ValueIndexMustBeTypeGot(509 ValType::Obj,510 ValType::Str,511 n.value_type(),512 )),513 (Val::Arr(v), Val::Num(n)) => {514 if n.fract() > f64::EPSILON {515 bail!(FractionalIndex)516 }517 v.get(n as usize)?518 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?519 }520 (Val::Arr(_), Val::Str(n)) => {521 bail!(AttemptedIndexAnArrayWithString(n.into_flat()))522 }523 (Val::Arr(_), n) => bail!(ValueIndexMustBeTypeGot(524 ValType::Arr,525 ValType::Num,526 n.value_type(),527 )),528529 (Val::Str(s), Val::Num(n)) => Val::Str({530 let v: IStr = s531 .clone()532 .into_flat()533 .chars()534 .skip(n as usize)535 .take(1)536 .collect::<String>()537 .into();538 if v.is_empty() {539 let size = s.into_flat().chars().count();540 bail!(StringBoundsError(n as usize, size))541 }542 StrValue::Flat(v)543 }),544 (Val::Str(_), n) => bail!(ValueIndexMustBeTypeGot(545 ValType::Str,546 ValType::Num,547 n.value_type(),548 )),549 #[cfg(feature = "exp-null-coaelse")]550 (Val::Null, _) if part.null_coaelse => return Ok(Val::Null),551 (v, _) => bail!(CantIndexInto(v.value_type())),552 };553 }554 indexable555 }556 LocalExpr(bindings, returned) => {557 let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =558 GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());559 let fctx = Context::new_future();560 for b in bindings {561 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;562 }563 let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);564 evaluate(ctx, &returned.clone())?565 }566 Arr(items) => {567 if items.is_empty() {568 Val::Arr(ArrValue::empty())569 } else if items.len() == 1 {570 #[derive(Trace)]571 struct ArrayElement {572 ctx: Context,573 item: LocExpr,574 }575 impl ThunkValue for ArrayElement {576 type Output = Val;577 fn get(self: Box<Self>) -> Result<Val> {578 evaluate(self.ctx, &self.item)579 }580 }581 Val::Arr(ArrValue::lazy(vec![Thunk::new(ArrayElement {582 ctx,583 item: items[0].clone(),584 })]))585 } else {586 Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))587 }588 }589 ArrComp(expr, comp_specs) => {590 let mut out = Vec::new();591 evaluate_comp(ctx, comp_specs, &mut |ctx| {592 out.push(evaluate(ctx, expr)?);593 Ok(())594 })?;595 Val::Arr(ArrValue::eager(out))596 }597 Obj(body) => Val::Obj(evaluate_object(ctx, body)?),598 ObjExtend(a, b) => evaluate_add_op(599 &evaluate(ctx.clone(), a)?,600 &Val::Obj(evaluate_object(ctx, b)?),601 )?,602 Apply(value, args, tailstrict) => {603 evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?604 }605 Function(params, body) => {606 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())607 }608 AssertExpr(assert, returned) => {609 evaluate_assert(ctx.clone(), assert)?;610 evaluate(ctx, returned)?611 }612 ErrorStmt(e) => State::push(613 CallLocation::new(loc),614 || "error statement".to_owned(),615 || bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),616 )?,617 IfElse {618 cond,619 cond_then,620 cond_else,621 } => {622 if State::push(623 CallLocation::new(loc),624 || "if condition".to_owned(),625 || bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),626 )? {627 evaluate(ctx, cond_then)?628 } else {629 match cond_else {630 Some(v) => evaluate(ctx, v)?,631 None => Val::Null,632 }633 }634 }635 Slice(value, desc) => {636 fn parse_idx<T: Typed>(637 loc: CallLocation<'_>,638 ctx: &Context,639 expr: Option<&LocExpr>,640 desc: &'static str,641 ) -> Result<Option<T>> {642 if let Some(value) = expr {643 Ok(Some(State::push(644 loc,645 || format!("slice {desc}"),646 || T::from_untyped(evaluate(ctx.clone(), value)?),647 )?))648 } else {649 Ok(None)650 }651 }652653 let indexable = evaluate(ctx.clone(), value)?;654 let loc = CallLocation::new(loc);655656 let start = parse_idx(loc, &ctx, desc.start.as_ref(), "start")?;657 let end = parse_idx(loc, &ctx, desc.end.as_ref(), "end")?;658 let step = parse_idx(loc, &ctx, desc.step.as_ref(), "step")?;659660 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?661 }662 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {663 let Expr::Str(path) = &*path.0 else {664 bail!("computed imports are not supported")665 };666 let tmp = loc.clone().0;667 let s = ctx.state();668 let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;669 match i {670 Import(_) => State::push(671 CallLocation::new(loc),672 || format!("import {:?}", path.clone()),673 || s.import_resolved(resolved_path),674 )?,675 ImportStr(_) => Val::string(s.import_resolved_str(resolved_path)?),676 ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),677 _ => unreachable!(),678 }679 }680 })681}crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -30,18 +30,12 @@
Ok(match (a, b) {
(Str(v1), Str(v2)) => Str(StrValue::concat(v1.clone(), v2.clone())),
- (Num(a), Str(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),
- (Str(a), Num(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),
+ (Num(a), Str(b)) => Val::string(format!("{a}{b}")),
+ (Str(a), Num(b)) => Val::string(format!("{a}{b}")),
- (Str(a), o) | (o, Str(a)) if a.is_empty() => {
- Val::Str(StrValue::Flat(o.clone().to_string()?))
- }
- (Str(a), o) => Str(StrValue::Flat(
- format!("{a}{}", o.clone().to_string()?).into(),
- )),
- (o, Str(a)) => Str(StrValue::Flat(
- format!("{}{a}", o.clone().to_string()?).into(),
- )),
+ (Str(a), o) | (o, Str(a)) if a.is_empty() => Val::string(o.clone().to_string()?),
+ (Str(a), o) => Val::string(format!("{a}{}", o.clone().to_string()?)),
+ (o, Str(a)) => Val::string(format!("{}{a}", o.clone().to_string()?)),
(Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),
(Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),
@@ -149,7 +143,7 @@
(Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone().into_flat(), true)),
(a, Mod, b) => evaluate_mod_op(a, b)?,
- (Str(v1), Mul, Num(v2)) => Str(StrValue::Flat(v1.to_string().repeat(*v2 as usize).into())),
+ (Str(v1), Mul, Num(v2)) => Val::string(v1.to_string().repeat(*v2 as usize)),
// Bool X Bool
(Bool(a), And, Bool(b)) => Bool(*a && *b),
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -61,7 +61,7 @@
impl ArgLike for TlaArg {
fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
match self {
- TlaArg::String(s) => Ok(Thunk::evaluated(Val::Str(StrValue::Flat(s.clone())))),
+ TlaArg::String(s) => Ok(Thunk::evaluated(Val::string(s.clone()))),
TlaArg::Code(code) => Ok(if tailstrict {
Thunk::evaluated(evaluate(ctx, code)?)
} else {
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -12,7 +12,10 @@
native::NativeDesc,
parse::{parse_default_function_call, parse_function_call},
};
-use crate::{evaluate, evaluate_trivial, gc::TraceBox, tb, Context, ContextBuilder, Result, Val};
+use crate::{
+ evaluate, evaluate_trivial, gc::TraceBox, tb, Context, ContextBuilder, Result,
+ Val,
+};
pub mod arglike;
pub mod builtin;
@@ -124,6 +127,9 @@
pub fn builtin(builtin: impl Builtin) -> Self {
Self::Builtin(Cc::new(tb!(builtin)))
}
+ pub fn static_builtin(static_builtin: &'static dyn StaticBuiltin) -> Self {
+ Self::StaticBuiltin(static_builtin)
+ }
pub fn params(&self) -> Vec<BuiltinParam> {
match self {
@@ -239,3 +245,17 @@
}
}
}
+
+impl<T> From<T> for FuncVal
+where
+ T: Builtin,
+{
+ fn from(value: T) -> Self {
+ Self::builtin(value)
+ }
+}
+impl From<&'static dyn StaticBuiltin> for FuncVal {
+ fn from(value: &'static dyn StaticBuiltin) -> Self {
+ Self::static_builtin(value)
+ }
+}
crates/jrsonnet-evaluator/src/function/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/native.rs
+++ b/crates/jrsonnet-evaluator/src/function/native.rs
@@ -2,7 +2,7 @@
arglike::{ArgLike, OptionalContext},
FuncVal,
};
-use crate::{error::Result, typed::Typed};
+use crate::{typed::Typed, Result};
pub trait NativeDesc {
type Value;
crates/jrsonnet-evaluator/src/gc.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/gc.rs
+++ b/crates/jrsonnet-evaluator/src/gc.rs
@@ -117,9 +117,7 @@
}
#[derive(Debug)]
-pub struct GcHashMap<K, V>(
- pub HashMap<K, V, BuildHasherDefault<FxHasher>>
-);
+pub struct GcHashMap<K, V>(pub HashMap<K, V, BuildHasherDefault<FxHasher>>);
impl<K, V> GcHashMap<K, V> {
pub fn new() -> Self {
Self(HashMap::default())
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -11,10 +11,8 @@
};
use crate::{
- arr::ArrValue,
- error::{Error as JrError, ErrorKind, Result},
- val::StrValue,
- ObjValue, ObjValueBuilder, State, Val,
+ arr::ArrValue, runtime_error, Error as JrError, ObjValue, ObjValueBuilder,
+ Result, State, Val,
};
impl<'de> Deserialize<'de> for Val {
@@ -57,7 +55,7 @@
where
E: serde::de::Error,
{
- Ok(Val::Str(StrValue::Flat(v.into())))
+ Ok(Val::string(v))
}
// visit_num! {
@@ -138,7 +136,7 @@
while let Some((k, v)) = map.next_entry::<Cow<'de, str>, Val>()? {
// Jsonnet ignores duplicate keys
- out.member(k.into()).value_unchecked(v);
+ out.field(k).value(v);
}
Ok(Val::Obj(out.build()))
@@ -264,7 +262,7 @@
let inner = Val::Arr(ArrValue::eager(self.data));
if let Some(variant) = self.variant {
let mut out = ObjValue::builder_with_capacity(1);
- out.member(variant).value_unchecked(inner);
+ out.field(variant).value(inner);
Ok(Val::Obj(out.build()))
} else {
Ok(inner)
@@ -365,7 +363,7 @@
{
let key = self.key.take().expect("no serialize_key called");
let value = value.serialize(IntoValSerializer)?;
- self.data.member(key).value(value)?;
+ self.data.field(key).try_value(value)?;
Ok(())
}
@@ -378,7 +376,7 @@
let key = key.serialize(IntoValSerializer)?;
let key = key.to_string()?;
let value = value.serialize(IntoValSerializer)?;
- self.data.member(key).value(value)?;
+ self.data.field(key).try_value(value)?;
Ok(())
}
@@ -386,7 +384,7 @@
let inner = Val::Obj(self.data.build());
if let Some(variant) = self.variant {
let mut out = ObjValue::builder_with_capacity(1);
- out.member(variant).value_unchecked(inner);
+ out.field(variant).value(inner);
Ok(Val::Obj(out.build()))
} else {
Ok(inner)
@@ -550,7 +548,7 @@
{
let mut out = ObjValue::builder_with_capacity(1);
let value = value.serialize(self)?;
- out.member(variant.into()).value_unchecked(value);
+ out.field(variant).value(value);
Ok(Val::Obj(out.build()))
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -498,7 +498,7 @@
pub struct InitialUnderscore(pub Thunk<Val>);
impl ContextInitializer for InitialUnderscore {
fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {
- builder.bind("_".into(), self.0.clone());
+ builder.bind("_", self.0.clone());
}
fn as_any(&self) -> &dyn Any {
crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -174,11 +174,13 @@
Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),
Val::Num(n) => write!(buf, "{n}").unwrap(),
#[cfg(feature = "exp-bigint")]
- Val::BigInt(n) => if options.preserve_bigints {
- write!(buf, "{n}").unwrap()
- } else {
- write!(buf, "{:?}", n.to_string()).unwrap()
- },
+ Val::BigInt(n) => {
+ if options.preserve_bigints {
+ write!(buf, "{n}").unwrap()
+ } else {
+ write!(buf, "{:?}", n.to_string()).unwrap()
+ }
+ }
Val::Arr(items) => {
buf.push('[');
if !items.is_empty() {
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -15,7 +15,7 @@
arr::{PickObjectKeyValues, PickObjectValues},
bail,
error::{suggest_object_fields, Error, ErrorKind::*},
- function::CallLocation,
+ function::{CallLocation, FuncVal},
gc::{GcHashMap, GcHashSet, TraceBox},
operator::evaluate_add_op,
tb,
@@ -345,7 +345,7 @@
pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {
let mut out = ObjValueBuilder::with_capacity(1);
out.with_super(self);
- let mut member = out.member(key);
+ let mut member = out.field(key);
if value.flags.add() {
member = member.add()
}
@@ -848,11 +848,27 @@
self.assertions.push(tb!(assertion));
self
}
- pub fn member(&mut self, name: IStr) -> ObjMemberBuilder<ValueBuilder<'_>> {
+ pub fn field(&mut self, name: impl Into<IStr>) -> ObjMemberBuilder<ValueBuilder<'_>> {
let field_index = self.next_field_index;
self.next_field_index = self.next_field_index.next();
- ObjMemberBuilder::new(ValueBuilder(self), name, field_index)
+ ObjMemberBuilder::new(ValueBuilder(self), name.into(), field_index)
}
+ /// Preset for common method definiton pattern:
+ /// Create a hidden field with the function value.
+ ///
+ /// `.field(name).hide().value(Val::function(value))`
+ pub fn method(&mut self, name: impl Into<IStr>, value: impl Into<FuncVal>) -> &mut Self {
+ self.field(name).hide().value(Val::Func(value.into()));
+ self
+ }
+ pub fn try_method(
+ &mut self,
+ name: impl Into<IStr>,
+ value: impl Into<FuncVal>,
+ ) -> Result<&mut Self> {
+ self.field(name).hide().try_value(Val::Func(value.into()))?;
+ Ok(self)
+ }
pub fn build(self) -> ObjValue {
if self.sup.is_none() && self.map.is_empty() && self.assertions.is_empty() {
@@ -930,18 +946,19 @@
pub struct ValueBuilder<'v>(&'v mut ObjValueBuilder);
impl ObjMemberBuilder<ValueBuilder<'_>> {
/// Inserts value, replacing if it is already defined
- pub fn value_unchecked(self, value: Val) {
+ pub fn value(self, value: impl Into<Val>) {
let (receiver, name, member) =
- self.build_member(MaybeUnbound::Bound(Thunk::evaluated(value)));
+ self.build_member(MaybeUnbound::Bound(Thunk::evaluated(value.into())));
let entry = receiver.0.map.entry(name);
entry.insert(member);
}
- pub fn value(self, value: Val) -> Result<()> {
- self.thunk(Thunk::evaluated(value))
+ /// Tries to insert value, returns an error if it was already defined
+ pub fn try_value(self, value: impl Into<Val>) -> Result<()> {
+ self.thunk(Thunk::evaluated(value.into()))
}
- pub fn thunk(self, value: Thunk<Val>) -> Result<()> {
- self.binding(MaybeUnbound::Bound(value))
+ pub fn thunk(self, value: impl Into<Thunk<Val>>) -> Result<()> {
+ self.binding(MaybeUnbound::Bound(value.into()))
}
pub fn bindable(self, bindable: impl Unbound<Bound = Val>) -> Result<()> {
self.binding(MaybeUnbound::Unbound(Cc::new(tb!(bindable))))
@@ -963,8 +980,8 @@
pub struct ExtendBuilder<'v>(&'v mut ObjValue);
impl ObjMemberBuilder<ExtendBuilder<'_>> {
- pub fn value(self, value: Val) {
- self.binding(MaybeUnbound::Bound(Thunk::evaluated(value)));
+ pub fn value(self, value: impl Into<Val>) {
+ self.binding(MaybeUnbound::Bound(Thunk::evaluated(value.into())));
}
pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Val>>) {
self.binding(MaybeUnbound::Unbound(Cc::new(bindable)));
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -276,7 +276,7 @@
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);
fn into_untyped(value: Self) -> Result<Val> {
- Ok(Val::Str(StrValue::Flat(value)))
+ Ok(Val::string(value))
}
fn from_untyped(value: Val) -> Result<Self> {
@@ -292,7 +292,7 @@
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);
fn into_untyped(value: Self) -> Result<Val> {
- Ok(Val::Str(StrValue::Flat(value.into())))
+ Ok(Val::string(value))
}
fn from_untyped(value: Val) -> Result<Self> {
@@ -308,7 +308,7 @@
const TYPE: &'static ComplexValType = &ComplexValType::Char;
fn into_untyped(value: Self) -> Result<Val> {
- Ok(Val::Str(StrValue::Flat(value.to_string().into())))
+ Ok(Val::string(value))
}
fn from_untyped(value: Val) -> Result<Self> {
@@ -356,7 +356,7 @@
bail!("map key should serialize to string");
};
let value = V::into_untyped(v)?;
- out.member(key).value_unchecked(value);
+ out.field(key).value(value);
}
Ok(Val::Obj(out.build()))
}
@@ -611,7 +611,7 @@
fn into_untyped(value: Self) -> Result<Val> {
match value {
- IndexableVal::Str(s) => Ok(Val::Str(StrValue::Flat(s))),
+ IndexableVal::Str(s) => Ok(Val::string(s)),
IndexableVal::Arr(a) => Ok(Val::Arr(a)),
}
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -142,6 +142,14 @@
}
}
}
+impl<T, V: Trace> From<T> for Thunk<V>
+where
+ T: ThunkValue<Output = V>,
+{
+ fn from(value: T) -> Self {
+ Thunk::new(value)
+ }
+}
impl<T: Trace + Default> Default for Thunk<T> {
fn default() -> Self {
@@ -323,21 +331,14 @@
}
}
}
-impl From<&str> for StrValue {
- fn from(value: &str) -> Self {
- Self::Flat(value.into())
- }
-}
-impl From<String> for StrValue {
- fn from(value: String) -> Self {
- Self::Flat(value.into())
+impl<T> From<T> for StrValue
+where
+ IStr: From<T>,
+{
+ fn from(value: T) -> Self {
+ Self::Flat(IStr::from(value))
}
}
-impl From<IStr> for StrValue {
- fn from(value: IStr) -> Self {
- Self::Flat(value)
- }
-}
impl Display for StrValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@@ -401,7 +402,7 @@
impl From<IndexableVal> for Val {
fn from(v: IndexableVal) -> Self {
match v {
- IndexableVal::Str(s) => Self::Str(StrValue::Flat(s)),
+ IndexableVal::Str(s) => Self::string(s),
IndexableVal::Arr(a) => Self::Arr(a),
}
}
@@ -499,6 +500,34 @@
_ => bail!(ValueIsNotIndexable(self.value_type())),
})
}
+
+ pub fn function(function: impl Into<FuncVal>) -> Self {
+ Self::Func(function.into())
+ }
+ pub fn string(string: impl Into<StrValue>) -> Self {
+ Self::Str(string.into())
+ }
+}
+
+impl From<IStr> for Val {
+ fn from(value: IStr) -> Self {
+ Self::string(value)
+ }
+}
+impl From<String> for Val {
+ fn from(value: String) -> Self {
+ Self::string(value)
+ }
+}
+impl From<&str> for Val {
+ fn from(value: &str) -> Self {
+ Self::string(value)
+ }
+}
+impl From<ObjValue> for Val {
+ fn from(value: ObjValue) -> Self {
+ Self::Obj(value)
+ }
}
const fn is_function_like(val: &Val) -> bool {
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -567,18 +567,18 @@
if self.is_option {
quote! {
if let Some(value) = self.#ident {
- out.member(#name.into())
+ out.field(#name)
#hide
#add
- .value(<#ty as Typed>::into_untyped(value)?)?;
+ .try_value(<#ty as Typed>::into_untyped(value)?)?;
}
}
} else {
quote! {
- out.member(#name.into())
+ out.field(#name)
#hide
#add
- .value(<#ty as Typed>::into_untyped(self.#ident)?)?;
+ .try_value(<#ty as Typed>::into_untyped(self.#ident)?)?;
}
}
} else if self.is_option {
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -36,7 +36,7 @@
#[builtin]
pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {
Ok(match what {
- Either2::A(s) => Val::Str(StrValue::Flat(s.repeat(count).into())),
+ Either2::A(s) => Val::string(s.repeat(count)),
Either2::B(arr) => Val::Arr(
ArrValue::repeated(arr, count)
.ok_or_else(|| runtime_error!("repeated length overflow"))?,
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -6,13 +6,12 @@
use jrsonnet_evaluator::{
error::{ErrorKind::*, Result},
- function::{builtin::Builtin, CallLocation, FuncVal, TlaArg},
- gc::TraceBox,
+ function::{CallLocation, FuncVal, TlaArg},
tb,
trace::PathResolver,
ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
};
-use jrsonnet_gcmodule::{Cc, Trace};
+use jrsonnet_gcmodule::Trace;
use jrsonnet_parser::Source;
mod expr;
@@ -182,38 +181,24 @@
.iter()
.cloned()
{
- builder
- .member(name.into())
- .hide()
- .value(Val::Func(FuncVal::StaticBuiltin(builtin)))
- .expect("no conflict");
+ builder.method(name, builtin);
}
- builder
- .member("extVar".into())
- .hide()
- .value(Val::Func(FuncVal::builtin(builtin_ext_var {
+ builder.method(
+ "extVar",
+ builtin_ext_var {
settings: settings.clone(),
- })))
- .expect("no conflict");
- builder
- .member("native".into())
- .hide()
- .value(Val::Func(FuncVal::builtin(builtin_native {
+ },
+ );
+ builder.method(
+ "native",
+ builtin_native {
settings: settings.clone(),
- })))
- .expect("no conflict");
- builder
- .member("trace".into())
- .hide()
- .value(Val::Func(FuncVal::builtin(builtin_trace { settings })))
- .expect("no conflict");
+ },
+ );
+ builder.method("trace", builtin_trace { settings });
- builder
- .member("id".into())
- .hide()
- .value(Val::Func(FuncVal::Id))
- .expect("no conflict");
+ builder.method("id", FuncVal::Id);
builder.build()
}
@@ -293,7 +278,7 @@
#[cfg(not(feature = "legacy-this-file"))]
context: {
let mut context = ContextBuilder::with_capacity(_s, 1);
- context.bind("std".into(), stdlib_thunk.clone());
+ context.bind("std", stdlib_thunk.clone());
context.build()
},
#[cfg(not(feature = "legacy-this-file"))]
@@ -338,10 +323,10 @@
.insert(name.into(), TlaArg::Code(parsed));
Ok(())
}
- pub fn add_native(&self, name: IStr, cb: impl Builtin) {
+ pub fn add_native(&self, name: impl Into<IStr>, cb: impl Into<FuncVal>) {
self.settings_mut()
.ext_natives
- .insert(name, Cc::new(tb!(cb)));
+ .insert(name.into(), cb.into());
}
}
impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {
@@ -354,7 +339,7 @@
}
#[cfg(not(feature = "legacy-this-file"))]
fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {
- builder.bind("std".into(), self.stdlib_thunk.clone());
+ builder.bind("std", self.stdlib_thunk.clone());
}
#[cfg(feature = "legacy-this-file")]
fn populate(&self, source: Source, builder: &mut ContextBuilder) {
@@ -362,14 +347,14 @@
let mut std = ObjValueBuilder::new();
std.with_super(self.stdlib_obj.clone());
- std.member("thisFile".into())
+ std.field("thisFile".into())
.hide()
- .value(Val::Str(StrValue::Flat(
+ .value(Val::string(
match source.source_path().path() {
Some(p) => self.settings().path_resolver.resolve(p).into(),
None => source.source_path().to_string().into(),
},
- )))
+ ))
.expect("this object builder is empty");
let stdlib_with_this_file = std.build();
crates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -47,7 +47,7 @@
.ext_natives
.get(&x)
.cloned()
- .map_or(Val::Null, |v| Val::Func(FuncVal::Builtin(v)))
+ .map_or(Val::Null, |v| Val::Func(v))
}
#[builtin(fields(
crates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/objects.rs
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -122,7 +122,7 @@
if k == key {
continue;
}
- new_obj.member(k).value_unchecked(v.unwrap())
+ new_obj.field(k).value(v.unwrap())
}
new_obj.build()
crates/jrsonnet-stdlib/src/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/operator.rs
+++ b/crates/jrsonnet-stdlib/src/operator.rs
@@ -16,7 +16,7 @@
evaluate_mod_op(
&match a {
A(v) => Val::Num(v),
- B(s) => Val::Str(StrValue::Flat(s)),
+ B(s) => Val::string(s),
},
&b,
)
crates/jrsonnet-stdlib/src/sets.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/sets.rs
+++ b/crates/jrsonnet-stdlib/src/sets.rs
@@ -1,11 +1,10 @@
use std::cmp::Ordering;
use jrsonnet_evaluator::{
- error::Result,
function::{builtin, FuncVal},
operator::evaluate_compare_op,
val::ArrValue,
- Thunk, Val,
+ Result, Thunk, Val,
};
use jrsonnet_parser::BinaryOpType;
@@ -108,7 +107,7 @@
}
};
}
- while let Some(ac) = &ak {
+ while let Some(_ac) = &ak {
// In a, but not in b
out.push(av.clone().expect("ak != None"));
av = a.next();
crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -3,7 +3,7 @@
error::{ErrorKind::*, Result},
function::builtin,
typed::{Either2, M1},
- val::{ArrValue, StrValue},
+ val::ArrValue,
Either, IStr, Val,
};
@@ -41,14 +41,8 @@
pub fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> ArrValue {
use Either2::*;
match maxsplits {
- A(n) => str
- .splitn(n + 1, &c as &str)
- .map(|s| Val::Str(StrValue::Flat(s.into())))
- .collect(),
- B(_) => str
- .split(&c as &str)
- .map(|s| Val::Str(StrValue::Flat(s.into())))
- .collect(),
+ A(n) => str.splitn(n + 1, &c as &str).map(Val::string).collect(),
+ B(_) => str.split(&c as &str).map(Val::string).collect(),
}
}
tests/tests/builtin.rsdiffbeforeafterboth--- a/tests/tests/builtin.rs
+++ b/tests/tests/builtin.rs
@@ -36,7 +36,7 @@
s.with_stdlib();
s.add_global(
"nativeAdd".into(),
- Thunk::evaluated(Val::Func(FuncVal::StaticBuiltin(native_add::INST))),
+ Thunk::evaluated(Val::function(native_add::INST)),
);
let v = s.evaluate_snippet(
@@ -69,7 +69,7 @@
s.with_stdlib();
s.add_global(
"curryAdd".into(),
- Thunk::evaluated(Val::Func(FuncVal::StaticBuiltin(curry_add::INST))),
+ Thunk::evaluated(Val::function(curry_add::INST)),
);
let v = s.evaluate_snippet(
tests/tests/common.rsdiffbeforeafterboth--- a/tests/tests/common.rs
+++ b/tests/tests/common.rs
@@ -77,12 +77,8 @@
#[allow(dead_code)]
pub fn with_test(s: &State) {
let mut bobj = ObjValueBuilder::new();
- bobj.member("assertThrow".into())
- .hide()
- .value_unchecked(Val::Func(FuncVal::StaticBuiltin(assert_throw::INST)));
- bobj.member("paramNames".into())
- .hide()
- .value_unchecked(Val::Func(FuncVal::StaticBuiltin(param_names::INST)));
+ bobj.method("assertThrow", assert_throw::INST);
+ bobj.method("paramNames", param_names::INST);
s.add_global("test".into(), Thunk::evaluated(Val::Obj(bobj.build())))
}