difftreelog
refactor(evaluator)! remove standard library
in: master
Implementation will be moved to jrsonnet-stdlib crate BREAKING CHANGE: `State::with_stdlib` was removed
9 files changed
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -7,9 +7,7 @@
edition = "2021"
[features]
-default = ["serialized-stdlib", "explaining-traces", "friendly-errors"]
-# Serializes standard library AST instead of parsing them every run
-serialized-stdlib = ["bincode", "jrsonnet-parser/serde"]
+default = ["explaining-traces", "friendly-errors"]
# Rustc-like trace visualization
explaining-traces = ["annotate-snippets"]
# Allows library authors to throw custom errors
@@ -23,11 +21,12 @@
exp-serde-preserve-order = ["serde_json/preserve_order"]
# Implements field destructuring
exp-destruct = ["jrsonnet-parser/exp-destruct"]
+# Provide Typed for conversions to/from serde_json::Value type
+serde_json = ["dep:serde_json"]
[dependencies]
jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }
jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.2" }
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.2" }
jrsonnet-types = { path = "../jrsonnet-types", version = "0.4.2" }
jrsonnet-macros = { path = "../jrsonnet-macros", version = "0.4.2" }
jrsonnet-gcmodule = { version = "0.3.4" }
@@ -36,15 +35,13 @@
hashbrown = "0.12.1"
static_assertions = "1.1"
-md5 = "0.7.0"
-base64 = "0.13.0"
rustc-hash = "1.1"
thiserror = "1.0"
serde = "1.0"
-serde_json = "1.0"
-serde_yaml_with_quirks = "0.8.24"
+# Optional integration
+serde_json = { version = "1.0.82", optional = true }
anyhow = { version = "1.0", optional = true }
# Friendly errors
@@ -53,9 +50,3 @@
bincode = { version = "1.3", optional = true }
# Explaining traces
annotate-snippets = { version = "0.9.1", features = ["color"], optional = true }
-
-[build-dependencies]
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.2" }
-jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.2" }
-serde = "1.0"
-bincode = "1.3"
crates/jrsonnet-evaluator/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/build.rs
+++ /dev/null
@@ -1,22 +0,0 @@
-use std::{borrow::Cow, env, fs::File, io::Write, path::Path};
-
-use bincode::serialize;
-use jrsonnet_parser::{parse, ParserSettings, Source};
-use jrsonnet_stdlib::STDLIB_STR;
-
-fn main() {
- let parsed = parse(
- STDLIB_STR,
- &ParserSettings {
- file_name: Source::new_virtual(Cow::Borrowed("<std>")),
- },
- )
- .expect("parse");
-
- {
- let out_dir = env::var("OUT_DIR").unwrap();
- let dest_path = Path::new(&out_dir).join("stdlib.bincode");
- let mut f = File::create(&dest_path).unwrap();
- f.write_all(&serialize(&parsed).unwrap()).unwrap();
- }
-}
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -6,10 +6,7 @@
use jrsonnet_types::ValType;
use thiserror::Error;
-use crate::{
- stdlib::{format::FormatError, sort::SortError},
- typed::TypeLocError,
-};
+use crate::{stdlib::format::FormatError, typed::TypeLocError};
fn format_found(list: &[IStr], what: &str) -> String {
if list.is_empty() {
@@ -169,13 +166,6 @@
Format(#[from] FormatError),
#[error("type error: {0}")]
TypeError(TypeLocError),
- #[error("sort error: {0}")]
- Sort(#[from] SortError),
-
- /// Thrown as error, as this is legacy feature, and error here
- /// is acceptable for defeating object field cache
- #[error("should not reach outside: std.thisFile")]
- MagicThisFileUsed,
#[cfg(feature = "anyhow-error")]
#[error(transparent)]
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -13,10 +13,9 @@
error::Error::*,
evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
function::{CallLocation, FuncDesc, FuncVal},
- stdlib::{std_slice, BUILTINS},
tb, throw,
typed::Typed,
- val::{ArrValue, CachedUnbound, Thunk, ThunkValue},
+ val::{ArrValue, CachedUnbound, IndexableVal, Thunk, ThunkValue},
Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,
Unbound, Val,
};
@@ -417,13 +416,13 @@
Literal(LiteralType::This) => {
Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
}
- Literal(LiteralType::Super) => Val::Obj(
- ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
+ Literal(LiteralType::Super) => {
+ Val::Obj(ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
ctx.this()
.clone()
.expect("if super exists - then this should to"),
- ),
- ),
+ ))
+ }
Literal(LiteralType::Dollar) => {
Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)
}
@@ -473,9 +472,6 @@
heap.into_iter().map(|(_, v)| v).collect()
))
}
- Err(e) if matches!(e.error(), MagicThisFileUsed) => {
- Ok(Val::Str(loc.0.full_path().into()))
- }
Err(e) => Err(e),
},
)?,
@@ -573,13 +569,6 @@
Function(params, body) => {
evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())
}
- Intrinsic(name) => Val::Func(FuncVal::StaticBuiltin(
- BUILTINS
- .with(|b| b.get(name).copied())
- .ok_or_else(|| IntrinsicNotFound(name.clone()))?,
- )),
- IntrinsicThisFile => return Err(MagicThisFileUsed.into()),
- IntrinsicId => Val::Func(FuncVal::identity()),
AssertExpr(assert, returned) => {
evaluate_assert(s.clone(), ctx.clone(), assert)?;
evaluate(s, ctx, returned)?
@@ -635,9 +624,9 @@
let start = parse_idx(loc, s.clone(), &ctx, &desc.start, "start")?;
let end = parse_idx(loc, s.clone(), &ctx, &desc.end, "end")?;
- let step = parse_idx(loc, s, &ctx, &desc.step, "step")?;
+ let step = parse_idx(loc, s.clone(), &ctx, &desc.step, "step")?;
- std_slice(indexable.into_indexable()?, start, end, step)?
+ IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?, s)?
}
i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {
let tmp = loc.clone().0;
crates/jrsonnet-evaluator/src/function/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/native.rs
+++ b/crates/jrsonnet-evaluator/src/function/native.rs
@@ -1,5 +1,5 @@
use super::{arglike::ArgLike, CallLocation, FuncVal};
-use crate::{error::Result, typed::Typed, State};
+use crate::{error::Result, typed::Typed, Context, State};
pub trait NativeDesc {
type Value;
@@ -19,7 +19,8 @@
Box::new(move |s: State, $($gen),*| {
let val = val.evaluate(
s.clone(),
- s.create_default_context(),
+ // This isn't intended to be used with ArgsDesc
+ Context::default(),
CallLocation::native(),
&($($gen,)*),
true
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1#![warn(clippy::all, clippy::nursery, clippy::pedantic)]2#![allow(3 macro_expanded_macro_exports_accessed_by_absolute_paths,4 clippy::ptr_arg,5 // Too verbose6 clippy::must_use_candidate,7 // A lot of functions pass around errors thrown by code8 clippy::missing_errors_doc,9 // A lot of pointers have interior Rc10 clippy::needless_pass_by_value,11 // Its fine12 clippy::wildcard_imports,13 clippy::enum_glob_use,14 clippy::module_name_repetitions,15 // TODO: fix individual issues, however this works as intended almost everywhere16 clippy::cast_precision_loss,17 clippy::cast_possible_wrap,18 clippy::cast_possible_truncation,19 clippy::cast_sign_loss,20 // False positives21 // https://github.com/rust-lang/rust-clippy/issues/690222 clippy::use_self,23 // https://github.com/rust-lang/rust-clippy/issues/853924 clippy::iter_with_drain,25)]2627// For jrsonnet-macros28extern crate self as jrsonnet_evaluator;2930mod ctx;31mod dynamic;32pub mod error;33mod evaluate;34pub mod function;35pub mod gc;36mod import;37mod integrations;38mod map;39mod obj;40mod stdlib;41pub mod trace;42pub mod typed;43pub mod val;4445use std::{46 borrow::Cow,47 cell::{Ref, RefCell, RefMut},48 collections::HashMap,49 fmt::{self, Debug},50 path::{Path, PathBuf},51 rc::Rc,52};5354pub use ctx::*;55pub use dynamic::*;56use error::{Error::*, LocError, Result, StackTraceElement};57pub use evaluate::*;58use function::{builtin::Builtin, CallLocation, TlaArg};59use gc::{GcHashMap, TraceBox};60use hashbrown::hash_map::RawEntryMut;61pub use import::*;62use jrsonnet_gcmodule::{Cc, Trace};63use jrsonnet_interner::IBytes;64pub use jrsonnet_interner::IStr;65pub use jrsonnet_parser as parser;66use jrsonnet_parser::*;67pub use obj::*;68use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};69pub use val::{ManifestFormat, Thunk, Val};7071pub trait Unbound: Trace {72 type Bound;73 fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;74}7576#[derive(Clone, Trace)]77pub enum LazyBinding {78 Bindable(Cc<TraceBox<dyn Unbound<Bound = Thunk<Val>>>>),79 Bound(Thunk<Val>),80}8182impl Debug for LazyBinding {83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {84 write!(f, "LazyBinding")85 }86}87impl LazyBinding {88 pub fn evaluate(89 &self,90 s: State,91 sup: Option<ObjValue>,92 this: Option<ObjValue>,93 ) -> Result<Thunk<Val>> {94 match self {95 Self::Bindable(v) => v.bind(s, sup, this),96 Self::Bound(v) => Ok(v.clone()),97 }98 }99}100101pub struct EvaluationSettings {102 /// Limits recursion by limiting the number of stack frames103 pub max_stack: usize,104 /// Limits amount of stack trace items preserved105 pub max_trace: usize,106 /// Used for s`td.extVar`107 pub ext_vars: HashMap<IStr, TlaArg>,108 /// Used for ext.native109 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,110 /// TLA vars111 pub tla_vars: HashMap<IStr, TlaArg>,112 /// Global variables are inserted in default context113 pub globals: HashMap<IStr, Val>,114 /// Used to resolve file locations/contents115 pub import_resolver: Box<dyn ImportResolver>,116 /// Used in manifestification functions117 pub manifest_format: ManifestFormat,118 /// Used for bindings119 pub trace_format: Box<dyn TraceFormat>,120}121impl Default for EvaluationSettings {122 fn default() -> Self {123 Self {124 max_stack: 200,125 max_trace: 20,126 globals: HashMap::default(),127 ext_vars: HashMap::default(),128 ext_natives: HashMap::default(),129 tla_vars: HashMap::default(),130 import_resolver: Box::new(DummyImportResolver),131 manifest_format: ManifestFormat::Json {132 padding: 4,133 #[cfg(feature = "exp-preserve-order")]134 preserve_order: false,135 },136 trace_format: Box::new(CompactFormat {137 padding: 4,138 resolver: trace::PathResolver::Absolute,139 }),140 }141 }142}143144#[derive(Default)]145struct EvaluationData {146 /// Used for stack overflow detection, stacktrace is populated on unwind147 stack_depth: usize,148 /// Updated every time stack entry is popt149 stack_generation: usize,150151 breakpoints: Breakpoints,152153 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces154 files: GcHashMap<PathBuf, FileData>,155 /// Contains tla arguments and others, which aren't needed to be obtained by name156 volatile_files: GcHashMap<String, String>,157}158struct FileData {159 string: Option<IStr>,160 bytes: Option<IBytes>,161 parsed: Option<LocExpr>,162 evaluated: Option<Val>,163164 evaluating: bool,165}166impl FileData {167 fn new_string(data: IStr) -> Self {168 Self {169 string: Some(data),170 bytes: None,171 parsed: None,172 evaluated: None,173 evaluating: false,174 }175 }176 fn new_bytes(data: IBytes) -> Self {177 Self {178 string: None,179 bytes: Some(data),180 parsed: None,181 evaluated: None,182 evaluating: false,183 }184 }185}186187#[allow(clippy::type_complexity)]188pub struct Breakpoint {189 loc: ExprLocation,190 collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,191}192#[derive(Default)]193struct Breakpoints(Vec<Rc<Breakpoint>>);194impl Breakpoints {195 fn insert(196 &self,197 stack_depth: usize,198 stack_generation: usize,199 loc: &ExprLocation,200 result: Result<Val>,201 ) -> Result<Val> {202 if self.0.is_empty() {203 return result;204 }205 for item in &self.0 {206 if item.loc.belongs_to(loc) {207 let mut collected = item.collected.borrow_mut();208 let (depth, vals) = collected.entry(stack_generation).or_default();209 if stack_depth > *depth {210 vals.clear();211 }212 vals.push(result.clone());213 }214 }215 result216 }217}218219#[derive(Default)]220pub struct EvaluationStateInternals {221 /// Internal state222 data: RefCell<EvaluationData>,223 /// Settings, safe to change at runtime224 settings: RefCell<EvaluationSettings>,225}226227/// Maintains stack trace and import resolution228#[derive(Default, Clone)]229pub struct State(Rc<EvaluationStateInternals>);230231impl State {232 pub fn import_str(&self, path: PathBuf) -> Result<IStr> {233 let mut data = self.data_mut();234 let mut file = data.files.raw_entry_mut().from_key(&path);235236 let file = match file {237 RawEntryMut::Occupied(ref mut d) => d.get_mut(),238 RawEntryMut::Vacant(v) => {239 let data = self.settings().import_resolver.load_file_contents(&path)?;240 v.insert(241 path.clone(),242 FileData::new_string(243 std::str::from_utf8(&data)244 .map_err(|_| ImportBadFileUtf8(path.clone()))?245 .into(),246 ),247 )248 .1249 }250 };251 if let Some(str) = &file.string {252 return Ok(str.clone());253 }254 if file.string.is_none() {255 file.string = Some(256 file.bytes257 .as_ref()258 .expect("either string or bytes should be set")259 .clone()260 .cast_str()261 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?,262 );263 }264 Ok(file.string.as_ref().expect("just set").clone())265 }266 pub fn import_bin(&self, path: PathBuf) -> Result<IBytes> {267 let mut data = self.data_mut();268 let mut file = data.files.raw_entry_mut().from_key(&path);269270 let file = match file {271 RawEntryMut::Occupied(ref mut d) => d.get_mut(),272 RawEntryMut::Vacant(v) => {273 let data = self.settings().import_resolver.load_file_contents(&path)?;274 v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))275 .1276 }277 };278 if let Some(str) = &file.bytes {279 return Ok(str.clone());280 }281 if file.bytes.is_none() {282 file.bytes = Some(283 file.string284 .as_ref()285 .expect("either string or bytes should be set")286 .clone()287 .cast_bytes(),288 );289 }290 Ok(file.bytes.as_ref().expect("just set").clone())291 }292 pub fn import(&self, path: PathBuf) -> Result<Val> {293 let mut data = self.data_mut();294 let mut file = data.files.raw_entry_mut().from_key(&path);295296 let file = match file {297 RawEntryMut::Occupied(ref mut d) => d.get_mut(),298 RawEntryMut::Vacant(v) => {299 let data = self.settings().import_resolver.load_file_contents(&path)?;300 v.insert(301 path.clone(),302 FileData::new_string(303 std::str::from_utf8(&data)304 .map_err(|_| ImportBadFileUtf8(path.clone()))?305 .into(),306 ),307 )308 .1309 }310 };311 if let Some(val) = &file.evaluated {312 return Ok(val.clone());313 }314 if file.string.is_none() {315 file.string = Some(316 std::str::from_utf8(317 file.bytes318 .as_ref()319 .expect("either string or bytes should be set"),320 )321 .map_err(|_| ImportBadFileUtf8(path.clone()))?322 .into(),323 );324 }325 let code = file.string.as_ref().expect("just set");326 let file_name = Source::new(path.clone()).expect("resolver should return correct name");327 if file.parsed.is_none() {328 file.parsed = Some(329 jrsonnet_parser::parse(330 code,331 &ParserSettings {332 file_name: file_name.clone(),333 },334 )335 .map_err(|e| ImportSyntaxError {336 path: file_name,337 source_code: code.clone(),338 error: Box::new(e),339 })?,340 );341 }342 let parsed = file.parsed.as_ref().expect("just set").clone();343 if file.evaluating {344 throw!(InfiniteRecursionDetected)345 }346 file.evaluating = true;347 // Dropping file here, as it borrows data, which may be used in evaluation348 drop(data);349 let res = evaluate(self.clone(), self.create_default_context(), &parsed);350351 let mut data = self.data_mut();352 let mut file = data.files.raw_entry_mut().from_key(&path);353354 let file = match file {355 RawEntryMut::Occupied(ref mut d) => d.get_mut(),356 RawEntryMut::Vacant(_) => unreachable!("this file was just here!"),357 };358 file.evaluating = false;359 match res {360 Ok(v) => {361 file.evaluated = Some(v.clone());362 Ok(v)363 }364 Err(e) => Err(e),365 }366 }367368 pub fn get_source(&self, name: Source) -> Option<String> {369 let data = self.data();370 match name.repr() {371 Ok(real) => data372 .files373 .get(real)374 .and_then(|f| f.string.as_ref())375 .map(ToString::to_string),376 Err(e) => data.volatile_files.get(e).map(ToOwned::to_owned),377 }378 }379 pub fn map_source_locations(&self, file: Source, locs: &[u32]) -> Vec<CodeLocation> {380 offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)381 }382 pub fn map_from_source_location(383 &self,384 file: Source,385 line: usize,386 column: usize,387 ) -> Option<usize> {388 location_to_offset(389 &self.get_source(file).expect("file not found"),390 line,391 column,392 )393 }394 /// Adds standard library global variable (std) to this evaluator395 pub fn with_stdlib(&self) -> &Self {396 let val = evaluate(397 self.clone(),398 self.create_default_context(),399 &stdlib::get_parsed_stdlib(),400 )401 .expect("std should not fail");402 self.settings_mut().globals.insert("std".into(), val);403 self404 }405406 /// Creates context with all passed global variables407 pub fn create_default_context(&self) -> Context {408 let globals = &self.settings().globals;409 let mut new_bindings = GcHashMap::with_capacity(globals.len());410 for (name, value) in globals.iter() {411 new_bindings.insert(name.clone(), Thunk::evaluated(value.clone()));412 }413 Context::new().extend(new_bindings, None, None, None)414 }415416 /// Executes code creating a new stack frame417 pub fn push<T>(418 &self,419 e: CallLocation,420 frame_desc: impl FnOnce() -> String,421 f: impl FnOnce() -> Result<T>,422 ) -> Result<T> {423 {424 let mut data = self.data_mut();425 let stack_depth = &mut data.stack_depth;426 if *stack_depth > self.max_stack() {427 // Error creation uses data, so i drop guard here428 drop(data);429 throw!(StackOverflow);430 }431 *stack_depth += 1;432 }433 let result = f();434 {435 let mut data = self.data_mut();436 data.stack_depth -= 1;437 data.stack_generation += 1;438 }439 if let Err(mut err) = result {440 err.trace_mut().0.push(StackTraceElement {441 location: e.0.cloned(),442 desc: frame_desc(),443 });444 return Err(err);445 }446 result447 }448449 /// Executes code creating a new stack frame450 pub fn push_val(451 &self,452 e: &ExprLocation,453 frame_desc: impl FnOnce() -> String,454 f: impl FnOnce() -> Result<Val>,455 ) -> Result<Val> {456 {457 let mut data = self.data_mut();458 let stack_depth = &mut data.stack_depth;459 if *stack_depth > self.max_stack() {460 // Error creation uses data, so i drop guard here461 drop(data);462 throw!(StackOverflow);463 }464 *stack_depth += 1;465 }466 let mut result = f();467 {468 let mut data = self.data_mut();469 data.stack_depth -= 1;470 data.stack_generation += 1;471 result = data472 .breakpoints473 .insert(data.stack_depth, data.stack_generation, e, result);474 }475 if let Err(mut err) = result {476 err.trace_mut().0.push(StackTraceElement {477 location: Some(e.clone()),478 desc: frame_desc(),479 });480 return Err(err);481 }482 result483 }484 /// Executes code creating a new stack frame485 pub fn push_description<T>(486 &self,487 frame_desc: impl FnOnce() -> String,488 f: impl FnOnce() -> Result<T>,489 ) -> Result<T> {490 {491 let mut data = self.data_mut();492 let stack_depth = &mut data.stack_depth;493 if *stack_depth > self.max_stack() {494 // Error creation uses data, so i drop guard here495 drop(data);496 throw!(StackOverflow);497 }498 *stack_depth += 1;499 }500 let result = f();501 {502 let mut data = self.data_mut();503 data.stack_depth -= 1;504 data.stack_generation += 1;505 }506 if let Err(mut err) = result {507 err.trace_mut().0.push(StackTraceElement {508 location: None,509 desc: frame_desc(),510 });511 return Err(err);512 }513 result514 }515516 /// # Panics517 /// In case of formatting failure518 pub fn stringify_err(&self, e: &LocError) -> String {519 let mut out = String::new();520 self.settings()521 .trace_format522 .write_trace(&mut out, self, e)523 .unwrap();524 out525 }526527 pub fn manifest(&self, val: Val) -> Result<IStr> {528 self.push_description(529 || "manifestification".to_string(),530 || val.manifest(self.clone(), &self.manifest_format()),531 )532 }533 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {534 val.manifest_multi(self.clone(), &self.manifest_format())535 }536 pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {537 val.manifest_stream(self.clone(), &self.manifest_format())538 }539540 /// If passed value is function then call with set TLA541 pub fn with_tla(&self, val: Val) -> Result<Val> {542 Ok(match val {543 Val::Func(func) => self.push_description(544 || "during TLA call".to_owned(),545 || {546 func.evaluate(547 self.clone(),548 self.create_default_context(),549 CallLocation::native(),550 &self.settings().tla_vars,551 true,552 )553 },554 )?,555 v => v,556 })557 }558}559560/// Internals561impl State {562 fn data(&self) -> Ref<EvaluationData> {563 self.0.data.borrow()564 }565 fn data_mut(&self) -> RefMut<EvaluationData> {566 self.0.data.borrow_mut()567 }568 pub fn settings(&self) -> Ref<EvaluationSettings> {569 self.0.settings.borrow()570 }571 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {572 self.0.settings.borrow_mut()573 }574}575576/// Raw methods evaluate passed values but don't perform TLA execution577impl State {578 /// Parses and evaluates the given snippet579 pub fn evaluate_snippet(&self, name: String, code: String) -> Result<Val> {580 let source = Source::new_virtual(Cow::Owned(name.clone()));581 let parsed = jrsonnet_parser::parse(582 &code,583 &ParserSettings {584 file_name: source.clone(),585 },586 )587 .map_err(|e| ImportSyntaxError {588 path: source,589 source_code: code.clone().into(),590 error: Box::new(e),591 })?;592 self.data_mut().volatile_files.insert(name, code);593 evaluate(self.clone(), self.create_default_context(), &parsed)594 }595}596597/// Settings utilities598impl State {599 pub fn add_ext_var(&self, name: IStr, value: Val) {600 self.settings_mut()601 .ext_vars602 .insert(name, TlaArg::Val(value));603 }604 pub fn add_ext_str(&self, name: IStr, value: IStr) {605 self.settings_mut()606 .ext_vars607 .insert(name, TlaArg::String(value));608 }609 pub fn add_ext_code(&self, name: &str, code: String) -> Result<()> {610 let source_name = format!("<extvar:{}>", name);611 let source = Source::new_virtual(Cow::Owned(source_name.clone()));612 let parsed = jrsonnet_parser::parse(613 &code,614 &ParserSettings {615 file_name: source.clone(),616 },617 )618 .map_err(|e| ImportSyntaxError {619 path: source,620 source_code: code.clone().into(),621 error: Box::new(e),622 })?;623 self.data_mut().volatile_files.insert(source_name, code);624 self.settings_mut()625 .ext_vars626 .insert(name.into(), TlaArg::Code(parsed));627 Ok(())628 }629630 pub fn add_tla(&self, name: IStr, value: Val) {631 self.settings_mut()632 .tla_vars633 .insert(name, TlaArg::Val(value));634 }635 pub fn add_tla_str(&self, name: IStr, value: IStr) {636 self.settings_mut()637 .tla_vars638 .insert(name, TlaArg::String(value));639 }640 pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {641 let source_name = format!("<top-level-arg:{}>", name);642 let source = Source::new_virtual(Cow::Owned(source_name.clone()));643 let parsed = jrsonnet_parser::parse(644 code,645 &ParserSettings {646 file_name: source.clone(),647 },648 )649 .map_err(|e| ImportSyntaxError {650 path: source,651 source_code: code.into(),652 error: Box::new(e),653 })?;654 self.data_mut()655 .volatile_files656 .insert(source_name, code.to_owned());657 self.settings_mut()658 .tla_vars659 .insert(name, TlaArg::Code(parsed));660 Ok(())661 }662663 pub fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {664 self.settings()665 .import_resolver666 .resolve_file(from, path.as_ref())667 }668669 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {670 Ref::map(self.settings(), |s| &*s.import_resolver)671 }672 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {673 self.settings_mut().import_resolver = resolver;674 }675676 pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {677 self.settings_mut().ext_natives.insert(name, cb);678 }679680 pub fn manifest_format(&self) -> ManifestFormat {681 self.settings().manifest_format.clone()682 }683 pub fn set_manifest_format(&self, format: ManifestFormat) {684 self.settings_mut().manifest_format = format;685 }686687 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {688 Ref::map(self.settings(), |s| &*s.trace_format)689 }690 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {691 self.settings_mut().trace_format = format;692 }693694 pub fn max_trace(&self) -> usize {695 self.settings().max_trace696 }697 pub fn set_max_trace(&self, trace: usize) {698 self.settings_mut().max_trace = trace;699 }700701 pub fn max_stack(&self) -> usize {702 self.settings().max_stack703 }704 pub fn set_max_stack(&self, trace: usize) {705 self.settings_mut().max_stack = trace;706 }707}crates/jrsonnet-evaluator/src/stdlib/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/expr.rs
+++ /dev/null
@@ -1,28 +0,0 @@
-use std::borrow::Cow;
-
-use jrsonnet_parser::{LocExpr, ParserSettings, Source};
-
-thread_local! {
- /// To avoid parsing again when issued from the same thread
- #[allow(unreachable_code)]
- static PARSED_STDLIB: LocExpr = {
- #[cfg(feature = "serialized-stdlib")]
- {
- // Should not panic, stdlib.bincode is generated in build.rs
- return bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")))
- .unwrap();
- }
-
- jrsonnet_parser::parse(
- jrsonnet_stdlib::STDLIB_STR,
- &ParserSettings {
- file_name: Source::new_virtual(Cow::Borrowed("<std>")),
- },
- )
- .unwrap()
- }
-}
-
-pub fn get_parsed_stdlib() -> LocExpr {
- PARSED_STDLIB.with(Clone::clone)
-}
crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -1,33 +1,13 @@
// All builtins should return results
#![allow(clippy::unnecessary_wraps)]
-use std::collections::HashMap;
-
use format::{format_arr, format_obj};
-use jrsonnet_gcmodule::Cc;
-use jrsonnet_interner::{IBytes, IStr};
-use serde::Deserialize;
-use serde_yaml_with_quirks::DeserializingQuirks;
-
-use crate::{
- error::{Error::*, Result},
- function::{builtin::StaticBuiltin, ArgLike, CallLocation, FuncVal},
- operator::evaluate_mod_op,
- stdlib::manifest::{manifest_yaml_ex, ManifestYamlOptions},
- throw,
- typed::{Any, BoundedUsize, Either2, Either4, PositiveF64, Typed, VecVal, M1},
- val::{equals, primitive_equals, ArrValue, IndexableVal, Slice},
- Either, ObjValue, State, Val,
-};
-
-pub mod expr;
-pub use expr::*;
+use jrsonnet_interner::IStr;
-use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};
+use crate::{error::Result, function::CallLocation, State, Val};
pub mod format;
pub mod manifest;
-pub mod sort;
pub fn std_format(s: State, str: IStr, vals: Val) -> Result<String> {
s.push(
@@ -39,704 +19,6 @@
Val::Obj(obj) => format_obj(s.clone(), &str, &obj)?,
o => format_arr(s.clone(), &str, &[o])?,
})
- },
- )
-}
-
-pub fn std_slice(
- indexable: IndexableVal,
- index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
- end: Option<BoundedUsize<0, { i32::MAX as usize }>>,
- step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
-) -> Result<Val> {
- match &indexable {
- IndexableVal::Str(s) => {
- let index = index.as_deref().copied().unwrap_or(0);
- let end = end.as_deref().copied().unwrap_or(usize::MAX);
- let step = step.as_deref().copied().unwrap_or(1);
-
- if index >= end {
- return Ok(Val::Str("".into()));
- }
-
- Ok(Val::Str(
- (s.chars()
- .skip(index)
- .take(end - index)
- .step_by(step)
- .collect::<String>())
- .into(),
- ))
- }
- IndexableVal::Arr(arr) => {
- let index = index.as_deref().copied().unwrap_or(0);
- let end = end.as_deref().copied().unwrap_or(usize::MAX).min(arr.len());
- let step = step.as_deref().copied().unwrap_or(1);
-
- if index >= end {
- return Ok(Val::Arr(ArrValue::new_eager()));
- }
-
- Ok(Val::Arr(ArrValue::Slice(Box::new(Slice {
- inner: arr.clone(),
- from: index as u32,
- to: end as u32,
- step: step as u32,
- }))))
- }
- }
-}
-
-type BuiltinsType = HashMap<IStr, &'static dyn StaticBuiltin>;
-
-thread_local! {
- pub static BUILTINS: BuiltinsType = {
- [
- ("length".into(), builtin_length::INST),
- ("type".into(), builtin_type::INST),
- ("makeArray".into(), builtin_make_array::INST),
- ("codepoint".into(), builtin_codepoint::INST),
- ("objectFieldsEx".into(), builtin_object_fields_ex::INST),
- ("objectHasEx".into(), builtin_object_has_ex::INST),
- ("slice".into(), builtin_slice::INST),
- ("substr".into(), builtin_substr::INST),
- ("primitiveEquals".into(), builtin_primitive_equals::INST),
- ("equals".into(), builtin_equals::INST),
- ("modulo".into(), builtin_modulo::INST),
- ("mod".into(), builtin_mod::INST),
- ("floor".into(), builtin_floor::INST),
- ("ceil".into(), builtin_ceil::INST),
- ("log".into(), builtin_log::INST),
- ("pow".into(), builtin_pow::INST),
- ("sqrt".into(), builtin_sqrt::INST),
- ("sin".into(), builtin_sin::INST),
- ("cos".into(), builtin_cos::INST),
- ("tan".into(), builtin_tan::INST),
- ("asin".into(), builtin_asin::INST),
- ("acos".into(), builtin_acos::INST),
- ("atan".into(), builtin_atan::INST),
- ("exp".into(), builtin_exp::INST),
- ("mantissa".into(), builtin_mantissa::INST),
- ("exponent".into(), builtin_exponent::INST),
- ("extVar".into(), builtin_ext_var::INST),
- ("native".into(), builtin_native::INST),
- ("filter".into(), builtin_filter::INST),
- ("map".into(), builtin_map::INST),
- ("flatMap".into(), builtin_flatmap::INST),
- ("foldl".into(), builtin_foldl::INST),
- ("foldr".into(), builtin_foldr::INST),
- ("sort".into(), builtin_sort::INST),
- ("format".into(), builtin_format::INST),
- ("range".into(), builtin_range::INST),
- ("char".into(), builtin_char::INST),
- ("encodeUTF8".into(), builtin_encode_utf8::INST),
- ("decodeUTF8".into(), builtin_decode_utf8::INST),
- ("md5".into(), builtin_md5::INST),
- ("base64".into(), builtin_base64::INST),
- ("base64DecodeBytes".into(), builtin_base64_decode_bytes::INST),
- ("base64Decode".into(), builtin_base64_decode::INST),
- ("trace".into(), builtin_trace::INST),
- ("join".into(), builtin_join::INST),
- ("escapeStringJson".into(), builtin_escape_string_json::INST),
- ("manifestJsonEx".into(), builtin_manifest_json_ex::INST),
- ("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),
- ("reverse".into(), builtin_reverse::INST),
- ("strReplace".into(), builtin_str_replace::INST),
- ("splitLimit".into(), builtin_splitlimit::INST),
- ("parseJson".into(), builtin_parse_json::INST),
- ("parseYaml".into(), builtin_parse_yaml::INST),
- ("asciiUpper".into(), builtin_ascii_upper::INST),
- ("asciiLower".into(), builtin_ascii_lower::INST),
- ("member".into(), builtin_member::INST),
- ("count".into(), builtin_count::INST),
- ("any".into(), builtin_any::INST),
- ("all".into(), builtin_all::INST),
- ].iter().cloned().collect()
- };
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {
- use Either4::*;
- Ok(match x {
- A(x) => x.chars().count(),
- B(x) => x.len(),
- C(x) => x.len(),
- D(f) => f.params_len(),
- })
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_type(x: Any) -> Result<IStr> {
- Ok(x.0.value_type().name().into())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_make_array(s: State, sz: usize, func: FuncVal) -> Result<VecVal> {
- let mut out = Vec::with_capacity(sz);
- for i in 0..sz {
- out.push(func.evaluate_simple(s.clone(), &(i as f64,))?);
- }
- Ok(VecVal(Cc::new(out)))
-}
-
-#[jrsonnet_macros::builtin]
-const fn builtin_codepoint(str: char) -> Result<u32> {
- Ok(str as u32)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_object_fields_ex(
- obj: ObjValue,
- inc_hidden: bool,
- #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
-) -> Result<VecVal> {
- #[cfg(feature = "exp-preserve-order")]
- let preserve_order = preserve_order.unwrap_or(false);
- let out = obj.fields_ex(
- inc_hidden,
- #[cfg(feature = "exp-preserve-order")]
- preserve_order,
- );
- Ok(VecVal(Cc::new(
- out.into_iter().map(Val::Str).collect::<Vec<_>>(),
- )))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_object_has_ex(obj: ObjValue, f: IStr, inc_hidden: bool) -> Result<bool> {
- Ok(obj.has_field_ex(f, inc_hidden))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_parse_json(st: State, s: IStr) -> Result<Any> {
- use serde_json::Value;
- let value: Value = serde_json::from_str(&s)
- .map_err(|e| RuntimeError(format!("failed to parse json: {}", e).into()))?;
- Ok(Any(Value::into_untyped(value, st)?))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_parse_yaml(st: State, s: IStr) -> Result<Any> {
- use serde_json::Value;
- let value = serde_yaml_with_quirks::Deserializer::from_str_with_quirks(
- &s,
- DeserializingQuirks { old_octals: true },
- );
- let mut out = vec![];
- for item in value {
- let value = Value::deserialize(item)
- .map_err(|e| RuntimeError(format!("failed to parse yaml: {}", e).into()))?;
- let val = Value::into_untyped(value, st.clone())?;
- out.push(val);
- }
- Ok(Any(if out.is_empty() {
- Val::Null
- } else if out.len() == 1 {
- out.into_iter().next().unwrap()
- } else {
- Val::Arr(out.into())
- }))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_slice(
- indexable: IndexableVal,
- index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
- end: Option<BoundedUsize<0, { i32::MAX as usize }>>,
- step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
-) -> Result<Any> {
- std_slice(indexable, index, end, step).map(Any)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {
- Ok(str.chars().skip(from as usize).take(len as usize).collect())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_primitive_equals(a: Any, b: Any) -> Result<bool> {
- primitive_equals(&a.0, &b.0)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_equals(s: State, a: Any, b: Any) -> Result<bool> {
- equals(s, &a.0, &b.0)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_modulo(a: f64, b: f64) -> Result<f64> {
- Ok(a % b)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_mod(s: State, a: Either![f64, IStr], b: Any) -> Result<Any> {
- use Either2::*;
- Ok(Any(evaluate_mod_op(
- s,
- &match a {
- A(v) => Val::Num(v),
- B(s) => Val::Str(s),
- },
- &b.0,
- )?))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_floor(x: f64) -> Result<f64> {
- Ok(x.floor())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_ceil(x: f64) -> Result<f64> {
- Ok(x.ceil())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_log(n: f64) -> Result<f64> {
- Ok(n.ln())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_pow(x: f64, n: f64) -> Result<f64> {
- Ok(x.powf(n))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_sqrt(x: PositiveF64) -> Result<f64> {
- Ok(x.0.sqrt())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_sin(x: f64) -> Result<f64> {
- Ok(x.sin())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_cos(x: f64) -> Result<f64> {
- Ok(x.cos())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_tan(x: f64) -> Result<f64> {
- Ok(x.tan())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_asin(x: f64) -> Result<f64> {
- Ok(x.asin())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_acos(x: f64) -> Result<f64> {
- Ok(x.acos())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_atan(x: f64) -> Result<f64> {
- Ok(x.atan())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_exp(x: f64) -> Result<f64> {
- Ok(x.exp())
-}
-
-fn frexp(s: f64) -> (f64, i16) {
- if 0.0 == s {
- (s, 0)
- } else {
- let lg = s.abs().log2();
- let x = (lg - lg.floor() - 1.0).exp2();
- let exp = lg.floor() + 1.0;
- (s.signum() * x, exp as i16)
- }
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_mantissa(x: f64) -> Result<f64> {
- Ok(frexp(x).0)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_exponent(x: f64) -> Result<i16> {
- Ok(frexp(x).1)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_ext_var(s: State, x: IStr) -> Result<Any> {
- let ctx = s.create_default_context();
- Ok(Any(s
- .clone()
- .settings()
- .ext_vars
- .get(&x)
- .cloned()
- .ok_or(UndefinedExternalVariable(x))?
- .evaluate_arg(s.clone(), ctx, true)?
- .evaluate(s)?))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_native(s: State, name: IStr) -> Result<Any> {
- Ok(Any(s
- .settings()
- .ext_natives
- .get(&name)
- .cloned()
- .map_or(Val::Null, |v| {
- Val::Func(FuncVal::Builtin(v.clone()))
- })))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_filter(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {
- arr.filter(s.clone(), |val| {
- bool::from_untyped(
- func.evaluate_simple(s.clone(), &(Any(val.clone()),))?,
- s.clone(),
- )
- })
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_map(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {
- arr.map(s.clone(), |val| {
- func.evaluate_simple(s.clone(), &(Any(val),))
- })
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_flatmap(s: State, func: FuncVal, arr: IndexableVal) -> Result<IndexableVal> {
- match arr {
- IndexableVal::Str(str) => {
- let mut out = String::new();
- for c in str.chars() {
- match func.evaluate_simple(s.clone(), &(c.to_string(),))? {
- Val::Str(o) => out.push_str(&o),
- Val::Null => continue,
- _ => throw!(RuntimeError(
- "in std.join all items should be strings".into()
- )),
- };
- }
- Ok(IndexableVal::Str(out.into()))
- }
- IndexableVal::Arr(a) => {
- let mut out = Vec::new();
- for el in a.iter(s.clone()) {
- let el = el?;
- match func.evaluate_simple(s.clone(), &(Any(el),))? {
- Val::Arr(o) => {
- for oe in o.iter(s.clone()) {
- out.push(oe?);
- }
- }
- Val::Null => continue,
- _ => throw!(RuntimeError(
- "in std.join all items should be arrays".into()
- )),
- };
- }
- Ok(IndexableVal::Arr(out.into()))
- }
- }
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_foldl(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {
- let mut acc = init.0;
- for i in arr.iter(s.clone()) {
- acc = func.evaluate_simple(s.clone(), &(Any(acc), Any(i?)))?;
- }
- Ok(Any(acc))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_foldr(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {
- let mut acc = init.0;
- for i in arr.iter(s.clone()).rev() {
- acc = func.evaluate_simple(s.clone(), &(Any(i?), Any(acc)))?;
- }
- Ok(Any(acc))
-}
-
-#[jrsonnet_macros::builtin]
-#[allow(non_snake_case)]
-fn builtin_sort(s: State, arr: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {
- if arr.len() <= 1 {
- return Ok(arr);
- }
- Ok(ArrValue::Eager(sort::sort(
- s.clone(),
- arr.evaluated(s)?,
- keyF.unwrap_or_else(FuncVal::identity),
- )?))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_format(s: State, str: IStr, vals: Any) -> Result<String> {
- std_format(s, str, vals.0)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {
- if to < from {
- return Ok(ArrValue::new_eager());
- }
- Ok(ArrValue::new_range(from, to))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_char(n: u32) -> Result<char> {
- Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_encode_utf8(str: IStr) -> Result<IBytes> {
- Ok(str.cast_bytes())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_decode_utf8(arr: IBytes) -> Result<IStr> {
- Ok(arr
- .cast_str()
- .ok_or_else(|| RuntimeError("bad utf8".into()))?)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_md5(str: IStr) -> Result<String> {
- Ok(format!("{:x}", md5::compute(&str.as_bytes())))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_trace(s: State, loc: CallLocation, str: IStr, rest: Any) -> Result<Any> {
- eprint!("TRACE:");
- if let Some(loc) = loc.0 {
- let locs = s.map_source_locations(loc.0.clone(), &[loc.1]);
- eprint!(" {}:{}", loc.0.short_display(), locs[0].line);
- }
- eprintln!(" {}", str);
- Ok(rest) as Result<Any>
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_base64(input: Either![IBytes, IStr]) -> Result<String> {
- use Either2::*;
- Ok(match input {
- A(a) => base64::encode(a.as_slice()),
- B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),
- })
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_base64_decode_bytes(input: IStr) -> Result<IBytes> {
- Ok(base64::decode(&input.as_bytes())
- .map_err(|_| RuntimeError("bad base64".into()))?
- .as_slice()
- .into())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_base64_decode(input: IStr) -> Result<String> {
- let bytes = base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?;
- Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_join(s: State, sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {
- Ok(match sep {
- IndexableVal::Arr(joiner_items) => {
- let mut out = Vec::new();
-
- let mut first = true;
- for item in arr.iter(s.clone()) {
- let item = item?.clone();
- if let Val::Arr(items) = item {
- if !first {
- out.reserve(joiner_items.len());
- // TODO: extend
- for item in joiner_items.iter(s.clone()) {
- out.push(item?);
- }
- }
- first = false;
- out.reserve(items.len());
- for item in items.iter(s.clone()) {
- out.push(item?);
- }
- } else if matches!(item, Val::Null) {
- continue;
- } else {
- throw!(RuntimeError(
- "in std.join all items should be arrays".into()
- ));
- }
- }
-
- IndexableVal::Arr(out.into())
- }
- IndexableVal::Str(sep) => {
- let mut out = String::new();
-
- let mut first = true;
- for item in arr.iter(s) {
- let item = item?.clone();
- if let Val::Str(item) = item {
- if !first {
- out += &sep;
- }
- first = false;
- out += &item;
- } else if matches!(item, Val::Null) {
- continue;
- } else {
- throw!(RuntimeError(
- "in std.join all items should be strings".into()
- ));
- }
- }
-
- IndexableVal::Str(out.into())
- }
- })
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_escape_string_json(str_: IStr) -> Result<String> {
- Ok(escape_string_json(&str_))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_manifest_json_ex(
- s: State,
- value: Any,
- indent: IStr,
- newline: Option<IStr>,
- key_val_sep: Option<IStr>,
- #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
-) -> Result<String> {
- let newline = newline.as_deref().unwrap_or("\n");
- let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");
- manifest_json_ex(
- s,
- &value.0,
- &ManifestJsonOptions {
- padding: &indent,
- mtype: ManifestType::Std,
- newline,
- key_val_sep,
- #[cfg(feature = "exp-preserve-order")]
- preserve_order: preserve_order.unwrap_or(false),
- },
- )
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_manifest_yaml_doc(
- s: State,
- value: Any,
- indent_array_in_object: Option<bool>,
- quote_keys: Option<bool>,
- #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
-) -> Result<String> {
- manifest_yaml_ex(
- s,
- &value.0,
- &ManifestYamlOptions {
- padding: " ",
- arr_element_padding: if indent_array_in_object.unwrap_or(false) {
- " "
- } else {
- ""
- },
- quote_keys: quote_keys.unwrap_or(true),
- #[cfg(feature = "exp-preserve-order")]
- preserve_order: preserve_order.unwrap_or(false),
},
)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {
- Ok(value.reversed())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {
- Ok(str.replace(&from as &str, &to as &str))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {
- use Either2::*;
- Ok(VecVal(Cc::new(match maxsplits {
- A(n) => str
- .splitn(n + 1, &c as &str)
- .map(|s| Val::Str(s.into()))
- .collect(),
- B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),
- })))
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_ascii_upper(str: IStr) -> Result<String> {
- Ok(str.to_ascii_uppercase())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_ascii_lower(str: IStr) -> Result<String> {
- Ok(str.to_ascii_lowercase())
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_member(s: State, arr: IndexableVal, x: Any) -> Result<bool> {
- match arr {
- IndexableVal::Str(str) => {
- let x: IStr = IStr::from_untyped(x.0, s)?;
- Ok(!x.is_empty() && str.contains(&*x))
- }
- IndexableVal::Arr(a) => {
- for item in a.iter(s.clone()) {
- let item = item?;
- if equals(s.clone(), &item, &x.0)? {
- return Ok(true);
- }
- }
- Ok(false)
- }
- }
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_count(s: State, arr: Vec<Any>, v: Any) -> Result<usize> {
- let mut count = 0;
- for item in &arr {
- if equals(s.clone(), &item.0, &v.0)? {
- count += 1;
- }
- }
- Ok(count)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_any(s: State, arr: ArrValue) -> Result<bool> {
- for v in arr.iter(s.clone()) {
- let v = bool::from_untyped(v?, s.clone())?;
- if v {
- return Ok(true);
- }
- }
- Ok(false)
-}
-
-#[jrsonnet_macros::builtin]
-fn builtin_all(s: State, arr: ArrValue) -> Result<bool> {
- for v in arr.iter(s.clone()) {
- let v = bool::from_untyped(v?, s.clone())?;
- if !v {
- return Ok(false);
- }
- }
- Ok(true)
}
crates/jrsonnet-evaluator/src/stdlib/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/sort.rs
+++ /dev/null
@@ -1,110 +0,0 @@
-use jrsonnet_gcmodule::{Cc, Trace};
-
-use crate::{
- error::{Error, LocError, Result},
- function::FuncVal,
- throw,
- typed::Any,
- State, Val,
-};
-
-#[derive(Debug, Clone, thiserror::Error, Trace)]
-pub enum SortError {
- #[error("sort key should be string or number")]
- SortKeyShouldBeStringOrNumber,
- #[error("sort elements should have equal types")]
- SortElementsShouldHaveEqualType,
-}
-
-impl From<SortError> for LocError {
- fn from(s: SortError) -> Self {
- Self::new(Error::Sort(s))
- }
-}
-
-#[derive(Copy, Clone)]
-enum SortKeyType {
- Number,
- String,
- Unknown,
-}
-
-#[derive(PartialEq)]
-struct NonNaNf64(f64);
-impl PartialOrd for NonNaNf64 {
- fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
- self.0.partial_cmp(&other.0)
- }
-}
-impl Eq for NonNaNf64 {}
-impl Ord for NonNaNf64 {
- fn cmp(&self, other: &Self) -> std::cmp::Ordering {
- self.partial_cmp(other).expect("non nan")
- }
-}
-
-fn get_sort_type<T>(
- values: &mut Vec<T>,
- key_getter: impl Fn(&mut T) -> &mut Val,
-) -> Result<SortKeyType> {
- let mut sort_type = SortKeyType::Unknown;
- for i in values.iter_mut() {
- let i = key_getter(i);
- match (i, sort_type) {
- (Val::Str(_), SortKeyType::Unknown) => sort_type = SortKeyType::String,
- (Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,
- (Val::Str(_), SortKeyType::String) | (Val::Num(_), SortKeyType::Number) => {}
- (Val::Str(_) | Val::Num(_), _) => {
- throw!(SortError::SortElementsShouldHaveEqualType)
- }
- _ => throw!(SortError::SortKeyShouldBeStringOrNumber),
- }
- }
- Ok(sort_type)
-}
-
-/// * `key_getter` - None, if identity sort required
-pub fn sort(s: State, values: Cc<Vec<Val>>, key_getter: FuncVal) -> Result<Cc<Vec<Val>>> {
- if values.len() <= 1 {
- return Ok(values);
- }
- if key_getter.is_identity() {
- // Fast path, identity key getter
- let mut values = (*values).clone();
- let sort_type = get_sort_type(&mut values, |k| k)?;
- match sort_type {
- SortKeyType::Number => values.sort_unstable_by_key(|v| match v {
- Val::Num(n) => NonNaNf64(*n),
- _ => unreachable!(),
- }),
- SortKeyType::String => values.sort_unstable_by_key(|v| match v {
- Val::Str(s) => s.clone(),
- _ => unreachable!(),
- }),
- SortKeyType::Unknown => unreachable!(),
- };
- Ok(Cc::new(values))
- } else {
- // Slow path, user provided key getter
- let mut vk = Vec::with_capacity(values.len());
- for value in values.iter() {
- vk.push((
- value.clone(),
- key_getter.evaluate_simple(s.clone(), &(Any(value.clone()),))?,
- ));
- }
- let sort_type = get_sort_type(&mut vk, |v| &mut v.1)?;
- match sort_type {
- SortKeyType::Number => vk.sort_by_key(|v| match v.1 {
- Val::Num(n) => NonNaNf64(n),
- _ => unreachable!(),
- }),
- SortKeyType::String => vk.sort_by_key(|v| match &v.1 {
- Val::Str(s) => s.clone(),
- _ => unreachable!(),
- }),
- SortKeyType::Unknown => unreachable!(),
- };
- Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
- }
-}