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.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -37,12 +37,13 @@
mod integrations;
mod map;
mod obj;
-mod stdlib;
+pub mod stdlib;
pub mod trace;
pub mod typed;
pub mod val;
use std::{
+ any::Any,
borrow::Cow,
cell::{Ref, RefCell, RefMut},
collections::HashMap,
@@ -55,13 +56,12 @@
pub use dynamic::*;
use error::{Error::*, LocError, Result, StackTraceElement};
pub use evaluate::*;
-use function::{builtin::Builtin, CallLocation, TlaArg};
+use function::{CallLocation, TlaArg};
use gc::{GcHashMap, TraceBox};
use hashbrown::hash_map::RawEntryMut;
pub use import::*;
use jrsonnet_gcmodule::{Cc, Trace};
-use jrsonnet_interner::IBytes;
-pub use jrsonnet_interner::IStr;
+pub use jrsonnet_interner::{IBytes, IStr};
pub use jrsonnet_parser as parser;
use jrsonnet_parser::*;
pub use obj::*;
@@ -98,19 +98,40 @@
}
}
+/// During import, this trait will be called to create initial context for file
+/// It may initialize global variables, stdlib for example
+pub trait ContextInitializer {
+ fn initialize(&self, state: State, for_file: Source) -> Context;
+
+ /// # Safety
+ ///
+ /// For use only in bindings, should not be used elsewhere.
+ /// Implementations which are not intended to be used in bindings
+ /// should panic on call to this method.
+ unsafe fn as_any(&self) -> &dyn Any;
+}
+
+/// Context initializer, which adds noth
+pub struct DummyContextInitializer;
+impl ContextInitializer for DummyContextInitializer {
+ fn initialize(&self, _state: State, _for_file: Source) -> Context {
+ Context::default()
+ }
+ unsafe fn as_any(&self) -> &dyn Any {
+ panic!("`as_any(&self)` is not supported by dummy initializer")
+ }
+}
+
pub struct EvaluationSettings {
/// Limits recursion by limiting the number of stack frames
pub max_stack: usize,
/// Limits amount of stack trace items preserved
pub max_trace: usize,
- /// Used for s`td.extVar`
- pub ext_vars: HashMap<IStr, TlaArg>,
- /// Used for ext.native
- pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,
/// TLA vars
pub tla_vars: HashMap<IStr, TlaArg>,
- /// Global variables are inserted in default context
- pub globals: HashMap<IStr, Val>,
+ /// Context initializer, which will be used for imports and everything
+ /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`
+ pub context_initializer: Box<dyn ContextInitializer>,
/// Used to resolve file locations/contents
pub import_resolver: Box<dyn ImportResolver>,
/// Used in manifestification functions
@@ -123,9 +144,7 @@
Self {
max_stack: 200,
max_trace: 20,
- globals: HashMap::default(),
- ext_vars: HashMap::default(),
- ext_natives: HashMap::default(),
+ context_initializer: Box::new(DummyContextInitializer),
tla_vars: HashMap::default(),
import_resolver: Box::new(DummyImportResolver),
manifest_format: ManifestFormat::Json {
@@ -152,7 +171,8 @@
/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
files: GcHashMap<PathBuf, FileData>,
- /// Contains tla arguments and others, which aren't needed to be obtained by name
+ /// Contains tla arguments and others, which aren't needed to be obtained by name, however may be used for receiving source
+ /// TODO: look into nix approach, storing source code in `Source` object
volatile_files: GcHashMap<String, String>,
}
struct FileData {
@@ -333,7 +353,7 @@
},
)
.map_err(|e| ImportSyntaxError {
- path: file_name,
+ path: file_name.clone(),
source_code: code.clone(),
error: Box::new(e),
})?,
@@ -346,7 +366,11 @@
file.evaluating = true;
// Dropping file here, as it borrows data, which may be used in evaluation
drop(data);
- let res = evaluate(self.clone(), self.create_default_context(), &parsed);
+ let res = evaluate(
+ self.clone(),
+ self.create_default_context(file_name),
+ &parsed,
+ );
let mut data = self.data_mut();
let mut file = data.files.raw_entry_mut().from_key(&path);
@@ -391,26 +415,11 @@
column,
)
}
- /// Adds standard library global variable (std) to this evaluator
- pub fn with_stdlib(&self) -> &Self {
- let val = evaluate(
- self.clone(),
- self.create_default_context(),
- &stdlib::get_parsed_stdlib(),
- )
- .expect("std should not fail");
- self.settings_mut().globals.insert("std".into(), val);
- self
- }
/// Creates context with all passed global variables
- pub fn create_default_context(&self) -> Context {
- let globals = &self.settings().globals;
- let mut new_bindings = GcHashMap::with_capacity(globals.len());
- for (name, value) in globals.iter() {
- new_bindings.insert(name.clone(), Thunk::evaluated(value.clone()));
- }
- Context::new().extend(new_bindings, None, None, None)
+ pub fn create_default_context(&self, source: Source) -> Context {
+ let context_initializer = &self.settings().context_initializer;
+ context_initializer.initialize(self.clone(), source)
}
/// Executes code creating a new stack frame
@@ -545,7 +554,7 @@
|| {
func.evaluate(
self.clone(),
- self.create_default_context(),
+ self.create_default_context(Source::new_virtual(Cow::Borrowed("<tla>"))),
CallLocation::native(),
&self.settings().tla_vars,
true,
@@ -585,48 +594,17 @@
},
)
.map_err(|e| ImportSyntaxError {
- path: source,
+ path: source.clone(),
source_code: code.clone().into(),
error: Box::new(e),
})?;
self.data_mut().volatile_files.insert(name, code);
- evaluate(self.clone(), self.create_default_context(), &parsed)
+ evaluate(self.clone(), self.create_default_context(source), &parsed)
}
}
/// Settings utilities
impl State {
- pub fn add_ext_var(&self, name: IStr, value: Val) {
- self.settings_mut()
- .ext_vars
- .insert(name, TlaArg::Val(value));
- }
- pub fn add_ext_str(&self, name: IStr, value: IStr) {
- self.settings_mut()
- .ext_vars
- .insert(name, TlaArg::String(value));
- }
- pub fn add_ext_code(&self, name: &str, code: String) -> Result<()> {
- let source_name = format!("<extvar:{}>", name);
- let source = Source::new_virtual(Cow::Owned(source_name.clone()));
- let parsed = jrsonnet_parser::parse(
- &code,
- &ParserSettings {
- file_name: source.clone(),
- },
- )
- .map_err(|e| ImportSyntaxError {
- path: source,
- source_code: code.clone().into(),
- error: Box::new(e),
- })?;
- self.data_mut().volatile_files.insert(source_name, code);
- self.settings_mut()
- .ext_vars
- .insert(name.into(), TlaArg::Code(parsed));
- Ok(())
- }
-
pub fn add_tla(&self, name: IStr, value: Val) {
self.settings_mut()
.tla_vars
@@ -672,9 +650,8 @@
pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {
self.settings_mut().import_resolver = resolver;
}
-
- pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {
- self.settings_mut().ext_natives.insert(name, cb);
+ pub fn context_initializer(&self) -> Ref<dyn ContextInitializer> {
+ Ref::map(self.settings(), |s| &*s.context_initializer)
}
pub fn manifest_format(&self) -> ManifestFormat {
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.rsdiffbeforeafterboth1// All builtins should return results2#![allow(clippy::unnecessary_wraps)]34use format::{format_arr, format_obj};5use jrsonnet_interner::IStr;67use crate::{error::Result, function::CallLocation, State, Val};89pub mod format;10pub mod manifest;1112pub fn std_format(s: State, str: IStr, vals: Val) -> Result<String> {13 s.push(14 CallLocation::native(),15 || format!("std.format of {}", str),16 || {17 Ok(match vals {18 Val::Arr(vals) => format_arr(s.clone(), &str, &vals.evaluated(s.clone())?)?,19 Val::Obj(obj) => format_obj(s.clone(), &str, &obj)?,20 o => format_arr(s.clone(), &str, &[o])?,21 })22 },23 )24}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()))
- }
-}