difftreelog
doc: review issues
in: master
19 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -93,7 +93,7 @@
/// # Safety
///
-/// Caller should pass correct callback function
+/// It should be safe to call `cb` using valid values with passed `ctx`
#[no_mangle]
pub unsafe extern "C" fn jsonnet_import_callback(
vm: &State,
@@ -109,10 +109,10 @@
/// # Safety
///
-/// Caller should pass correct path: it should contain correct utf-8, and be \0-terminated
+/// `path` should be a NUL-terminated string
#[no_mangle]
-pub unsafe extern "C" fn jsonnet_jpath_add(vm: &State, v: *const c_char) {
- let cstr = CStr::from_ptr(v);
+pub unsafe extern "C" fn jsonnet_jpath_add(vm: &State, path: *const c_char) {
+ let cstr = CStr::from_ptr(path);
let path = PathBuf::from(cstr.to_str().unwrap());
let any_resolver = vm.import_resolver();
let resolver = any_resolver
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -41,17 +41,10 @@
let str = OsStr::from_bytes(input.to_bytes());
Cow::Borrowed(Path::new(str))
}
- #[cfg(target_family = "windows")]
+ #[cfg(not(target_family = "unix"))]
{
- use std::os::windows::ffi::OsStringExt;
- let str = input.to_str().expect("input is not utf8");
- let wide = str.encode_utf16().collect::<Vec<_>>();
- let wide = OsString::from_wide(&wide);
- Cow::Owned(PathBuf::new(wide))
- }
- #[cfg(not(any(target_family = "unix", target_family = "windows")))]
- {
- compile_error!("unsupported os")
+ let string = input.to_str().expect("bad utf-8");
+ Cow::Borrowed(string.as_ref())
}
}
@@ -62,9 +55,11 @@
let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");
Cow::Owned(str)
}
- #[cfg(not(any(target_family = "unix", target_family = "windows")))]
+ #[cfg(not(target_family = "unix"))]
{
- compile_error!("unsupported os")
+ let str = input.as_os_str().to_str().expect("bad utf-8");
+ let cstr = CString::new(str).expect("input has NUL inside");
+ Cow::Owned(cstr)
}
}
@@ -169,7 +164,7 @@
///
/// # Safety
///
-/// `filename` should be a \0-terminated string
+/// `filename` should be a NUL-terminated string
#[no_mangle]
pub unsafe extern "C" fn jsonnet_evaluate_file(
vm: &State,
@@ -200,7 +195,7 @@
///
/// # Safety
///
-/// `filename`, `snippet` should be a \0-terminated strings
+/// `filename`, `snippet` should be a NUL-terminated strings
#[no_mangle]
pub unsafe extern "C" fn jsonnet_evaluate_snippet(
vm: &State,
bindings/jsonnet/src/native.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -65,9 +65,9 @@
/// # Safety
///
/// `vm` should be a vm allocated by `jsonnet_make`
-/// `cb` should be a correct function pointer
-/// `raw_params` should point to a NULL-terminated string array
-/// `name`, `raw_params` elements should be a \0-terminated strings
+/// `name` should be a NUL-terminated string
+/// `cb` should be a function pointer
+/// `raw_params` should point to a NULL-terminated array of NUL-terminated strings
#[no_mangle]
pub unsafe extern "C" fn jsonnet_native_callback(
vm: &State,
bindings/jsonnet/src/val_make.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_make.rs
+++ b/bindings/jsonnet/src/val_make.rs
@@ -12,7 +12,7 @@
///
/// # Safety
///
-/// `v` should be a \0-terminated string
+/// `v` should be a NUL-terminated string
#[no_mangle]
pub unsafe extern "C" fn jsonnet_json_make_string(_vm: &State, val: *const c_char) -> *mut Val {
let val = CStr::from_ptr(val);
bindings/jsonnet/src/val_modify.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -11,8 +11,8 @@
///
/// # Safety
///
-/// `arr` should be correct pointer to array value allocated by make_array, or returned by other library call
-/// `val` should be correct pointer to value allocated using this library
+/// `arr` should be a pointer to array value allocated by make_array, or returned by other library call
+/// `val` should be a pointer to value allocated using this library
#[no_mangle]
pub unsafe extern "C" fn jsonnet_json_array_append(_vm: &State, arr: &mut Val, val: &Val) {
match arr {
@@ -35,8 +35,8 @@
///
/// # Safety
///
-/// `obj` should be a valid pointer to object value allocated by `make_object`, or returned by other library call
-/// `name` should be \0-terminated string
+/// `obj` should be a pointer to object value allocated by `make_object`, or returned by other library call
+/// `name` should be NUL-terminated string
#[no_mangle]
pub unsafe extern "C" fn jsonnet_json_object_append(
_vm: &State,
bindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -4,13 +4,13 @@
use jrsonnet_evaluator::State;
-/// Bind a Jsonnet external var to the given string.
+/// Binds a Jsonnet external variable to the given string.
///
-/// Argument values are copied so memory should be managed by caller.
+/// Argument values are copied so memory should be managed by the caller.
///
/// # Safety
///
-/// Caller should pass correct pointers as `name` and `code`, they need to be \0-terminated strings
+/// `name`, `code` should be a NUL-terminated strings
#[no_mangle]
pub unsafe extern "C" fn jsonnet_ext_var(vm: &State, name: *const c_char, value: *const c_char) {
let name = CStr::from_ptr(name);
@@ -27,13 +27,13 @@
)
}
-/// Bind a Jsonnet external var to the given code.
+/// Binds a Jsonnet external variable to the given code.
///
-/// Argument values are copied so memory should be managed by caller.
+/// Argument values are copied so memory should be managed by the caller.
///
/// # Safety
///
-/// Caller should pass correct pointers as `name` and `code`, they need to be \0-terminated strings
+/// `name`, `code` should be a NUL-terminated strings
#[no_mangle]
pub unsafe extern "C" fn jsonnet_ext_code(vm: &State, name: *const c_char, code: *const c_char) {
let name = CStr::from_ptr(name);
@@ -51,13 +51,13 @@
.expect("can't parse ext code")
}
-/// Bind a string top-level argument for a top-level parameter.
+/// Binds a top-level string argument for a top-level parameter.
///
-/// Argument values are copied so memory should be managed by caller.
+/// Argument values are copied so memory should be managed by the caller.
///
/// # Safety
///
-/// Caller should pass correct pointers as `name` and `value`, they need to be \0-terminated strings
+/// `name`, `value` should be a NUL-terminated strings
#[no_mangle]
pub unsafe extern "C" fn jsonnet_tla_var(vm: &State, name: *const c_char, value: *const c_char) {
let name = CStr::from_ptr(name);
@@ -68,13 +68,13 @@
)
}
-/// Bind a code top-level argument for a top-level parameter.
+/// Binds a top-level code argument for a top-level parameter.
///
-/// Argument values are copied so memory should be managed by caller.
+/// Argument values are copied so memory should be managed by the caller.
///
/// # Safety
///
-/// Caller should pass correct pointers as `name` and `code`, they need to be \0-terminated strings
+/// `name`, `code` should be a NUL-terminated strings
#[no_mangle]
pub unsafe extern "C" fn jsonnet_tla_code(vm: &State, name: *const c_char, code: *const c_char) {
let name = CStr::from_ptr(name);
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -20,6 +20,9 @@
}
}
+/// Context keeps information about current lexical code location
+///
+/// This information includes local variables, top-level object (`$`), current object (`this`), and super object (`super`)
#[derive(Debug, Clone, Trace)]
pub struct Context(Cc<ContextInternals>);
impl Context {
@@ -160,8 +163,11 @@
extend: Some(parent),
}
}
+ /// # Panics
+ /// If `name` is already bound
pub fn bind(&mut self, name: IStr, value: Thunk<Val>) -> &mut Self {
- self.bindings.insert(name, value);
+ let old = self.bindings.insert(name, value);
+ assert!(old.is_none(), "variable bound twice in single context call");
self
}
pub fn build(self) -> Context {
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -67,7 +67,10 @@
type FunctionSignature = Vec<(Option<IStr>, bool)>;
+/// Possible errors
+#[allow(missing_docs)]
#[derive(Error, Debug, Clone, Trace)]
+#[non_exhaustive]
pub enum Error {
#[error("intrinsic not found: {0}")]
IntrinsicNotFound(IStr),
@@ -217,9 +220,13 @@
}
}
+/// Single stack trace frame
#[derive(Clone, Debug, Trace)]
pub struct StackTraceElement {
+ /// Source of this frame
+ /// Some frames only act as description, without attached source
pub location: Option<ExprLocation>,
+ /// Frame description
pub desc: String,
}
#[derive(Debug, Clone, Trace)]
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -269,9 +269,7 @@
}
}
let this = builder.build();
- let _ctx = ctx
- .extend(GcHashMap::new(), None, None, Some(this.clone()))
- .into_future(fctx);
+ fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));
Ok(this)
}
@@ -356,7 +354,7 @@
ctx: Context,
value: &LocExpr,
args: &ArgsDesc,
- loc: CallLocation,
+ loc: CallLocation<'_>,
tailstrict: bool,
) -> Result<Val> {
let value = evaluate(s.clone(), ctx.clone(), value)?;
@@ -602,7 +600,7 @@
}
Slice(value, desc) => {
fn parse_idx<T: Typed>(
- loc: CallLocation,
+ loc: CallLocation<'_>,
s: State,
ctx: &Context,
expr: &Option<LocExpr>,
crates/jrsonnet-evaluator/src/function/builtin.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/builtin.rs
+++ b/crates/jrsonnet-evaluator/src/function/builtin.rs
@@ -24,7 +24,13 @@
/// Parameter names for named calls
fn params(&self) -> &[BuiltinParam];
/// Call the builtin
- fn call(&self, s: State, ctx: Context, loc: CallLocation, args: &dyn ArgsLike) -> Result<Val>;
+ fn call(
+ &self,
+ s: State,
+ ctx: Context,
+ loc: CallLocation<'_>,
+ args: &dyn ArgsLike,
+ ) -> Result<Val>;
}
pub trait StaticBuiltin: Builtin + Send + Sync
@@ -70,7 +76,13 @@
&self.params
}
- fn call(&self, s: State, ctx: Context, _loc: CallLocation, args: &dyn ArgsLike) -> Result<Val> {
+ fn call(
+ &self,
+ s: State,
+ ctx: Context,
+ _loc: CallLocation<'_>,
+ args: &dyn ArgsLike,
+ ) -> Result<Val> {
let args = parse_builtin_call(s.clone(), ctx, &self.params, args, true)?;
let args = args
.into_iter()
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -18,43 +18,50 @@
pub mod native;
pub mod parse;
+/// Function callsite location.
+/// Either from other jsonnet code, specified by expression location, or from native (without location).
#[derive(Clone, Copy)]
pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);
impl<'l> CallLocation<'l> {
+ /// Construct new location for calls coming from specified jsonnet expression location.
pub const fn new(loc: &'l ExprLocation) -> Self {
Self(Some(loc))
}
}
impl CallLocation<'static> {
+ /// Construct new location for calls coming from native code.
pub const fn native() -> Self {
Self(None)
}
}
-/// Function implemented in jsonnet
+/// Represents Jsonnet function defined in code.
#[derive(Debug, PartialEq, Trace)]
pub struct FuncDesc {
- /// In expressions like
+ /// # Example
+ ///
+ /// In expressions like this, deducted to `a`, unspecified otherwise.
/// ```jsonnet
/// local a = function() ...
/// local a() ...
/// { a: function() ... }
/// { a() = ... }
/// ```
- ///
- /// Deducted to `a`, unspecified otherwise
pub name: IStr,
- /// Context, in which this function was evaluated
+ /// Context, in which this function was evaluated.
///
- /// I.e in
+ /// # Example
+ /// In
/// ```jsonnet
/// local a = 2;
/// function() ...
/// ```
- /// context will contain `a`
+ /// context will contain `a`.
pub ctx: Context,
+ /// Function parameter definition
pub params: ParamsDesc,
+ /// Function body
pub body: LocExpr,
}
impl FuncDesc {
@@ -82,17 +89,17 @@
}
}
-/// Any possible function value, including plain functions and user-provided builtins
+/// Represents a Jsonnet function value, including plain functions and user-provided builtins.
#[allow(clippy::module_name_repetitions)]
#[derive(Trace, Clone)]
pub enum FuncVal {
- /// std.id
+ /// Identity function, kept this way for comparsions.
Id,
- /// Plain function implemented in jsonnet
+ /// Plain function implemented in jsonnet.
Normal(Cc<FuncDesc>),
- /// Standard library function
+ /// Standard library function.
StaticBuiltin(#[trace(skip)] &'static dyn StaticBuiltin),
- /// User-provided function
+ /// User-provided function.
Builtin(Cc<TraceBox<dyn Builtin>>),
}
@@ -110,9 +117,7 @@
}
impl FuncVal {
- pub fn into_native<D: NativeDesc>(self) -> D::Value {
- D::into_native(self)
- }
+ /// Amount of non-default required arguments
pub fn params_len(&self) -> usize {
match self {
Self::Id => 1,
@@ -121,6 +126,7 @@
Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),
}
}
+ /// Function name, as defined in code.
pub fn name(&self) -> IStr {
match self {
Self::Id => "id".into(),
@@ -129,11 +135,14 @@
Self::Builtin(builtin) => builtin.name().into(),
}
}
+ /// Call function using arguments evaluated in specified `call_ctx` [`Context`].
+ ///
+ /// If `tailstrict` is specified - then arguments will be evaluated before being passed to function body.
pub fn evaluate(
&self,
s: State,
call_ctx: Context,
- loc: CallLocation,
+ loc: CallLocation<'_>,
args: &dyn ArgsLike,
tailstrict: bool,
) -> Result<Val> {
@@ -156,13 +165,22 @@
Self::Builtin(b) => b.call(s, call_ctx, loc, args),
}
}
+ /// Helper method, which calls [`Self::evaluate`] with sensible defaults for native code.
pub fn evaluate_simple(&self, s: State, args: &dyn ArgsLike) -> Result<Val> {
self.evaluate(s, Context::default(), CallLocation::native(), args, true)
}
+ /// Convert jsonnet function to plain `Fn` value.
+ pub fn into_native<D: NativeDesc>(self) -> D::Value {
+ D::into_native(self)
+ }
+ /// Is this function an indentity function.
+ ///
+ /// Currently only works for builtin `std.id`, aka `Self::Id` value, `function(x) x` defined by jsonnet will not count as identity.
pub const fn is_identity(&self) -> bool {
matches!(self, Self::Id)
}
+ /// Identity function value.
pub const fn identity() -> Self {
Self::Id
}
crates/jrsonnet-evaluator/src/gc.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/gc.rs
+++ b/crates/jrsonnet-evaluator/src/gc.rs
@@ -22,7 +22,7 @@
}
impl<T: ?Sized + Trace> Trace for TraceBox<T> {
- fn trace(&self, tracer: &mut Tracer) {
+ fn trace(&self, tracer: &mut Tracer<'_>) {
self.0.trace(tracer);
}
@@ -92,7 +92,7 @@
where
V: Trace,
{
- fn trace(&self, tracer: &mut jrsonnet_gcmodule::Tracer) {
+ fn trace(&self, tracer: &mut Tracer<'_>) {
for v in &self.0 {
v.trace(tracer);
}
@@ -133,7 +133,7 @@
K: Trace,
V: Trace,
{
- fn trace(&self, tracer: &mut jrsonnet_gcmodule::Tracer) {
+ fn trace(&self, tracer: &mut Tracer<'_>) {
for (k, v) in &self.0 {
k.trace(tracer);
v.trace(tracer);
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1,4 +1,18 @@
-#![warn(clippy::all, clippy::nursery, clippy::pedantic)]
+//! jsonnet interpreter implementation
+
+#![deny(unsafe_op_in_unsafe_fn)]
+#![warn(
+ clippy::all,
+ clippy::nursery,
+ clippy::pedantic,
+ // missing_docs,
+ elided_lifetimes_in_paths,
+ explicit_outlives_requirements,
+ noop_method_call,
+ single_use_lifetimes,
+ variant_size_differences,
+ rustdoc::all
+)]
#![allow(
macro_expanded_macro_exports_accessed_by_absolute_paths,
clippy::ptr_arg,
@@ -67,23 +81,32 @@
use trace::{CompactFormat, TraceFormat};
pub use val::{ManifestFormat, Thunk, Val};
+/// Thunk without bound `super`/`this`
+/// object inheritance may be overriden multiple times, and will be fixed only on field read
pub trait Unbound: Trace {
+ /// Type of value after object context is bound
type Bound;
+ /// Create value bound to specified object context
fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;
}
+/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code
+/// Standard jsonnet fields are always unbound
#[derive(Clone, Trace)]
-pub enum LazyBinding {
- Bindable(Cc<TraceBox<dyn Unbound<Bound = Thunk<Val>>>>),
+pub enum MaybeUnbound {
+ /// Value needs to be bound to `this`/`super`
+ Unbound(Cc<TraceBox<dyn Unbound<Bound = Thunk<Val>>>>),
+ /// Value is object-independent
Bound(Thunk<Val>),
}
-impl Debug for LazyBinding {
+impl Debug for MaybeUnbound {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "LazyBinding")
+ write!(f, "MaybeUnbound")
}
}
-impl LazyBinding {
+impl MaybeUnbound {
+ /// Attach object context to value, if required
pub fn evaluate(
&self,
s: State,
@@ -91,17 +114,19 @@
this: Option<ObjValue>,
) -> Result<Thunk<Val>> {
match self {
- Self::Bindable(v) => v.bind(s, sup, this),
+ Self::Unbound(v) => v.bind(s, sup, this),
Self::Bound(v) => Ok(v.clone()),
}
}
}
-/// During import, this trait will be called to create initial context for file
-/// It may initialize global variables, stdlib for example
+/// During import, this trait will be called to create initial context for file.
+/// It may initialize global variables, stdlib for example.
pub trait ContextInitializer {
+ /// Initialize default file context.
fn initialize(&self, state: State, for_file: Source) -> Context;
-
+ /// Allows upcasting from abstract to concrete context initializer.
+ /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.
fn as_any(&self) -> &dyn Any;
}
@@ -116,6 +141,7 @@
}
}
+/// Dynamically reconfigurable evaluation settings
pub struct EvaluationSettings {
/// Limits recursion by limiting the number of stack frames
pub max_stack: usize,
@@ -401,7 +427,7 @@
/// Executes code creating a new stack frame
pub fn push<T>(
&self,
- e: CallLocation,
+ e: CallLocation<'_>,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<T>,
) -> Result<T> {
@@ -547,16 +573,13 @@
/// Internals
impl State {
- // fn data(&self) -> Ref<EvaluationData> {
- // self.0.data.borrow()
- // }
- fn data_mut(&self) -> RefMut<EvaluationData> {
+ fn data_mut(&self) -> RefMut<'_, EvaluationData> {
self.0.data.borrow_mut()
}
- pub fn settings(&self) -> Ref<EvaluationSettings> {
+ pub fn settings(&self) -> Ref<'_, EvaluationSettings> {
self.0.settings.borrow()
}
- pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {
+ pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {
self.0.settings.borrow_mut()
}
}
@@ -623,13 +646,13 @@
pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {
self.import_resolver().resolve(path.as_ref())
}
- pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {
+ pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {
Ref::map(self.settings(), |s| &*s.import_resolver)
}
pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {
self.settings_mut().import_resolver = resolver;
}
- pub fn context_initializer(&self) -> Ref<dyn ContextInitializer> {
+ pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {
Ref::map(self.settings(), |s| &*s.context_initializer)
}
@@ -640,7 +663,7 @@
self.settings_mut().manifest_format = format;
}
- pub fn trace_format(&self) -> Ref<dyn TraceFormat> {
+ pub fn trace_format(&self) -> Ref<'_, dyn TraceFormat> {
Ref::map(self.settings(), |s| &*s.trace_format)
}
pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -15,7 +15,7 @@
function::CallLocation,
gc::{GcHashMap, GcHashSet, TraceBox},
operator::evaluate_add_op,
- throw, LazyBinding, Result, State, Thunk, Unbound, Val,
+ throw, MaybeUnbound, Result, State, Thunk, Unbound, Val,
};
#[cfg(not(feature = "exp-preserve-order"))]
@@ -100,7 +100,7 @@
pub add: bool,
pub visibility: Visibility,
original_index: FieldIndex,
- pub invoke: LazyBinding,
+ pub invoke: MaybeUnbound,
pub location: Option<ExprLocation>,
}
@@ -208,7 +208,7 @@
new.insert(key, value);
Self::new(Some(self), Cc::new(new), Cc::new(Vec::new()))
}
- pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder> {
+ pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {
ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())
}
@@ -239,6 +239,8 @@
}
/// Run callback for every field found in object
+ ///
+ /// Returns true if ended prematurely
pub(crate) fn enum_fields(
&self,
depth: SuperDepth,
@@ -500,7 +502,7 @@
self.assertions.push(assertion);
self
}
- pub fn member(&mut self, name: IStr) -> ObjMemberBuilder<ValueBuilder> {
+ pub fn member(&mut self, name: IStr) -> ObjMemberBuilder<ValueBuilder<'_>> {
let field_index = self.next_field_index;
self.next_field_index = self.next_field_index.next();
ObjMemberBuilder::new(ValueBuilder(self), name, field_index)
@@ -558,7 +560,7 @@
self.location = Some(location);
self
}
- fn build_member(self, binding: LazyBinding) -> (Kind, IStr, ObjMember) {
+ fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {
(
self.kind,
self.name,
@@ -574,18 +576,18 @@
}
pub struct ValueBuilder<'v>(&'v mut ObjValueBuilder);
-impl<'v> ObjMemberBuilder<ValueBuilder<'v>> {
+impl ObjMemberBuilder<ValueBuilder<'_>> {
pub fn value(self, s: State, value: Val) -> Result<()> {
- self.binding(s, LazyBinding::Bound(Thunk::evaluated(value)))
+ self.binding(s, MaybeUnbound::Bound(Thunk::evaluated(value)))
}
pub fn bindable(
self,
s: State,
bindable: TraceBox<dyn Unbound<Bound = Thunk<Val>>>,
) -> Result<()> {
- self.binding(s, LazyBinding::Bindable(Cc::new(bindable)))
+ self.binding(s, MaybeUnbound::Unbound(Cc::new(bindable)))
}
- pub fn binding(self, s: State, binding: LazyBinding) -> Result<()> {
+ pub fn binding(self, s: State, binding: MaybeUnbound) -> Result<()> {
let (receiver, name, member) = self.build_member(binding);
let location = member.location.clone();
let old = receiver.0.map.insert(name.clone(), member);
@@ -601,14 +603,14 @@
}
pub struct ExtendBuilder<'v>(&'v mut ObjValue);
-impl<'v> ObjMemberBuilder<ExtendBuilder<'v>> {
+impl ObjMemberBuilder<ExtendBuilder<'_>> {
pub fn value(self, value: Val) {
- self.binding(LazyBinding::Bound(Thunk::evaluated(value)));
+ self.binding(MaybeUnbound::Bound(Thunk::evaluated(value)));
}
pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Thunk<Val>>>) {
- self.binding(LazyBinding::Bindable(Cc::new(bindable)));
+ self.binding(MaybeUnbound::Unbound(Cc::new(bindable)));
}
- pub fn binding(self, binding: LazyBinding) {
+ pub fn binding(self, binding: MaybeUnbound) {
let (receiver, name, member) = self.build_member(binding);
let new = receiver.0.clone();
*receiver.0 = new.extend_with_raw_member(name, member);
crates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/format.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs
@@ -36,7 +36,7 @@
type ParseResult<'t, T> = std::result::Result<(T, &'t str), FormatError>;
-pub fn try_parse_mapping_key(str: &str) -> ParseResult<&str> {
+pub fn try_parse_mapping_key(str: &str) -> ParseResult<'_, &str> {
if str.is_empty() {
return Err(TruncatedFormatCode);
}
@@ -96,7 +96,7 @@
pub sign: bool,
}
-pub fn try_parse_cflags(str: &str) -> ParseResult<CFlags> {
+pub fn try_parse_cflags(str: &str) -> ParseResult<'_, CFlags> {
if str.is_empty() {
return Err(TruncatedFormatCode);
}
@@ -125,7 +125,7 @@
Star,
Fixed(usize),
}
-pub fn try_parse_field_width(str: &str) -> ParseResult<Width> {
+pub fn try_parse_field_width(str: &str) -> ParseResult<'_, Width> {
if str.is_empty() {
return Err(TruncatedFormatCode);
}
@@ -146,7 +146,7 @@
Ok((Width::Fixed(out), &str[digits..]))
}
-pub fn try_parse_precision(str: &str) -> ParseResult<Option<Width>> {
+pub fn try_parse_precision(str: &str) -> ParseResult<'_, Option<Width>> {
if str.is_empty() {
return Err(TruncatedFormatCode);
}
@@ -159,7 +159,7 @@
}
// Only skips
-pub fn try_parse_length_modifier(str: &str) -> ParseResult<()> {
+pub fn try_parse_length_modifier(str: &str) -> ParseResult<'_, ()> {
if str.is_empty() {
return Err(TruncatedFormatCode);
}
@@ -191,7 +191,7 @@
caps: bool,
}
-pub fn parse_conversion_type(str: &str) -> ParseResult<ConvType> {
+pub fn parse_conversion_type(str: &str) -> ParseResult<'_, ConvType> {
if str.is_empty() {
return Err(TruncatedFormatCode);
}
@@ -226,7 +226,7 @@
convtype: ConvTypeV,
caps: bool,
}
-pub fn parse_code(str: &str) -> ParseResult<Code> {
+pub fn parse_code(str: &str) -> ParseResult<'_, Code<'_>> {
if str.is_empty() {
return Err(TruncatedFormatCode);
}
@@ -255,7 +255,7 @@
String(&'s str),
Code(Code<'s>),
}
-pub fn parse_codes(mut str: &str) -> Result<Vec<Element>> {
+pub fn parse_codes(mut str: &str) -> Result<Vec<Element<'_>>> {
let mut bytes = str.as_bytes();
let mut out = vec![];
let mut offset = 0;
@@ -475,7 +475,7 @@
s: State,
out: &mut String,
value: &Val,
- code: &Code,
+ code: &Code<'_>,
width: usize,
precision: Option<usize>,
) -> Result<()> {
crates/jrsonnet-evaluator/src/stdlib/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
@@ -116,7 +116,7 @@
|| {
let value = obj.get(s.clone(), field.clone())?.unwrap();
manifest_json_ex_buf(s.clone(), &value, buf, cur_padding, options)?;
- Ok(Val::Null)
+ Ok(())
},
)?;
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -186,16 +186,26 @@
}
}
+/// Represents a Jsonnet array value.
#[derive(Debug, Clone, Trace)]
// may contrain other ArrValue
#[trace(tracking(force))]
pub enum ArrValue {
+ /// Layout optimized byte array.
Bytes(#[trace(skip)] IBytes),
+ /// Every element is lazy evaluated.
Lazy(Cc<Vec<Thunk<Val>>>),
+ /// Every field is already evaluated.
Eager(Cc<Vec<Val>>),
+ /// Concatenation of two arrays of any kind.
Extended(Box<(Self, Self)>),
+ /// Represents a integer array in form `[start, start + 1, ... end - 1, end]`.
+ /// This kind of arrays is generated by `std.range(start, end)` call, and used for loops.
Range(i32, i32),
+ /// Sliced array view.
Slice(Box<Slice>),
+ /// Reversed array view.
+ /// Returned by `std.reverse(other)` call
Reversed(Box<Self>),
}
@@ -237,6 +247,7 @@
}))
}
+ /// Array length.
pub fn len(&self) -> usize {
match self {
Self::Bytes(i) => i.len(),
@@ -249,10 +260,14 @@
}
}
+ /// Is array contains no elements?
pub fn is_empty(&self) -> bool {
self.len() == 0
}
+ /// Get array element by index, evaluating it, if it is lazy.
+ ///
+ /// Returns `None` on out-of-bounds condition.
pub fn get(&self, s: State, index: usize) -> Result<Option<Val>> {
match self {
Self::Bytes(i) => i
@@ -297,6 +312,9 @@
}
}
+ /// Get array element by index, without evaluation.
+ ///
+ /// Returns `None` on out-of-bounds condition.
pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
match self {
Self::Bytes(i) => i
@@ -337,6 +355,7 @@
}
}
+ /// Evaluate all array elements, returning new array.
pub fn evaluated(&self, s: State) -> Result<Cc<Vec<Val>>> {
Ok(match self {
Self::Bytes(i) => {
@@ -389,6 +408,7 @@
})
}
+ /// Iterate over elements, evaluating them.
pub fn iter(&self, s: State) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {
(0..self.len()).map(move |idx| match self {
Self::Bytes(b) => Ok(Val::Num(f64::from(b[idx]))),
@@ -400,6 +420,7 @@
})
}
+ /// Iterate over elements, returning lazy values.
pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = Thunk<Val>> + '_ {
(0..self.len()).map(move |idx| match self {
Self::Bytes(b) => Thunk::evaluated(Val::Num(f64::from(b[idx]))),
@@ -411,11 +432,13 @@
})
}
+ /// Return a reversed view on current array.
#[must_use]
pub fn reversed(self) -> Self {
Self::Reversed(Box::new(self))
}
+ /// Return a new array, produced by passing every element of current array to specified callback function.
pub fn map(self, s: State, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {
let mut out = Vec::with_capacity(self.len());
@@ -426,6 +449,7 @@
Ok(Self::Eager(Cc::new(out)))
}
+ /// Return a new array, produced from current array by removing every value, for which specified callback function returns false.
pub fn filter(self, s: State, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
let mut out = Vec::with_capacity(self.len());
@@ -460,12 +484,22 @@
}
}
+/// Represents a Jsonnet value, which can be spliced or indexed (string or array).
#[allow(clippy::module_name_repetitions)]
pub enum IndexableVal {
+ /// String.
Str(IStr),
+ /// Array.
Arr(ArrValue),
}
impl IndexableVal {
+ /// Slice the value.
+ ///
+ /// # Implementation
+ ///
+ /// For strings, will create a copy of specified interval.
+ ///
+ /// For arrays, nothing will be copied on this call, instead [`ArrValue::Slice`] view will be returned.
pub fn slice(
self,
index: Option<BoundedUsize<0, { i32::MAX as usize }>>,
@@ -511,14 +545,24 @@
}
}
+/// Represents any valid Jsonnet value.
#[derive(Debug, Clone, Trace)]
pub enum Val {
+ /// Represents a Jsonnet boolean.
Bool(bool),
+ /// Represents a Jsonnet null value.
Null,
+ /// Represents a Jsonnet string.
Str(IStr),
+ /// Represents a Jsonnet number.
+ /// Should be finite, and not NaN
+ /// This restriction isn't enforced by enum, as enum field can't be marked as private
Num(f64),
+ /// Represents a Jsonnet array.
Arr(ArrValue),
+ /// Represents a Jsonnet object.
Obj(ObjValue),
+ /// Represents a Jsonnet function.
Func(FuncVal),
}
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth1use std::{2 cell::{Ref, RefCell, RefMut},3 collections::HashMap,4 rc::Rc,5};67use jrsonnet_evaluator::{8 error::{Error::*, Result},9 function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},10 gc::{GcHashMap, TraceBox},11 tb, throw_runtime,12 trace::PathResolver,13 typed::{Any, Either, Either2, Either4, VecVal, M1},14 val::{equals, ArrValue},15 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,16};17use jrsonnet_gcmodule::Cc;18use jrsonnet_macros::builtin;19use jrsonnet_parser::Source;2021mod expr;22mod types;23pub use types::*;24mod arrays;25pub use arrays::*;26mod math;27pub use math::*;28mod operator;29pub use operator::*;30mod sort;31pub use sort::*;32mod hash;33pub use hash::*;34mod encoding;35pub use encoding::*;36mod objects;37pub use objects::*;38mod manifest;39pub use manifest::*;40mod parse;41pub use parse::*;4243pub fn stdlib_uncached(s: State, settings: Rc<RefCell<Settings>>) -> ObjValue {44 let mut builder = ObjValueBuilder::new();4546 let expr = expr::stdlib_expr();47 let eval = jrsonnet_evaluator::evaluate(s.clone(), Context::default(), &expr)48 .expect("stdlib.jsonnet should have no errors")49 .as_obj()50 .expect("stdlib.jsonnet should evaluate to object");5152 builder.with_super(eval);5354 for (name, builtin) in [55 ("length".into(), builtin_length::INST),56 // Types57 ("type".into(), builtin_type::INST),58 ("isString".into(), builtin_is_string::INST),59 ("isNumber".into(), builtin_is_number::INST),60 ("isBoolean".into(), builtin_is_boolean::INST),61 ("isObject".into(), builtin_is_object::INST),62 ("isArray".into(), builtin_is_array::INST),63 ("isFunction".into(), builtin_is_function::INST),64 // Arrays65 ("makeArray".into(), builtin_make_array::INST),66 ("slice".into(), builtin_slice::INST),67 ("map".into(), builtin_map::INST),68 ("flatMap".into(), builtin_flatmap::INST),69 ("filter".into(), builtin_filter::INST),70 ("foldl".into(), builtin_foldl::INST),71 ("foldr".into(), builtin_foldr::INST),72 ("range".into(), builtin_range::INST),73 ("join".into(), builtin_join::INST),74 ("reverse".into(), builtin_reverse::INST),75 ("any".into(), builtin_any::INST),76 ("all".into(), builtin_all::INST),77 ("member".into(), builtin_member::INST),78 ("count".into(), builtin_count::INST),79 // Math80 ("modulo".into(), builtin_modulo::INST),81 ("floor".into(), builtin_floor::INST),82 ("ceil".into(), builtin_ceil::INST),83 ("log".into(), builtin_log::INST),84 ("pow".into(), builtin_pow::INST),85 ("sqrt".into(), builtin_sqrt::INST),86 ("sin".into(), builtin_sin::INST),87 ("cos".into(), builtin_cos::INST),88 ("tan".into(), builtin_tan::INST),89 ("asin".into(), builtin_asin::INST),90 ("acos".into(), builtin_acos::INST),91 ("atan".into(), builtin_atan::INST),92 ("exp".into(), builtin_exp::INST),93 ("mantissa".into(), builtin_mantissa::INST),94 ("exponent".into(), builtin_exponent::INST),95 // Operator96 ("mod".into(), builtin_mod::INST),97 ("primitiveEquals".into(), builtin_primitive_equals::INST),98 ("equals".into(), builtin_equals::INST),99 ("format".into(), builtin_format::INST),100 // Sort101 ("sort".into(), builtin_sort::INST),102 // Hash103 ("md5".into(), builtin_md5::INST),104 // Encoding105 ("encodeUTF8".into(), builtin_encode_utf8::INST),106 ("decodeUTF8".into(), builtin_decode_utf8::INST),107 ("base64".into(), builtin_base64::INST),108 ("base64Decode".into(), builtin_base64_decode::INST),109 (110 "base64DecodeBytes".into(),111 builtin_base64_decode_bytes::INST,112 ),113 // Objects114 ("objectFieldsEx".into(), builtin_object_fields_ex::INST),115 ("objectHasEx".into(), builtin_object_has_ex::INST),116 // Manifest117 ("escapeStringJson".into(), builtin_escape_string_json::INST),118 ("manifestJsonEx".into(), builtin_manifest_json_ex::INST),119 ("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),120 // Parsing121 ("parseJson".into(), builtin_parse_json::INST),122 ("parseYaml".into(), builtin_parse_yaml::INST),123 // Misc124 ("codepoint".into(), builtin_codepoint::INST),125 ("substr".into(), builtin_substr::INST),126 ("char".into(), builtin_char::INST),127 ("strReplace".into(), builtin_str_replace::INST),128 ("splitLimit".into(), builtin_splitlimit::INST),129 ("asciiUpper".into(), builtin_ascii_upper::INST),130 ("asciiLower".into(), builtin_ascii_lower::INST),131 ("findSubstr".into(), builtin_find_substr::INST),132 ("startsWith".into(), builtin_starts_with::INST),133 ("endsWith".into(), builtin_ends_with::INST),134 ]135 .iter()136 .cloned()137 {138 builder139 .member(name)140 .hide()141 .value(s.clone(), Val::Func(FuncVal::StaticBuiltin(builtin)))142 .expect("no conflict");143 }144145 builder146 .member("extVar".into())147 .hide()148 .value(149 s.clone(),150 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {151 settings: settings.clone()152 })))),153 )154 .expect("no conflict");155 builder156 .member("native".into())157 .hide()158 .value(159 s.clone(),160 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {161 settings: settings.clone()162 })))),163 )164 .expect("no conflict");165 builder166 .member("trace".into())167 .hide()168 .value(169 s.clone(),170 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace { settings })))),171 )172 .expect("no conflict");173174 builder175 .member("id".into())176 .hide()177 .value(s, Val::Func(FuncVal::Id))178 .expect("no conflict");179180 builder.build()181}182183pub trait TracePrinter {184 fn print_trace(&self, s: State, loc: CallLocation, value: IStr);185}186187pub struct StdTracePrinter {188 resolver: PathResolver,189}190impl StdTracePrinter {191 pub fn new(resolver: PathResolver) -> Self {192 Self { resolver }193 }194}195impl TracePrinter for StdTracePrinter {196 fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {197 eprint!("TRACE:");198 if let Some(loc) = loc.0 {199 let locs = loc.0.map_source_locations(&[loc.1]);200 eprint!(201 " {}:{}",202 match loc.0.source_path().path() {203 Some(p) => self.resolver.resolve(p),204 None => loc.0.source_path().to_string(),205 },206 locs[0].line207 );208 }209 eprintln!(" {}", value);210 }211}212213pub struct Settings {214 /// Used for `std.extVar`215 pub ext_vars: HashMap<IStr, TlaArg>,216 /// Used for `std.native`217 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,218 /// Helper to add globals without implementing custom ContextInitializer219 pub globals: GcHashMap<IStr, Thunk<Val>>,220 /// Used for `std.trace`221 pub trace_printer: Box<dyn TracePrinter>,222 /// Used for `std.thisFile`223 pub path_resolver: PathResolver,224}225226pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {227 let source_name = format!("<extvar:{}>", name);228 Source::new_virtual(source_name.into(), code.into())229}230231pub struct ContextInitializer {232 // When we don't need to support legacy-this-file, we can reuse same context for all files233 #[cfg(not(feature = "legacy-this-file"))]234 context: Context,235 // Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it236 #[cfg(feature = "legacy-this-file")]237 stdlib_obj: ObjValue,238 settings: Rc<RefCell<Settings>>,239}240impl ContextInitializer {241 pub fn new(s: State, resolver: PathResolver) -> Self {242 let settings = Settings {243 ext_vars: Default::default(),244 ext_natives: Default::default(),245 globals: Default::default(),246 trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),247 path_resolver: resolver,248 };249 let settings = Rc::new(RefCell::new(settings));250 Self {251 #[cfg(not(feature = "legacy-this-file"))]252 context: {253 let mut context = ContextBuilder::with_capacity(1);254 context.bind(255 "std".into(),256 Thunk::evaluated(Val::Obj(stdlib_uncached(s, settings.clone()))),257 );258 context.build()259 },260 #[cfg(feature = "legacy-this-file")]261 stdlib_obj: stdlib_uncached(s, settings.clone()),262 settings,263 }264 }265 pub fn settings(&self) -> Ref<Settings> {266 self.settings.borrow()267 }268 pub fn settings_mut(&self) -> RefMut<Settings> {269 self.settings.borrow_mut()270 }271 pub fn add_ext_var(&self, name: IStr, value: Val) {272 self.settings_mut()273 .ext_vars274 .insert(name, TlaArg::Val(value));275 }276 pub fn add_ext_str(&self, name: IStr, value: IStr) {277 self.settings_mut()278 .ext_vars279 .insert(name, TlaArg::String(value));280 }281 pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {282 let code = code.into();283 let source = extvar_source(name, code.clone());284 let parsed = jrsonnet_parser::parse(285 &code,286 &jrsonnet_parser::ParserSettings {287 file_name: source.clone(),288 },289 )290 .map_err(|e| ImportSyntaxError {291 path: source,292 error: Box::new(e),293 })?;294 // self.data_mut().volatile_files.insert(source_name, code);295 self.settings_mut()296 .ext_vars297 .insert(name.into(), TlaArg::Code(parsed));298 Ok(())299 }300 pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {301 self.settings_mut().ext_natives.insert(name, cb);302 }303}304impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {305 #[cfg(not(feature = "legacy-this-file"))]306 fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {307 let out = self.context.clone();308 let globals = &self.settings().globals;309 if globals.is_empty() {310 return out;311 }312313 let mut out = ContextBuilder::extend(out);314 for (k, v) in globals.iter() {315 out.bind(k.clone(), v.clone());316 }317 out.build()318 }319 #[cfg(feature = "legacy-this-file")]320 fn initialize(&self, s: State, source: Source) -> jrsonnet_evaluator::Context {321 let mut builder = ObjValueBuilder::new();322 builder.with_super(self.stdlib_obj.clone());323 builder324 .member("thisFile".into())325 .hide()326 .value(327 s,328 Val::Str(match source.source_path().path() {329 Some(p) => self.settings().path_resolver.resolve(p).into(),330 None => source.source_path().to_string().into(),331 }),332 )333 .expect("this object builder is empty");334 let stdlib_with_this_file = builder.build();335336 let mut context = ContextBuilder::with_capacity(1);337 context.bind(338 "std".into(),339 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),340 );341 for (k, v) in self.settings().globals.iter() {342 context.bind(k.clone(), v.clone());343 }344 context.build()345 }346 fn as_any(&self) -> &dyn std::any::Any {347 self348 }349}350351#[builtin]352fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {353 use Either4::*;354 Ok(match x {355 A(x) => x.chars().count(),356 B(x) => x.len(),357 C(x) => x.len(),358 D(f) => f.params_len(),359 })360}361362#[builtin]363const fn builtin_codepoint(str: char) -> Result<u32> {364 Ok(str as u32)365}366367#[builtin]368fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {369 Ok(str.chars().skip(from).take(len).collect())370}371372#[builtin(fields(373 settings: Rc<RefCell<Settings>>,374))]375fn builtin_ext_var(this: &builtin_ext_var, s: State, x: IStr) -> Result<Any> {376 let ctx = s.create_default_context(extvar_source(&x, ""));377 Ok(Any(this378 .settings379 .borrow()380 .ext_vars381 .get(&x)382 .cloned()383 .ok_or_else(|| UndefinedExternalVariable(x))?384 .evaluate_arg(s.clone(), ctx, true)?385 .evaluate(s)?))386}387388#[builtin(fields(389 settings: Rc<RefCell<Settings>>,390))]391fn builtin_native(this: &builtin_native, name: IStr) -> Result<Any> {392 Ok(Any(this393 .settings394 .borrow()395 .ext_natives396 .get(&name)397 .cloned()398 .map_or(Val::Null, |v| {399 Val::Func(FuncVal::Builtin(v.clone()))400 })))401}402403#[builtin]404fn builtin_char(n: u32) -> Result<char> {405 Ok(std::char::from_u32(n).ok_or_else(|| InvalidUnicodeCodepointGot(n))?)406}407408#[builtin(fields(409 settings: Rc<RefCell<Settings>>,410))]411fn builtin_trace(412 this: &builtin_trace,413 s: State,414 loc: CallLocation,415 str: IStr,416 rest: Thunk<Val>,417) -> Result<Any> {418 this.settings419 .borrow()420 .trace_printer421 .print_trace(s.clone(), loc, str);422 Ok(Any(rest.evaluate(s)?))423}424425#[builtin]426fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {427 Ok(str.replace(&from as &str, &to as &str))428}429430#[builtin]431fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {432 use Either2::*;433 Ok(VecVal(Cc::new(match maxsplits {434 A(n) => str435 .splitn(n + 1, &c as &str)436 .map(|s| Val::Str(s.into()))437 .collect(),438 B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),439 })))440}441442#[builtin]443fn builtin_ascii_upper(str: IStr) -> Result<String> {444 Ok(str.to_ascii_uppercase())445}446447#[builtin]448fn builtin_ascii_lower(str: IStr) -> Result<String> {449 Ok(str.to_ascii_lowercase())450}451452#[builtin]453fn builtin_find_substr(pat: IStr, str: IStr) -> Result<ArrValue> {454 if pat.is_empty() || str.is_empty() || pat.len() > str.len() {455 return Ok(ArrValue::empty());456 }457458 let str = str.as_str();459 let pat = pat.as_bytes();460 let strb = str.as_bytes();461462 let max_pos = str.len() - pat.len();463464 let mut out: Vec<Val> = Vec::new();465 for (ch_idx, (i, _)) in str466 .char_indices()467 .take_while(|(i, _)| i <= &max_pos)468 .enumerate()469 {470 if &strb[i..i + pat.len()] == pat {471 out.push(Val::Num(ch_idx as f64))472 }473 }474 Ok(out.into())475}476477#[allow(clippy::comparison_chain)]478#[builtin]479fn builtin_starts_with(480 s: State,481 a: Either![IStr, ArrValue],482 b: Either![IStr, ArrValue],483) -> Result<bool> {484 Ok(match (a, b) {485 (Either2::A(a), Either2::A(b)) => a.starts_with(b.as_str()),486 (Either2::B(a), Either2::B(b)) => {487 if b.len() > a.len() {488 return Ok(false);489 } else if b.len() == a.len() {490 return equals(s, &Val::Arr(a), &Val::Arr(b));491 } else {492 for (a, b) in a493 .slice(None, Some(b.len()), None)494 .iter(s.clone())495 .zip(b.iter(s.clone()))496 {497 let a = a?;498 let b = b?;499 if !equals(s.clone(), &a, &b)? {500 return Ok(false);501 }502 }503 true504 }505 }506 _ => throw_runtime!("both arguments should be of the same type"),507 })508}509510#[allow(clippy::comparison_chain)]511#[builtin]512fn builtin_ends_with(513 s: State,514 a: Either![IStr, ArrValue],515 b: Either![IStr, ArrValue],516) -> Result<bool> {517 Ok(match (a, b) {518 (Either2::A(a), Either2::A(b)) => a.ends_with(b.as_str()),519 (Either2::B(a), Either2::B(b)) => {520 if b.len() > a.len() {521 return Ok(false);522 } else if b.len() == a.len() {523 return equals(s, &Val::Arr(a), &Val::Arr(b));524 } else {525 let a_len = a.len();526 for (a, b) in a527 .slice(Some(a_len - b.len()), None, None)528 .iter(s.clone())529 .zip(b.iter(s.clone()))530 {531 let a = a?;532 let b = b?;533 if !equals(s.clone(), &a, &b)? {534 return Ok(false);535 }536 }537 true538 }539 }540 _ => throw_runtime!("both arguments should be of the same type"),541 })542}543544pub trait StateExt {545 /// This method was previously implemented in jrsonnet-evaluator itself546 fn with_stdlib(&self);547 fn add_global(&self, name: IStr, value: Thunk<Val>);548}549550impl StateExt for State {551 fn with_stdlib(&self) {552 let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());553 self.settings_mut().context_initializer = Box::new(initializer)554 }555 fn add_global(&self, name: IStr, value: Thunk<Val>) {556 self.settings()557 .context_initializer558 .as_any()559 .downcast_ref::<ContextInitializer>()560 .expect("not standard context initializer")561 .settings_mut()562 .globals563 .insert(name, value);564 }565}1use std::{2 cell::{Ref, RefCell, RefMut},3 collections::HashMap,4 rc::Rc,5};67use jrsonnet_evaluator::{8 error::{Error::*, Result},9 function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},10 gc::{GcHashMap, TraceBox},11 tb, throw_runtime,12 trace::PathResolver,13 typed::{Any, Either, Either2, Either4, VecVal, M1},14 val::{equals, ArrValue},15 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,16};17use jrsonnet_gcmodule::Cc;18use jrsonnet_macros::builtin;19use jrsonnet_parser::Source;2021mod expr;22mod types;23pub use types::*;24mod arrays;25pub use arrays::*;26mod math;27pub use math::*;28mod operator;29pub use operator::*;30mod sort;31pub use sort::*;32mod hash;33pub use hash::*;34mod encoding;35pub use encoding::*;36mod objects;37pub use objects::*;38mod manifest;39pub use manifest::*;40mod parse;41pub use parse::*;4243pub fn stdlib_uncached(s: State, settings: Rc<RefCell<Settings>>) -> ObjValue {44 let mut builder = ObjValueBuilder::new();4546 let expr = expr::stdlib_expr();47 let eval = jrsonnet_evaluator::evaluate(s.clone(), Context::default(), &expr)48 .expect("stdlib.jsonnet should have no errors")49 .as_obj()50 .expect("stdlib.jsonnet should evaluate to object");5152 builder.with_super(eval);5354 for (name, builtin) in [55 ("length", builtin_length::INST),56 // Types57 ("type", builtin_type::INST),58 ("isString", builtin_is_string::INST),59 ("isNumber", builtin_is_number::INST),60 ("isBoolean", builtin_is_boolean::INST),61 ("isObject", builtin_is_object::INST),62 ("isArray", builtin_is_array::INST),63 ("isFunction", builtin_is_function::INST),64 // Arrays65 ("makeArray", builtin_make_array::INST),66 ("slice", builtin_slice::INST),67 ("map", builtin_map::INST),68 ("flatMap", builtin_flatmap::INST),69 ("filter", builtin_filter::INST),70 ("foldl", builtin_foldl::INST),71 ("foldr", builtin_foldr::INST),72 ("range", builtin_range::INST),73 ("join", builtin_join::INST),74 ("reverse", builtin_reverse::INST),75 ("any", builtin_any::INST),76 ("all", builtin_all::INST),77 ("member", builtin_member::INST),78 ("count", builtin_count::INST),79 // Math80 ("modulo", builtin_modulo::INST),81 ("floor", builtin_floor::INST),82 ("ceil", builtin_ceil::INST),83 ("log", builtin_log::INST),84 ("pow", builtin_pow::INST),85 ("sqrt", builtin_sqrt::INST),86 ("sin", builtin_sin::INST),87 ("cos", builtin_cos::INST),88 ("tan", builtin_tan::INST),89 ("asin", builtin_asin::INST),90 ("acos", builtin_acos::INST),91 ("atan", builtin_atan::INST),92 ("exp", builtin_exp::INST),93 ("mantissa", builtin_mantissa::INST),94 ("exponent", builtin_exponent::INST),95 // Operator96 ("mod", builtin_mod::INST),97 ("primitiveEquals", builtin_primitive_equals::INST),98 ("equals", builtin_equals::INST),99 ("format", builtin_format::INST),100 // Sort101 ("sort", builtin_sort::INST),102 // Hash103 ("md5", builtin_md5::INST),104 // Encoding105 ("encodeUTF8", builtin_encode_utf8::INST),106 ("decodeUTF8", builtin_decode_utf8::INST),107 ("base64", builtin_base64::INST),108 ("base64Decode", builtin_base64_decode::INST),109 ("base64DecodeBytes", builtin_base64_decode_bytes::INST),110 // Objects111 ("objectFieldsEx", builtin_object_fields_ex::INST),112 ("objectHasEx", builtin_object_has_ex::INST),113 // Manifest114 ("escapeStringJson", builtin_escape_string_json::INST),115 ("manifestJsonEx", builtin_manifest_json_ex::INST),116 ("manifestYamlDoc", builtin_manifest_yaml_doc::INST),117 // Parsing118 ("parseJson", builtin_parse_json::INST),119 ("parseYaml", builtin_parse_yaml::INST),120 // Misc121 ("codepoint", builtin_codepoint::INST),122 ("substr", builtin_substr::INST),123 ("char", builtin_char::INST),124 ("strReplace", builtin_str_replace::INST),125 ("splitLimit", builtin_splitlimit::INST),126 ("asciiUpper", builtin_ascii_upper::INST),127 ("asciiLower", builtin_ascii_lower::INST),128 ("findSubstr", builtin_find_substr::INST),129 ("startsWith", builtin_starts_with::INST),130 ("endsWith", builtin_ends_with::INST),131 ]132 .iter()133 .cloned()134 {135 builder136 .member(name.into())137 .hide()138 .value(s.clone(), Val::Func(FuncVal::StaticBuiltin(builtin)))139 .expect("no conflict");140 }141142 builder143 .member("extVar".into())144 .hide()145 .value(146 s.clone(),147 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {148 settings: settings.clone()149 })))),150 )151 .expect("no conflict");152 builder153 .member("native".into())154 .hide()155 .value(156 s.clone(),157 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {158 settings: settings.clone()159 })))),160 )161 .expect("no conflict");162 builder163 .member("trace".into())164 .hide()165 .value(166 s.clone(),167 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace { settings })))),168 )169 .expect("no conflict");170171 builder172 .member("id".into())173 .hide()174 .value(s, Val::Func(FuncVal::Id))175 .expect("no conflict");176177 builder.build()178}179180pub trait TracePrinter {181 fn print_trace(&self, s: State, loc: CallLocation, value: IStr);182}183184pub struct StdTracePrinter {185 resolver: PathResolver,186}187impl StdTracePrinter {188 pub fn new(resolver: PathResolver) -> Self {189 Self { resolver }190 }191}192impl TracePrinter for StdTracePrinter {193 fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {194 eprint!("TRACE:");195 if let Some(loc) = loc.0 {196 let locs = loc.0.map_source_locations(&[loc.1]);197 eprint!(198 " {}:{}",199 match loc.0.source_path().path() {200 Some(p) => self.resolver.resolve(p),201 None => loc.0.source_path().to_string(),202 },203 locs[0].line204 );205 }206 eprintln!(" {}", value);207 }208}209210pub struct Settings {211 /// Used for `std.extVar`212 pub ext_vars: HashMap<IStr, TlaArg>,213 /// Used for `std.native`214 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,215 /// Helper to add globals without implementing custom ContextInitializer216 pub globals: GcHashMap<IStr, Thunk<Val>>,217 /// Used for `std.trace`218 pub trace_printer: Box<dyn TracePrinter>,219 /// Used for `std.thisFile`220 pub path_resolver: PathResolver,221}222223pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {224 let source_name = format!("<extvar:{}>", name);225 Source::new_virtual(source_name.into(), code.into())226}227228pub struct ContextInitializer {229 // When we don't need to support legacy-this-file, we can reuse same context for all files230 #[cfg(not(feature = "legacy-this-file"))]231 context: Context,232 // Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it233 #[cfg(feature = "legacy-this-file")]234 stdlib_obj: ObjValue,235 settings: Rc<RefCell<Settings>>,236}237impl ContextInitializer {238 pub fn new(s: State, resolver: PathResolver) -> Self {239 let settings = Settings {240 ext_vars: Default::default(),241 ext_natives: Default::default(),242 globals: Default::default(),243 trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),244 path_resolver: resolver,245 };246 let settings = Rc::new(RefCell::new(settings));247 Self {248 #[cfg(not(feature = "legacy-this-file"))]249 context: {250 let mut context = ContextBuilder::with_capacity(1);251 context.bind(252 "std".into(),253 Thunk::evaluated(Val::Obj(stdlib_uncached(s, settings.clone()))),254 );255 context.build()256 },257 #[cfg(feature = "legacy-this-file")]258 stdlib_obj: stdlib_uncached(s, settings.clone()),259 settings,260 }261 }262 pub fn settings(&self) -> Ref<Settings> {263 self.settings.borrow()264 }265 pub fn settings_mut(&self) -> RefMut<Settings> {266 self.settings.borrow_mut()267 }268 pub fn add_ext_var(&self, name: IStr, value: Val) {269 self.settings_mut()270 .ext_vars271 .insert(name, TlaArg::Val(value));272 }273 pub fn add_ext_str(&self, name: IStr, value: IStr) {274 self.settings_mut()275 .ext_vars276 .insert(name, TlaArg::String(value));277 }278 pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {279 let code = code.into();280 let source = extvar_source(name, code.clone());281 let parsed = jrsonnet_parser::parse(282 &code,283 &jrsonnet_parser::ParserSettings {284 file_name: source.clone(),285 },286 )287 .map_err(|e| ImportSyntaxError {288 path: source,289 error: Box::new(e),290 })?;291 // self.data_mut().volatile_files.insert(source_name, code);292 self.settings_mut()293 .ext_vars294 .insert(name.into(), TlaArg::Code(parsed));295 Ok(())296 }297 pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {298 self.settings_mut().ext_natives.insert(name, cb);299 }300}301impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {302 #[cfg(not(feature = "legacy-this-file"))]303 fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {304 let out = self.context.clone();305 let globals = &self.settings().globals;306 if globals.is_empty() {307 return out;308 }309310 let mut out = ContextBuilder::extend(out);311 for (k, v) in globals.iter() {312 out.bind(k.clone(), v.clone());313 }314 out.build()315 }316 #[cfg(feature = "legacy-this-file")]317 fn initialize(&self, s: State, source: Source) -> jrsonnet_evaluator::Context {318 let mut builder = ObjValueBuilder::new();319 builder.with_super(self.stdlib_obj.clone());320 builder321 .member("thisFile".into())322 .hide()323 .value(324 s,325 Val::Str(match source.source_path().path() {326 Some(p) => self.settings().path_resolver.resolve(p).into(),327 None => source.source_path().to_string().into(),328 }),329 )330 .expect("this object builder is empty");331 let stdlib_with_this_file = builder.build();332333 let mut context = ContextBuilder::with_capacity(1);334 context.bind(335 "std".into(),336 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),337 );338 for (k, v) in self.settings().globals.iter() {339 context.bind(k.clone(), v.clone());340 }341 context.build()342 }343 fn as_any(&self) -> &dyn std::any::Any {344 self345 }346}347348#[builtin]349fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {350 use Either4::*;351 Ok(match x {352 A(x) => x.chars().count(),353 B(x) => x.len(),354 C(x) => x.len(),355 D(f) => f.params_len(),356 })357}358359#[builtin]360const fn builtin_codepoint(str: char) -> Result<u32> {361 Ok(str as u32)362}363364#[builtin]365fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {366 Ok(str.chars().skip(from).take(len).collect())367}368369#[builtin(fields(370 settings: Rc<RefCell<Settings>>,371))]372fn builtin_ext_var(this: &builtin_ext_var, s: State, x: IStr) -> Result<Any> {373 let ctx = s.create_default_context(extvar_source(&x, ""));374 Ok(Any(this375 .settings376 .borrow()377 .ext_vars378 .get(&x)379 .cloned()380 .ok_or_else(|| UndefinedExternalVariable(x))?381 .evaluate_arg(s.clone(), ctx, true)?382 .evaluate(s)?))383}384385#[builtin(fields(386 settings: Rc<RefCell<Settings>>,387))]388fn builtin_native(this: &builtin_native, name: IStr) -> Result<Any> {389 Ok(Any(this390 .settings391 .borrow()392 .ext_natives393 .get(&name)394 .cloned()395 .map_or(Val::Null, |v| {396 Val::Func(FuncVal::Builtin(v.clone()))397 })))398}399400#[builtin]401fn builtin_char(n: u32) -> Result<char> {402 Ok(std::char::from_u32(n).ok_or_else(|| InvalidUnicodeCodepointGot(n))?)403}404405#[builtin(fields(406 settings: Rc<RefCell<Settings>>,407))]408fn builtin_trace(409 this: &builtin_trace,410 s: State,411 loc: CallLocation,412 str: IStr,413 rest: Thunk<Val>,414) -> Result<Any> {415 this.settings416 .borrow()417 .trace_printer418 .print_trace(s.clone(), loc, str);419 Ok(Any(rest.evaluate(s)?))420}421422#[builtin]423fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {424 Ok(str.replace(&from as &str, &to as &str))425}426427#[builtin]428fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {429 use Either2::*;430 Ok(VecVal(Cc::new(match maxsplits {431 A(n) => str432 .splitn(n + 1, &c as &str)433 .map(|s| Val::Str(s.into()))434 .collect(),435 B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),436 })))437}438439#[builtin]440fn builtin_ascii_upper(str: IStr) -> Result<String> {441 Ok(str.to_ascii_uppercase())442}443444#[builtin]445fn builtin_ascii_lower(str: IStr) -> Result<String> {446 Ok(str.to_ascii_lowercase())447}448449#[builtin]450fn builtin_find_substr(pat: IStr, str: IStr) -> Result<ArrValue> {451 if pat.is_empty() || str.is_empty() || pat.len() > str.len() {452 return Ok(ArrValue::empty());453 }454455 let str = str.as_str();456 let pat = pat.as_bytes();457 let strb = str.as_bytes();458459 let max_pos = str.len() - pat.len();460461 let mut out: Vec<Val> = Vec::new();462 for (ch_idx, (i, _)) in str463 .char_indices()464 .take_while(|(i, _)| i <= &max_pos)465 .enumerate()466 {467 if &strb[i..i + pat.len()] == pat {468 out.push(Val::Num(ch_idx as f64))469 }470 }471 Ok(out.into())472}473474#[allow(clippy::comparison_chain)]475#[builtin]476fn builtin_starts_with(477 s: State,478 a: Either![IStr, ArrValue],479 b: Either![IStr, ArrValue],480) -> Result<bool> {481 Ok(match (a, b) {482 (Either2::A(a), Either2::A(b)) => a.starts_with(b.as_str()),483 (Either2::B(a), Either2::B(b)) => {484 if b.len() > a.len() {485 return Ok(false);486 } else if b.len() == a.len() {487 return equals(s, &Val::Arr(a), &Val::Arr(b));488 } else {489 for (a, b) in a490 .slice(None, Some(b.len()), None)491 .iter(s.clone())492 .zip(b.iter(s.clone()))493 {494 let a = a?;495 let b = b?;496 if !equals(s.clone(), &a, &b)? {497 return Ok(false);498 }499 }500 true501 }502 }503 _ => throw_runtime!("both arguments should be of the same type"),504 })505}506507#[allow(clippy::comparison_chain)]508#[builtin]509fn builtin_ends_with(510 s: State,511 a: Either![IStr, ArrValue],512 b: Either![IStr, ArrValue],513) -> Result<bool> {514 Ok(match (a, b) {515 (Either2::A(a), Either2::A(b)) => a.ends_with(b.as_str()),516 (Either2::B(a), Either2::B(b)) => {517 if b.len() > a.len() {518 return Ok(false);519 } else if b.len() == a.len() {520 return equals(s, &Val::Arr(a), &Val::Arr(b));521 } else {522 let a_len = a.len();523 for (a, b) in a524 .slice(Some(a_len - b.len()), None, None)525 .iter(s.clone())526 .zip(b.iter(s.clone()))527 {528 let a = a?;529 let b = b?;530 if !equals(s.clone(), &a, &b)? {531 return Ok(false);532 }533 }534 true535 }536 }537 _ => throw_runtime!("both arguments should be of the same type"),538 })539}540541pub trait StateExt {542 /// This method was previously implemented in jrsonnet-evaluator itself543 fn with_stdlib(&self);544 fn add_global(&self, name: IStr, value: Thunk<Val>);545}546547impl StateExt for State {548 fn with_stdlib(&self) {549 let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());550 self.settings_mut().context_initializer = Box::new(initializer)551 }552 fn add_global(&self, name: IStr, value: Thunk<Val>) {553 self.settings()554 .context_initializer555 .as_any()556 .downcast_ref::<ContextInitializer>()557 .expect("not standard context initializer")558 .settings_mut()559 .globals560 .insert(name, value);561 }562}tests/src/lib.rsdiffbeforeafterboth--- a/tests/src/lib.rs
+++ b/tests/src/lib.rs
@@ -1 +1 @@
-
+//! See tests/, suite/ and golden/ directories for tests