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

difftreelog

refactor(evaluator)! remove standard library

Yaroslav Bolyukin2022-07-23parent: #34a152f.patch.diff
in: master
Implementation will be moved to jrsonnet-stdlib crate

BREAKING CHANGE: `State::with_stdlib` was removed

9 files changed

modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
7edition = "2021"7edition = "2021"
88
9[features]9[features]
10default = ["serialized-stdlib", "explaining-traces", "friendly-errors"]10default = ["explaining-traces", "friendly-errors"]
11# Serializes standard library AST instead of parsing them every run
12serialized-stdlib = ["bincode", "jrsonnet-parser/serde"]
13# Rustc-like trace visualization11# Rustc-like trace visualization
14explaining-traces = ["annotate-snippets"]12explaining-traces = ["annotate-snippets"]
15# Allows library authors to throw custom errors13# Allows library authors to throw custom errors
23exp-serde-preserve-order = ["serde_json/preserve_order"]21exp-serde-preserve-order = ["serde_json/preserve_order"]
24# Implements field destructuring22# Implements field destructuring
25exp-destruct = ["jrsonnet-parser/exp-destruct"]23exp-destruct = ["jrsonnet-parser/exp-destruct"]
24# Provide Typed for conversions to/from serde_json::Value type
25serde_json = ["dep:serde_json"]
2626
27[dependencies]27[dependencies]
28jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }28jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }
29jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.2" }29jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.2" }
30jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.2" }
31jrsonnet-types = { path = "../jrsonnet-types", version = "0.4.2" }30jrsonnet-types = { path = "../jrsonnet-types", version = "0.4.2" }
32jrsonnet-macros = { path = "../jrsonnet-macros", version = "0.4.2" }31jrsonnet-macros = { path = "../jrsonnet-macros", version = "0.4.2" }
33jrsonnet-gcmodule = { version = "0.3.4" }32jrsonnet-gcmodule = { version = "0.3.4" }
36hashbrown = "0.12.1"35hashbrown = "0.12.1"
37static_assertions = "1.1"36static_assertions = "1.1"
3837
39md5 = "0.7.0"
40base64 = "0.13.0"
41rustc-hash = "1.1"38rustc-hash = "1.1"
4239
43thiserror = "1.0"40thiserror = "1.0"
4441
45serde = "1.0"42serde = "1.0"
43# Optional integration
46serde_json = "1.0"44serde_json = { version = "1.0.82", optional = true }
47serde_yaml_with_quirks = "0.8.24"
4845
49anyhow = { version = "1.0", optional = true }46anyhow = { version = "1.0", optional = true }
50# Friendly errors47# Friendly errors
54# Explaining traces51# Explaining traces
55annotate-snippets = { version = "0.9.1", features = ["color"], optional = true }52annotate-snippets = { version = "0.9.1", features = ["color"], optional = true }
56
57[build-dependencies]
58jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.2" }
59jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.2" }
60serde = "1.0"
61bincode = "1.3"
6253
deletedcrates/jrsonnet-evaluator/build.rsdiffbeforeafterboth

no changes

modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
7use thiserror::Error;7use thiserror::Error;
88
9use crate::{9use crate::{stdlib::format::FormatError, typed::TypeLocError};
10 stdlib::{format::FormatError, sort::SortError},
11 typed::TypeLocError,
12};
1310
14fn format_found(list: &[IStr], what: &str) -> String {11fn format_found(list: &[IStr], what: &str) -> String {
169 Format(#[from] FormatError),166 Format(#[from] FormatError),
170 #[error("type error: {0}")]167 #[error("type error: {0}")]
171 TypeError(TypeLocError),168 TypeError(TypeLocError),
172 #[error("sort error: {0}")]
173 Sort(#[from] SortError),
174
175 /// Thrown as error, as this is legacy feature, and error here
176 /// is acceptable for defeating object field cache
177 #[error("should not reach outside: std.thisFile")]
178 MagicThisFileUsed,
179169
180 #[cfg(feature = "anyhow-error")]170 #[cfg(feature = "anyhow-error")]
181 #[error(transparent)]171 #[error(transparent)]
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
13 error::Error::*,13 error::Error::*,
14 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},14 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
15 function::{CallLocation, FuncDesc, FuncVal},15 function::{CallLocation, FuncDesc, FuncVal},
16 stdlib::{std_slice, BUILTINS},
17 tb, throw,16 tb, throw,
18 typed::Typed,17 typed::Typed,
19 val::{ArrValue, CachedUnbound, Thunk, ThunkValue},18 val::{ArrValue, CachedUnbound, IndexableVal, Thunk, ThunkValue},
20 Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,19 Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,
21 Unbound, Val,20 Unbound, Val,
22};21};
417 Literal(LiteralType::This) => {416 Literal(LiteralType::This) => {
418 Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)417 Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
419 }418 }
420 Literal(LiteralType::Super) => Val::Obj(419 Literal(LiteralType::Super) => {
421 ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(420 Val::Obj(ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
422 ctx.this()421 ctx.this()
423 .clone()422 .clone()
424 .expect("if super exists - then this should to"),423 .expect("if super exists - then this should to"),
425 ),424 ))
426 ),425 }
427 Literal(LiteralType::Dollar) => {426 Literal(LiteralType::Dollar) => {
428 Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)427 Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)
429 }428 }
473 heap.into_iter().map(|(_, v)| v).collect()472 heap.into_iter().map(|(_, v)| v).collect()
474 ))473 ))
475 }474 }
476 Err(e) if matches!(e.error(), MagicThisFileUsed) => {
477 Ok(Val::Str(loc.0.full_path().into()))
478 }
479 Err(e) => Err(e),475 Err(e) => Err(e),
480 },476 },
481 )?,477 )?,
573 Function(params, body) => {569 Function(params, body) => {
574 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())570 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())
575 }571 }
576 Intrinsic(name) => Val::Func(FuncVal::StaticBuiltin(
577 BUILTINS
578 .with(|b| b.get(name).copied())
579 .ok_or_else(|| IntrinsicNotFound(name.clone()))?,
580 )),
581 IntrinsicThisFile => return Err(MagicThisFileUsed.into()),
582 IntrinsicId => Val::Func(FuncVal::identity()),
583 AssertExpr(assert, returned) => {572 AssertExpr(assert, returned) => {
584 evaluate_assert(s.clone(), ctx.clone(), assert)?;573 evaluate_assert(s.clone(), ctx.clone(), assert)?;
585 evaluate(s, ctx, returned)?574 evaluate(s, ctx, returned)?
635624
636 let start = parse_idx(loc, s.clone(), &ctx, &desc.start, "start")?;625 let start = parse_idx(loc, s.clone(), &ctx, &desc.start, "start")?;
637 let end = parse_idx(loc, s.clone(), &ctx, &desc.end, "end")?;626 let end = parse_idx(loc, s.clone(), &ctx, &desc.end, "end")?;
638 let step = parse_idx(loc, s, &ctx, &desc.step, "step")?;627 let step = parse_idx(loc, s.clone(), &ctx, &desc.step, "step")?;
639628
640 std_slice(indexable.into_indexable()?, start, end, step)?629 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?, s)?
641 }630 }
642 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {631 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {
643 let tmp = loc.clone().0;632 let tmp = loc.clone().0;
modifiedcrates/jrsonnet-evaluator/src/function/native.rsdiffbeforeafterboth
1use super::{arglike::ArgLike, CallLocation, FuncVal};1use super::{arglike::ArgLike, CallLocation, FuncVal};
2use crate::{error::Result, typed::Typed, State};2use crate::{error::Result, typed::Typed, Context, State};
33
4pub trait NativeDesc {4pub trait NativeDesc {
5 type Value;5 type Value;
19 Box::new(move |s: State, $($gen),*| {19 Box::new(move |s: State, $($gen),*| {
20 let val = val.evaluate(20 let val = val.evaluate(
21 s.clone(),21 s.clone(),
22 // This isn't intended to be used with ArgsDesc
22 s.create_default_context(),23 Context::default(),
23 CallLocation::native(),24 CallLocation::native(),
24 &($($gen,)*),25 &($($gen,)*),
25 true26 true
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
37mod integrations;37mod integrations;
38mod map;38mod map;
39mod obj;39mod obj;
40mod stdlib;40pub mod stdlib;
41pub mod trace;41pub mod trace;
42pub mod typed;42pub mod typed;
43pub mod val;43pub mod val;
4444
45use std::{45use std::{
46 any::Any,
46 borrow::Cow,47 borrow::Cow,
47 cell::{Ref, RefCell, RefMut},48 cell::{Ref, RefCell, RefMut},
48 collections::HashMap,49 collections::HashMap,
55pub use dynamic::*;56pub use dynamic::*;
56use error::{Error::*, LocError, Result, StackTraceElement};57use error::{Error::*, LocError, Result, StackTraceElement};
57pub use evaluate::*;58pub use evaluate::*;
58use function::{builtin::Builtin, CallLocation, TlaArg};59use function::{CallLocation, TlaArg};
59use gc::{GcHashMap, TraceBox};60use gc::{GcHashMap, TraceBox};
60use hashbrown::hash_map::RawEntryMut;61use hashbrown::hash_map::RawEntryMut;
61pub use import::*;62pub use import::*;
62use jrsonnet_gcmodule::{Cc, Trace};63use jrsonnet_gcmodule::{Cc, Trace};
63use jrsonnet_interner::IBytes;
64pub use jrsonnet_interner::IStr;64pub use jrsonnet_interner::{IBytes, IStr};
65pub use jrsonnet_parser as parser;65pub use jrsonnet_parser as parser;
66use jrsonnet_parser::*;66use jrsonnet_parser::*;
67pub use obj::*;67pub use obj::*;
98 }98 }
99}99}
100
101/// During import, this trait will be called to create initial context for file
102/// It may initialize global variables, stdlib for example
103pub trait ContextInitializer {
104 fn initialize(&self, state: State, for_file: Source) -> Context;
105
106 /// # Safety
107 ///
108 /// For use only in bindings, should not be used elsewhere.
109 /// Implementations which are not intended to be used in bindings
110 /// should panic on call to this method.
111 unsafe fn as_any(&self) -> &dyn Any;
112}
113
114/// Context initializer, which adds noth
115pub struct DummyContextInitializer;
116impl ContextInitializer for DummyContextInitializer {
117 fn initialize(&self, _state: State, _for_file: Source) -> Context {
118 Context::default()
119 }
120 unsafe fn as_any(&self) -> &dyn Any {
121 panic!("`as_any(&self)` is not supported by dummy initializer")
122 }
123}
100124
101pub struct EvaluationSettings {125pub struct EvaluationSettings {
102 /// Limits recursion by limiting the number of stack frames126 /// Limits recursion by limiting the number of stack frames
103 pub max_stack: usize,127 pub max_stack: usize,
104 /// Limits amount of stack trace items preserved128 /// Limits amount of stack trace items preserved
105 pub max_trace: usize,129 pub max_trace: usize,
106 /// Used for s`td.extVar`
107 pub ext_vars: HashMap<IStr, TlaArg>,
108 /// Used for ext.native
109 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,
110 /// TLA vars130 /// TLA vars
111 pub tla_vars: HashMap<IStr, TlaArg>,131 pub tla_vars: HashMap<IStr, TlaArg>,
112 /// Global variables are inserted in default context132 /// Context initializer, which will be used for imports and everything
133 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`
113 pub globals: HashMap<IStr, Val>,134 pub context_initializer: Box<dyn ContextInitializer>,
114 /// Used to resolve file locations/contents135 /// Used to resolve file locations/contents
115 pub import_resolver: Box<dyn ImportResolver>,136 pub import_resolver: Box<dyn ImportResolver>,
116 /// Used in manifestification functions137 /// Used in manifestification functions
123 Self {144 Self {
124 max_stack: 200,145 max_stack: 200,
125 max_trace: 20,146 max_trace: 20,
126 globals: HashMap::default(),147 context_initializer: Box::new(DummyContextInitializer),
127 ext_vars: HashMap::default(),
128 ext_natives: HashMap::default(),
129 tla_vars: HashMap::default(),148 tla_vars: HashMap::default(),
130 import_resolver: Box::new(DummyImportResolver),149 import_resolver: Box::new(DummyImportResolver),
131 manifest_format: ManifestFormat::Json {150 manifest_format: ManifestFormat::Json {
152171
153 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces172 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
154 files: GcHashMap<PathBuf, FileData>,173 files: GcHashMap<PathBuf, FileData>,
155 /// Contains tla arguments and others, which aren't needed to be obtained by name174 /// Contains tla arguments and others, which aren't needed to be obtained by name, however may be used for receiving source
175 /// TODO: look into nix approach, storing source code in `Source` object
156 volatile_files: GcHashMap<String, String>,176 volatile_files: GcHashMap<String, String>,
157}177}
158struct FileData {178struct FileData {
333 },353 },
334 )354 )
335 .map_err(|e| ImportSyntaxError {355 .map_err(|e| ImportSyntaxError {
336 path: file_name,356 path: file_name.clone(),
337 source_code: code.clone(),357 source_code: code.clone(),
338 error: Box::new(e),358 error: Box::new(e),
339 })?,359 })?,
348 drop(data);368 drop(data);
349 let res = evaluate(self.clone(), self.create_default_context(), &parsed);369 let res = evaluate(
370 self.clone(),
371 self.create_default_context(file_name),
372 &parsed,
373 );
350374
391 column,415 column,
392 )416 )
393 }417 }
394 /// Adds standard library global variable (std) to this evaluator
395 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 self
404 }
405418
406 /// Creates context with all passed global variables419 /// Creates context with all passed global variables
407 pub fn create_default_context(&self) -> Context {420 pub fn create_default_context(&self, source: Source) -> Context {
408 let globals = &self.settings().globals;421 let context_initializer = &self.settings().context_initializer;
409 let mut new_bindings = GcHashMap::with_capacity(globals.len());
410 for (name, value) in globals.iter() {422 context_initializer.initialize(self.clone(), source)
411 new_bindings.insert(name.clone(), Thunk::evaluated(value.clone()));
412 }
413 Context::new().extend(new_bindings, None, None, None)
414 }423 }
415424
416 /// Executes code creating a new stack frame425 /// Executes code creating a new stack frame
545 || {554 || {
546 func.evaluate(555 func.evaluate(
547 self.clone(),556 self.clone(),
548 self.create_default_context(),557 self.create_default_context(Source::new_virtual(Cow::Borrowed("<tla>"))),
549 CallLocation::native(),558 CallLocation::native(),
550 &self.settings().tla_vars,559 &self.settings().tla_vars,
551 true,560 true,
585 },594 },
586 )595 )
587 .map_err(|e| ImportSyntaxError {596 .map_err(|e| ImportSyntaxError {
588 path: source,597 path: source.clone(),
589 source_code: code.clone().into(),598 source_code: code.clone().into(),
590 error: Box::new(e),599 error: Box::new(e),
591 })?;600 })?;
592 self.data_mut().volatile_files.insert(name, code);601 self.data_mut().volatile_files.insert(name, code);
593 evaluate(self.clone(), self.create_default_context(), &parsed)602 evaluate(self.clone(), self.create_default_context(source), &parsed)
594 }603 }
595}604}
596605
597/// Settings utilities606/// Settings utilities
598impl State {607impl State {
599 pub fn add_ext_var(&self, name: IStr, value: Val) {
600 self.settings_mut()
601 .ext_vars
602 .insert(name, TlaArg::Val(value));
603 }
604 pub fn add_ext_str(&self, name: IStr, value: IStr) {
605 self.settings_mut()
606 .ext_vars
607 .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_vars
626 .insert(name.into(), TlaArg::Code(parsed));
627 Ok(())
628 }
629
630 pub fn add_tla(&self, name: IStr, value: Val) {608 pub fn add_tla(&self, name: IStr, value: Val) {
631 self.settings_mut()609 self.settings_mut()
673 self.settings_mut().import_resolver = resolver;651 self.settings_mut().import_resolver = resolver;
674 }652 }
675
676 pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {653 pub fn context_initializer(&self) -> Ref<dyn ContextInitializer> {
677 self.settings_mut().ext_natives.insert(name, cb);654 Ref::map(self.settings(), |s| &*s.context_initializer)
678 }655 }
679656
680 pub fn manifest_format(&self) -> ManifestFormat {657 pub fn manifest_format(&self) -> ManifestFormat {
deletedcrates/jrsonnet-evaluator/src/stdlib/expr.rsdiffbeforeafterboth

no changes

modifiedcrates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth
1// All builtins should return results1// All builtins should return results
2#![allow(clippy::unnecessary_wraps)]2#![allow(clippy::unnecessary_wraps)]
3
4use std::collections::HashMap;
53
6use format::{format_arr, format_obj};4use format::{format_arr, format_obj};
7use jrsonnet_gcmodule::Cc;
8use jrsonnet_interner::{IBytes, IStr};5use jrsonnet_interner::IStr;
9use serde::Deserialize;
10use serde_yaml_with_quirks::DeserializingQuirks;
116
12use crate::{7use crate::{error::Result, function::CallLocation, State, Val};
13 error::{Error::*, Result},
14 function::{builtin::StaticBuiltin, ArgLike, CallLocation, FuncVal},
15 operator::evaluate_mod_op,
16 stdlib::manifest::{manifest_yaml_ex, ManifestYamlOptions},
17 throw,
18 typed::{Any, BoundedUsize, Either2, Either4, PositiveF64, Typed, VecVal, M1},
19 val::{equals, primitive_equals, ArrValue, IndexableVal, Slice},
20 Either, ObjValue, State, Val,
21};
22
23pub mod expr;
24pub use expr::*;
25
26use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};
278
28pub mod format;9pub mod format;
29pub mod manifest;10pub mod manifest;
30pub mod sort;
3111
32pub fn std_format(s: State, str: IStr, vals: Val) -> Result<String> {12pub fn std_format(s: State, str: IStr, vals: Val) -> Result<String> {
33 s.push(13 s.push(
43 )23 )
44}24}
45
46pub fn std_slice(
47 indexable: IndexableVal,
48 index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
49 end: Option<BoundedUsize<0, { i32::MAX as usize }>>,
50 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
51) -> Result<Val> {
52 match &indexable {
53 IndexableVal::Str(s) => {
54 let index = index.as_deref().copied().unwrap_or(0);
55 let end = end.as_deref().copied().unwrap_or(usize::MAX);
56 let step = step.as_deref().copied().unwrap_or(1);
57
58 if index >= end {
59 return Ok(Val::Str("".into()));
60 }
61
62 Ok(Val::Str(
63 (s.chars()
64 .skip(index)
65 .take(end - index)
66 .step_by(step)
67 .collect::<String>())
68 .into(),
69 ))
70 }
71 IndexableVal::Arr(arr) => {
72 let index = index.as_deref().copied().unwrap_or(0);
73 let end = end.as_deref().copied().unwrap_or(usize::MAX).min(arr.len());
74 let step = step.as_deref().copied().unwrap_or(1);
75
76 if index >= end {
77 return Ok(Val::Arr(ArrValue::new_eager()));
78 }
79
80 Ok(Val::Arr(ArrValue::Slice(Box::new(Slice {
81 inner: arr.clone(),
82 from: index as u32,
83 to: end as u32,
84 step: step as u32,
85 }))))
86 }
87 }
88}
89
90type BuiltinsType = HashMap<IStr, &'static dyn StaticBuiltin>;
91
92thread_local! {
93 pub static BUILTINS: BuiltinsType = {
94 [
95 ("length".into(), builtin_length::INST),
96 ("type".into(), builtin_type::INST),
97 ("makeArray".into(), builtin_make_array::INST),
98 ("codepoint".into(), builtin_codepoint::INST),
99 ("objectFieldsEx".into(), builtin_object_fields_ex::INST),
100 ("objectHasEx".into(), builtin_object_has_ex::INST),
101 ("slice".into(), builtin_slice::INST),
102 ("substr".into(), builtin_substr::INST),
103 ("primitiveEquals".into(), builtin_primitive_equals::INST),
104 ("equals".into(), builtin_equals::INST),
105 ("modulo".into(), builtin_modulo::INST),
106 ("mod".into(), builtin_mod::INST),
107 ("floor".into(), builtin_floor::INST),
108 ("ceil".into(), builtin_ceil::INST),
109 ("log".into(), builtin_log::INST),
110 ("pow".into(), builtin_pow::INST),
111 ("sqrt".into(), builtin_sqrt::INST),
112 ("sin".into(), builtin_sin::INST),
113 ("cos".into(), builtin_cos::INST),
114 ("tan".into(), builtin_tan::INST),
115 ("asin".into(), builtin_asin::INST),
116 ("acos".into(), builtin_acos::INST),
117 ("atan".into(), builtin_atan::INST),
118 ("exp".into(), builtin_exp::INST),
119 ("mantissa".into(), builtin_mantissa::INST),
120 ("exponent".into(), builtin_exponent::INST),
121 ("extVar".into(), builtin_ext_var::INST),
122 ("native".into(), builtin_native::INST),
123 ("filter".into(), builtin_filter::INST),
124 ("map".into(), builtin_map::INST),
125 ("flatMap".into(), builtin_flatmap::INST),
126 ("foldl".into(), builtin_foldl::INST),
127 ("foldr".into(), builtin_foldr::INST),
128 ("sort".into(), builtin_sort::INST),
129 ("format".into(), builtin_format::INST),
130 ("range".into(), builtin_range::INST),
131 ("char".into(), builtin_char::INST),
132 ("encodeUTF8".into(), builtin_encode_utf8::INST),
133 ("decodeUTF8".into(), builtin_decode_utf8::INST),
134 ("md5".into(), builtin_md5::INST),
135 ("base64".into(), builtin_base64::INST),
136 ("base64DecodeBytes".into(), builtin_base64_decode_bytes::INST),
137 ("base64Decode".into(), builtin_base64_decode::INST),
138 ("trace".into(), builtin_trace::INST),
139 ("join".into(), builtin_join::INST),
140 ("escapeStringJson".into(), builtin_escape_string_json::INST),
141 ("manifestJsonEx".into(), builtin_manifest_json_ex::INST),
142 ("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),
143 ("reverse".into(), builtin_reverse::INST),
144 ("strReplace".into(), builtin_str_replace::INST),
145 ("splitLimit".into(), builtin_splitlimit::INST),
146 ("parseJson".into(), builtin_parse_json::INST),
147 ("parseYaml".into(), builtin_parse_yaml::INST),
148 ("asciiUpper".into(), builtin_ascii_upper::INST),
149 ("asciiLower".into(), builtin_ascii_lower::INST),
150 ("member".into(), builtin_member::INST),
151 ("count".into(), builtin_count::INST),
152 ("any".into(), builtin_any::INST),
153 ("all".into(), builtin_all::INST),
154 ].iter().cloned().collect()
155 };
156}
157
158#[jrsonnet_macros::builtin]
159fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {
160 use Either4::*;
161 Ok(match x {
162 A(x) => x.chars().count(),
163 B(x) => x.len(),
164 C(x) => x.len(),
165 D(f) => f.params_len(),
166 })
167}
168
169#[jrsonnet_macros::builtin]
170fn builtin_type(x: Any) -> Result<IStr> {
171 Ok(x.0.value_type().name().into())
172}
173
174#[jrsonnet_macros::builtin]
175fn builtin_make_array(s: State, sz: usize, func: FuncVal) -> Result<VecVal> {
176 let mut out = Vec::with_capacity(sz);
177 for i in 0..sz {
178 out.push(func.evaluate_simple(s.clone(), &(i as f64,))?);
179 }
180 Ok(VecVal(Cc::new(out)))
181}
182
183#[jrsonnet_macros::builtin]
184const fn builtin_codepoint(str: char) -> Result<u32> {
185 Ok(str as u32)
186}
187
188#[jrsonnet_macros::builtin]
189fn builtin_object_fields_ex(
190 obj: ObjValue,
191 inc_hidden: bool,
192 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
193) -> Result<VecVal> {
194 #[cfg(feature = "exp-preserve-order")]
195 let preserve_order = preserve_order.unwrap_or(false);
196 let out = obj.fields_ex(
197 inc_hidden,
198 #[cfg(feature = "exp-preserve-order")]
199 preserve_order,
200 );
201 Ok(VecVal(Cc::new(
202 out.into_iter().map(Val::Str).collect::<Vec<_>>(),
203 )))
204}
205
206#[jrsonnet_macros::builtin]
207fn builtin_object_has_ex(obj: ObjValue, f: IStr, inc_hidden: bool) -> Result<bool> {
208 Ok(obj.has_field_ex(f, inc_hidden))
209}
210
211#[jrsonnet_macros::builtin]
212fn builtin_parse_json(st: State, s: IStr) -> Result<Any> {
213 use serde_json::Value;
214 let value: Value = serde_json::from_str(&s)
215 .map_err(|e| RuntimeError(format!("failed to parse json: {}", e).into()))?;
216 Ok(Any(Value::into_untyped(value, st)?))
217}
218
219#[jrsonnet_macros::builtin]
220fn builtin_parse_yaml(st: State, s: IStr) -> Result<Any> {
221 use serde_json::Value;
222 let value = serde_yaml_with_quirks::Deserializer::from_str_with_quirks(
223 &s,
224 DeserializingQuirks { old_octals: true },
225 );
226 let mut out = vec![];
227 for item in value {
228 let value = Value::deserialize(item)
229 .map_err(|e| RuntimeError(format!("failed to parse yaml: {}", e).into()))?;
230 let val = Value::into_untyped(value, st.clone())?;
231 out.push(val);
232 }
233 Ok(Any(if out.is_empty() {
234 Val::Null
235 } else if out.len() == 1 {
236 out.into_iter().next().unwrap()
237 } else {
238 Val::Arr(out.into())
239 }))
240}
241
242#[jrsonnet_macros::builtin]
243fn builtin_slice(
244 indexable: IndexableVal,
245 index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
246 end: Option<BoundedUsize<0, { i32::MAX as usize }>>,
247 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
248) -> Result<Any> {
249 std_slice(indexable, index, end, step).map(Any)
250}
251
252#[jrsonnet_macros::builtin]
253fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {
254 Ok(str.chars().skip(from as usize).take(len as usize).collect())
255}
256
257#[jrsonnet_macros::builtin]
258fn builtin_primitive_equals(a: Any, b: Any) -> Result<bool> {
259 primitive_equals(&a.0, &b.0)
260}
261
262#[jrsonnet_macros::builtin]
263fn builtin_equals(s: State, a: Any, b: Any) -> Result<bool> {
264 equals(s, &a.0, &b.0)
265}
266
267#[jrsonnet_macros::builtin]
268fn builtin_modulo(a: f64, b: f64) -> Result<f64> {
269 Ok(a % b)
270}
271
272#[jrsonnet_macros::builtin]
273fn builtin_mod(s: State, a: Either![f64, IStr], b: Any) -> Result<Any> {
274 use Either2::*;
275 Ok(Any(evaluate_mod_op(
276 s,
277 &match a {
278 A(v) => Val::Num(v),
279 B(s) => Val::Str(s),
280 },
281 &b.0,
282 )?))
283}
284
285#[jrsonnet_macros::builtin]
286fn builtin_floor(x: f64) -> Result<f64> {
287 Ok(x.floor())
288}
289
290#[jrsonnet_macros::builtin]
291fn builtin_ceil(x: f64) -> Result<f64> {
292 Ok(x.ceil())
293}
294
295#[jrsonnet_macros::builtin]
296fn builtin_log(n: f64) -> Result<f64> {
297 Ok(n.ln())
298}
299
300#[jrsonnet_macros::builtin]
301fn builtin_pow(x: f64, n: f64) -> Result<f64> {
302 Ok(x.powf(n))
303}
304
305#[jrsonnet_macros::builtin]
306fn builtin_sqrt(x: PositiveF64) -> Result<f64> {
307 Ok(x.0.sqrt())
308}
309
310#[jrsonnet_macros::builtin]
311fn builtin_sin(x: f64) -> Result<f64> {
312 Ok(x.sin())
313}
314
315#[jrsonnet_macros::builtin]
316fn builtin_cos(x: f64) -> Result<f64> {
317 Ok(x.cos())
318}
319
320#[jrsonnet_macros::builtin]
321fn builtin_tan(x: f64) -> Result<f64> {
322 Ok(x.tan())
323}
324
325#[jrsonnet_macros::builtin]
326fn builtin_asin(x: f64) -> Result<f64> {
327 Ok(x.asin())
328}
329
330#[jrsonnet_macros::builtin]
331fn builtin_acos(x: f64) -> Result<f64> {
332 Ok(x.acos())
333}
334
335#[jrsonnet_macros::builtin]
336fn builtin_atan(x: f64) -> Result<f64> {
337 Ok(x.atan())
338}
339
340#[jrsonnet_macros::builtin]
341fn builtin_exp(x: f64) -> Result<f64> {
342 Ok(x.exp())
343}
344
345fn frexp(s: f64) -> (f64, i16) {
346 if 0.0 == s {
347 (s, 0)
348 } else {
349 let lg = s.abs().log2();
350 let x = (lg - lg.floor() - 1.0).exp2();
351 let exp = lg.floor() + 1.0;
352 (s.signum() * x, exp as i16)
353 }
354}
355
356#[jrsonnet_macros::builtin]
357fn builtin_mantissa(x: f64) -> Result<f64> {
358 Ok(frexp(x).0)
359}
360
361#[jrsonnet_macros::builtin]
362fn builtin_exponent(x: f64) -> Result<i16> {
363 Ok(frexp(x).1)
364}
365
366#[jrsonnet_macros::builtin]
367fn builtin_ext_var(s: State, x: IStr) -> Result<Any> {
368 let ctx = s.create_default_context();
369 Ok(Any(s
370 .clone()
371 .settings()
372 .ext_vars
373 .get(&x)
374 .cloned()
375 .ok_or(UndefinedExternalVariable(x))?
376 .evaluate_arg(s.clone(), ctx, true)?
377 .evaluate(s)?))
378}
379
380#[jrsonnet_macros::builtin]
381fn builtin_native(s: State, name: IStr) -> Result<Any> {
382 Ok(Any(s
383 .settings()
384 .ext_natives
385 .get(&name)
386 .cloned()
387 .map_or(Val::Null, |v| {
388 Val::Func(FuncVal::Builtin(v.clone()))
389 })))
390}
391
392#[jrsonnet_macros::builtin]
393fn builtin_filter(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {
394 arr.filter(s.clone(), |val| {
395 bool::from_untyped(
396 func.evaluate_simple(s.clone(), &(Any(val.clone()),))?,
397 s.clone(),
398 )
399 })
400}
401
402#[jrsonnet_macros::builtin]
403fn builtin_map(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {
404 arr.map(s.clone(), |val| {
405 func.evaluate_simple(s.clone(), &(Any(val),))
406 })
407}
408
409#[jrsonnet_macros::builtin]
410fn builtin_flatmap(s: State, func: FuncVal, arr: IndexableVal) -> Result<IndexableVal> {
411 match arr {
412 IndexableVal::Str(str) => {
413 let mut out = String::new();
414 for c in str.chars() {
415 match func.evaluate_simple(s.clone(), &(c.to_string(),))? {
416 Val::Str(o) => out.push_str(&o),
417 Val::Null => continue,
418 _ => throw!(RuntimeError(
419 "in std.join all items should be strings".into()
420 )),
421 };
422 }
423 Ok(IndexableVal::Str(out.into()))
424 }
425 IndexableVal::Arr(a) => {
426 let mut out = Vec::new();
427 for el in a.iter(s.clone()) {
428 let el = el?;
429 match func.evaluate_simple(s.clone(), &(Any(el),))? {
430 Val::Arr(o) => {
431 for oe in o.iter(s.clone()) {
432 out.push(oe?);
433 }
434 }
435 Val::Null => continue,
436 _ => throw!(RuntimeError(
437 "in std.join all items should be arrays".into()
438 )),
439 };
440 }
441 Ok(IndexableVal::Arr(out.into()))
442 }
443 }
444}
445
446#[jrsonnet_macros::builtin]
447fn builtin_foldl(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {
448 let mut acc = init.0;
449 for i in arr.iter(s.clone()) {
450 acc = func.evaluate_simple(s.clone(), &(Any(acc), Any(i?)))?;
451 }
452 Ok(Any(acc))
453}
454
455#[jrsonnet_macros::builtin]
456fn builtin_foldr(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {
457 let mut acc = init.0;
458 for i in arr.iter(s.clone()).rev() {
459 acc = func.evaluate_simple(s.clone(), &(Any(i?), Any(acc)))?;
460 }
461 Ok(Any(acc))
462}
463
464#[jrsonnet_macros::builtin]
465#[allow(non_snake_case)]
466fn builtin_sort(s: State, arr: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {
467 if arr.len() <= 1 {
468 return Ok(arr);
469 }
470 Ok(ArrValue::Eager(sort::sort(
471 s.clone(),
472 arr.evaluated(s)?,
473 keyF.unwrap_or_else(FuncVal::identity),
474 )?))
475}
476
477#[jrsonnet_macros::builtin]
478fn builtin_format(s: State, str: IStr, vals: Any) -> Result<String> {
479 std_format(s, str, vals.0)
480}
481
482#[jrsonnet_macros::builtin]
483fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {
484 if to < from {
485 return Ok(ArrValue::new_eager());
486 }
487 Ok(ArrValue::new_range(from, to))
488}
489
490#[jrsonnet_macros::builtin]
491fn builtin_char(n: u32) -> Result<char> {
492 Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)
493}
494
495#[jrsonnet_macros::builtin]
496fn builtin_encode_utf8(str: IStr) -> Result<IBytes> {
497 Ok(str.cast_bytes())
498}
499
500#[jrsonnet_macros::builtin]
501fn builtin_decode_utf8(arr: IBytes) -> Result<IStr> {
502 Ok(arr
503 .cast_str()
504 .ok_or_else(|| RuntimeError("bad utf8".into()))?)
505}
506
507#[jrsonnet_macros::builtin]
508fn builtin_md5(str: IStr) -> Result<String> {
509 Ok(format!("{:x}", md5::compute(&str.as_bytes())))
510}
511
512#[jrsonnet_macros::builtin]
513fn builtin_trace(s: State, loc: CallLocation, str: IStr, rest: Any) -> Result<Any> {
514 eprint!("TRACE:");
515 if let Some(loc) = loc.0 {
516 let locs = s.map_source_locations(loc.0.clone(), &[loc.1]);
517 eprint!(" {}:{}", loc.0.short_display(), locs[0].line);
518 }
519 eprintln!(" {}", str);
520 Ok(rest) as Result<Any>
521}
522
523#[jrsonnet_macros::builtin]
524fn builtin_base64(input: Either![IBytes, IStr]) -> Result<String> {
525 use Either2::*;
526 Ok(match input {
527 A(a) => base64::encode(a.as_slice()),
528 B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),
529 })
530}
531
532#[jrsonnet_macros::builtin]
533fn builtin_base64_decode_bytes(input: IStr) -> Result<IBytes> {
534 Ok(base64::decode(&input.as_bytes())
535 .map_err(|_| RuntimeError("bad base64".into()))?
536 .as_slice()
537 .into())
538}
539
540#[jrsonnet_macros::builtin]
541fn builtin_base64_decode(input: IStr) -> Result<String> {
542 let bytes = base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?;
543 Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)
544}
545
546#[jrsonnet_macros::builtin]
547fn builtin_join(s: State, sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {
548 Ok(match sep {
549 IndexableVal::Arr(joiner_items) => {
550 let mut out = Vec::new();
551
552 let mut first = true;
553 for item in arr.iter(s.clone()) {
554 let item = item?.clone();
555 if let Val::Arr(items) = item {
556 if !first {
557 out.reserve(joiner_items.len());
558 // TODO: extend
559 for item in joiner_items.iter(s.clone()) {
560 out.push(item?);
561 }
562 }
563 first = false;
564 out.reserve(items.len());
565 for item in items.iter(s.clone()) {
566 out.push(item?);
567 }
568 } else if matches!(item, Val::Null) {
569 continue;
570 } else {
571 throw!(RuntimeError(
572 "in std.join all items should be arrays".into()
573 ));
574 }
575 }
576
577 IndexableVal::Arr(out.into())
578 }
579 IndexableVal::Str(sep) => {
580 let mut out = String::new();
581
582 let mut first = true;
583 for item in arr.iter(s) {
584 let item = item?.clone();
585 if let Val::Str(item) = item {
586 if !first {
587 out += &sep;
588 }
589 first = false;
590 out += &item;
591 } else if matches!(item, Val::Null) {
592 continue;
593 } else {
594 throw!(RuntimeError(
595 "in std.join all items should be strings".into()
596 ));
597 }
598 }
599
600 IndexableVal::Str(out.into())
601 }
602 })
603}
604
605#[jrsonnet_macros::builtin]
606fn builtin_escape_string_json(str_: IStr) -> Result<String> {
607 Ok(escape_string_json(&str_))
608}
609
610#[jrsonnet_macros::builtin]
611fn builtin_manifest_json_ex(
612 s: State,
613 value: Any,
614 indent: IStr,
615 newline: Option<IStr>,
616 key_val_sep: Option<IStr>,
617 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
618) -> Result<String> {
619 let newline = newline.as_deref().unwrap_or("\n");
620 let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");
621 manifest_json_ex(
622 s,
623 &value.0,
624 &ManifestJsonOptions {
625 padding: &indent,
626 mtype: ManifestType::Std,
627 newline,
628 key_val_sep,
629 #[cfg(feature = "exp-preserve-order")]
630 preserve_order: preserve_order.unwrap_or(false),
631 },
632 )
633}
634
635#[jrsonnet_macros::builtin]
636fn builtin_manifest_yaml_doc(
637 s: State,
638 value: Any,
639 indent_array_in_object: Option<bool>,
640 quote_keys: Option<bool>,
641 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
642) -> Result<String> {
643 manifest_yaml_ex(
644 s,
645 &value.0,
646 &ManifestYamlOptions {
647 padding: " ",
648 arr_element_padding: if indent_array_in_object.unwrap_or(false) {
649 " "
650 } else {
651 ""
652 },
653 quote_keys: quote_keys.unwrap_or(true),
654 #[cfg(feature = "exp-preserve-order")]
655 preserve_order: preserve_order.unwrap_or(false),
656 },
657 )
658}
659
660#[jrsonnet_macros::builtin]
661fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {
662 Ok(value.reversed())
663}
664
665#[jrsonnet_macros::builtin]
666fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {
667 Ok(str.replace(&from as &str, &to as &str))
668}
669
670#[jrsonnet_macros::builtin]
671fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {
672 use Either2::*;
673 Ok(VecVal(Cc::new(match maxsplits {
674 A(n) => str
675 .splitn(n + 1, &c as &str)
676 .map(|s| Val::Str(s.into()))
677 .collect(),
678 B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),
679 })))
680}
681
682#[jrsonnet_macros::builtin]
683fn builtin_ascii_upper(str: IStr) -> Result<String> {
684 Ok(str.to_ascii_uppercase())
685}
686
687#[jrsonnet_macros::builtin]
688fn builtin_ascii_lower(str: IStr) -> Result<String> {
689 Ok(str.to_ascii_lowercase())
690}
691
692#[jrsonnet_macros::builtin]
693fn builtin_member(s: State, arr: IndexableVal, x: Any) -> Result<bool> {
694 match arr {
695 IndexableVal::Str(str) => {
696 let x: IStr = IStr::from_untyped(x.0, s)?;
697 Ok(!x.is_empty() && str.contains(&*x))
698 }
699 IndexableVal::Arr(a) => {
700 for item in a.iter(s.clone()) {
701 let item = item?;
702 if equals(s.clone(), &item, &x.0)? {
703 return Ok(true);
704 }
705 }
706 Ok(false)
707 }
708 }
709}
710
711#[jrsonnet_macros::builtin]
712fn builtin_count(s: State, arr: Vec<Any>, v: Any) -> Result<usize> {
713 let mut count = 0;
714 for item in &arr {
715 if equals(s.clone(), &item.0, &v.0)? {
716 count += 1;
717 }
718 }
719 Ok(count)
720}
721
722#[jrsonnet_macros::builtin]
723fn builtin_any(s: State, arr: ArrValue) -> Result<bool> {
724 for v in arr.iter(s.clone()) {
725 let v = bool::from_untyped(v?, s.clone())?;
726 if v {
727 return Ok(true);
728 }
729 }
730 Ok(false)
731}
732
733#[jrsonnet_macros::builtin]
734fn builtin_all(s: State, arr: ArrValue) -> Result<bool> {
735 for v in arr.iter(s.clone()) {
736 let v = bool::from_untyped(v?, s.clone())?;
737 if !v {
738 return Ok(false);
739 }
740 }
741 Ok(true)
742}
74325
deletedcrates/jrsonnet-evaluator/src/stdlib/sort.rsdiffbeforeafterboth

no changes