git.delta.rocks / jrsonnet / refs/commits / 4c008687f967

difftreelog

perf lazy slice

Yaroslav Bolyukin2022-04-20parent: #9c0fa01.patch.diff
in: master

5 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/builtin/mod.rs
1use crate::function::{CallLocation, StaticBuiltin};2use crate::typed::{Any, Bytes, PositiveF64, VecVal, M1};3use crate::{4	builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},5	equals,6	error::{Error::*, Result},7	operator::evaluate_mod_op,8	primitive_equals, push_frame, throw,9	typed::{Either2, Either4},10	with_state, ArrValue, FuncVal, IndexableVal, Val,11};12use crate::{Either, ObjValue};13use format::{format_arr, format_obj};14use gcmodule::Cc;15use jrsonnet_interner::IStr;16use serde::Deserialize;17use serde_yaml::DeserializingQuirks;18use std::collections::HashMap;19use std::convert::{TryFrom, TryInto};2021pub mod stdlib;22pub use stdlib::*;2324use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};2526pub mod format;27pub mod manifest;28pub mod sort;2930pub fn std_format(str: IStr, vals: Val) -> Result<String> {31	push_frame(32		CallLocation::native(),33		|| format!("std.format of {}", str),34		|| {35			Ok(match vals {36				Val::Arr(vals) => format_arr(&str, &vals.evaluated()?)?,37				Val::Obj(obj) => format_obj(&str, &obj)?,38				o => format_arr(&str, &[o])?,39			})40		},41	)42}4344pub fn std_slice(45	indexable: IndexableVal,46	index: Option<usize>,47	end: Option<usize>,48	step: Option<usize>,49) -> Result<Val> {50	let index = index.unwrap_or(0);51	let end = end.unwrap_or_else(|| match &indexable {52		IndexableVal::Str(_) => usize::MAX,53		IndexableVal::Arr(v) => v.len(),54	});55	let step = step.unwrap_or(1);56	match &indexable {57		IndexableVal::Str(s) => Ok(Val::Str(58			(s.chars()59				.skip(index)60				.take(end - index)61				.step_by(step)62				.collect::<String>())63			.into(),64		)),65		IndexableVal::Arr(arr) => Ok(Val::Arr(66			(arr.iter()67				.skip(index)68				.take(end - index)69				.step_by(step)70				.collect::<Result<Vec<Val>>>()?)71			.into(),72		)),73	}74}7576type BuiltinsType = HashMap<IStr, &'static dyn StaticBuiltin>;7778thread_local! {79	pub static BUILTINS: BuiltinsType = {80		[81			("length".into(), builtin_length::INST),82			("type".into(), builtin_type::INST),83			("makeArray".into(), builtin_make_array::INST),84			("codepoint".into(), builtin_codepoint::INST),85			("objectFieldsEx".into(), builtin_object_fields_ex::INST),86			("objectHasEx".into(), builtin_object_has_ex::INST),87			("slice".into(), builtin_slice::INST),88			("substr".into(), builtin_substr::INST),89			("primitiveEquals".into(), builtin_primitive_equals::INST),90			("equals".into(), builtin_equals::INST),91			("modulo".into(), builtin_modulo::INST),92			("mod".into(), builtin_mod::INST),93			("floor".into(), builtin_floor::INST),94			("ceil".into(), builtin_ceil::INST),95			("log".into(), builtin_log::INST),96			("pow".into(), builtin_pow::INST),97			("sqrt".into(), builtin_sqrt::INST),98			("sin".into(), builtin_sin::INST),99			("cos".into(), builtin_cos::INST),100			("tan".into(), builtin_tan::INST),101			("asin".into(), builtin_asin::INST),102			("acos".into(), builtin_acos::INST),103			("atan".into(), builtin_atan::INST),104			("exp".into(), builtin_exp::INST),105			("mantissa".into(), builtin_mantissa::INST),106			("exponent".into(), builtin_exponent::INST),107			("extVar".into(), builtin_ext_var::INST),108			("native".into(), builtin_native::INST),109			("filter".into(), builtin_filter::INST),110			("map".into(), builtin_map::INST),111			("flatMap".into(), builtin_flatmap::INST),112			("foldl".into(), builtin_foldl::INST),113			("foldr".into(), builtin_foldr::INST),114			("sort".into(), builtin_sort::INST),115			("format".into(), builtin_format::INST),116			("range".into(), builtin_range::INST),117			("char".into(), builtin_char::INST),118			("encodeUTF8".into(), builtin_encode_utf8::INST),119			("decodeUTF8".into(), builtin_decode_utf8::INST),120			("md5".into(), builtin_md5::INST),121			("base64".into(), builtin_base64::INST),122			("base64DecodeBytes".into(), builtin_base64_decode_bytes::INST),123			("base64Decode".into(), builtin_base64_decode::INST),124			("trace".into(), builtin_trace::INST),125			("join".into(), builtin_join::INST),126			("escapeStringJson".into(), builtin_escape_string_json::INST),127			("manifestJsonEx".into(), builtin_manifest_json_ex::INST),128			("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),129			("reverse".into(), builtin_reverse::INST),130			("id".into(), builtin_id::INST),131			("strReplace".into(), builtin_str_replace::INST),132			("splitLimit".into(), builtin_splitlimit::INST),133			("parseJson".into(), builtin_parse_json::INST),134			("parseYaml".into(), builtin_parse_yaml::INST),135			("asciiUpper".into(), builtin_ascii_upper::INST),136			("asciiLower".into(), builtin_ascii_lower::INST),137			("member".into(), builtin_member::INST),138			("count".into(), builtin_count::INST),139			("any".into(), builtin_any::INST),140			("all".into(), builtin_all::INST),141		].iter().cloned().collect()142	};143}144145#[jrsonnet_macros::builtin]146fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {147	use Either4::*;148	Ok(match x {149		A(x) => x.chars().count(),150		B(x) => x.len(),151		C(x) => x152			.fields_visibility()153			.into_iter()154			.filter(|(_k, v)| *v)155			.count(),156		D(f) => f.args_len(),157	})158}159160#[jrsonnet_macros::builtin]161fn builtin_type(x: Any) -> Result<IStr> {162	Ok(x.0.value_type().name().into())163}164165#[jrsonnet_macros::builtin]166fn builtin_make_array(sz: usize, func: FuncVal) -> Result<VecVal> {167	let mut out = Vec::with_capacity(sz);168	for i in 0..sz {169		out.push(func.evaluate_simple(&[i as f64].as_slice())?)170	}171	Ok(VecVal(Cc::new(out)))172}173174#[jrsonnet_macros::builtin]175const fn builtin_codepoint(str: char) -> Result<u32> {176	Ok(str as u32)177}178179#[jrsonnet_macros::builtin]180fn builtin_object_fields_ex(obj: ObjValue, inc_hidden: bool) -> Result<VecVal> {181	let out = obj.fields_ex(inc_hidden);182	Ok(VecVal(Cc::new(183		out.into_iter().map(Val::Str).collect::<Vec<_>>(),184	)))185}186187#[jrsonnet_macros::builtin]188fn builtin_object_has_ex(obj: ObjValue, f: IStr, inc_hidden: bool) -> Result<bool> {189	Ok(obj.has_field_ex(f, inc_hidden))190}191192#[jrsonnet_macros::builtin]193fn builtin_parse_json(s: IStr) -> Result<Any> {194	let value: serde_json::Value = serde_json::from_str(&s)195		.map_err(|e| RuntimeError(format!("failed to parse json: {}", e).into()))?;196	Ok(Any(Val::try_from(&value)?))197}198199#[jrsonnet_macros::builtin]200fn builtin_parse_yaml(s: IStr) -> Result<Any> {201	let value = serde_yaml::Deserializer::from_str_with_quirks(202		&s,203		DeserializingQuirks { old_octals: true },204	);205	let mut out = vec![];206	for item in value {207		let value = serde_json::Value::deserialize(item)208			.map_err(|e| RuntimeError(format!("failed to parse yaml: {}", e).into()))?;209		let val = Val::try_from(&value)?;210		out.push(val);211	}212	Ok(Any(if out.is_empty() {213		Val::Null214	} else if out.len() == 1 {215		out.into_iter().next().unwrap()216	} else {217		Val::Arr(out.into())218	}))219}220221#[jrsonnet_macros::builtin]222fn builtin_slice(223	indexable: IndexableVal,224	index: Option<usize>,225	end: Option<usize>,226	step: Option<usize>,227) -> Result<Any> {228	std_slice(indexable, index, end, step).map(Any)229}230231#[jrsonnet_macros::builtin]232fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {233	Ok(str.chars().skip(from as usize).take(len as usize).collect())234}235236#[jrsonnet_macros::builtin]237fn builtin_primitive_equals(a: Any, b: Any) -> Result<bool> {238	primitive_equals(&a.0, &b.0)239}240241#[jrsonnet_macros::builtin]242fn builtin_equals(a: Any, b: Any) -> Result<bool> {243	equals(&a.0, &b.0)244}245246#[jrsonnet_macros::builtin]247fn builtin_modulo(a: f64, b: f64) -> Result<f64> {248	Ok(a % b)249}250251#[jrsonnet_macros::builtin]252fn builtin_mod(a: Either![f64, IStr], b: Any) -> Result<Any> {253	use Either2::*;254	Ok(Any(evaluate_mod_op(255		&match a {256			A(v) => Val::Num(v),257			B(s) => Val::Str(s),258		},259		&b.0,260	)?))261}262263#[jrsonnet_macros::builtin]264fn builtin_floor(x: f64) -> Result<f64> {265	Ok(x.floor())266}267268#[jrsonnet_macros::builtin]269fn builtin_ceil(x: f64) -> Result<f64> {270	Ok(x.ceil())271}272273#[jrsonnet_macros::builtin]274fn builtin_log(n: f64) -> Result<f64> {275	Ok(n.ln())276}277278#[jrsonnet_macros::builtin]279fn builtin_pow(x: f64, n: f64) -> Result<f64> {280	Ok(x.powf(n))281}282283#[jrsonnet_macros::builtin]284fn builtin_sqrt(x: PositiveF64) -> Result<f64> {285	Ok(x.0.sqrt())286}287288#[jrsonnet_macros::builtin]289fn builtin_sin(x: f64) -> Result<f64> {290	Ok(x.sin())291}292293#[jrsonnet_macros::builtin]294fn builtin_cos(x: f64) -> Result<f64> {295	Ok(x.cos())296}297298#[jrsonnet_macros::builtin]299fn builtin_tan(x: f64) -> Result<f64> {300	Ok(x.tan())301}302303#[jrsonnet_macros::builtin]304fn builtin_asin(x: f64) -> Result<f64> {305	Ok(x.asin())306}307308#[jrsonnet_macros::builtin]309fn builtin_acos(x: f64) -> Result<f64> {310	Ok(x.acos())311}312313#[jrsonnet_macros::builtin]314fn builtin_atan(x: f64) -> Result<f64> {315	Ok(x.atan())316}317318#[jrsonnet_macros::builtin]319fn builtin_exp(x: f64) -> Result<f64> {320	Ok(x.exp())321}322323fn frexp(s: f64) -> (f64, i16) {324	if 0.0 == s {325		(s, 0)326	} else {327		let lg = s.abs().log2();328		let x = (lg - lg.floor() - 1.0).exp2();329		let exp = lg.floor() + 1.0;330		(s.signum() * x, exp as i16)331	}332}333334#[jrsonnet_macros::builtin]335fn builtin_mantissa(x: f64) -> Result<f64> {336	Ok(frexp(x).0)337}338339#[jrsonnet_macros::builtin]340fn builtin_exponent(x: f64) -> Result<i16> {341	Ok(frexp(x).1)342}343344#[jrsonnet_macros::builtin]345fn builtin_ext_var(x: IStr) -> Result<Any> {346	Ok(Any(with_state(|s| s.settings().ext_vars.get(&x).cloned())347		.ok_or(UndefinedExternalVariable(x))?))348}349350#[jrsonnet_macros::builtin]351fn builtin_native(name: IStr) -> Result<FuncVal> {352	Ok(with_state(|s| s.settings().ext_natives.get(&name).cloned())353		.map(|v| FuncVal::Builtin(v.clone()))354		.ok_or(UndefinedExternalFunction(name))?)355}356357#[jrsonnet_macros::builtin]358fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {359	arr.filter(|val| bool::try_from(func.evaluate_simple(&[Any(val.clone())].as_slice())?))360}361362#[jrsonnet_macros::builtin]363fn builtin_map(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {364	arr.map(|val| func.evaluate_simple(&[Any(val)].as_slice()))365}366367#[jrsonnet_macros::builtin]368fn builtin_flatmap(func: FuncVal, arr: IndexableVal) -> Result<IndexableVal> {369	match arr {370		IndexableVal::Str(s) => {371			let mut out = String::new();372			for c in s.chars() {373				match func.evaluate_simple(&[c.to_string()].as_slice())? {374					Val::Str(o) => out.push_str(&o),375					_ => throw!(RuntimeError(376						"in std.join all items should be strings".into()377					)),378				};379			}380			Ok(IndexableVal::Str(out.into()))381		}382		IndexableVal::Arr(a) => {383			let mut out = Vec::new();384			for el in a.iter() {385				let el = el?;386				match func.evaluate_simple(&[Any(el)].as_slice())? {387					Val::Arr(o) => {388						for oe in o.iter() {389							out.push(oe?)390						}391					}392					_ => throw!(RuntimeError(393						"in std.join all items should be arrays".into()394					)),395				};396			}397			Ok(IndexableVal::Arr(out.into()))398		}399	}400}401402#[jrsonnet_macros::builtin]403fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {404	let mut acc = init.0;405	for i in arr.iter() {406		acc = func.evaluate_simple(&[Any(acc), Any(i?)].as_slice())?;407	}408	Ok(Any(acc))409}410411#[jrsonnet_macros::builtin]412fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {413	let mut acc = init.0;414	for i in arr.iter().rev() {415		acc = func.evaluate_simple(&[Any(i?), Any(acc)].as_slice())?;416	}417	Ok(Any(acc))418}419420#[jrsonnet_macros::builtin]421#[allow(non_snake_case)]422fn builtin_sort(arr: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {423	if arr.len() <= 1 {424		return Ok(arr);425	}426	Ok(ArrValue::Eager(sort::sort(427		arr.evaluated()?,428		keyF.as_ref(),429	)?))430}431432#[jrsonnet_macros::builtin]433fn builtin_format(str: IStr, vals: Any) -> Result<String> {434	std_format(str, vals.0)435}436437#[jrsonnet_macros::builtin]438fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {439	if to < from {440		return Ok(ArrValue::new_eager());441	}442	Ok(ArrValue::new_range(from, to))443}444445#[jrsonnet_macros::builtin]446fn builtin_char(n: u32) -> Result<char> {447	Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)448}449450#[jrsonnet_macros::builtin]451fn builtin_encode_utf8(str: IStr) -> Result<Bytes> {452	Ok(Bytes(str.bytes().collect::<Vec<u8>>().into()))453}454455#[jrsonnet_macros::builtin]456fn builtin_decode_utf8(arr: Bytes) -> Result<IStr> {457	Ok(std::str::from_utf8(&arr.0)458		.map_err(|_| RuntimeError("bad utf8".into()))?459		.into())460}461462#[jrsonnet_macros::builtin]463fn builtin_md5(str: IStr) -> Result<String> {464	Ok(format!("{:x}", md5::compute(&str.as_bytes())))465}466467#[jrsonnet_macros::builtin]468fn builtin_trace(loc: CallLocation, str: IStr, rest: Any) -> Result<Any> {469	eprint!("TRACE:");470	if let Some(loc) = loc.0 {471		with_state(|s| {472			let locs = s.map_source_locations(&loc.0, &[loc.1]);473			eprint!(474				" {}:{}",475				loc.0.file_name().unwrap().to_str().unwrap(),476				locs[0].line477			);478		});479	}480	eprintln!(" {}", str);481	Ok(rest) as Result<Any>482}483484#[jrsonnet_macros::builtin]485fn builtin_base64(input: Either![Bytes, IStr]) -> Result<String> {486	use Either2::*;487	Ok(match input {488		A(a) => base64::encode(a.0),489		B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),490	})491}492493#[jrsonnet_macros::builtin]494fn builtin_base64_decode_bytes(input: IStr) -> Result<Bytes> {495	Ok(Bytes(496		base64::decode(&input.as_bytes())497			.map_err(|_| RuntimeError("bad base64".into()))?498			.into(),499	))500}501502#[jrsonnet_macros::builtin]503fn builtin_base64_decode(input: IStr) -> Result<String> {504	let bytes = base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?;505	Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)506}507508#[jrsonnet_macros::builtin]509fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {510	Ok(match sep {511		IndexableVal::Arr(joiner_items) => {512			let mut out = Vec::new();513514			let mut first = true;515			for item in arr.iter() {516				let item = item?.clone();517				if let Val::Arr(items) = item {518					if !first {519						out.reserve(joiner_items.len());520						// TODO: extend521						for item in joiner_items.iter() {522							out.push(item?);523						}524					}525					first = false;526					out.reserve(items.len());527					// TODO: extend528					for item in items.iter() {529						out.push(item?);530					}531				} else {532					throw!(RuntimeError(533						"in std.join all items should be arrays".into()534					));535				}536			}537538			IndexableVal::Arr(out.into())539		}540		IndexableVal::Str(sep) => {541			let mut out = String::new();542543			let mut first = true;544			for item in arr.iter() {545				let item = item?.clone();546				if let Val::Str(item) = item {547					if !first {548						out += &sep;549					}550					first = false;551					out += &item;552				} else {553					throw!(RuntimeError(554						"in std.join all items should be strings".into()555					));556				}557			}558559			IndexableVal::Str(out.into())560		}561	})562}563564#[jrsonnet_macros::builtin]565fn builtin_escape_string_json(str_: IStr) -> Result<String> {566	Ok(escape_string_json(&str_))567}568569#[jrsonnet_macros::builtin]570fn builtin_manifest_json_ex(571	value: Any,572	indent: IStr,573	newline: Option<IStr>,574	key_val_sep: Option<IStr>,575) -> Result<String> {576	let newline = newline.as_deref().unwrap_or("\n");577	let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");578	manifest_json_ex(579		&value.0,580		&ManifestJsonOptions {581			padding: &indent,582			mtype: ManifestType::Std,583			newline,584			key_val_sep,585		},586	)587}588589#[jrsonnet_macros::builtin]590fn builtin_manifest_yaml_doc(591	value: Any,592	indent_array_in_object: Option<bool>,593	quote_keys: Option<bool>,594) -> Result<String> {595	manifest_yaml_ex(596		&value.0,597		&ManifestYamlOptions {598			padding: "  ",599			arr_element_padding: if indent_array_in_object.unwrap_or(false) {600				"  "601			} else {602				""603			},604			quote_keys: quote_keys.unwrap_or(true),605		},606	)607}608609#[jrsonnet_macros::builtin]610fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {611	Ok(value.reversed())612}613614#[jrsonnet_macros::builtin]615const fn builtin_id(v: Any) -> Result<Any> {616	Ok(v)617}618619#[jrsonnet_macros::builtin]620fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {621	Ok(str.replace(&from as &str, &to as &str))622}623624#[jrsonnet_macros::builtin]625fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {626	use Either2::*;627	Ok(VecVal(Cc::new(match maxsplits {628		A(n) => str629			.splitn(n + 1, &c as &str)630			.map(|s| Val::Str(s.into()))631			.collect(),632		B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),633	})))634}635636#[jrsonnet_macros::builtin]637fn builtin_ascii_upper(str: IStr) -> Result<String> {638	Ok(str.to_ascii_uppercase())639}640641#[jrsonnet_macros::builtin]642fn builtin_ascii_lower(str: IStr) -> Result<String> {643	Ok(str.to_ascii_lowercase())644}645646#[jrsonnet_macros::builtin]647fn builtin_member(arr: IndexableVal, x: Any) -> Result<bool> {648	match arr {649		IndexableVal::Str(s) => {650			let x: IStr = IStr::try_from(x.0)?;651			Ok(!x.is_empty() && s.contains(&*x))652		}653		IndexableVal::Arr(a) => {654			for item in a.iter() {655				let item = item?;656				if equals(&item, &x.0)? {657					return Ok(true);658				}659			}660			Ok(false)661		}662	}663}664665#[jrsonnet_macros::builtin]666fn builtin_count(arr: Vec<Any>, v: Any) -> Result<usize> {667	let mut count = 0;668	for item in arr.iter() {669		if equals(&item.0, &v.0)? {670			count += 1;671		}672	}673	Ok(count)674}675676#[jrsonnet_macros::builtin]677fn builtin_any(arr: ArrValue) -> Result<bool> {678	for v in arr.iter() {679		let v: bool = v?.try_into()?;680		if v {681			return Ok(true);682		}683	}684	Ok(false)685}686687#[jrsonnet_macros::builtin]688fn builtin_all(arr: ArrValue) -> Result<bool> {689	for v in arr.iter() {690		let v: bool = v?.try_into()?;691		if !v {692			return Ok(false);693		}694	}695	Ok(true)696}
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -191,3 +191,10 @@
 		return Err($e.into())
 	};
 }
+
+#[macro_export]
+macro_rules! throw_runtime {
+	($($tt:tt)*) => {
+		return Err($crate::error::Error::RuntimeError(format!($($tt)*).into()).into())
+	};
+}
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -665,23 +665,28 @@
 		}
 		Slice(value, desc) => {
 			let indexable = evaluate(context.clone(), value)?;
+			let loc = CallLocation::new(loc);
 
-			fn parse_num(
+			fn parse_idx<const MIN: usize>(
+				loc: CallLocation,
 				context: &Context,
-				expr: Option<&LocExpr>,
+				expr: &Option<LocExpr>,
 				desc: &'static str,
-			) -> Result<Option<usize>> {
-				Ok(match expr {
-					Some(s) => evaluate(context.clone(), s)?
-						.try_cast_nullable_num(desc)?
-						.map(|v| v as usize),
-					None => None,
-				})
+			) -> Result<Option<BoundedUsize<MIN, { i32::MAX as usize }>>> {
+				if let Some(value) = expr {
+					Ok(Some(push_frame(
+						loc,
+						|| format!("slice {}", desc),
+						|| Ok(evaluate(context.clone(), value)?.try_into()?),
+					)?))
+				} else {
+					Ok(None)
+				}
 			}
 
-			let start = parse_num(&context, desc.start.as_ref(), "start")?;
-			let end = parse_num(&context, desc.end.as_ref(), "end")?;
-			let step = parse_num(&context, desc.step.as_ref(), "step")?;
+			let start = parse_idx(loc, &context, &desc.start, "start")?;
+			let end = parse_idx(loc, &context, &desc.end, "end")?;
+			let step = parse_idx(loc, &context, &desc.step, "step")?;
 
 			std_slice(indexable.into_indexable()?, start, end, step)?
 		}
modifiedcrates/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},
+	ops::Deref,
 	rc::Rc,
 };
 
@@ -12,7 +13,8 @@
 	error::{Error::*, LocError, Result},
 	throw,
 	typed::CheckType,
-	ArrValue, FuncDesc, FuncVal, IndexableVal, ObjValue, ObjValueBuilder, Val,
+	val::{ArrValue, FuncDesc, FuncVal, IndexableVal},
+	ObjValue, ObjValueBuilder, Val,
 };
 
 pub trait TypedObj: Typed {
@@ -69,6 +71,76 @@
 
 impl_int!(i8 u8 i16 u16 i32 u32);
 
+macro_rules! impl_bounded_int {
+	($($name:ident = $ty:ty)*) => {$(
+		#[derive(Clone, Copy)]
+		pub struct $name<const MIN: $ty, const MAX: $ty>($ty);
+		impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {
+			pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {
+				if value >= MIN && value <= MAX {
+					Some(Self(value))
+				} else {
+					None
+				}
+			}
+			pub const fn value(self) -> $ty {
+				self.0
+			}
+		}
+		impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {
+			type Target = $ty;
+			fn deref(&self) -> &Self::Target {
+				&self.0
+			}
+		}
+
+		impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {
+			const TYPE: &'static ComplexValType =
+				&ComplexValType::BoundedNumber(
+					Some(MIN as f64),
+					Some(MAX as f64),
+				);
+		}
+		impl<const MIN: $ty, const MAX: $ty> TryFrom<Val> for $name<MIN, MAX> {
+			type Error = LocError;
+
+			fn try_from(value: Val) -> Result<Self> {
+				<Self as Typed>::TYPE.check(&value)?;
+				match value {
+					Val::Num(n) => {
+						if n.trunc() != n {
+							throw!(RuntimeError(
+								format!(
+									"cannot convert number with fractional part to {}",
+									stringify!($ty)
+								)
+								.into()
+							))
+						}
+						Ok(Self(n as $ty))
+					}
+					_ => unreachable!(),
+				}
+			}
+		}
+		impl<const MIN: $ty, const MAX: $ty> TryFrom<$name<MIN, MAX>> for Val {
+			type Error = LocError;
+
+			fn try_from(value: $name<MIN, MAX>) -> Result<Self> {
+				Ok(Self::Num(value.0 as f64))
+			}
+		}
+	)*};
+}
+
+impl_bounded_int!(
+	BoundedI8 = i8
+	BoundedI16 = i16
+	BoundedI32 = i32
+	BoundedI64 = i64
+	BoundedUsize = usize
+);
+
 impl Typed for f64 {
 	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);
 }
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -172,6 +172,37 @@
 }
 
 #[derive(Debug, Clone, Trace)]
+pub struct Slice {
+	pub(crate) inner: ArrValue,
+	pub(crate) from: u32,
+	pub(crate) to: u32,
+	pub(crate) step: u32,
+}
+impl Slice {
+	fn from(&self) -> usize {
+		self.from as usize
+	}
+	fn to(&self) -> usize {
+		self.to as usize
+	}
+	fn step(&self) -> usize {
+		self.step as usize
+	}
+	fn len(&self) -> usize {
+		// TODO: use div_ceil
+		let diff = self.to() - self.from();
+		let rem = diff % self.step();
+		let div = diff / self.step();
+
+		if rem != 0 {
+			div + 1
+		} else {
+			div
+		}
+	}
+}
+
+#[derive(Debug, Clone, Trace)]
 #[force_tracking]
 pub enum ArrValue {
 	Bytes(#[skip_trace] Rc<[u8]>),
@@ -179,6 +210,7 @@
 	Eager(Cc<Vec<Val>>),
 	Extended(Box<(Self, Self)>),
 	Range(i32, i32),
+	Slice(Box<Slice>),
 	Reversed(Box<Self>),
 }
 impl ArrValue {
@@ -190,6 +222,22 @@
 		Self::Range(a, b)
 	}
 
+	pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {
+		let len = self.len();
+		let from = from.unwrap_or(0);
+		let to = to.unwrap_or(len).min(len);
+		let step = step.unwrap_or(1);
+		assert!(from < to);
+		assert!(step > 0);
+
+		Self::Slice(Box::new(Slice {
+			inner: self,
+			from: from as u32,
+			to: to as u32,
+			step: step as u32,
+		}))
+	}
+
 	pub fn len(&self) -> usize {
 		match self {
 			Self::Bytes(i) => i.len(),
@@ -198,6 +246,7 @@
 			Self::Extended(v) => v.0.len() + v.1.len(),
 			Self::Range(a, b) => a.abs_diff(*b) as usize,
 			Self::Reversed(i) => i.len(),
+			Self::Slice(s) => s.len(),
 		}
 	}
 
@@ -239,6 +288,13 @@
 				}
 				v.get(len - index - 1)
 			}
+			Self::Slice(s) => {
+				let index = s.from() + index * s.step();
+				if index >= s.to() {
+					return Ok(None);
+				}
+				s.inner.get(index as usize)
+			}
 		}
 	}
 
@@ -272,6 +328,13 @@
 				}
 				v.get_lazy(len - index - 1)
 			}
+			Self::Slice(s) => {
+				let index = s.from() + index * s.step();
+				if index >= s.to() {
+					return None;
+				}
+				s.inner.get_lazy(index as usize)
+			}
 		}
 	}
 
@@ -311,33 +374,43 @@
 				Cc::update_with(&mut r, |v| v.reverse());
 				r
 			}
+			Self::Slice(v) => {
+				let mut out = Vec::with_capacity(v.inner.len());
+				for v in v
+					.inner
+					.iter_lazy()
+					.skip(v.from())
+					.take(v.to() - v.from())
+					.step_by(v.step())
+				{
+					out.push(v.evaluate()?)
+				}
+				Cc::new(out)
+			}
 		})
 	}
 
 	pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {
-		// if let Self::Reversed(v) = self {
-		// 	return v.iter().rev();
-		// }
-		let len = self.len();
-		(0..len).map(move |idx| match self {
+		(0..self.len()).map(move |idx| match self {
 			Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),
 			Self::Lazy(l) => l[idx].evaluate(),
 			Self::Eager(e) => Ok(e[idx].clone()),
 			Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),
 			Self::Range(..) => self.get(idx).map(|e| e.unwrap()),
-			Self::Reversed(..) => self.get(len - idx - 1).map(|e| e.unwrap()),
+			Self::Reversed(..) => self.get(idx).map(|e| e.unwrap()),
+			Self::Slice(..) => self.get(idx).map(|e| e.unwrap()),
 		})
 	}
 
 	pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {
-		let len = self.len();
-		(0..len).map(move |idx| match self {
+		(0..self.len()).map(move |idx| match self {
 			Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),
 			Self::Lazy(l) => l[idx].clone(),
 			Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),
 			Self::Extended(_) => self.get_lazy(idx).unwrap(),
 			Self::Range(..) => self.get_lazy(idx).unwrap(),
-			Self::Reversed(..) => self.get_lazy(len - idx - 1).unwrap(),
+			Self::Reversed(..) => self.get_lazy(idx).unwrap(),
+			Self::Slice(..) => self.get_lazy(idx).unwrap(),
 		})
 	}
 
@@ -459,17 +532,6 @@
 		}
 	}
 
-	pub fn try_cast_nullable_num(self, context: &'static str) -> Result<Option<f64>> {
-		Ok(match self {
-			Val::Null => None,
-			Val::Num(num) => Some(num),
-			_ => throw!(TypeMismatch(
-				context,
-				vec![ValType::Null, ValType::Num],
-				self.value_type()
-			)),
-		})
-	}
 	pub const fn value_type(&self) -> ValType {
 		match self {
 			Self::Str(..) => ValType::Str,