difftreelog
feat lazy values in builtin
in: master
8 files changed
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,4 +1,4 @@
-use crate::function::StaticBuiltin;
+use crate::function::{CallLocation, StaticBuiltin};
use crate::typed::{Any, PositiveF64, VecVal, M1};
use crate::{
builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
@@ -12,7 +12,6 @@
use crate::{Either, ObjValue};
use format::{format_arr, format_obj};
use jrsonnet_interner::IStr;
-use jrsonnet_parser::ExprLocation;
use serde::Deserialize;
use serde_yaml::DeserializingQuirks;
use std::collections::HashMap;
@@ -29,7 +28,7 @@
pub fn std_format(str: IStr, vals: Val) -> Result<String> {
push_frame(
- None,
+ CallLocation::native(),
|| format!("std.format of {}", str),
|| {
Ok(match vals {
@@ -467,9 +466,9 @@
}
#[jrsonnet_macros::builtin]
-fn builtin_trace(#[location] loc: Option<&ExprLocation>, str: IStr, rest: Any) -> Result<Any> {
+fn builtin_trace(loc: CallLocation, str: IStr, rest: Any) -> Result<Any> {
eprint!("TRACE:");
- if let Some(loc) = loc {
+ if let Some(loc) = loc.0 {
with_state(|s| {
let locs = s.map_source_locations(&loc.0, &[loc.1]);
eprint!(
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -4,6 +4,7 @@
builtin::{std_slice, BUILTINS},
error::Error::*,
evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
+ function::CallLocation,
gc::TraceBox,
push_frame, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
FutureWrapper, GcHashMap, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder,
@@ -12,8 +13,8 @@
use gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{
- ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, ExprLocation, FieldMember, ForSpecData,
- IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
+ ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, ForSpecData, IfSpecData,
+ LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
};
use jrsonnet_types::ValType;
pub mod operator;
@@ -192,7 +193,7 @@
Ok(match field_name {
jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),
jrsonnet_parser::FieldName::Dyn(expr) => push_frame(
- Some(&expr.1),
+ CallLocation::new(&expr.1),
|| "evaluating field name".to_string(),
|| {
let value = evaluate(context, expr)?;
@@ -442,7 +443,7 @@
context: Context,
value: &LocExpr,
args: &ArgsDesc,
- loc: Option<&ExprLocation>,
+ loc: CallLocation,
tailstrict: bool,
) -> Result<Val> {
let value = evaluate(context.clone(), value)?;
@@ -463,13 +464,13 @@
let value = &assertion.0;
let msg = &assertion.1;
let assertion_result = push_frame(
- Some(&value.1),
+ CallLocation::new(&value.1),
|| "assertion condition".to_owned(),
|| bool::try_from(evaluate(context.clone(), value)?),
)?;
if !assertion_result {
push_frame(
- Some(&value.1),
+ CallLocation::new(&value.1),
|| "assertion failure".to_owned(),
|| {
if let Some(msg) = msg {
@@ -519,7 +520,7 @@
BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
Var(name) => push_frame(
- Some(loc),
+ CallLocation::new(loc),
|| format!("variable <{}> access", name),
|| context.binding(name.clone())?.evaluate(),
)?,
@@ -528,7 +529,7 @@
(Val::Obj(v), Val::Str(s)) => {
let sn = s.clone();
push_frame(
- Some(loc),
+ CallLocation::new(loc),
|| format!("field <{}> access", sn),
|| {
if let Some(v) = v.get(s.clone())? {
@@ -625,7 +626,7 @@
&Val::Obj(evaluate_object(context, t)?),
)?,
Apply(value, args, tailstrict) => {
- evaluate_apply(context, value, args, Some(loc), *tailstrict)?
+ evaluate_apply(context, value, args, CallLocation::new(loc), *tailstrict)?
}
Function(params, body) => {
evaluate_method(context, "anonymous".into(), params.clone(), body.clone())
@@ -640,7 +641,7 @@
evaluate(context, returned)?
}
ErrorStmt(e) => push_frame(
- Some(loc),
+ CallLocation::new(loc),
|| "error statement".to_owned(),
|| throw!(RuntimeError(IStr::try_from(evaluate(context, e)?)?,)),
)?,
@@ -650,7 +651,7 @@
cond_else,
} => {
if push_frame(
- Some(loc),
+ CallLocation::new(loc),
|| "if condition".to_owned(),
|| bool::try_from(evaluate(context.clone(), &cond.0)?),
)? {
@@ -689,7 +690,7 @@
let mut import_location = tmp.to_path_buf();
import_location.pop();
push_frame(
- Some(loc),
+ CallLocation::new(loc),
|| format!("import {:?}", path),
|| with_state(|s| s.import_file(&import_location, path)),
)?
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth1use crate::{2 error::{Error::*, LocError},3 evaluate, evaluate_named,4 gc::TraceBox,5 throw,6 typed::Typed,7 Context, FutureWrapper, GcHashMap, LazyVal, LazyValValue, Result, Val,8};9use gcmodule::Trace;10use jrsonnet_interner::IStr;11pub use jrsonnet_macros::builtin;12use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};13use std::{borrow::Cow, collections::HashMap, convert::TryFrom};1415#[derive(Clone, Copy)]16pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);17impl<'l> CallLocation<'l> {18 pub fn new(loc: &'l ExprLocation) -> Self {19 Self(Some(loc))20 }21}22impl CallLocation<'static> {23 pub fn native() -> Self {24 Self(None)25 }26}2728#[derive(Trace)]29struct EvaluateLazyVal {30 context: Context,31 expr: LocExpr,32}33impl LazyValValue for EvaluateLazyVal {34 fn get(self: Box<Self>) -> Result<Val> {35 evaluate(self.context, &self.expr)36 }37}3839#[derive(Trace)]40struct EvaluateNamedLazyVal {41 future_context: FutureWrapper<Context>,42 name: IStr,43 value: LocExpr,44}45impl LazyValValue for EvaluateNamedLazyVal {46 fn get(self: Box<Self>) -> Result<Val> {47 evaluate_named(self.future_context.unwrap(), &self.value, self.name)48 }49}5051pub trait ArgLike {52 fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<LazyVal>;53}54impl ArgLike for &LocExpr {55 fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<LazyVal> {56 Ok(if tailstrict {57 LazyVal::new_resolved(evaluate(ctx, self)?)58 } else {59 LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {60 context: ctx,61 expr: (*self).clone(),62 })))63 })64 }65}66impl<T> ArgLike for T67where68 T: Typed + Clone,69 Val: TryFrom<T, Error = LocError>,70{71 fn evaluate_arg(&self, _ctx: Context, _tailstrict: bool) -> Result<LazyVal> {72 let val: Val = Val::try_from(self.clone())?;73 Ok(LazyVal::new_resolved(val))74 }75}76pub enum TlaArg {77 String(IStr),78 Code(LocExpr),79 Val(Val),80}81impl ArgLike for TlaArg {82 fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<LazyVal> {83 match self {84 TlaArg::String(s) => Ok(LazyVal::new_resolved(Val::Str(s.clone()))),85 TlaArg::Code(code) => Ok(if tailstrict {86 LazyVal::new_resolved(evaluate(ctx, code)?)87 } else {88 LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {89 context: ctx,90 expr: code.clone(),91 })))92 }),93 TlaArg::Val(val) => Ok(LazyVal::new_resolved(val.clone())),94 }95 }96}9798pub trait ArgsLike {99 fn unnamed_len(&self) -> usize;100 fn unnamed_iter(101 &self,102 ctx: Context,103 tailstrict: bool,104 handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,105 ) -> Result<()>;106 fn named_iter(107 &self,108 ctx: Context,109 tailstrict: bool,110 handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,111 ) -> Result<()>;112 fn named_names(&self, handler: &mut dyn FnMut(&IStr));113}114115impl ArgsLike for ArgsDesc {116 fn unnamed_len(&self) -> usize {117 self.unnamed.len()118 }119120 fn unnamed_iter(121 &self,122 ctx: Context,123 tailstrict: bool,124 handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,125 ) -> Result<()> {126 for (id, arg) in self.unnamed.iter().enumerate() {127 handler(128 id,129 if tailstrict {130 LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)131 } else {132 LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {133 context: ctx.clone(),134 expr: arg.clone(),135 })))136 },137 )?;138 }139 Ok(())140 }141142 fn named_iter(143 &self,144 ctx: Context,145 tailstrict: bool,146 handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,147 ) -> Result<()> {148 for (name, arg) in self.named.iter() {149 handler(150 name,151 if tailstrict {152 LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)153 } else {154 LazyVal::new(TraceBox(Box::new(EvaluateLazyVal {155 context: ctx.clone(),156 expr: arg.clone(),157 })))158 },159 )?;160 }161 Ok(())162 }163164 fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {165 for (name, _) in self.named.iter() {166 handler(name)167 }168 }169}170171impl<A: ArgLike> ArgsLike for [(IStr, A)] {172 fn unnamed_len(&self) -> usize {173 0174 }175176 fn unnamed_iter(177 &self,178 _ctx: Context,179 _tailstrict: bool,180 _handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,181 ) -> Result<()> {182 Ok(())183 }184185 fn named_iter(186 &self,187 ctx: Context,188 tailstrict: bool,189 handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,190 ) -> Result<()> {191 for (name, val) in self.iter() {192 handler(name, val.evaluate_arg(ctx.clone(), tailstrict)?)?;193 }194 Ok(())195 }196197 fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {198 for (name, _) in self.iter() {199 handler(name);200 }201 }202}203204impl<A: ArgLike> ArgsLike for HashMap<IStr, A> {205 fn unnamed_len(&self) -> usize {206 0207 }208209 fn unnamed_iter(210 &self,211 _ctx: Context,212 _tailstrict: bool,213 _handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,214 ) -> Result<()> {215 Ok(())216 }217218 fn named_iter(219 &self,220 ctx: Context,221 tailstrict: bool,222 handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,223 ) -> Result<()> {224 for (name, value) in self.iter() {225 handler(name, value.evaluate_arg(ctx.clone(), tailstrict)?)?;226 }227 Ok(())228 }229230 fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {231 for (name, _) in self.iter() {232 handler(name);233 }234 }235}236237impl<A: ArgLike> ArgsLike for [A] {238 fn unnamed_len(&self) -> usize {239 self.len()240 }241242 fn unnamed_iter(243 &self,244 ctx: Context,245 tailstrict: bool,246 handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,247 ) -> Result<()> {248 for (i, arg) in self.iter().enumerate() {249 handler(i, arg.evaluate_arg(ctx.clone(), tailstrict)?)?;250 }251 Ok(())252 }253254 fn named_iter(255 &self,256 _ctx: Context,257 _tailstrict: bool,258 _handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,259 ) -> Result<()> {260 Ok(())261 }262263 fn named_names(&self, _handler: &mut dyn FnMut(&IStr)) {}264}265impl<A: ArgLike> ArgsLike for &[A] {266 fn unnamed_len(&self) -> usize {267 (*self).unnamed_len()268 }269270 fn unnamed_iter(271 &self,272 ctx: Context,273 tailstrict: bool,274 handler: &mut dyn FnMut(usize, LazyVal) -> Result<()>,275 ) -> Result<()> {276 (*self).unnamed_iter(ctx, tailstrict, handler)277 }278279 fn named_iter(280 &self,281 ctx: Context,282 tailstrict: bool,283 handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,284 ) -> Result<()> {285 (*self).named_iter(ctx, tailstrict, handler)286 }287288 fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {289 (*self).named_names(handler)290 }291}292293/// Creates correct [context](Context) for function body evaluation returning error on invalid call.294///295/// ## Parameters296/// * `ctx`: used for passed argument expressions' execution and for body execution (if `body_ctx` is not set)297/// * `body_ctx`: used for default parameter values' execution and for body execution (if set)298/// * `params`: function parameters' definition299/// * `args`: passed function arguments300/// * `tailstrict`: if set to `true` function arguments are eagerly executed, otherwise - lazily301pub fn parse_function_call(302 ctx: Context,303 body_ctx: Context,304 params: &ParamsDesc,305 args: &dyn ArgsLike,306 tailstrict: bool,307) -> Result<Context> {308 let mut passed_args = GcHashMap::with_capacity(params.len());309 if args.unnamed_len() > params.len() {310 throw!(TooManyArgsFunctionHas(params.len()))311 }312313 let mut filled_args = 0;314315 args.unnamed_iter(ctx.clone(), tailstrict, &mut |id, arg| {316 let name = params[id].0.clone();317 passed_args.insert(name, arg);318 filled_args += 1;319 Ok(())320 })?;321322 args.named_iter(ctx, tailstrict, &mut |name, value| {323 // FIXME: O(n) for arg existence check324 if !params.iter().any(|p| &p.0 == name) {325 throw!(UnknownFunctionParameter((name as &str).to_owned()));326 }327 if passed_args.insert(name.clone(), value).is_some() {328 throw!(BindingParameterASecondTime(name.clone()));329 }330 filled_args += 1;331 Ok(())332 })?;333334 if filled_args < params.len() {335 // Some args are unset, but maybe we have defaults for them336 // Default values should be created in newly created context337 let future_context = Context::new_future();338 let mut defaults = GcHashMap::with_capacity(params.len() - filled_args);339340 for param in params.iter().filter(|p| p.1.is_some()) {341 if passed_args.contains_key(¶m.0.clone()) {342 continue;343 }344 LazyVal::new(TraceBox(Box::new(EvaluateNamedLazyVal {345 future_context: future_context.clone(),346 name: param.0.clone(),347 value: param.1.clone().unwrap(),348 })));349350 defaults.insert(351 param.0.clone(),352 LazyVal::new(TraceBox(Box::new(EvaluateNamedLazyVal {353 future_context: future_context.clone(),354 name: param.0.clone(),355 value: param.1.clone().unwrap(),356 }))),357 );358 filled_args += 1;359 }360361 // Some args still wasn't filled362 if filled_args != params.len() {363 for param in params.iter().skip(args.unnamed_len()) {364 let mut found = false;365 args.named_names(&mut |name| {366 if name == ¶m.0 {367 found = true;368 }369 });370 if !found {371 throw!(FunctionParameterNotBoundInCall(param.0.clone()));372 }373 }374 unreachable!();375 }376377 Ok(body_ctx378 .extend(passed_args, None, None, None)379 .extend_bound(defaults)380 .into_future(future_context))381 } else {382 let body_ctx = body_ctx.extend(passed_args, None, None, None);383 Ok(body_ctx)384 }385}386387type BuiltinParamName = Cow<'static, str>;388389#[derive(Clone, Trace)]390pub struct BuiltinParam {391 pub name: BuiltinParamName,392 pub has_default: bool,393}394395/// Do not implement it directly, instead use #[builtin] macro396pub trait Builtin: Trace {397 fn name(&self) -> &str;398 fn params(&self) -> &[BuiltinParam];399 fn call(&self, context: Context, loc: CallLocation, args: &dyn ArgsLike) -> Result<Val>;400}401402pub trait StaticBuiltin: Builtin + Send + Sync403where404 Self: 'static,405{406 // In impl, to make it object safe:407 // const INST: &'static Self;408}409410/// You shouldn't probally use this function, use jrsonnet_macros::builtin instead411///412/// ## Parameters413/// * `ctx`: used for passed argument expressions' execution and for body execution (if `body_ctx` is not set)414/// * `params`: function parameters' definition415/// * `args`: passed function arguments416/// * `tailstrict`: if set to `true` function arguments are eagerly executed, otherwise - lazily417pub fn parse_builtin_call(418 ctx: Context,419 params: &[BuiltinParam],420 args: &dyn ArgsLike,421 tailstrict: bool,422) -> Result<GcHashMap<BuiltinParamName, LazyVal>> {423 let mut passed_args = GcHashMap::with_capacity(params.len());424 if args.unnamed_len() > params.len() {425 throw!(TooManyArgsFunctionHas(params.len()))426 }427428 let mut filled_args = 0;429430 args.unnamed_iter(ctx.clone(), tailstrict, &mut |id, arg| {431 let name = params[id].name.clone();432 passed_args.insert(name, arg);433 filled_args += 1;434 Ok(())435 })?;436437 args.named_iter(ctx, tailstrict, &mut |name, arg| {438 // FIXME: O(n) for arg existence check439 let p = params440 .iter()441 .find(|p| p.name == name as &str)442 .ok_or_else(|| UnknownFunctionParameter((name as &str).to_owned()))?;443 if passed_args.insert(p.name.clone(), arg).is_some() {444 throw!(BindingParameterASecondTime(name.clone()));445 }446 filled_args += 1;447 Ok(())448 })?;449450 if filled_args < params.len() {451 for param in params.iter().filter(|p| p.has_default) {452 if passed_args.contains_key(¶m.name) {453 continue;454 }455 filled_args += 1;456 }457458 // Some args still wasn't filled459 if filled_args != params.len() {460 for param in params.iter().skip(args.unnamed_len()) {461 let mut found = false;462 args.named_names(&mut |name| {463 if name as &str == ¶m.name as &str {464 found = true;465 }466 });467 if !found {468 throw!(FunctionParameterNotBoundInCall(param.name.clone().into()));469 }470 }471 unreachable!();472 }473 }474 Ok(passed_args)475}476477/// Creates Context, which has all argument default values applied478/// and with unbound values causing error to be returned479pub fn parse_default_function_call(body_ctx: Context, params: &ParamsDesc) -> Context {480 let ctx = Context::new_future();481482 let mut bindings = GcHashMap::new();483484 #[derive(Trace)]485 struct DependsOnUnbound(IStr);486 impl LazyValValue for DependsOnUnbound {487 fn get(self: Box<Self>) -> Result<Val> {488 Err(FunctionParameterNotBoundInCall(self.0.clone()).into())489 }490 }491492 for param in params.iter() {493 if let Some(v) = ¶m.1 {494 bindings.insert(495 param.0.clone(),496 LazyVal::new(TraceBox(Box::new(EvaluateNamedLazyVal {497 future_context: ctx.clone(),498 name: param.0.clone(),499 value: v.clone(),500 }))),501 );502 } else {503 bindings.insert(504 param.0.clone(),505 LazyVal::new(TraceBox(Box::new(DependsOnUnbound(param.0.clone())))),506 );507 }508 }509510 body_ctx.extend(bindings, None, None, None).into_future(ctx)511}crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -28,7 +28,7 @@
pub use dynamic::*;
use error::{Error::*, LocError, Result, StackTraceElement};
pub use evaluate::*;
-use function::{Builtin, TlaArg};
+use function::{Builtin, CallLocation, TlaArg};
use gc::{GcHashMap, TraceBox};
use gcmodule::{Cc, Trace, Weak};
pub use import::*;
@@ -173,6 +173,7 @@
/// Global state is fine here.
pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)
}
+
pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {
EVAL_STATE.with(|s| {
f(s.borrow().as_ref().expect(
@@ -181,7 +182,7 @@
})
}
pub fn push_frame<T>(
- e: Option<&ExprLocation>,
+ e: CallLocation,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<T>,
) -> Result<T> {
@@ -345,7 +346,7 @@
/// Executes code creating a new stack frame
pub fn push<T>(
&self,
- e: Option<&ExprLocation>,
+ e: CallLocation,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<T>,
) -> Result<T> {
@@ -368,7 +369,7 @@
}
if let Err(mut err) = result {
err.trace_mut().0.push(StackTraceElement {
- location: e.cloned(),
+ location: e.0.cloned(),
desc: frame_desc(),
});
return Err(err);
@@ -512,7 +513,7 @@
|| {
func.evaluate(
self.create_default_context(),
- None,
+ CallLocation::native(),
&self.settings().tla_vars,
true,
)
crates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -1,11 +1,10 @@
#![allow(clippy::type_complexity)]
-use crate::function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam};
+use crate::function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam, CallLocation};
use crate::gc::TraceBox;
use crate::Context;
use crate::{error::Result, Val};
use gcmodule::Trace;
-use jrsonnet_parser::ExprLocation;
use std::path::Path;
use std::rc::Rc;
@@ -32,18 +31,13 @@
&self.params
}
- fn call(
- &self,
- context: Context,
- loc: Option<&ExprLocation>,
- args: &dyn ArgsLike,
- ) -> Result<Val> {
+ fn call(&self, context: Context, loc: CallLocation, args: &dyn ArgsLike) -> Result<Val> {
let args = parse_builtin_call(context, &self.params, args, true)?;
let mut out_args = Vec::with_capacity(self.params.len());
for p in self.params.iter() {
out_args.push(args[&p.name].evaluate()?);
}
- self.handler.call(loc.map(|l| l.0.clone()), &out_args)
+ self.handler.call(loc.0.map(|l| l.0.clone()), &out_args)
}
}
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -1,5 +1,6 @@
use std::convert::{TryFrom, TryInto};
+use gcmodule::Cc;
use jrsonnet_interner::IStr;
pub use jrsonnet_macros::Typed;
use jrsonnet_types::{ComplexValType, ValType};
@@ -8,7 +9,7 @@
error::{Error::*, LocError, Result},
throw,
typed::CheckType,
- ArrValue, FuncVal, IndexableVal, ObjValue, ObjValueBuilder, Val,
+ ArrValue, FuncDesc, FuncVal, IndexableVal, ObjValue, ObjValueBuilder, Val,
};
pub trait TypedObj: Typed {
@@ -431,6 +432,30 @@
Ok(Self::Func(value))
}
}
+
+impl Typed for Cc<FuncDesc> {
+ const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);
+}
+impl TryFrom<Val> for Cc<FuncDesc> {
+ type Error = LocError;
+
+ fn try_from(value: Val) -> Result<Self, Self::Error> {
+ <Self as Typed>::TYPE.check(&value)?;
+ match value {
+ Val::Func(FuncVal::Normal(desc)) => Ok(desc.clone()),
+ Val::Func(_) => throw!(RuntimeError("expected normal function, not builtin".into())),
+ _ => unreachable!(),
+ }
+ }
+}
+impl TryFrom<Cc<FuncDesc>> for Val {
+ type Error = LocError;
+
+ fn try_from(value: Cc<FuncDesc>) -> Result<Self, Self::Error> {
+ Ok(Self::Func(FuncVal::Normal(value)))
+ }
+}
+
impl Typed for ObjValue {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -6,14 +6,15 @@
error::{Error::*, LocError},
evaluate,
function::{
- parse_default_function_call, parse_function_call, ArgsLike, Builtin, StaticBuiltin,
+ parse_default_function_call, parse_function_call, ArgsLike, Builtin, CallLocation,
+ StaticBuiltin,
},
gc::TraceBox,
throw, Context, ObjValue, Result,
};
use gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ExprLocation, LocExpr, ParamsDesc};
+use jrsonnet_parser::{LocExpr, ParamsDesc};
use jrsonnet_types::ValType;
use std::{cell::RefCell, fmt::Debug, rc::Rc};
@@ -152,7 +153,7 @@
pub fn evaluate(
&self,
call_ctx: Context,
- loc: Option<&ExprLocation>,
+ loc: CallLocation,
args: &dyn ArgsLike,
tailstrict: bool,
) -> Result<Val> {
@@ -166,7 +167,7 @@
}
}
pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {
- self.evaluate(Context::default(), None, args, true)
+ self.evaluate(Context::default(), CallLocation::native(), args, true)
}
}
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -1,5 +1,5 @@
use proc_macro2::TokenStream;
-use quote::{quote, quote_spanned};
+use quote::quote;
use syn::{
parenthesized,
parse::{Parse, ParseStream},
@@ -7,8 +7,8 @@
punctuated::Punctuated,
spanned::Spanned,
token::Comma,
- Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, PatType,
- Path, PathArguments, Result, Token, Type,
+ Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, Path,
+ PathArguments, Result, ReturnType, Token, Type,
};
fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>
@@ -33,49 +33,42 @@
Ok(Some(attr))
}
-fn is_location_arg(t: &PatType) -> bool {
- t.attrs.iter().any(|a| a.path.is_ident("location"))
-}
-fn is_self_arg(t: &PatType) -> bool {
- t.attrs.iter().any(|a| a.path.is_ident("self"))
+fn path_is(path: &Path, needed: &str) -> bool {
+ path.leading_colon.is_none()
+ && path.segments.len() >= 1
+ && path.segments.iter().last().unwrap().ident == needed
}
-trait RetainHad<T> {
- fn retain_had(&mut self, h: impl FnMut(&T) -> bool) -> bool;
-}
-impl<T> RetainHad<T> for Vec<T> {
- fn retain_had(&mut self, h: impl FnMut(&T) -> bool) -> bool {
- let before = self.len();
- self.retain(h);
- let after = self.len();
- before != after
+fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {
+ match ty {
+ Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {
+ let args = &path.path.segments.iter().last().unwrap().arguments;
+ Some(args)
+ }
+ _ => None,
}
}
-fn extract_type_from_option(ty: &Type) -> Option<&Type> {
- fn path_is_option(path: &Path) -> bool {
- path.leading_colon.is_none()
- && path.segments.len() == 1
- && path.segments.iter().next().unwrap().ident == "Option"
- }
-
- match ty {
- Type::Path(typepath) if typepath.qself.is_none() && path_is_option(&typepath.path) => {
- // Get the first segment of the path (there is only one, in fact: "Option"):
- let type_params = &typepath.path.segments.iter().next().unwrap().arguments;
- // It should have only on angle-bracketed param ("<String>"):
- let generic_arg = match type_params {
- PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),
- _ => panic!("missing option generic"),
- };
- // This argument must be a type:
- match generic_arg {
- GenericArgument::Type(ty) => Some(ty),
- _ => panic!("option generic should be a type"),
+fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {
+ Ok(if let Some(args) = type_is_path(ty, "Option") {
+ // It should have only on angle-bracketed param ("<String>"):
+ let generic_arg = match args {
+ PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),
+ _ => return Err(Error::new(args.span(), "missing option generic")),
+ };
+ // This argument must be a type:
+ match generic_arg {
+ GenericArgument::Type(ty) => Some(ty),
+ _ => {
+ return Err(Error::new(
+ generic_arg.span(),
+ "option generic should be a type",
+ ))
}
}
- _ => None,
- }
+ } else {
+ None
+ })
}
struct Field {
@@ -101,7 +94,7 @@
struct EmptyAttr;
impl Parse for EmptyAttr {
- fn parse(input: ParseStream) -> Result<Self> {
+ fn parse(_input: ParseStream) -> Result<Self> {
Ok(Self)
}
}
@@ -124,107 +117,158 @@
}
}
+enum ArgInfo {
+ Normal {
+ ty: Type,
+ is_option: bool,
+ name: String,
+ // ident: Ident,
+ },
+ Lazy {
+ is_option: bool,
+ name: String,
+ },
+ Location,
+ This,
+}
+
+impl ArgInfo {
+ fn parse(arg: &FnArg) -> Result<Self> {
+ let typed = match arg {
+ FnArg::Receiver(_) => unreachable!(),
+ FnArg::Typed(a) => a,
+ };
+ let ident = match &typed.pat as &Pat {
+ Pat::Ident(i) => i.ident.clone(),
+ _ => {
+ return Err(Error::new(
+ typed.pat.span(),
+ "arg should be plain identifier",
+ ))
+ }
+ };
+ let ty = &typed.ty as &Type;
+ if type_is_path(&ty, "CallLocation").is_some() {
+ return Ok(Self::Location);
+ } else if type_is_path(&ty, "Self").is_some() {
+ return Ok(Self::This);
+ } else if type_is_path(&ty, "LazyVal").is_some() {
+ return Ok(Self::Lazy {
+ is_option: false,
+ name: ident.to_string(),
+ });
+ }
+
+ let (is_option, ty) = if let Some(ty) = extract_type_from_option(&ty)? {
+ if type_is_path(&ty, "LazyVal").is_some() {
+ return Ok(Self::Lazy {
+ is_option: true,
+ name: ident.to_string(),
+ });
+ }
+
+ (true, ty.clone())
+ } else {
+ (false, ty.clone())
+ };
+
+ Ok(Self::Normal {
+ ty,
+ is_option,
+ name: ident.to_string(),
+ // ident,
+ })
+ }
+}
+
#[proc_macro_attribute]
pub fn builtin(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
- let attrs = parse_macro_input!(attr as BuiltinAttrs);
- let mut fun: ItemFn = parse_macro_input!(item);
+ let attr = parse_macro_input!(attr as BuiltinAttrs);
+ let item: ItemFn = parse_macro_input!(item);
+
+ match builtin_inner(attr, item) {
+ Ok(v) => v.into(),
+ Err(e) => e.into_compile_error().into(),
+ }
+}
+fn builtin_inner(attr: BuiltinAttrs, fun: ItemFn) -> syn::Result<TokenStream> {
let result = match fun.sig.output {
- syn::ReturnType::Default => {
- return quote_spanned! { fun.sig.span() =>
- compile_error!("builtins should return something");
- }
- .into()
+ ReturnType::Default => {
+ return Err(Error::new(
+ fun.sig.span(),
+ "builtin should return something",
+ ))
}
- syn::ReturnType::Type(_, ref ty) => ty.clone(),
+ ReturnType::Type(_, ref ty) => ty.clone(),
};
- let params = fun
+ let args = fun
.sig
.inputs
.iter()
- .map(|i| match i {
- FnArg::Receiver(_) => unreachable!(),
- FnArg::Typed(t) => t,
- })
- .filter(|a| !is_location_arg(a) && !is_self_arg(a))
- .map(|t| {
- let ident = match &t.pat as &Pat {
- Pat::Ident(i) => i.ident.to_string(),
- _ => {
- return quote_spanned! { t.pat.span() =>
- compile_error!("args should be plain identifiers")
- }
- .into()
- }
- };
- let optional = extract_type_from_option(&t.ty).is_some();
- quote! {
- BuiltinParam {
- name: std::borrow::Cow::Borrowed(#ident),
- has_default: #optional,
- }
+ .map(|a| ArgInfo::parse(a))
+ .collect::<Result<Vec<_>>>()?;
+
+ let params_desc = args.iter().flat_map(|a| match a {
+ ArgInfo::Normal {
+ is_option, name, ..
+ }
+ | ArgInfo::Lazy { is_option, name } => Some(quote! {
+ BuiltinParam {
+ name: std::borrow::Cow::Borrowed(#name),
+ has_default: #is_option,
}
- })
- .collect::<Vec<_>>();
+ }),
+ ArgInfo::Location => None,
+ ArgInfo::This => None,
+ });
- let args = fun
- .sig
- .inputs
- .iter_mut()
- .map(|i| match i {
- FnArg::Receiver(_) => unreachable!(),
- FnArg::Typed(t) => t,
- })
- .map(|t| {
- if t.attrs.retain_had(|a| !a.path.is_ident("location")) {
- quote! {{
- loc
+ let pass = args.iter().map(|a| match a {
+ ArgInfo::Normal {
+ ty,
+ is_option,
+ name,
+ // ident,
+ } => {
+ let eval = quote! {::jrsonnet_evaluator::push_description_frame(
+ || format!("argument <{}> evaluation", #name),
+ || <#ty>::try_from(value.evaluate()?),
+ )?};
+ if *is_option {
+ quote! {if let Some(value) = parsed.get(#name) {
+ Some(#eval)
+ } else {
+ None
}}
- } else if t.attrs.retain_had(|a| !a.path.is_ident("self")) {
+ } else {
quote! {{
- self
+ let value = parsed.get(#name).expect("args shape is checked");
+ #eval
}}
- } else {
- let ident = match &t.pat as &Pat {
- Pat::Ident(i) => i.ident.to_string(),
- _ => {
- return quote_spanned! { t.pat.span() =>
- compile_error!("args should be plain identifiers")
- }
- .into()
- }
- };
- let ty = &t.ty;
- if let Some(opt_ty) = extract_type_from_option(&t.ty) {
- quote! {{
- if let Some(value) = parsed.get(#ident) {
- Some(::jrsonnet_evaluator::push_description_frame(
- || format!("argument <{}> evaluation", #ident),
- || <#opt_ty>::try_from(value.evaluate()?),
- )?)
- } else {
- None
- }
- }}
+ }
+ }
+ ArgInfo::Lazy { is_option, name } => {
+ if *is_option {
+ quote! {if let Some(value) = parsed.get(#name) {
+ Some(value.clone())
} else {
- quote! {{
- let value = parsed.get(#ident).unwrap();
-
- ::jrsonnet_evaluator::push_description_frame(
- || format!("argument <{}> evaluation", #ident),
- || <#ty>::try_from(value.evaluate()?),
- )?
- }}
+ None
+ }}
+ } else {
+ quote! {
+ parsed.get(#name).expect("args shape is correct").clone()
}
}
- })
- .collect::<Vec<_>>();
+ }
+ ArgInfo::Location => quote! {location},
+ ArgInfo::This => quote! {self},
+ });
- let fields = attrs.fields.iter().map(|field| {
+ let fields = attr.fields.iter().map(|field| {
let name = &field.name;
let ty = &field.ty;
quote! {
@@ -234,7 +278,7 @@
let name = &fun.sig.ident;
let vis = &fun.vis;
- let static_ext = if attrs.fields.is_empty() {
+ let static_ext = if attr.fields.is_empty() {
quote! {
impl #name {
pub const INST: &'static dyn StaticBuiltin = &#name {};
@@ -244,13 +288,13 @@
} else {
quote! {}
};
- let static_derive_copy = if attrs.fields.is_empty() {
+ let static_derive_copy = if attr.fields.is_empty() {
quote! {, Copy}
} else {
quote! {}
};
- (quote! {
+ Ok(quote! {
#fun
#[doc(hidden)]
#[allow(non_camel_case_types)]
@@ -260,12 +304,12 @@
}
const _: () = {
use ::jrsonnet_evaluator::{
- function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike, parse_builtin_call},
+ function::{Builtin, CallLocation, StaticBuiltin, BuiltinParam, ArgsLike, parse_builtin_call},
error::Result, Context,
parser::ExprLocation,
};
const PARAMS: &'static [BuiltinParam] = &[
- #(#params),*
+ #(#params_desc),*
];
#static_ext
@@ -279,17 +323,16 @@
fn params(&self) -> &[BuiltinParam] {
PARAMS
}
- fn call(&self, context: Context, loc: Option<&ExprLocation>, args: &dyn ArgsLike) -> Result<Val> {
+ fn call(&self, context: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {
let parsed = parse_builtin_call(context, &PARAMS, args, false)?;
- let result: #result = #name(#(#args),*);
+ let result: #result = #name(#(#pass),*);
let result = result?;
result.try_into()
}
}
};
})
- .into()
}
#[derive(Default)]
@@ -366,15 +409,6 @@
)
}
- fn expand_shallow_field(&self) -> Option<TokenStream> {
- if self.is_option() {
- return None;
- }
- let name = self.name()?;
- Some(quote! {
- (#name, ComplexValType::Any)
- })
- }
fn expand_field(&self) -> Option<TokenStream> {
if self.is_option() {
return None;
@@ -450,7 +484,7 @@
}
fn as_option(&self) -> Option<&Type> {
- extract_type_from_option(&self.0.ty)
+ extract_type_from_option(&self.0.ty).unwrap()
}
fn is_option(&self) -> bool {
self.as_option().is_some()
@@ -460,25 +494,25 @@
#[proc_macro_derive(Typed, attributes(typed))]
pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(item as DeriveInput);
+
+ match derive_typed_inner(input) {
+ Ok(v) => v.into(),
+ Err(e) => e.to_compile_error().into(),
+ }
+}
+
+fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {
let data = match &input.data {
syn::Data::Struct(s) => s,
- _ => {
- return syn::Error::new(input.span(), "only structs supported")
- .to_compile_error()
- .into()
- }
+ _ => return Err(Error::new(input.span(), "only structs supported")),
};
let ident = &input.ident;
- let fields = match data
+ let fields = data
.fields
.iter()
.map(TypedField::try_new)
- .collect::<Result<Vec<_>>>()
- {
- Ok(v) => v,
- Err(e) => return e.to_compile_error().into(),
- };
+ .collect::<Result<Vec<_>>>()?;
let typed = {
let fields = fields
@@ -499,7 +533,7 @@
let fields_parse = fields.iter().map(TypedField::expand_parse);
let fields_serialize = fields.iter().map(TypedField::expand_serialize);
- quote! {
+ Ok(quote! {
const _: () = {
use ::jrsonnet_evaluator::{
typed::{ComplexValType, Typed, TypedObj, CheckType},
@@ -540,6 +574,5 @@
}
()
};
- }
- .into()
+ })
}