git.delta.rocks / jrsonnet / refs/commits / 562e7b71e322

difftreelog

refactor remove manual ThunkValue implementations

Yaroslav Bolyukin2024-08-30parent: #7cd3ab4.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth
3use jrsonnet_interner::IStr;3use jrsonnet_interner::IStr;
4use jrsonnet_parser::{ArgsDesc, LocExpr};4use jrsonnet_parser::{ArgsDesc, LocExpr};
55
6use crate::{evaluate, gc::GcHashMap, typed::Typed, val::ThunkValue, Context, Result, Thunk, Val};6use crate::{evaluate, gc::GcHashMap, typed::Typed, Context, Result, Thunk, Val};
77
8/// Marker for arguments, which can be evaluated with context set to None8/// Marker for arguments, which can be evaluated with context set to None
9pub trait OptionalContext {}9pub trait OptionalContext {}
10
11#[derive(Trace)]
12struct EvaluateThunk {
13 ctx: Context,
14 expr: LocExpr,
15}
16impl ThunkValue for EvaluateThunk {
17 type Output = Val;
18 fn get(self: Box<Self>) -> Result<Val> {
19 evaluate(self.ctx, &self.expr)
20 }
21}
2210
23pub trait ArgLike {11pub trait ArgLike {
24 fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>>;12 fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>>;
29 Ok(if tailstrict {17 Ok(if tailstrict {
30 Thunk::evaluated(evaluate(ctx, self)?)18 Thunk::evaluated(evaluate(ctx, self)?)
31 } else {19 } else {
32 Thunk::new(EvaluateThunk {
33 ctx,
34 expr: (*self).clone(),20 let expr = (*self).clone();
35 })21 Thunk!(move || evaluate(ctx, &expr))
36 })22 })
37 }23 }
38}24}
65 Self::Code(code) => Ok(if tailstrict {51 Self::Code(code) => Ok(if tailstrict {
66 Thunk::evaluated(evaluate(ctx, code)?)52 Thunk::evaluated(evaluate(ctx, code)?)
67 } else {53 } else {
68 Thunk::new(EvaluateThunk {
69 ctx,
70 expr: code.clone(),54 let code = code.clone();
71 })55 Thunk!(move || evaluate(ctx, &code))
72 }),56 }),
73 Self::Val(val) => Ok(Thunk::evaluated(val.clone())),57 Self::Val(val) => Ok(Thunk::evaluated(val.clone())),
74 Self::Lazy(lazy) => Ok(lazy.clone()),58 Self::Lazy(lazy) => Ok(lazy.clone()),
140 if tailstrict {124 if tailstrict {
141 Thunk::evaluated(evaluate(ctx.clone(), arg)?)125 Thunk::evaluated(evaluate(ctx.clone(), arg)?)
142 } else {126 } else {
143 Thunk::new(EvaluateThunk {
144 ctx: ctx.clone(),127 let ctx = ctx.clone();
145 expr: arg.clone(),128 let arg = arg.clone();
146 })129
130 Thunk!(move || evaluate(ctx, &arg))
147 },131 },
148 )?;132 )?;
149 }133 }
162 if tailstrict {146 if tailstrict {
163 Thunk::evaluated(evaluate(ctx.clone(), arg)?)147 Thunk::evaluated(evaluate(ctx.clone(), arg)?)
164 } else {148 } else {
165 Thunk::new(EvaluateThunk {
166 ctx: ctx.clone(),149 let ctx = ctx.clone();
167 expr: arg.clone(),150 let arg = arg.clone();
168 })151
152 Thunk!(move || evaluate(ctx, &arg))
169 },153 },
170 )?;154 )?;
171 }155 }
modifiedcrates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -12,24 +12,9 @@
 	evaluate_named,
 	function::builtin::ParamDefault,
 	gc::GcHashMap,
-	val::ThunkValue,
 	Context, Pending, Thunk, Val,
 };
-
-#[derive(Trace)]
-struct EvaluateNamedThunk {
-	ctx: Pending<Context>,
-	name: IStr,
-	value: LocExpr,
-}
 
-impl ThunkValue for EvaluateNamedThunk {
-	type Output = Val;
-	fn get(self: Box<Self>) -> Result<Val> {
-		evaluate_named(self.ctx.unwrap(), &self.value, self.name)
-	}
-}
-
 /// Creates correct [context](Context) for function body evaluation returning error on invalid call.
 ///
 /// ## Parameters
@@ -105,11 +90,12 @@
 
 			destruct(
 				&param.0,
-				Thunk::new(EvaluateNamedThunk {
-					ctx: fctx.clone(),
-					name: param.0.name().unwrap_or_else(|| "<destruct>".into()),
-					value: param.1.clone().expect("default exists"),
-				}),
+				{
+					let ctx = fctx.clone();
+					let name = param.0.name().unwrap_or_else(|| "<destruct>".into());
+					let value = param.1.clone().expect("default exists");
+					Thunk!(move || evaluate_named(ctx.unwrap(), &value, name))
+				},
 				fctx.clone(),
 				&mut defaults,
 			)?;
@@ -241,11 +227,12 @@
 		if let Some(v) = &param.1 {
 			destruct(
 				&param.0.clone(),
-				Thunk::new(EvaluateNamedThunk {
-					ctx: fctx.clone(),
-					name: param.0.name().unwrap_or_else(|| "<destruct>".into()),
-					value: v.clone(),
-				}),
+				{
+					let ctx = fctx.clone();
+					let name = param.0.name().unwrap_or_else(|| "<destruct>".into());
+					let value = v.clone();
+					Thunk!(move || evaluate_named(ctx.unwrap(), &value, name))
+				},
 				fctx.clone(),
 				&mut bindings,
 			)?;
modifiednix/benchmarks.nixdiffbeforeafterboth
--- a/nix/benchmarks.nix
+++ b/nix/benchmarks.nix
@@ -52,6 +52,7 @@
       mkdir -p $out
       cp -r ${src}/* $out/
       cd $out
+      chmod u+w jsonnetfile.lock.json
       mkdir vendor
       ${jsonnet-bundler}/bin/jb install
     '';
@@ -125,7 +126,7 @@
           go-jsonnet $path > generated.jsonnet
           path=generated.jsonnet
         ''}
-        hyperfine -N -w4 -m20 --output=pipe --style=basic --export-markdown result.md \
+        hyperfine -N -w4 -m20 --output=pipe --style=basic --export-asciidoc result.adoc \
           ${concatStringsSep " " (forEach jrsonnetVariants (
           variant: "\"${variant.drv}/bin/jrsonnet $path${optionalString (vendor != "") " -J${vendor}"}\" -n \"Rust${
             if variant.name != ""
@@ -137,7 +138,7 @@
           ${optionalString (skipGo == "") "\"go-jsonnet $path${optionalString (vendor != "") " -J ${vendor}"}\" -n \"Go\""} \
           ${optionalString (skipScala == "") "\"sjsonnet $path${optionalString (vendor != "") " -J ${vendor}"}\" -n \"Scala\""} \
           ${optionalString (skipCpp == "") "\"jsonnet $path${optionalString (vendor != "") " -J ${vendor}"}\" -n \"C++\""}
-        cat result.md >> $out
+        cat result.adoc >> $out
       '';
     in ''
       set -oux