git.delta.rocks / jrsonnet / refs/commits / ff649630ca7e

difftreelog

feat implement builtins for trivial numeric functions

Petr Portnov2022-11-26parent: #5f0f8de.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/lib.rs
1use std::{2	cell::{Ref, RefCell, RefMut},3	collections::HashMap,4	rc::Rc,5};67use jrsonnet_evaluator::{8	error::{ErrorKind::*, Result},9	function::{builtin::Builtin, CallLocation, FuncVal, TlaArg},10	gc::{GcHashMap, TraceBox},11	tb,12	trace::PathResolver,13	Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,14};15use jrsonnet_gcmodule::{Cc, Trace};16use jrsonnet_parser::Source;1718mod expr;19mod types;20pub use types::*;21mod arrays;22pub use arrays::*;23mod math;24pub use math::*;25mod operator;26pub use operator::*;27mod sort;28pub use sort::*;29mod hash;30pub use hash::*;31mod encoding;32pub use encoding::*;33mod objects;34pub use objects::*;35mod manifest;36pub use manifest::*;37mod parse;38pub use parse::*;39mod strings;40pub use strings::*;41mod misc;42pub use misc::*;4344pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {45	let mut builder = ObjValueBuilder::new();4647	let expr = expr::stdlib_expr();48	let eval = jrsonnet_evaluator::evaluate(ContextBuilder::dangerous_empty_state().build(), &expr)49		.expect("stdlib.jsonnet should have no errors")50		.as_obj()51		.expect("stdlib.jsonnet should evaluate to object");5253	builder.with_super(eval);5455	for (name, builtin) in [56		// Types57		("type", builtin_type::INST),58		("isString", builtin_is_string::INST),59		("isNumber", builtin_is_number::INST),60		("isBoolean", builtin_is_boolean::INST),61		("isObject", builtin_is_object::INST),62		("isArray", builtin_is_array::INST),63		("isFunction", builtin_is_function::INST),64		// Arrays65		("makeArray", builtin_make_array::INST),66		("slice", builtin_slice::INST),67		("map", builtin_map::INST),68		("flatMap", builtin_flatmap::INST),69		("filter", builtin_filter::INST),70		("foldl", builtin_foldl::INST),71		("foldr", builtin_foldr::INST),72		("range", builtin_range::INST),73		("join", builtin_join::INST),74		("reverse", builtin_reverse::INST),75		("any", builtin_any::INST),76		("all", builtin_all::INST),77		("member", builtin_member::INST),78		("count", builtin_count::INST),79		// Math80		("modulo", builtin_modulo::INST),81		("floor", builtin_floor::INST),82		("ceil", builtin_ceil::INST),83		("log", builtin_log::INST),84		("pow", builtin_pow::INST),85		("sqrt", builtin_sqrt::INST),86		("sin", builtin_sin::INST),87		("cos", builtin_cos::INST),88		("tan", builtin_tan::INST),89		("asin", builtin_asin::INST),90		("acos", builtin_acos::INST),91		("atan", builtin_atan::INST),92		("exp", builtin_exp::INST),93		("mantissa", builtin_mantissa::INST),94		("exponent", builtin_exponent::INST),95		// Operator96		("mod", builtin_mod::INST),97		("primitiveEquals", builtin_primitive_equals::INST),98		("equals", builtin_equals::INST),99		("format", builtin_format::INST),100		// Sort101		("sort", builtin_sort::INST),102		// Hash103		("md5", builtin_md5::INST),104		#[cfg(feature = "exp-more-hashes")]105		("sha256", builtin_sha256::INST),106		// Encoding107		("encodeUTF8", builtin_encode_utf8::INST),108		("decodeUTF8", builtin_decode_utf8::INST),109		("base64", builtin_base64::INST),110		("base64Decode", builtin_base64_decode::INST),111		("base64DecodeBytes", builtin_base64_decode_bytes::INST),112		// Objects113		("objectFieldsEx", builtin_object_fields_ex::INST),114		("objectHasEx", builtin_object_has_ex::INST),115		// Manifest116		("escapeStringJson", builtin_escape_string_json::INST),117		("manifestJsonEx", builtin_manifest_json_ex::INST),118		("manifestYamlDoc", builtin_manifest_yaml_doc::INST),119		// Parsing120		("parseJson", builtin_parse_json::INST),121		("parseYaml", builtin_parse_yaml::INST),122		// Strings123		("codepoint", builtin_codepoint::INST),124		("substr", builtin_substr::INST),125		("char", builtin_char::INST),126		("strReplace", builtin_str_replace::INST),127		("splitLimit", builtin_splitlimit::INST),128		("asciiUpper", builtin_ascii_upper::INST),129		("asciiLower", builtin_ascii_lower::INST),130		("findSubstr", builtin_find_substr::INST),131		("parseInt", builtin_parse_int::INST),132		("parseOctal", builtin_parse_octal::INST),133		("parseHex", builtin_parse_hex::INST),134		// Misc135		("length", builtin_length::INST),136		("startsWith", builtin_starts_with::INST),137		("endsWith", builtin_ends_with::INST),138	]139	.iter()140	.cloned()141	{142		builder143			.member(name.into())144			.hide()145			.value(Val::Func(FuncVal::StaticBuiltin(builtin)))146			.expect("no conflict");147	}148149	builder150		.member("extVar".into())151		.hide()152		.value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {153			settings: settings.clone()154		})))))155		.expect("no conflict");156	builder157		.member("native".into())158		.hide()159		.value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {160			settings: settings.clone()161		})))))162		.expect("no conflict");163	builder164		.member("trace".into())165		.hide()166		.value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace {167			settings168		})))))169		.expect("no conflict");170171	builder172		.member("id".into())173		.hide()174		.value(Val::Func(FuncVal::Id))175		.expect("no conflict");176177	builder.build()178}179180pub trait TracePrinter {181	fn print_trace(&self, loc: CallLocation, value: IStr);182}183184pub struct StdTracePrinter {185	resolver: PathResolver,186}187impl StdTracePrinter {188	pub fn new(resolver: PathResolver) -> Self {189		Self { resolver }190	}191}192impl TracePrinter for StdTracePrinter {193	fn print_trace(&self, loc: CallLocation, value: IStr) {194		eprint!("TRACE:");195		if let Some(loc) = loc.0 {196			let locs = loc.0.map_source_locations(&[loc.1]);197			eprint!(198				" {}:{}",199				match loc.0.source_path().path() {200					Some(p) => self.resolver.resolve(p),201					None => loc.0.source_path().to_string(),202				},203				locs[0].line204			);205		}206		eprintln!(" {}", value);207	}208}209210pub struct Settings {211	/// Used for `std.extVar`212	pub ext_vars: HashMap<IStr, TlaArg>,213	/// Used for `std.native`214	pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,215	/// Helper to add globals without implementing custom ContextInitializer216	pub globals: GcHashMap<IStr, Thunk<Val>>,217	/// Used for `std.trace`218	pub trace_printer: Box<dyn TracePrinter>,219	/// Used for `std.thisFile`220	pub path_resolver: PathResolver,221}222223fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {224	let source_name = format!("<extvar:{}>", name);225	Source::new_virtual(source_name.into(), code.into())226}227228#[derive(Trace)]229pub struct ContextInitializer {230	// When we don't need to support legacy-this-file, we can reuse same context for all files231	#[cfg(not(feature = "legacy-this-file"))]232	context: Context,233	// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it234	#[cfg(feature = "legacy-this-file")]235	stdlib_obj: ObjValue,236	settings: Rc<RefCell<Settings>>,237}238impl ContextInitializer {239	pub fn new(s: State, resolver: PathResolver) -> Self {240		let settings = Settings {241			ext_vars: Default::default(),242			ext_natives: Default::default(),243			globals: Default::default(),244			trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),245			path_resolver: resolver,246		};247		let settings = Rc::new(RefCell::new(settings));248		Self {249			#[cfg(not(feature = "legacy-this-file"))]250			context: {251				let mut context = ContextBuilder::with_capacity(s, 1);252				context.bind(253					"std".into(),254					Thunk::evaluated(Val::Obj(stdlib_uncached(settings.clone()))),255				);256				context.build()257			},258			#[cfg(feature = "legacy-this-file")]259			stdlib_obj: stdlib_uncached(settings.clone()),260			settings,261		}262	}263	pub fn settings(&self) -> Ref<Settings> {264		self.settings.borrow()265	}266	pub fn settings_mut(&self) -> RefMut<Settings> {267		self.settings.borrow_mut()268	}269	pub fn add_ext_var(&self, name: IStr, value: Val) {270		self.settings_mut()271			.ext_vars272			.insert(name, TlaArg::Val(value));273	}274	pub fn add_ext_str(&self, name: IStr, value: IStr) {275		self.settings_mut()276			.ext_vars277			.insert(name, TlaArg::String(value));278	}279	pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {280		let code = code.into();281		let source = extvar_source(name, code.clone());282		let parsed = jrsonnet_parser::parse(283			&code,284			&jrsonnet_parser::ParserSettings {285				source: source.clone(),286			},287		)288		.map_err(|e| ImportSyntaxError {289			path: source,290			error: Box::new(e),291		})?;292		// self.data_mut().volatile_files.insert(source_name, code);293		self.settings_mut()294			.ext_vars295			.insert(name.into(), TlaArg::Code(parsed));296		Ok(())297	}298	pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {299		self.settings_mut().ext_natives.insert(name, cb);300	}301}302impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {303	#[cfg(not(feature = "legacy-this-file"))]304	fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {305		let out = self.context.clone();306		let globals = &self.settings().globals;307		if globals.is_empty() {308			return out;309		}310311		let mut out = ContextBuilder::extend(out);312		for (k, v) in globals.iter() {313			out.bind(k.clone(), v.clone());314		}315		out.build()316	}317	#[cfg(feature = "legacy-this-file")]318	fn initialize(&self, s: State, source: Source) -> Context {319		let mut builder = ObjValueBuilder::new();320		builder.with_super(self.stdlib_obj.clone());321		builder322			.member("thisFile".into())323			.hide()324			.value(Val::Str(match source.source_path().path() {325				Some(p) => self.settings().path_resolver.resolve(p).into(),326				None => source.source_path().to_string().into(),327			}))328			.expect("this object builder is empty");329		let stdlib_with_this_file = builder.build();330331		let mut context = ContextBuilder::with_capacity(s, 1);332		context.bind(333			"std".into(),334			Thunk::evaluated(Val::Obj(stdlib_with_this_file)),335		);336		for (k, v) in self.settings().globals.iter() {337			context.bind(k.clone(), v.clone());338		}339		context.build()340	}341	fn as_any(&self) -> &dyn std::any::Any {342		self343	}344}345346pub trait StateExt {347	/// This method was previously implemented in jrsonnet-evaluator itself348	fn with_stdlib(&self);349	fn add_global(&self, name: IStr, value: Thunk<Val>);350}351352impl StateExt for State {353	fn with_stdlib(&self) {354		let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());355		self.settings_mut().context_initializer = tb!(initializer)356	}357	fn add_global(&self, name: IStr, value: Thunk<Val>) {358		self.settings()359			.context_initializer360			.as_any()361			.downcast_ref::<ContextInitializer>()362			.expect("not standard context initializer")363			.settings_mut()364			.globals365			.insert(name, value);366	}367}
modifiedcrates/jrsonnet-stdlib/src/math.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/math.rs
+++ b/crates/jrsonnet-stdlib/src/math.rs
@@ -1,6 +1,42 @@
 use jrsonnet_evaluator::{error::Result, function::builtin, typed::PositiveF64};
 
 #[builtin]
+pub fn builtin_abs(x: f64) -> Result<f64> {
+	Ok(x.abs())
+}
+
+#[builtin]
+pub fn builtin_sign(x: f64) -> Result<f64> {
+	Ok(if x == 0. { 0. } else { x.signum() })
+}
+
+#[builtin]
+pub fn builtin_max(x: f64, y: f64) -> Result<f64> {
+	Ok(x.max(y))
+}
+
+#[builtin]
+pub fn builtin_min(x: f64, y: f64) -> Result<f64> {
+	Ok(x.min(y))
+}
+
+#[builtin]
+pub fn builtin_clamp(x: f64, min_val: f64, max_val: f64) -> Result<f64> {
+	debug_assert!(x.is_finite(), "jsonnet number are always finite");
+	debug_assert!(min_val.is_finite(), "jsonnet number are always finite");
+	debug_assert!(max_val.is_finite(), "jsonnet number are always finite");
+
+	// `f64::clamp` should noe be used here since it requires extra checks to guarantee NaN-safety
+	Ok(if x < min_val {
+		min_val
+	} else if x > max_val {
+		max_val
+	} else {
+		x
+	})
+}
+
+#[builtin]
 pub fn builtin_modulo(a: f64, b: f64) -> Result<f64> {
 	Ok(a % b)
 }
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -77,38 +77,6 @@
     else
       error 'Assertion failed. ' + a + ' != ' + b,
 
-  abs(n)::
-    if !std.isNumber(n) then
-      error 'std.abs expected number, got ' + std.type(n)
-    else
-      if n > 0 then n else -n,
-
-  sign(n)::
-    if !std.isNumber(n) then
-      error 'std.sign expected number, got ' + std.type(n)
-    else
-      if n > 0 then
-        1
-      else if n < 0 then
-        -1
-      else 0,
-
-  max(a, b)::
-    if !std.isNumber(a) then
-      error 'std.max first param expected number, got ' + std.type(a)
-    else if !std.isNumber(b) then
-      error 'std.max second param expected number, got ' + std.type(b)
-    else
-      if a > b then a else b,
-
-  min(a, b)::
-    if !std.isNumber(a) then
-      error 'std.min first param expected number, got ' + std.type(a)
-    else if !std.isNumber(b) then
-      error 'std.min second param expected number, got ' + std.type(b)
-    else
-      if a < b then a else b,
-
   clamp(x, minVal, maxVal)::
     if x < minVal then minVal
     else if x > maxVal then maxVal