git.delta.rocks / jrsonnet / refs/commits / 54c4db58ad3d

difftreelog

fix makeArray should be lazy

Yaroslav Bolyukin2022-12-03parent: #2afd5ff.patch.diff
in: master

7 files changed

modifiedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -32,6 +32,8 @@
 	Reverse(Box<ReverseArray>),
 	/// Returned by `std.map` call
 	Mapped(MappedArray),
+	/// Returned by `std.repeat` call
+	Repeated(RepeatedArray),
 }
 
 impl ArrValue {
@@ -51,6 +53,10 @@
 		Self::Eager(EagerArray(values))
 	}
 
+	pub fn repeated(data: ArrValue, repeats: usize) -> Option<Self> {
+		Some(Self::Repeated(RepeatedArray::new(data, repeats)?))
+	}
+
 	pub fn bytes(bytes: IBytes) -> Self {
 		Self::Bytes(BytesArray(bytes))
 	}
@@ -76,7 +82,11 @@
 		// TODO: benchmark for an optimal value, currently just a arbitrary choice
 		const ARR_EXTEND_THRESHOLD: usize = 100;
 
-		if a.len() + b.len() > ARR_EXTEND_THRESHOLD {
+		if a.is_empty() {
+			b
+		} else if b.is_empty() {
+			a
+		} else if a.len() + b.len() > ARR_EXTEND_THRESHOLD {
 			Self::Extended(Cc::new(ExtendedArray::new(a, b)))
 		} else if let (Some(a), Some(b)) = (a.iter_cheap(), b.iter_cheap()) {
 			let mut out = Vec::with_capacity(a.len() + b.len());
@@ -189,10 +199,8 @@
 			(ArrValue::Lazy(a), ArrValue::Lazy(b)) => Cc::ptr_eq(&a.0, &b.0),
 			(ArrValue::Expr(a), ArrValue::Expr(b)) => Cc::ptr_eq(&a.0, &b.0),
 			(ArrValue::Eager(a), ArrValue::Eager(b)) => Cc::ptr_eq(&a.0, &b.0),
-			(ArrValue::Extended(a), ArrValue::Extended(b)) => Cc::ptr_eq(&a, &b),
+			(ArrValue::Extended(a), ArrValue::Extended(b)) => Cc::ptr_eq(a, b),
 			(ArrValue::Range(a), ArrValue::Range(b)) => a == b,
-			(ArrValue::Slice(_), ArrValue::Slice(_)) => false,
-			(ArrValue::Reverse(_), ArrValue::Reverse(_)) => false,
 			_ => false,
 		}
 	}
@@ -203,6 +211,7 @@
 			ArrValue::Extended(v) => v.a.is_cheap() && v.b.is_cheap(),
 			ArrValue::Slice(r) => r.inner.is_cheap(),
 			ArrValue::Reverse(i) => i.0.is_cheap(),
+			ArrValue::Repeated(v) => v.is_cheap(),
 			ArrValue::Expr(_) | ArrValue::Lazy(_) | ArrValue::Mapped(_) => false,
 		}
 	}
modifiedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -740,6 +740,96 @@
 }
 // impl MappedArray
 
+#[derive(Trace, Debug)]
+pub struct RepeatedArrayInner {
+	data: ArrValue,
+	repeats: usize,
+	total_len: usize,
+}
+#[derive(Trace, Debug, Clone)]
+pub struct RepeatedArray(Cc<RepeatedArrayInner>);
+impl RepeatedArray {
+	pub fn new(data: ArrValue, repeats: usize) -> Option<Self> {
+		let total_len = data.len().checked_mul(repeats)?;
+		Some(Self(Cc::new(RepeatedArrayInner {
+			data,
+			repeats,
+			total_len,
+		})))
+	}
+	pub fn is_cheap(&self) -> bool {
+		self.0.data.is_cheap()
+	}
+}
+
+type RepeatedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+type RepeatedArrayLazyIter<'t> =
+	impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+type RepeatedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
+impl ArrayLike for RepeatedArray {
+	type Iter<'t> = RepeatedArrayIter<'t>;
+	type IterLazy<'t> = RepeatedArrayLazyIter<'t>;
+	type IterCheap<'t> = RepeatedArrayCheapIter<'t>;
+
+	fn len(&self) -> usize {
+		self.0.total_len
+	}
+
+	fn get(&self, index: usize) -> Result<Option<Val>> {
+		if index > self.0.total_len {
+			return Ok(None);
+		}
+		self.0.data.get(index % self.0.data.len())
+	}
+
+	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
+		if index > self.0.total_len {
+			return None;
+		}
+		self.0.data.get_lazy(index % self.0.data.len())
+	}
+
+	fn get_cheap(&self, index: usize) -> Option<Val> {
+		if index > self.0.total_len {
+			return None;
+		}
+		self.0.data.get_cheap(index % self.0.data.len())
+	}
+
+	fn evaluated(&self) -> Result<Vec<Val>> {
+		let mut data = self.0.data.evaluated()?;
+		let data_range = 0..data.len();
+		for _ in 1..self.0.repeats {
+			data.extend_from_within(data_range.clone());
+		}
+		Ok(data)
+	}
+
+	fn iter(&self) -> RepeatedArrayIter<'_> {
+		(0..self.0.total_len)
+			.map(|i| self.get(i))
+			.map(Result::transpose)
+			.map(Option::unwrap)
+	}
+
+	fn iter_lazy(&self) -> RepeatedArrayLazyIter<'_> {
+		(0..self.0.total_len)
+			.map(|i| self.get_lazy(i))
+			.map(Option::unwrap)
+	}
+
+	fn iter_cheap(&self) -> Option<RepeatedArrayCheapIter<'_>> {
+		if !self.0.data.is_cheap() {
+			return None;
+		}
+		Some(
+			(0..self.0.total_len)
+				.map(|i| self.get_cheap(i))
+				.map(Option::unwrap),
+		)
+	}
+}
+
 macro_rules! impl_iter_enum {
 	($n:ident => $v:ident) => {
 		pub enum $n<'t> {
@@ -752,6 +842,7 @@
 			Extended(Box<<ExtendedArray as ArrayLike>::$v<'t>>),
 			Reverse(Box<<ReverseArray as ArrayLike>::$v<'t>>),
 			Mapped(Box<<MappedArray as ArrayLike>::$v<'t>>),
+			Repeated(Box<<RepeatedArray as ArrayLike>::$v<'t>>),
 		}
 	};
 }
@@ -768,6 +859,7 @@
 			Self::Extended(e) => e.$m($($ident)*),
 			Self::Reverse(e) => e.$m($($ident)*),
 			Self::Mapped(e) => e.$m($($ident)*),
+			Self::Repeated(e) => e.$m($($ident)*),
 		}
 	};
 }
@@ -785,6 +877,7 @@
 			ArrValue::Extended(e) => $e::Extended(Box::new($($wrap!)?(e.$c()))),
 			ArrValue::Reverse(e) => $e::Reverse(Box::new($($wrap!)?(e.$c()))),
 			ArrValue::Mapped(e) => $e::Mapped(Box::new($($wrap!)?(e.$c()))),
+			ArrValue::Repeated(e) => $e::Repeated(Box::new($($wrap!)?(e.$c()))),
 		}
 	};
 }
@@ -827,6 +920,7 @@
 					}
 					Self::Reverse(e) => e.len(),
 					Self::Mapped(e) => e.len(),
+					Self::Repeated(e) => e.len(),
 				}
 			}
 		}
modifiedcrates/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,9 @@
 	native::NativeDesc,
 	parse::{parse_default_function_call, parse_function_call},
 };
-use crate::{evaluate, gc::TraceBox, typed::Any, Context, ContextBuilder, Result, Val};
+use crate::{
+	evaluate, evaluate_trivial, gc::TraceBox, typed::Any, Context, ContextBuilder, Result, Val,
+};
 
 pub mod arglike;
 pub mod builtin;
@@ -80,6 +82,10 @@
 	) -> Result<Context> {
 		parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)
 	}
+
+	pub fn evaluate_trivial(&self) -> Option<Val> {
+		evaluate_trivial(&self.body)
+	}
 }
 
 /// Represents a Jsonnet function value, including plain functions and user-provided builtins.
@@ -201,4 +207,11 @@
 	pub const fn identity() -> Self {
 		Self::Id
 	}
+
+	pub fn evaluate_trivial(&self) -> Option<Val> {
+		match self {
+			FuncVal::Normal(n) => n.evaluate_trivial(),
+			_ => None,
+		}
+	}
 }
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -1,23 +1,41 @@
 use jrsonnet_evaluator::{
-	error::Result,
+	error::{ErrorKind::RuntimeError, Result},
 	function::{builtin, FuncVal},
 	throw,
-	typed::{Any, BoundedUsize, Either2, NativeFn, Typed, VecVal},
-	val::{equals, ArrValue, IndexableVal},
+	typed::{Any, BoundedI32, BoundedUsize, Either2, NativeFn, Typed},
+	val::{equals, ArrValue, IndexableVal, StrValue},
 	Either, IStr, Val,
 };
 use jrsonnet_gcmodule::Cc;
 
 #[builtin]
-pub fn builtin_make_array(sz: usize, func: NativeFn<((f64,), Any)>) -> Result<VecVal> {
-	let mut out = Vec::with_capacity(sz);
-	for i in 0..sz {
-		out.push(func(i as f64)?.0);
+pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {
+	if *sz == 0 {
+		return Ok(ArrValue::empty());
+	}
+	if let Some(trivial) = func.evaluate_trivial() {
+		let mut out = Vec::with_capacity(*sz as usize);
+		for _ in 0..*sz {
+			out.push(trivial.clone())
+		}
+		Ok(ArrValue::eager(Cc::new(out)))
+	} else {
+		Ok(ArrValue::range_exclusive(0, *sz).map(func))
 	}
-	Ok(VecVal(Cc::new(out)))
 }
 
 #[builtin]
+pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Any> {
+	Ok(Any(match what {
+		Either2::A(s) => Val::Str(StrValue::Flat(s.repeat(count).into())),
+		Either2::B(arr) => Val::Arr(
+			ArrValue::repeated(arr, count)
+				.ok_or_else(|| RuntimeError("repeated length overflow".into()))?,
+		),
+	}))
+}
+
+#[builtin]
 pub fn builtin_slice(
 	indexable: IndexableVal,
 	index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -63,6 +63,7 @@
 		("isFunction", builtin_is_function::INST),
 		// Arrays
 		("makeArray", builtin_make_array::INST),
+		("repeat", builtin_repeat::INST),
 		("slice", builtin_slice::INST),
 		("map", builtin_map::INST),
 		("flatMap", builtin_flatmap::INST),
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,3  local id = std.id,45  thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',67  toString(a):: '' + a,89  lstripChars(str, chars)::10    if std.length(str) > 0 && std.member(chars, str[0]) then11      std.lstripChars(str[1:], chars)12    else13      str,1415  rstripChars(str, chars)::16    local len = std.length(str);17    if len > 0 && std.member(chars, str[len - 1]) then18      std.rstripChars(str[:len - 1], chars)19    else20      str,2122  stripChars(str, chars)::23    std.lstripChars(std.rstripChars(str, chars), chars),2425  stringChars(str)::26    std.makeArray(std.length(str), function(i) str[i]),2728  split(str, c):: std.splitLimit(str, c, -1),2930  repeat(what, count)::31    local joiner =32      if std.isString(what) then ''33      else if std.isArray(what) then []34      else error 'std.repeat first argument must be an array or a string';35    std.join(joiner, std.makeArray(count, function(i) what)),3637  mapWithIndex(func, arr)::38    if !std.isFunction(func) then39      error ('std.mapWithIndex first param must be function, got ' + std.type(func))40    else if !std.isArray(arr) && !std.isString(arr) then41      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))42    else43      std.makeArray(std.length(arr), function(i) func(i, arr[i])),4445  mapWithKey(func, obj)::46    if !std.isFunction(func) then47      error ('std.mapWithKey first param must be function, got ' + std.type(func))48    else if !std.isObject(obj) then49      error ('std.mapWithKey second param must be object, got ' + std.type(obj))50    else51      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },5253  lines(arr)::54    std.join('\n', arr + ['']),5556  deepJoin(arr)::57    if std.isString(arr) then58      arr59    else if std.isArray(arr) then60      std.join('', [std.deepJoin(x) for x in arr])61    else62      error 'Expected string or array, got %s' % std.type(arr),6364  filterMap(filter_func, map_func, arr)::65    if !std.isFunction(filter_func) then66      error ('std.filterMap first param must be function, got ' + std.type(filter_func))67    else if !std.isFunction(map_func) then68      error ('std.filterMap second param must be function, got ' + std.type(map_func))69    else if !std.isArray(arr) then70      error ('std.filterMap third param must be array, got ' + std.type(arr))71    else72      std.map(map_func, std.filter(filter_func, arr)),7374  assertEqual(a, b)::75    if a == b then76      true77    else78      error 'Assertion failed. ' + a + ' != ' + b,7980  clamp(x, minVal, maxVal)::81    if x < minVal then minVal82    else if x > maxVal then maxVal83    else x,8485  flattenArrays(arrs)::86    std.foldl(function(a, b) a + b, arrs, []),8788  manifestIni(ini)::89    local body_lines(body) =90      std.join([], [91        local value_or_values = body[k];92        if std.isArray(value_or_values) then93          ['%s = %s' % [k, value] for value in value_or_values]94        else95          ['%s = %s' % [k, value_or_values]]9697        for k in std.objectFields(body)98      ]);99100    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),101          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],102          all_sections = [103      section_lines(k, ini.sections[k])104      for k in std.objectFields(ini.sections)105    ];106    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),107108  manifestToml(value):: std.manifestTomlEx(value, '  '),109110  manifestTomlEx(value, indent)::111    local112      escapeStringToml = std.escapeStringJson,113      escapeKeyToml(key) =114        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));115        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),116      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),117      isSection(v) = std.isObject(v) || isTableArray(v),118      renderValue(v, indexedPath, inline, cindent) =119        if v == true then120          'true'121        else if v == false then122          'false'123        else if v == null then124          error 'Tried to manifest "null" at ' + indexedPath125        else if std.isNumber(v) then126          '' + v127        else if std.isString(v) then128          escapeStringToml(v)129        else if std.isFunction(v) then130          error 'Tried to manifest function at ' + indexedPath131        else if std.isArray(v) then132          if std.length(v) == 0 then133            '[]'134          else135            local range = std.range(0, std.length(v) - 1);136            local new_indent = if inline then '' else cindent + indent;137            local separator = if inline then ' ' else '\n';138            local lines = ['[' + separator]139                          + std.join([',' + separator],140                                     [141                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]142                                       for i in range143                                     ])144                          + [separator + (if inline then '' else cindent) + ']'];145            std.join('', lines)146        else if std.isObject(v) then147          local lines = ['{ ']148                        + std.join([', '],149                                   [150                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]151                                     for k in std.objectFields(v)152                                   ])153                        + [' }'];154          std.join('', lines),155      renderTableInternal(v, path, indexedPath, cindent) =156        local kvp = std.flattenArrays([157          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]158          for k in std.objectFields(v)159          if !isSection(v[k])160        ]);161        local sections = [std.join('\n', kvp)] + [162          (163            if std.isObject(v[k]) then164              renderTable(v[k], path + [k], indexedPath + [k], cindent)165            else166              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)167          )168          for k in std.objectFields(v)169          if isSection(v[k])170        ];171        std.join('\n\n', sections),172      renderTable(v, path, indexedPath, cindent) =173        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'174        + (if v == {} then '' else '\n')175        + renderTableInternal(v, path, indexedPath, cindent + indent),176      renderTableArray(v, path, indexedPath, cindent) =177        local range = std.range(0, std.length(v) - 1);178        local sections = [179          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'180           + (if v[i] == {} then '' else '\n')181           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))182          for i in range183        ];184        std.join('\n\n', sections);185    if std.isObject(value) then186      renderTableInternal(value, [], [], '')187    else188      error 'TOML body must be an object. Got ' + std.type(value),189190  escapeStringPython(str)::191    std.escapeStringJson(str),192193  escapeStringBash(str_)::194    local str = std.toString(str_);195    local trans(ch) =196      if ch == "'" then197        "'\"'\"'"198      else199        ch;200    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),201202  escapeStringDollars(str_)::203    local str = std.toString(str_);204    local trans(ch) =205      if ch == '$' then206        '$$'207      else208        ch;209    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),210211  manifestJson(value):: std.manifestJsonEx(value, '    ') tailstrict,212213  manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),214215  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::216    if !std.isArray(value) then217      error 'manifestYamlStream only takes arrays, got ' + std.type(value)218    else219      '---\n' + std.join(220        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]221      ) + if c_document_end then '\n...\n' else '\n',222223224  manifestPython(v)::225    if std.isObject(v) then226      local fields = [227        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]228        for k in std.objectFields(v)229      ];230      '{%s}' % [std.join(', ', fields)]231    else if std.isArray(v) then232      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]233    else if std.isString(v) then234      '%s' % [std.escapeStringPython(v)]235    else if std.isFunction(v) then236      error 'cannot manifest function'237    else if std.isNumber(v) then238      std.toString(v)239    else if v == true then240      'True'241    else if v == false then242      'False'243    else if v == null then244      'None',245246  manifestPythonVars(conf)::247    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];248    std.join('\n', vars + ['']),249250  manifestXmlJsonml(value)::251    if !std.isArray(value) then252      error 'Expected a JSONML value (an array), got %s' % std.type(value)253    else254      local aux(v) =255        if std.isString(v) then256          v257        else258          local tag = v[0];259          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);260          local attrs = if has_attrs then v[1] else {};261          local children = if has_attrs then v[2:] else v[1:];262          local attrs_str =263            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);264          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);265266      aux(value),267268  uniq(arr, keyF=id)::269    local f(a, b) =270      if std.length(a) == 0 then271        [b]272      else if keyF(a[std.length(a) - 1]) == keyF(b) then273        a274      else275        a + [b];276    std.foldl(f, arr, []),277278  set(arr, keyF=id)::279    std.uniq(std.sort(arr, keyF), keyF),280281  setMember(x, arr, keyF=id)::282    // TODO(dcunnin): Binary chop for O(log n) complexity283    std.length(std.setInter([x], arr, keyF)) > 0,284285  setUnion(a, b, keyF=id)::286    // NOTE: order matters, values in `a` win287    local aux(a, b, i, j, acc) =288      if i >= std.length(a) then289        acc + b[j:]290      else if j >= std.length(b) then291        acc + a[i:]292      else293        local ak = keyF(a[i]);294        local bk = keyF(b[j]);295        if ak == bk then296          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict297        else if ak < bk then298          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict299        else300          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;301    aux(a, b, 0, 0, []),302303  setInter(a, b, keyF=id)::304    local aux(a, b, i, j, acc) =305      if i >= std.length(a) || j >= std.length(b) then306        acc307      else308        if keyF(a[i]) == keyF(b[j]) then309          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict310        else if keyF(a[i]) < keyF(b[j]) then311          aux(a, b, i + 1, j, acc) tailstrict312        else313          aux(a, b, i, j + 1, acc) tailstrict;314    aux(a, b, 0, 0, []) tailstrict,315316  setDiff(a, b, keyF=id)::317    local aux(a, b, i, j, acc) =318      if i >= std.length(a) then319        acc320      else if j >= std.length(b) then321        acc + a[i:]322      else323        if keyF(a[i]) == keyF(b[j]) then324          aux(a, b, i + 1, j + 1, acc) tailstrict325        else if keyF(a[i]) < keyF(b[j]) then326          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict327        else328          aux(a, b, i, j + 1, acc) tailstrict;329    aux(a, b, 0, 0, []) tailstrict,330331  mergePatch(target, patch)::332    if std.isObject(patch) then333      local target_object =334        if std.isObject(target) then target else {};335336      local target_fields =337        if std.isObject(target_object) then std.objectFields(target_object) else [];338339      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];340      local both_fields = std.setUnion(target_fields, std.objectFields(patch));341342      {343        [k]:344          if !std.objectHas(patch, k) then345            target_object[k]346          else if !std.objectHas(target_object, k) then347            std.mergePatch(null, patch[k]) tailstrict348          else349            std.mergePatch(target_object[k], patch[k]) tailstrict350        for k in std.setDiff(both_fields, null_fields)351      }352    else353      patch,354355  get(o, f, default=null, inc_hidden=true)::356    if std.objectHasEx(o, f, inc_hidden) then o[f] else default,357358  objectFields(o)::359    std.objectFieldsEx(o, false),360361  objectFieldsAll(o)::362    std.objectFieldsEx(o, true),363364  objectHas(o, f)::365    std.objectHasEx(o, f, false),366367  objectHasAll(o, f)::368    std.objectHasEx(o, f, true),369370  objectValues(o)::371    [o[k] for k in std.objectFields(o)],372373  objectValuesAll(o)::374    [o[k] for k in std.objectFieldsAll(o)],375376  resolvePath(f, r)::377    local arr = std.split(f, '/');378    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),379380  prune(a)::381    local isContent(b) =382      if b == null then383        false384      else if std.isArray(b) then385        std.length(b) > 0386      else if std.isObject(b) then387        std.length(b) > 0388      else389        true;390    if std.isArray(a) then391      [std.prune(x) for x in a if isContent($.prune(x))]392    else if std.isObject(a) then {393      [x]: $.prune(a[x])394      for x in std.objectFields(a)395      if isContent(std.prune(a[x]))396    } else397      a,398399  find(value, arr)::400    if !std.isArray(arr) then401      error 'find second parameter should be an array, got ' + std.type(arr)402    else403      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),404}
addednix/jrsonnet-release.nixdiffbeforeafterboth
--- /dev/null
+++ b/nix/jrsonnet-release.nix
@@ -0,0 +1,24 @@
+{ lib, fetchFromGitHub, rustPlatform, runCommand, makeWrapper }:
+
+
+rustPlatform.buildRustPackage rec {
+  pname = "jrsonnet";
+  version = "5f0f8de9f52f961e2ff162e0a3fd4ca20a275f1d";
+
+  src = fetchFromGitHub {
+    owner = "CertainLach";
+    repo = pname;
+    rev = version;
+    hash = lib.fakeHash;
+  };
+
+  cargoTestFlags = [ "--package=jrsonnet --features=mimalloc,legacy-this-file" ];
+  cargoBuildFlags = [ "--package=jrsonnet --features=mimalloc,legacy-this-file" ];
+
+  buildInputs = [ makeWrapper ];
+
+  postInstall = ''
+    mv $out/bin/jrsonnet $out/bin/jrsonnet-release
+    wrapProgram $out/bin/jrsonnet-release --add-flags "--max-stack=200000 --os-stack=200000"
+  '';
+}