difftreelog
refactor! implement stack size limit using thread local
in: master
18 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -85,8 +85,8 @@
/// Set the maximum stack depth.
#[no_mangle]
-pub extern "C" fn jsonnet_max_stack(vm: &State, v: c_uint) {
- vm.settings_mut().max_stack = v as usize;
+pub extern "C" fn jsonnet_max_stack(_vm: &State, _v: c_uint) {
+ todo!()
}
/// Set the number of objects required before a garbage collection cycle is allowed.
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -5,7 +5,7 @@
use clap::{AppSettings, IntoApp, Parser};
use clap_complete::Shell;
-use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, ManifestOpts, OutputOpts};
+use jrsonnet_cli::{ConfigureState, GeneralOpts, ManifestOpts, OutputOpts};
use jrsonnet_evaluator::{error::LocError, State};
#[cfg(feature = "mimalloc")]
@@ -60,8 +60,6 @@
output: OutputOpts,
#[clap(flatten)]
debug: DebugOpts,
- #[clap(flatten)]
- gc: GcOpts,
}
fn main() {
@@ -113,7 +111,6 @@
}
fn main_catch(opts: Opts) -> bool {
- let _printer = opts.gc.stats_printer();
let s = State::default();
if let Err(e) = main_real(&s, opts) {
if let Error::Evaluation(e) = e {
@@ -127,7 +124,7 @@
}
fn main_real(s: &State, opts: Opts) -> Result<(), Error> {
- opts.general.configure(s)?;
+ let _guards = opts.general.configure(s)?;
opts.manifest.configure(s)?;
let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
crates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -3,10 +3,12 @@
mod tla;
mod trace;
-use std::{env, path::PathBuf};
+use std::{env, marker::PhantomData, path::PathBuf};
use clap::Parser;
-use jrsonnet_evaluator::{error::Result, FileImportResolver, State};
+use jrsonnet_evaluator::{
+ error::Result, stack::StackDepthLimitOverrideGuard, FileImportResolver, State,
+};
use jrsonnet_gcmodule::with_thread_object_space;
pub use manifest::*;
pub use stdlib::*;
@@ -14,7 +16,9 @@
pub use trace::*;
pub trait ConfigureState {
- fn configure(&self, s: &State) -> Result<()>;
+ type Guards;
+
+ fn configure(&self, s: &State) -> Result<Self::Guards>;
}
#[derive(Parser)]
@@ -44,7 +48,8 @@
jpath: Vec<PathBuf>,
}
impl ConfigureState for MiscOpts {
- fn configure(&self, s: &State) -> Result<()> {
+ type Guards = StackDepthLimitOverrideGuard;
+ fn configure(&self, s: &State) -> Result<Self::Guards> {
let mut library_paths = self.jpath.clone();
library_paths.reverse();
if let Some(path) = env::var_os("JSONNET_PATH") {
@@ -53,8 +58,8 @@
s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));
- s.set_max_stack(self.max_stack);
- Ok(())
+ let _depth_limit = jrsonnet_evaluator::stack::limit_stack_depth(self.max_stack);
+ Ok(_depth_limit)
}
}
@@ -72,16 +77,24 @@
#[clap(flatten)]
trace: TraceOpts,
+
+ #[clap(flatten)]
+ gc: GcOpts,
}
impl ConfigureState for GeneralOpts {
- fn configure(&self, s: &State) -> Result<()> {
+ type Guards = (
+ <MiscOpts as ConfigureState>::Guards,
+ <GcOpts as ConfigureState>::Guards,
+ );
+ fn configure(&self, s: &State) -> Result<Self::Guards> {
// Configure trace first, because tla-code/ext-code can throw
self.trace.configure(s)?;
- self.misc.configure(s)?;
+ let misc_guards = self.misc.configure(s)?;
self.tla.configure(s)?;
self.std.configure(s)?;
- Ok(())
+ let gc_guards = self.gc.configure(s)?;
+ Ok((misc_guards, gc_guards))
}
}
@@ -100,20 +113,22 @@
#[clap(long)]
gc_collect_before_printing_stats: bool,
}
-impl GcOpts {
- pub fn stats_printer(&self) -> (Option<GcStatsPrinter>, Option<LeakSpace>) {
+impl ConfigureState for GcOpts {
+ type Guards = (Option<GcStatsPrinter>, Option<LeakSpace>);
+
+ fn configure(&self, _s: &State) -> Result<Self::Guards> {
// Constructed structs have side-effects in Drop impl
#[allow(clippy::unnecessary_lazy_evaluations)]
- (
+ Ok((
self.gc_print_stats.then(|| GcStatsPrinter {
collect_before_printing_stats: self.gc_collect_before_printing_stats,
}),
- (!self.gc_collect_on_exit).then(|| LeakSpace {}),
- )
+ (!self.gc_collect_on_exit).then(|| LeakSpace(PhantomData)),
+ ))
}
}
-pub struct LeakSpace {}
+pub struct LeakSpace(PhantomData<()>);
impl Drop for LeakSpace {
fn drop(&mut self) {
crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -47,6 +47,7 @@
exp_preserve_order: bool,
}
impl ConfigureState for ManifestOpts {
+ type Guards = ();
fn configure(&self, s: &State) -> Result<()> {
if self.string {
s.set_manifest_format(ManifestFormat::String);
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -106,6 +106,7 @@
ext_code_file: Vec<ExtFile>,
}
impl ConfigureState for StdOpts {
+ type Guards = ();
fn configure(&self, s: &State) -> Result<()> {
if self.no_stdlib {
return Ok(());
crates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -47,6 +47,7 @@
tla_code_file: Vec<ExtFile>,
}
impl ConfigureState for TLAOpts {
+ type Guards = ();
fn configure(&self, s: &State) -> Result<()> {
for tla in self.tla_str.iter() {
s.add_tla_str((&tla.name as &str).into(), (&tla.value as &str).into());
crates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/trace.rs
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -41,6 +41,7 @@
max_trace: usize,
}
impl ConfigureState for TraceOpts {
+ type Guards = ();
fn configure(&self, s: &State) -> Result<()> {
let resolver = PathResolver::new_cwd_fallback();
match self
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -20,8 +20,8 @@
exp-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"]
+# Improves performance, and implements some useful things using nightly-only features
+nightly = []
[dependencies]
jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.2" }
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,11 +2,11 @@
use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, SourcePath, UnaryOpType};
+use jrsonnet_parser::{BinaryOpType, ExprLocation, LocExpr, Source, SourcePath, UnaryOpType};
use jrsonnet_types::ValType;
use thiserror::Error;
-use crate::{stdlib::format::FormatError, typed::TypeLocError};
+use crate::{function::CallLocation, stdlib::format::FormatError, typed::TypeLocError};
fn format_found(list: &[IStr], what: &str) -> String {
if list.is_empty() {
@@ -262,7 +262,72 @@
}
}
+pub trait ErrorSource {
+ fn to_location(self) -> Option<ExprLocation>;
+}
+impl ErrorSource for &LocExpr {
+ fn to_location(self) -> Option<ExprLocation> {
+ Some(self.1.clone())
+ }
+}
+impl ErrorSource for &ExprLocation {
+ fn to_location(self) -> Option<ExprLocation> {
+ Some(self.clone())
+ }
+}
+impl ErrorSource for CallLocation<'_> {
+ fn to_location(self) -> Option<ExprLocation> {
+ self.0.cloned()
+ }
+}
+
pub type Result<V, E = LocError> = std::result::Result<V, E>;
+pub trait ResultExt: Sized {
+ #[must_use]
+ fn with_description<O: Into<String>>(self, msg: impl FnOnce() -> O) -> Self;
+ #[must_use]
+ fn description(self, msg: &str) -> Self {
+ self.with_description(|| msg)
+ }
+
+ #[must_use]
+ fn with_description_src<O: Into<String>>(
+ self,
+ src: impl ErrorSource,
+ msg: impl FnOnce() -> O,
+ ) -> Self;
+ #[must_use]
+ fn description_src(self, src: impl ErrorSource, msg: &str) -> Self {
+ self.with_description_src(src, || msg)
+ }
+}
+impl<T> ResultExt for Result<T, LocError> {
+ fn with_description<O: Into<String>>(mut self, msg: impl FnOnce() -> O) -> Self {
+ if let Err(e) = &mut self {
+ let trace = e.trace_mut();
+ trace.0.push(StackTraceElement {
+ location: None,
+ desc: msg().into(),
+ });
+ }
+ self
+ }
+
+ fn with_description_src<O: Into<String>>(
+ mut self,
+ src: impl ErrorSource,
+ msg: impl FnOnce() -> O,
+ ) -> Self {
+ if let Err(e) = &mut self {
+ let trace = e.trace_mut();
+ trace.0.push(StackTraceElement {
+ location: src.to_location(),
+ desc: msg().into(),
+ });
+ }
+ self
+ }
+}
#[macro_export]
macro_rules! throw {
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -34,7 +34,7 @@
pub fn evaluate_field_name(s: State, ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {
Ok(match field_name {
FieldName::Fixed(n) => Some(n.clone()),
- FieldName::Dyn(expr) => s.push(
+ FieldName::Dyn(expr) => State::push(
CallLocation::new(&expr.1),
|| "evaluating field name".to_string(),
|| {
@@ -184,14 +184,11 @@
.with_add(*plus)
.with_visibility(*visibility)
.with_location(value.1.clone())
- .bindable(
- s.clone(),
- tb!(UnboundValue {
- uctx: uctx.clone(),
- value: value.clone(),
- name: name.clone()
- }),
- )?;
+ .bindable(tb!(UnboundValue {
+ uctx: uctx.clone(),
+ value: value.clone(),
+ name: name.clone()
+ }))?;
}
Member::Field(FieldMember {
name,
@@ -233,15 +230,12 @@
.member(name.clone())
.hide()
.with_location(value.1.clone())
- .bindable(
- s.clone(),
- tb!(UnboundMethod {
- uctx: uctx.clone(),
- value: value.clone(),
- params: params.clone(),
- name: name.clone()
- }),
- )?;
+ .bindable(tb!(UnboundMethod {
+ uctx: uctx.clone(),
+ value: value.clone(),
+ params: params.clone(),
+ name: name.clone()
+ }))?;
}
Member::BindStmt(_) => {}
Member::AssertStmt(stmt) => {
@@ -324,13 +318,10 @@
.member(n)
.with_location(obj.value.1.clone())
.with_add(obj.plus)
- .bindable(
- s.clone(),
- tb!(UnboundValue {
- uctx,
- value: obj.value.clone(),
- }),
- )?;
+ .bindable(tb!(UnboundValue {
+ uctx,
+ value: obj.value.clone(),
+ }))?;
}
v => throw!(FieldMustBeStringGot(v.value_type())),
}
@@ -364,7 +355,7 @@
if tailstrict {
body()?
} else {
- s.push(loc, || format!("function <{}> call", f.name()), body)?
+ State::push(loc, || format!("function <{}> call", f.name()), body)?
}
}
v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),
@@ -374,13 +365,13 @@
pub fn evaluate_assert(s: State, ctx: Context, assertion: &AssertStmt) -> Result<()> {
let value = &assertion.0;
let msg = &assertion.1;
- let assertion_result = s.push(
+ let assertion_result = State::push(
CallLocation::new(&value.1),
|| "assertion condition".to_owned(),
|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),
)?;
if !assertion_result {
- s.push(
+ State::push(
CallLocation::new(&value.1),
|| "assertion failure".to_owned(),
|| {
@@ -432,7 +423,7 @@
Num(v) => Val::new_checked_num(*v)?,
BinaryOp(v1, o, v2) => evaluate_binary_op_special(s, ctx, v1, *o, v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(s, ctx, v)?)?,
- Var(name) => s.push(
+ Var(name) => State::push(
CallLocation::new(loc),
|| format!("variable <{name}> access"),
|| ctx.binding(name.clone())?.evaluate(s.clone()),
@@ -442,7 +433,7 @@
evaluate(s.clone(), ctx.clone(), value)?,
evaluate(s.clone(), ctx, index)?,
) {
- (Val::Obj(v), Val::Str(key)) => s.push(
+ (Val::Obj(v), Val::Str(key)) => State::push(
CallLocation::new(loc),
|| format!("field <{key}> access"),
|| match v.get(s.clone(), key.clone()) {
@@ -571,7 +562,7 @@
evaluate_assert(s.clone(), ctx.clone(), assert)?;
evaluate(s, ctx, returned)?
}
- ErrorStmt(e) => s.push(
+ ErrorStmt(e) => State::push(
CallLocation::new(loc),
|| "error statement".to_owned(),
|| {
@@ -585,7 +576,7 @@
cond_then,
cond_else,
} => {
- if s.push(
+ if State::push(
CallLocation::new(loc),
|| "if condition".to_owned(),
|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), &cond.0)?, s.clone()),
@@ -607,7 +598,7 @@
desc: &'static str,
) -> Result<Option<T>> {
if let Some(value) = expr {
- Ok(Some(s.push(
+ Ok(Some(State::push(
loc,
|| format!("slice {desc}"),
|| T::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),
@@ -630,7 +621,7 @@
let tmp = loc.clone().0;
let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;
match i {
- Import(_) => s.push(
+ Import(_) => State::push(
CallLocation::new(loc),
|| format!("import {:?}", path.clone()),
|| s.import_resolved(resolved_path),
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1,5 +1,5 @@
//! jsonnet interpreter implementation
-
+#![cfg_attr(feature = "nightly", feature(thread_local))]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(
clippy::all,
@@ -51,6 +51,7 @@
mod integrations;
mod map;
mod obj;
+pub mod stack;
pub mod stdlib;
pub mod trace;
pub mod typed;
@@ -67,7 +68,7 @@
pub use ctx::*;
pub use dynamic::*;
-use error::{Error::*, LocError, Result, StackTraceElement};
+use error::{Error::*, LocError, Result, ResultExt};
pub use evaluate::*;
use function::{CallLocation, TlaArg};
use gc::{GcHashMap, TraceBox};
@@ -78,6 +79,7 @@
pub use jrsonnet_parser as parser;
use jrsonnet_parser::*;
pub use obj::*;
+use stack::check_depth;
use trace::{CompactFormat, TraceFormat};
pub use val::{ManifestFormat, Thunk, Val};
@@ -143,8 +145,6 @@
/// Dynamically reconfigurable evaluation settings
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,
/// TLA vars
@@ -162,7 +162,6 @@
impl Default for EvaluationSettings {
fn default() -> Self {
Self {
- max_stack: 200,
max_trace: 20,
context_initializer: Box::new(DummyContextInitializer),
tla_vars: HashMap::default(),
@@ -179,19 +178,7 @@
}
}
}
-
-#[derive(Default)]
-struct EvaluationData {
- /// Used for stack overflow detection, stacktrace is populated on unwind
- stack_depth: usize,
- /// Updated every time stack entry is popt
- stack_generation: usize,
- breakpoints: Breakpoints,
-
- /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
- files: GcHashMap<SourcePath, FileData>,
-}
struct FileData {
string: Option<IStr>,
bytes: Option<IBytes>,
@@ -217,46 +204,14 @@
parsed: None,
evaluated: None,
evaluating: false,
- }
- }
-}
-
-#[allow(clippy::type_complexity)]
-pub struct Breakpoint {
- loc: ExprLocation,
- collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,
-}
-#[derive(Default)]
-struct Breakpoints(Vec<Rc<Breakpoint>>);
-impl Breakpoints {
- fn insert(
- &self,
- stack_depth: usize,
- stack_generation: usize,
- loc: &ExprLocation,
- result: Result<Val>,
- ) -> Result<Val> {
- if self.0.is_empty() {
- return result;
- }
- for item in &self.0 {
- if item.loc.belongs_to(loc) {
- let mut collected = item.collected.borrow_mut();
- let (depth, vals) = collected.entry(stack_generation).or_default();
- if stack_depth > *depth {
- vals.clear();
- }
- vals.push(result.clone());
- }
}
- result
}
}
#[derive(Default)]
pub struct EvaluationStateInternals {
/// Internal state
- data: RefCell<EvaluationData>,
+ file_cache: RefCell<GcHashMap<SourcePath, FileData>>,
/// Settings, safe to change at runtime
settings: RefCell<EvaluationSettings>,
}
@@ -268,8 +223,8 @@
impl State {
/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {
- let mut data = self.data_mut();
- let mut file = data.files.raw_entry_mut().from_key(&path);
+ let mut file_cache = self.file_cache();
+ let mut file = file_cache.raw_entry_mut().from_key(&path);
let file = match file {
RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -303,8 +258,8 @@
}
/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {
- let mut data = self.data_mut();
- let mut file = data.files.raw_entry_mut().from_key(&path);
+ let mut file_cache = self.file_cache();
+ let mut file = file_cache.raw_entry_mut().from_key(&path);
let file = match file {
RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -330,8 +285,8 @@
}
/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {
- let mut data = self.data_mut();
- let mut file = data.files.raw_entry_mut().from_key(&path);
+ let mut file_cache = self.file_cache();
+ let mut file = file_cache.raw_entry_mut().from_key(&path);
let file = match file {
RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -383,16 +338,16 @@
throw!(InfiniteRecursionDetected)
}
file.evaluating = true;
- // Dropping file here, as it borrows data, which may be used in evaluation
- drop(data);
+ // Dropping file cache guard here, as evaluation may use this map too
+ drop(file_cache);
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);
+ let mut file_cache = self.file_cache();
+ let mut file = file_cache.raw_entry_mut().from_key(&path);
let file = match file {
RawEntryMut::Occupied(ref mut d) => d.get_mut(),
@@ -426,35 +381,13 @@
/// Executes code creating a new stack frame
pub fn push<T>(
- &self,
e: CallLocation<'_>,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<T>,
) -> Result<T> {
- {
- let mut data = self.data_mut();
- let stack_depth = &mut data.stack_depth;
- if *stack_depth > self.max_stack() {
- // Error creation uses data, so i drop guard here
- drop(data);
- throw!(StackOverflow);
- }
- *stack_depth += 1;
- }
- let result = f();
- {
- let mut data = self.data_mut();
- data.stack_depth -= 1;
- data.stack_generation += 1;
- }
- if let Err(mut err) = result {
- err.trace_mut().0.push(StackTraceElement {
- location: e.0.cloned(),
- desc: frame_desc(),
- });
- return Err(err);
- }
- result
+ let _guard = check_depth()?;
+
+ f().with_description_src(e, frame_desc)
}
/// Executes code creating a new stack frame
@@ -464,64 +397,18 @@
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<Val>,
) -> Result<Val> {
- {
- let mut data = self.data_mut();
- let stack_depth = &mut data.stack_depth;
- if *stack_depth > self.max_stack() {
- // Error creation uses data, so i drop guard here
- drop(data);
- throw!(StackOverflow);
- }
- *stack_depth += 1;
- }
- let mut result = f();
- {
- let mut data = self.data_mut();
- data.stack_depth -= 1;
- data.stack_generation += 1;
- result = data
- .breakpoints
- .insert(data.stack_depth, data.stack_generation, e, result);
- }
- if let Err(mut err) = result {
- err.trace_mut().0.push(StackTraceElement {
- location: Some(e.clone()),
- desc: frame_desc(),
- });
- return Err(err);
- }
- result
+ let _guard = check_depth()?;
+
+ f().with_description_src(e, frame_desc)
}
/// Executes code creating a new stack frame
pub fn push_description<T>(
- &self,
frame_desc: impl FnOnce() -> String,
f: impl FnOnce() -> Result<T>,
) -> Result<T> {
- {
- let mut data = self.data_mut();
- let stack_depth = &mut data.stack_depth;
- if *stack_depth > self.max_stack() {
- // Error creation uses data, so i drop guard here
- drop(data);
- throw!(StackOverflow);
- }
- *stack_depth += 1;
- }
- let result = f();
- {
- let mut data = self.data_mut();
- data.stack_depth -= 1;
- data.stack_generation += 1;
- }
- if let Err(mut err) = result {
- err.trace_mut().0.push(StackTraceElement {
- location: None,
- desc: frame_desc(),
- });
- return Err(err);
- }
- result
+ let _guard = check_depth()?;
+
+ f().with_description(frame_desc)
}
/// # Panics
@@ -536,7 +423,7 @@
}
pub fn manifest(&self, val: Val) -> Result<IStr> {
- self.push_description(
+ Self::push_description(
|| "manifestification".to_string(),
|| val.manifest(self.clone(), &self.manifest_format()),
)
@@ -551,7 +438,7 @@
/// If passed value is function then call with set TLA
pub fn with_tla(&self, val: Val) -> Result<Val> {
Ok(match val {
- Val::Func(func) => self.push_description(
+ Val::Func(func) => State::push_description(
|| "during TLA call".to_owned(),
|| {
func.evaluate(
@@ -573,8 +460,8 @@
/// Internals
impl State {
- fn data_mut(&self) -> RefMut<'_, EvaluationData> {
- self.0.data.borrow_mut()
+ fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {
+ self.0.file_cache.borrow_mut()
}
pub fn settings(&self) -> Ref<'_, EvaluationSettings> {
self.0.settings.borrow()
@@ -675,12 +562,5 @@
}
pub fn set_max_trace(&self, trace: usize) {
self.settings_mut().max_trace = trace;
- }
-
- pub fn max_stack(&self) -> usize {
- self.settings().max_stack
- }
- pub fn set_max_stack(&self, trace: usize) {
- self.settings_mut().max_stack = trace;
}
}
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -577,22 +577,29 @@
pub struct ValueBuilder<'v>(&'v mut ObjValueBuilder);
impl ObjMemberBuilder<ValueBuilder<'_>> {
- pub fn value(self, s: State, value: Val) -> Result<()> {
- self.binding(s, MaybeUnbound::Bound(Thunk::evaluated(value)))
+ /// Inserts value, replacing if it is already defined
+ pub fn value_unchecked(self, value: Val) {
+ let (receiver, name, member) =
+ self.build_member(MaybeUnbound::Bound(Thunk::evaluated(value)));
+ let entry = receiver.0.map.entry(name);
+ entry.insert(member);
}
- pub fn bindable(
- self,
- s: State,
- bindable: TraceBox<dyn Unbound<Bound = Thunk<Val>>>,
- ) -> Result<()> {
- self.binding(s, MaybeUnbound::Unbound(Cc::new(bindable)))
+
+ pub fn value(self, value: Val) -> Result<()> {
+ self.thunk(Thunk::evaluated(value))
+ }
+ pub fn thunk(self, value: Thunk<Val>) -> Result<()> {
+ self.binding(MaybeUnbound::Bound(value))
+ }
+ pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Thunk<Val>>>) -> Result<()> {
+ self.binding(MaybeUnbound::Unbound(Cc::new(bindable)))
}
- pub fn binding(self, s: State, binding: MaybeUnbound) -> Result<()> {
+ pub fn binding(self, 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);
if old.is_some() {
- s.push(
+ State::push(
CallLocation(location.as_ref()),
|| format!("field <{}> initializtion", name.clone()),
|| throw!(DuplicateFieldName(name.clone())),
crates/jrsonnet-evaluator/src/stack.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/stack.rs
@@ -0,0 +1,109 @@
+use std::{cell::Cell, marker::PhantomData};
+
+use crate::error::{Error, LocError};
+
+struct StackLimit {
+ max_stack_size: Cell<usize>,
+ current_depth: Cell<usize>,
+}
+
+#[cfg(feature = "nightly")]
+#[thread_local]
+static STACK_LIMIT: StackLimit = StackLimit {
+ max_stack_size: Cell::new(200),
+ current_depth: Cell::new(0),
+};
+#[cfg(not(feature = "nightly"))]
+thread_local! {
+ static STACK_LIMIT: StackLimit = StackLimit {
+ max_stack_size: Cell::new(200),
+ current_depth: Cell::new(0),
+ };
+}
+
+pub struct StackOverflowError;
+impl From<StackOverflowError> for Error {
+ fn from(_: StackOverflowError) -> Self {
+ Error::StackOverflow
+ }
+}
+impl From<StackOverflowError> for LocError {
+ fn from(_: StackOverflowError) -> Self {
+ Error::StackOverflow.into()
+ }
+}
+
+/// Used to implement stack depth limitation
+pub struct StackDepthGuard(PhantomData<()>);
+impl Drop for StackDepthGuard {
+ #[cfg(feature = "nightly")]
+ fn drop(&mut self) {
+ STACK_LIMIT
+ .current_depth
+ .set(STACK_LIMIT.current_depth.get() - 1)
+ }
+ #[cfg(not(feature = "nightly"))]
+ fn drop(&mut self) {
+ STACK_LIMIT.with(|limit| limit.current_depth.set(limit.current_depth.get() - 1));
+ }
+}
+
+// #[cfg(feature = "nightly")]
+pub fn check_depth() -> Result<StackDepthGuard, StackOverflowError> {
+ fn internal(limit: &StackLimit) -> Result<StackDepthGuard, StackOverflowError> {
+ let current = limit.current_depth.get();
+ if current < limit.max_stack_size.get() {
+ limit.current_depth.set(current + 1);
+ Ok(StackDepthGuard(PhantomData))
+ } else {
+ Err(StackOverflowError)
+ }
+ }
+ #[cfg(feature = "nightly")]
+ {
+ internal(&STACK_LIMIT)
+ }
+ #[cfg(not(feature = "nightly"))]
+ {
+ STACK_LIMIT.with(internal)
+ }
+}
+
+pub struct StackDepthLimitOverrideGuard {
+ old_limit: usize,
+}
+impl Drop for StackDepthLimitOverrideGuard {
+ #[cfg(feature = "nightly")]
+ fn drop(&mut self) {
+ STACK_LIMIT.max_stack_size.set(self.old_limit)
+ }
+ #[cfg(not(feature = "nightly"))]
+ fn drop(&mut self) {
+ STACK_LIMIT.with(|limit| limit.max_stack_size.set(self.old_limit));
+ }
+}
+
+pub fn limit_stack_depth(depth_limit: usize) -> StackDepthLimitOverrideGuard {
+ fn internal(limit: &StackLimit, depth_limit: usize) -> StackDepthLimitOverrideGuard {
+ let old_limit = limit.max_stack_size.get();
+ let current_depth = limit.current_depth.get();
+
+ limit.max_stack_size.set(current_depth + depth_limit);
+ StackDepthLimitOverrideGuard { old_limit }
+ }
+ #[cfg(feature = "nightly")]
+ {
+ internal(&STACK_LIMIT, depth_limit)
+ }
+ #[cfg(not(feature = "nightly"))]
+ {
+ STACK_LIMIT.with(|limit| internal(limit, depth_limit))
+ }
+}
+
+/// Like [`limit_stack_depth`], but set depth is not guarded, and will be kept
+///
+/// Used to implement `set_max_stack` in C api, prefer to use [`limit_stack_depth`] instead
+pub fn set_stack_depth_limit(depth_limit: usize) {
+ std::mem::forget(limit_stack_depth(depth_limit));
+}
crates/jrsonnet-evaluator/src/stdlib/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/manifest.rs
@@ -111,7 +111,7 @@
buf.push_str(cur_padding);
escape_string_json_buf(&field, buf);
buf.push_str(options.key_val_sep);
- s.push_description(
+ State::push_description(
|| format!("field <{}> manifestification", field.clone()),
|| {
let value = obj.get(s.clone(), field.clone())?.unwrap();
crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -10,7 +10,7 @@
pub mod manifest;
pub fn std_format(s: State, str: IStr, vals: Val) -> Result<String> {
- s.push(
+ State::push(
CallLocation::native(),
|| format!("std.format of {str}"),
|| {
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -85,12 +85,11 @@
}
fn push_type_description(
- s: State,
error_reason: impl Fn() -> String,
path: impl Fn() -> ValuePathItem,
item: impl Fn() -> Result<()>,
) -> Result<()> {
- s.push_description(error_reason, || match item() {
+ State::push_description(error_reason, || match item() {
Ok(_) => Ok(()),
Err(mut e) => {
if let Error::TypeError(e) = &mut e.error_mut() {
@@ -170,7 +169,6 @@
Val::Arr(a) => {
for (i, item) in a.iter(s.clone()).enumerate() {
push_type_description(
- s.clone(),
|| format!("array index {i}"),
|| ValuePathItem::Index(i as u64),
|| elem_type.check(s.clone(), &item.clone()?),
@@ -184,7 +182,6 @@
Val::Arr(a) => {
for (i, item) in a.iter(s.clone()).enumerate() {
push_type_description(
- s.clone(),
|| format!("array index {i}"),
|| ValuePathItem::Index(i as u64),
|| elem_type.check(s.clone(), &item.clone()?),
@@ -199,7 +196,6 @@
for (k, v) in elems.iter() {
if let Some(got_v) = obj.get(s.clone(), (*k).into())? {
push_type_description(
- s.clone(),
|| format!("property {k}"),
|| ValuePathItem::Field((*k).into()),
|| v.check(s.clone(), &got_v),
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth1use proc_macro2::TokenStream;2use quote::quote;3use syn::{4 parenthesized,5 parse::{Parse, ParseStream},6 parse_macro_input,7 punctuated::Punctuated,8 spanned::Spanned,9 token::{self, Comma},10 Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, Path,11 PathArguments, Result, ReturnType, Token, Type,12};1314fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>15where16 Ident: PartialEq<I>,17{18 let attrs = attrs19 .iter()20 .filter(|a| a.path.is_ident(&ident))21 .collect::<Vec<_>>();22 if attrs.len() > 1 {23 return Err(Error::new(24 attrs[1].span(),25 "this attribute may be specified only once",26 ));27 } else if attrs.is_empty() {28 return Ok(None);29 }30 let attr = attrs[0];31 let attr = attr.parse_args::<A>()?;3233 Ok(Some(attr))34}3536fn path_is(path: &Path, needed: &str) -> bool {37 path.leading_colon.is_none()38 && !path.segments.is_empty()39 && path.segments.iter().last().unwrap().ident == needed40}4142fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {43 match ty {44 Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {45 let args = &path.path.segments.iter().last().unwrap().arguments;46 Some(args)47 }48 _ => None,49 }50}5152fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {53 Ok(if let Some(args) = type_is_path(ty, "Option") {54 // It should have only on angle-bracketed param ("<String>"):55 let generic_arg = match args {56 PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),57 _ => return Err(Error::new(args.span(), "missing option generic")),58 };59 // This argument must be a type:60 match generic_arg {61 GenericArgument::Type(ty) => Some(ty),62 _ => {63 return Err(Error::new(64 generic_arg.span(),65 "option generic should be a type",66 ))67 }68 }69 } else {70 None71 })72}7374struct Field {75 name: Ident,76 _colon: Token![:],77 ty: Type,78}79impl Parse for Field {80 fn parse(input: ParseStream) -> syn::Result<Self> {81 Ok(Self {82 name: input.parse()?,83 _colon: input.parse()?,84 ty: input.parse()?,85 })86 }87}8889mod kw {90 syn::custom_keyword!(fields);91 syn::custom_keyword!(rename);92 syn::custom_keyword!(flatten);93 syn::custom_keyword!(ok);94}9596struct EmptyAttr;97impl Parse for EmptyAttr {98 fn parse(_input: ParseStream) -> Result<Self> {99 Ok(Self)100 }101}102103struct BuiltinAttrs {104 fields: Vec<Field>,105}106impl Parse for BuiltinAttrs {107 fn parse(input: ParseStream) -> syn::Result<Self> {108 if input.is_empty() {109 return Ok(Self { fields: Vec::new() });110 }111 input.parse::<kw::fields>()?;112 let fields;113 parenthesized!(fields in input);114 let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;115 Ok(Self {116 fields: p.into_iter().collect(),117 })118 }119}120121enum ArgInfo {122 Normal {123 ty: Box<Type>,124 is_option: bool,125 name: Option<String>,126 cfg_attrs: Vec<Attribute>,127 // ident: Ident,128 },129 Lazy {130 is_option: bool,131 name: Option<String>,132 },133 State,134 Location,135 This,136}137138impl ArgInfo {139 fn parse(name: &str, arg: &FnArg) -> Result<Self> {140 let arg = match arg {141 FnArg::Receiver(_) => unreachable!(),142 FnArg::Typed(a) => a,143 };144 let ident = match &arg.pat as &Pat {145 Pat::Ident(i) => Some(i.ident.clone()),146 _ => None,147 };148 let ty = &arg.ty;149 if type_is_path(ty, "State").is_some() {150 return Ok(Self::State);151 } else if type_is_path(ty, "CallLocation").is_some() {152 return Ok(Self::Location);153 } else if type_is_path(ty, "Thunk").is_some() {154 return Ok(Self::Lazy {155 is_option: false,156 name: ident.map(|v| v.to_string()),157 });158 }159160 match ty as &Type {161 Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),162 _ => {}163 }164165 let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? {166 if type_is_path(ty, "Thunk").is_some() {167 return Ok(Self::Lazy {168 is_option: true,169 name: ident.map(|v| v.to_string()),170 });171 }172173 (true, Box::new(ty.clone()))174 } else {175 (false, ty.clone())176 };177178 let cfg_attrs = arg179 .attrs180 .iter()181 .filter(|a| a.path.is_ident("cfg"))182 .cloned()183 .collect();184185 Ok(Self::Normal {186 ty,187 is_option,188 name: ident.map(|v| v.to_string()),189 cfg_attrs,190 })191 }192}193194#[proc_macro_attribute]195pub fn builtin(196 attr: proc_macro::TokenStream,197 item: proc_macro::TokenStream,198) -> proc_macro::TokenStream {199 let attr = parse_macro_input!(attr as BuiltinAttrs);200 let item: ItemFn = parse_macro_input!(item);201202 match builtin_inner(attr, item) {203 Ok(v) => v.into(),204 Err(e) => e.into_compile_error().into(),205 }206}207208fn builtin_inner(attr: BuiltinAttrs, fun: ItemFn) -> syn::Result<TokenStream> {209 let result = match fun.sig.output {210 ReturnType::Default => {211 return Err(Error::new(212 fun.sig.span(),213 "builtin should return something",214 ))215 }216 ReturnType::Type(_, ref ty) => ty.clone(),217 };218 let result_inner = if let Some(args) = type_is_path(&result, "Result") {219 let generic_arg = match args {220 PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),221 _ => return Err(Error::new(args.span(), "missing result generic")),222 };223 // This argument must be a type:224 match generic_arg {225 GenericArgument::Type(ty) => ty,226 _ => {227 return Err(Error::new(228 generic_arg.span(),229 "option generic should be a type",230 ))231 }232 }233 } else {234 return Err(Error::new(result.span(), "return value should be result"));235 };236237 let name = fun.sig.ident.to_string();238 let args = fun239 .sig240 .inputs241 .iter()242 .map(|arg| ArgInfo::parse(&name, arg))243 .collect::<Result<Vec<_>>>()?;244245 let params_desc = args.iter().flat_map(|a| match a {246 ArgInfo::Normal {247 is_option,248 name,249 cfg_attrs,250 ..251 } => {252 let name = name253 .as_ref()254 .map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})255 .unwrap_or_else(|| quote! {None});256 Some(quote! {257 #(#cfg_attrs)*258 BuiltinParam {259 name: #name,260 has_default: #is_option,261 },262 })263 }264 ArgInfo::Lazy { is_option, name } => {265 let name = name266 .as_ref()267 .map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})268 .unwrap_or_else(|| quote! {None});269 Some(quote! {270 BuiltinParam {271 name: #name,272 has_default: #is_option,273 },274 })275 }276 ArgInfo::State => None,277 ArgInfo::Location => None,278 ArgInfo::This => None,279 });280281 let mut id = 0usize;282 let pass = args283 .iter()284 .map(|a| match a {285 ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {286 let cid = id;287 id += 1;288 (quote! {#cid}, a)289 }290 ArgInfo::State | ArgInfo::Location | ArgInfo::This => {291 (quote! {compile_error!("should not use id")}, a)292 }293 })294 .map(|(id, a)| match a {295 ArgInfo::Normal {296 ty,297 is_option,298 name,299 cfg_attrs,300 } => {301 let name = name.as_ref().map(|v| v.as_str()).unwrap_or("<unnamed>");302 let eval = quote! {s.push_description(303 || format!("argument <{}> evaluation", #name),304 || <#ty>::from_untyped(value.evaluate(s.clone())?, s.clone()),305 )?};306 let value = if *is_option {307 quote! {if let Some(value) = &parsed[#id] {308 Some(#eval)309 } else {310 None311 },}312 } else {313 quote! {{314 let value = parsed[#id].as_ref().expect("args shape is checked");315 #eval316 },}317 };318 quote! {319 #(#cfg_attrs)*320 #value321 }322 }323 ArgInfo::Lazy { is_option, .. } => {324 if *is_option {325 quote! {if let Some(value) = &parsed[#id] {326 Some(value.clone())327 } else {328 None329 }}330 } else {331 quote! {332 parsed[#id].as_ref().expect("args shape is correct").clone(),333 }334 }335 }336 ArgInfo::State => quote! {s.clone(),},337 ArgInfo::Location => quote! {location,},338 ArgInfo::This => quote! {self,},339 });340341 let fields = attr.fields.iter().map(|field| {342 let name = &field.name;343 let ty = &field.ty;344 quote! {345 pub #name: #ty,346 }347 });348349 let name = &fun.sig.ident;350 let vis = &fun.vis;351 let static_ext = if attr.fields.is_empty() {352 quote! {353 impl #name {354 pub const INST: &'static dyn StaticBuiltin = &#name {};355 }356 impl StaticBuiltin for #name {}357 }358 } else {359 quote! {}360 };361 let static_derive_copy = if attr.fields.is_empty() {362 quote! {, Copy}363 } else {364 quote! {}365 };366367 Ok(quote! {368 #fun369 #[doc(hidden)]370 #[allow(non_camel_case_types)]371 #[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]372 #vis struct #name {373 #(#fields)*374 }375 const _: () = {376 use ::jrsonnet_evaluator::{377 State, Val,378 function::{builtin::{Builtin, StaticBuiltin, BuiltinParam}, CallLocation, ArgsLike, parse::parse_builtin_call},379 error::Result, Context, typed::Typed,380 parser::ExprLocation,381 };382 const PARAMS: &'static [BuiltinParam] = &[383 #(#params_desc)*384 ];385386 #static_ext387 impl Builtin for #name388 where389 Self: 'static390 {391 fn name(&self) -> &str {392 stringify!(#name)393 }394 fn params(&self) -> &[BuiltinParam] {395 PARAMS396 }397 fn call(&self, s: State, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {398 let parsed = parse_builtin_call(s.clone(), ctx, &PARAMS, args, false)?;399400 let result: #result = #name(#(#pass)*);401 let result = result?;402 <#result_inner>::into_untyped(result, s)403 }404 }405 };406 })407}408409#[derive(Default)]410struct TypedAttr {411 rename: Option<String>,412 flatten: bool,413 /// flatten(ok) strategy for flattened optionals414 /// field would be None in case of any parsing error (as in serde)415 flatten_ok: bool,416}417impl Parse for TypedAttr {418 fn parse(input: ParseStream) -> syn::Result<Self> {419 let mut out = Self::default();420 loop {421 let lookahead = input.lookahead1();422 if lookahead.peek(kw::rename) {423 input.parse::<kw::rename>()?;424 input.parse::<Token![=]>()?;425 let name = input.parse::<LitStr>()?;426 if out.rename.is_some() {427 return Err(Error::new(428 name.span(),429 "rename attribute may only be specified once",430 ));431 }432 out.rename = Some(name.value());433 } else if lookahead.peek(kw::flatten) {434 input.parse::<kw::flatten>()?;435 out.flatten = true;436 if input.peek(token::Paren) {437 let content;438 parenthesized!(content in input);439 let lookahead = content.lookahead1();440 if lookahead.peek(kw::ok) {441 content.parse::<kw::ok>()?;442 out.flatten_ok = true;443 } else {444 return Err(lookahead.error());445 }446 }447 } else if input.is_empty() {448 break;449 } else {450 return Err(lookahead.error());451 }452 if input.peek(Token![,]) {453 input.parse::<Token![,]>()?;454 } else {455 break;456 }457 }458 // input.parse::<kw::rename>()?;459 // input.parse::<Token![=]>()?;460 // let rename = input.parse::<LitStr>()?.value();461 Ok(out)462 }463}464465struct TypedField {466 attr: TypedAttr,467 ident: Ident,468 ty: Type,469 is_option: bool,470}471impl TypedField {472 fn parse(field: &syn::Field) -> Result<Self> {473 let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();474 let ident = if let Some(ident) = field.ident.clone() {475 ident476 } else {477 return Err(Error::new(478 field.span(),479 "this field should appear in output object, but it has no visible name",480 ));481 };482 let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {483 (true, ty.clone())484 } else {485 (false, field.ty.clone())486 };487 if is_option && attr.flatten {488 if !attr.flatten_ok {489 return Err(Error::new(490 field.span(),491 "strategy should be set when flattening Option",492 ));493 }494 } else if attr.flatten_ok {495 return Err(Error::new(496 field.span(),497 "flatten(ok) is only useable on optional fields",498 ));499 }500501 Ok(Self {502 attr,503 ident,504 ty,505 is_option,506 })507 }508 /// None if this field is flattened in jsonnet output509 fn name(&self) -> Option<String> {510 if self.attr.flatten {511 return None;512 }513 Some(514 self.attr515 .rename516 .clone()517 .unwrap_or_else(|| self.ident.to_string()),518 )519 }520521 fn expand_field(&self) -> Option<TokenStream> {522 if self.is_option {523 return None;524 }525 let name = self.name()?;526 let ty = &self.ty;527 Some(quote! {528 (#name, <#ty>::TYPE)529 })530 }531 fn expand_parse(&self) -> TokenStream {532 let ident = &self.ident;533 let ty = &self.ty;534 if self.attr.flatten {535 // optional flatten is handled in same way as serde536 return if self.is_option {537 quote! {538 #ident: <#ty>::parse(&obj, s.clone()).ok(),539 }540 } else {541 quote! {542 #ident: <#ty>::parse(&obj, s.clone())?,543 }544 };545 };546547 let name = self.name().unwrap();548 let value = if self.is_option {549 quote! {550 if let Some(value) = obj.get(s.clone(), #name.into())? {551 Some(<#ty>::from_untyped(value, s.clone())?)552 } else {553 None554 }555 }556 } else {557 quote! {558 <#ty>::from_untyped(obj.get(s.clone(), #name.into())?.ok_or_else(|| Error::NoSuchField(#name.into(), vec![]))?, s.clone())?559 }560 };561562 quote! {563 #ident: #value,564 }565 }566 fn expand_serialize(&self) -> Result<TokenStream> {567 let ident = &self.ident;568 let ty = &self.ty;569 Ok(if let Some(name) = self.name() {570 if self.is_option {571 quote! {572 if let Some(value) = self.#ident {573 out.member(#name.into()).value(s.clone(), <#ty>::into_untyped(value, s.clone())?)?;574 }575 }576 } else {577 quote! {578 out.member(#name.into()).value(s.clone(), <#ty>::into_untyped(self.#ident, s.clone())?)?;579 }580 }581 } else if self.is_option {582 quote! {583 if let Some(value) = self.#ident {584 value.serialize(s.clone(), out)?;585 }586 }587 } else {588 quote! {589 self.#ident.serialize(s.clone(), out)?;590 }591 })592 }593}594595#[proc_macro_derive(Typed, attributes(typed))]596pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {597 let input = parse_macro_input!(item as DeriveInput);598599 match derive_typed_inner(input) {600 Ok(v) => v.into(),601 Err(e) => e.to_compile_error().into(),602 }603}604605fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {606 let data = match &input.data {607 syn::Data::Struct(s) => s,608 _ => return Err(Error::new(input.span(), "only structs supported")),609 };610611 let ident = &input.ident;612 let fields = data613 .fields614 .iter()615 .map(TypedField::parse)616 .collect::<Result<Vec<_>>>()?;617618 let typed = {619 let fields = fields620 .iter()621 .flat_map(TypedField::expand_field)622 .collect::<Vec<_>>();623 let len = fields.len();624 quote! {625 const ITEMS: [(&'static str, &'static ComplexValType); #len] = [626 #(#fields,)*627 ];628 impl Typed for #ident {629 const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);630631 fn from_untyped(value: Val, s: State) -> Result<Self> {632 let obj = value.as_obj().expect("shape is correct");633 Self::parse(&obj, s)634 }635636 fn into_untyped(value: Self, s: State) -> Result<Val> {637 let mut out = ObjValueBuilder::new();638 value.serialize(s, &mut out)?;639 Ok(Val::Obj(out.build()))640 }641642 }643 }644 };645646 let fields_parse = fields.iter().map(TypedField::expand_parse);647 let fields_serialize = fields648 .iter()649 .map(TypedField::expand_serialize)650 .collect::<Result<Vec<_>>>()?;651652 Ok(quote! {653 const _: () = {654 use ::jrsonnet_evaluator::{655 typed::{ComplexValType, Typed, TypedObj, CheckType},656 Val, State,657 error::{LocError, Error, Result},658 ObjValueBuilder, ObjValue,659 };660661 #typed662663 impl TypedObj for #ident {664 fn serialize(self, s: State, out: &mut ObjValueBuilder) -> Result<(), LocError> {665 #(#fields_serialize)*666667 Ok(())668 }669 fn parse(obj: &ObjValue, s: State) -> Result<Self, LocError> {670 Ok(Self {671 #(#fields_parse)*672 })673 }674 }675 };676 })677}1use proc_macro2::TokenStream;2use quote::quote;3use syn::{4 parenthesized,5 parse::{Parse, ParseStream},6 parse_macro_input,7 punctuated::Punctuated,8 spanned::Spanned,9 token::{self, Comma},10 Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, Path,11 PathArguments, Result, ReturnType, Token, Type,12};1314fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>15where16 Ident: PartialEq<I>,17{18 let attrs = attrs19 .iter()20 .filter(|a| a.path.is_ident(&ident))21 .collect::<Vec<_>>();22 if attrs.len() > 1 {23 return Err(Error::new(24 attrs[1].span(),25 "this attribute may be specified only once",26 ));27 } else if attrs.is_empty() {28 return Ok(None);29 }30 let attr = attrs[0];31 let attr = attr.parse_args::<A>()?;3233 Ok(Some(attr))34}3536fn path_is(path: &Path, needed: &str) -> bool {37 path.leading_colon.is_none()38 && !path.segments.is_empty()39 && path.segments.iter().last().unwrap().ident == needed40}4142fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {43 match ty {44 Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {45 let args = &path.path.segments.iter().last().unwrap().arguments;46 Some(args)47 }48 _ => None,49 }50}5152fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {53 Ok(if let Some(args) = type_is_path(ty, "Option") {54 // It should have only on angle-bracketed param ("<String>"):55 let generic_arg = match args {56 PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),57 _ => return Err(Error::new(args.span(), "missing option generic")),58 };59 // This argument must be a type:60 match generic_arg {61 GenericArgument::Type(ty) => Some(ty),62 _ => {63 return Err(Error::new(64 generic_arg.span(),65 "option generic should be a type",66 ))67 }68 }69 } else {70 None71 })72}7374struct Field {75 name: Ident,76 _colon: Token![:],77 ty: Type,78}79impl Parse for Field {80 fn parse(input: ParseStream) -> syn::Result<Self> {81 Ok(Self {82 name: input.parse()?,83 _colon: input.parse()?,84 ty: input.parse()?,85 })86 }87}8889mod kw {90 syn::custom_keyword!(fields);91 syn::custom_keyword!(rename);92 syn::custom_keyword!(flatten);93 syn::custom_keyword!(ok);94}9596struct EmptyAttr;97impl Parse for EmptyAttr {98 fn parse(_input: ParseStream) -> Result<Self> {99 Ok(Self)100 }101}102103struct BuiltinAttrs {104 fields: Vec<Field>,105}106impl Parse for BuiltinAttrs {107 fn parse(input: ParseStream) -> syn::Result<Self> {108 if input.is_empty() {109 return Ok(Self { fields: Vec::new() });110 }111 input.parse::<kw::fields>()?;112 let fields;113 parenthesized!(fields in input);114 let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;115 Ok(Self {116 fields: p.into_iter().collect(),117 })118 }119}120121enum ArgInfo {122 Normal {123 ty: Box<Type>,124 is_option: bool,125 name: Option<String>,126 cfg_attrs: Vec<Attribute>,127 // ident: Ident,128 },129 Lazy {130 is_option: bool,131 name: Option<String>,132 },133 State,134 Location,135 This,136}137138impl ArgInfo {139 fn parse(name: &str, arg: &FnArg) -> Result<Self> {140 let arg = match arg {141 FnArg::Receiver(_) => unreachable!(),142 FnArg::Typed(a) => a,143 };144 let ident = match &arg.pat as &Pat {145 Pat::Ident(i) => Some(i.ident.clone()),146 _ => None,147 };148 let ty = &arg.ty;149 if type_is_path(ty, "State").is_some() {150 return Ok(Self::State);151 } else if type_is_path(ty, "CallLocation").is_some() {152 return Ok(Self::Location);153 } else if type_is_path(ty, "Thunk").is_some() {154 return Ok(Self::Lazy {155 is_option: false,156 name: ident.map(|v| v.to_string()),157 });158 }159160 match ty as &Type {161 Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),162 _ => {}163 }164165 let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? {166 if type_is_path(ty, "Thunk").is_some() {167 return Ok(Self::Lazy {168 is_option: true,169 name: ident.map(|v| v.to_string()),170 });171 }172173 (true, Box::new(ty.clone()))174 } else {175 (false, ty.clone())176 };177178 let cfg_attrs = arg179 .attrs180 .iter()181 .filter(|a| a.path.is_ident("cfg"))182 .cloned()183 .collect();184185 Ok(Self::Normal {186 ty,187 is_option,188 name: ident.map(|v| v.to_string()),189 cfg_attrs,190 })191 }192}193194#[proc_macro_attribute]195pub fn builtin(196 attr: proc_macro::TokenStream,197 item: proc_macro::TokenStream,198) -> proc_macro::TokenStream {199 let attr = parse_macro_input!(attr as BuiltinAttrs);200 let item: ItemFn = parse_macro_input!(item);201202 match builtin_inner(attr, item) {203 Ok(v) => v.into(),204 Err(e) => e.into_compile_error().into(),205 }206}207208fn builtin_inner(attr: BuiltinAttrs, fun: ItemFn) -> syn::Result<TokenStream> {209 let result = match fun.sig.output {210 ReturnType::Default => {211 return Err(Error::new(212 fun.sig.span(),213 "builtin should return something",214 ))215 }216 ReturnType::Type(_, ref ty) => ty.clone(),217 };218 let result_inner = if let Some(args) = type_is_path(&result, "Result") {219 let generic_arg = match args {220 PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),221 _ => return Err(Error::new(args.span(), "missing result generic")),222 };223 // This argument must be a type:224 match generic_arg {225 GenericArgument::Type(ty) => ty,226 _ => {227 return Err(Error::new(228 generic_arg.span(),229 "option generic should be a type",230 ))231 }232 }233 } else {234 return Err(Error::new(result.span(), "return value should be result"));235 };236237 let name = fun.sig.ident.to_string();238 let args = fun239 .sig240 .inputs241 .iter()242 .map(|arg| ArgInfo::parse(&name, arg))243 .collect::<Result<Vec<_>>>()?;244245 let params_desc = args.iter().flat_map(|a| match a {246 ArgInfo::Normal {247 is_option,248 name,249 cfg_attrs,250 ..251 } => {252 let name = name253 .as_ref()254 .map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})255 .unwrap_or_else(|| quote! {None});256 Some(quote! {257 #(#cfg_attrs)*258 BuiltinParam {259 name: #name,260 has_default: #is_option,261 },262 })263 }264 ArgInfo::Lazy { is_option, name } => {265 let name = name266 .as_ref()267 .map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})268 .unwrap_or_else(|| quote! {None});269 Some(quote! {270 BuiltinParam {271 name: #name,272 has_default: #is_option,273 },274 })275 }276 ArgInfo::State => None,277 ArgInfo::Location => None,278 ArgInfo::This => None,279 });280281 let mut id = 0usize;282 let pass = args283 .iter()284 .map(|a| match a {285 ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {286 let cid = id;287 id += 1;288 (quote! {#cid}, a)289 }290 ArgInfo::State | ArgInfo::Location | ArgInfo::This => {291 (quote! {compile_error!("should not use id")}, a)292 }293 })294 .map(|(id, a)| match a {295 ArgInfo::Normal {296 ty,297 is_option,298 name,299 cfg_attrs,300 } => {301 let name = name.as_ref().map(|v| v.as_str()).unwrap_or("<unnamed>");302 let eval = quote! {jrsonnet_evaluator::State::push_description(303 || format!("argument <{}> evaluation", #name),304 || <#ty>::from_untyped(value.evaluate(s.clone())?, s.clone()),305 )?};306 let value = if *is_option {307 quote! {if let Some(value) = &parsed[#id] {308 Some(#eval)309 } else {310 None311 },}312 } else {313 quote! {{314 let value = parsed[#id].as_ref().expect("args shape is checked");315 #eval316 },}317 };318 quote! {319 #(#cfg_attrs)*320 #value321 }322 }323 ArgInfo::Lazy { is_option, .. } => {324 if *is_option {325 quote! {if let Some(value) = &parsed[#id] {326 Some(value.clone())327 } else {328 None329 }}330 } else {331 quote! {332 parsed[#id].as_ref().expect("args shape is correct").clone(),333 }334 }335 }336 ArgInfo::State => quote! {s.clone(),},337 ArgInfo::Location => quote! {location,},338 ArgInfo::This => quote! {self,},339 });340341 let fields = attr.fields.iter().map(|field| {342 let name = &field.name;343 let ty = &field.ty;344 quote! {345 pub #name: #ty,346 }347 });348349 let name = &fun.sig.ident;350 let vis = &fun.vis;351 let static_ext = if attr.fields.is_empty() {352 quote! {353 impl #name {354 pub const INST: &'static dyn StaticBuiltin = &#name {};355 }356 impl StaticBuiltin for #name {}357 }358 } else {359 quote! {}360 };361 let static_derive_copy = if attr.fields.is_empty() {362 quote! {, Copy}363 } else {364 quote! {}365 };366367 Ok(quote! {368 #fun369 #[doc(hidden)]370 #[allow(non_camel_case_types)]371 #[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]372 #vis struct #name {373 #(#fields)*374 }375 const _: () = {376 use ::jrsonnet_evaluator::{377 State, Val,378 function::{builtin::{Builtin, StaticBuiltin, BuiltinParam}, CallLocation, ArgsLike, parse::parse_builtin_call},379 error::Result, Context, typed::Typed,380 parser::ExprLocation,381 };382 const PARAMS: &'static [BuiltinParam] = &[383 #(#params_desc)*384 ];385386 #static_ext387 impl Builtin for #name388 where389 Self: 'static390 {391 fn name(&self) -> &str {392 stringify!(#name)393 }394 fn params(&self) -> &[BuiltinParam] {395 PARAMS396 }397 fn call(&self, s: State, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {398 let parsed = parse_builtin_call(s.clone(), ctx, &PARAMS, args, false)?;399400 let result: #result = #name(#(#pass)*);401 let result = result?;402 <#result_inner>::into_untyped(result, s)403 }404 }405 };406 })407}408409#[derive(Default)]410struct TypedAttr {411 rename: Option<String>,412 flatten: bool,413 /// flatten(ok) strategy for flattened optionals414 /// field would be None in case of any parsing error (as in serde)415 flatten_ok: bool,416}417impl Parse for TypedAttr {418 fn parse(input: ParseStream) -> syn::Result<Self> {419 let mut out = Self::default();420 loop {421 let lookahead = input.lookahead1();422 if lookahead.peek(kw::rename) {423 input.parse::<kw::rename>()?;424 input.parse::<Token![=]>()?;425 let name = input.parse::<LitStr>()?;426 if out.rename.is_some() {427 return Err(Error::new(428 name.span(),429 "rename attribute may only be specified once",430 ));431 }432 out.rename = Some(name.value());433 } else if lookahead.peek(kw::flatten) {434 input.parse::<kw::flatten>()?;435 out.flatten = true;436 if input.peek(token::Paren) {437 let content;438 parenthesized!(content in input);439 let lookahead = content.lookahead1();440 if lookahead.peek(kw::ok) {441 content.parse::<kw::ok>()?;442 out.flatten_ok = true;443 } else {444 return Err(lookahead.error());445 }446 }447 } else if input.is_empty() {448 break;449 } else {450 return Err(lookahead.error());451 }452 if input.peek(Token![,]) {453 input.parse::<Token![,]>()?;454 } else {455 break;456 }457 }458 // input.parse::<kw::rename>()?;459 // input.parse::<Token![=]>()?;460 // let rename = input.parse::<LitStr>()?.value();461 Ok(out)462 }463}464465struct TypedField {466 attr: TypedAttr,467 ident: Ident,468 ty: Type,469 is_option: bool,470}471impl TypedField {472 fn parse(field: &syn::Field) -> Result<Self> {473 let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();474 let ident = if let Some(ident) = field.ident.clone() {475 ident476 } else {477 return Err(Error::new(478 field.span(),479 "this field should appear in output object, but it has no visible name",480 ));481 };482 let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {483 (true, ty.clone())484 } else {485 (false, field.ty.clone())486 };487 if is_option && attr.flatten {488 if !attr.flatten_ok {489 return Err(Error::new(490 field.span(),491 "strategy should be set when flattening Option",492 ));493 }494 } else if attr.flatten_ok {495 return Err(Error::new(496 field.span(),497 "flatten(ok) is only useable on optional fields",498 ));499 }500501 Ok(Self {502 attr,503 ident,504 ty,505 is_option,506 })507 }508 /// None if this field is flattened in jsonnet output509 fn name(&self) -> Option<String> {510 if self.attr.flatten {511 return None;512 }513 Some(514 self.attr515 .rename516 .clone()517 .unwrap_or_else(|| self.ident.to_string()),518 )519 }520521 fn expand_field(&self) -> Option<TokenStream> {522 if self.is_option {523 return None;524 }525 let name = self.name()?;526 let ty = &self.ty;527 Some(quote! {528 (#name, <#ty>::TYPE)529 })530 }531 fn expand_parse(&self) -> TokenStream {532 let ident = &self.ident;533 let ty = &self.ty;534 if self.attr.flatten {535 // optional flatten is handled in same way as serde536 return if self.is_option {537 quote! {538 #ident: <#ty>::parse(&obj, s.clone()).ok(),539 }540 } else {541 quote! {542 #ident: <#ty>::parse(&obj, s.clone())?,543 }544 };545 };546547 let name = self.name().unwrap();548 let value = if self.is_option {549 quote! {550 if let Some(value) = obj.get(s.clone(), #name.into())? {551 Some(<#ty>::from_untyped(value, s.clone())?)552 } else {553 None554 }555 }556 } else {557 quote! {558 <#ty>::from_untyped(obj.get(s.clone(), #name.into())?.ok_or_else(|| Error::NoSuchField(#name.into(), vec![]))?, s.clone())?559 }560 };561562 quote! {563 #ident: #value,564 }565 }566 fn expand_serialize(&self) -> Result<TokenStream> {567 let ident = &self.ident;568 let ty = &self.ty;569 Ok(if let Some(name) = self.name() {570 if self.is_option {571 quote! {572 if let Some(value) = self.#ident {573 out.member(#name.into()).value(s.clone(), <#ty>::into_untyped(value, s.clone())?)?;574 }575 }576 } else {577 quote! {578 out.member(#name.into()).value(s.clone(), <#ty>::into_untyped(self.#ident, s.clone())?)?;579 }580 }581 } else if self.is_option {582 quote! {583 if let Some(value) = self.#ident {584 value.serialize(s.clone(), out)?;585 }586 }587 } else {588 quote! {589 self.#ident.serialize(s.clone(), out)?;590 }591 })592 }593}594595#[proc_macro_derive(Typed, attributes(typed))]596pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {597 let input = parse_macro_input!(item as DeriveInput);598599 match derive_typed_inner(input) {600 Ok(v) => v.into(),601 Err(e) => e.to_compile_error().into(),602 }603}604605fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {606 let data = match &input.data {607 syn::Data::Struct(s) => s,608 _ => return Err(Error::new(input.span(), "only structs supported")),609 };610611 let ident = &input.ident;612 let fields = data613 .fields614 .iter()615 .map(TypedField::parse)616 .collect::<Result<Vec<_>>>()?;617618 let typed = {619 let fields = fields620 .iter()621 .flat_map(TypedField::expand_field)622 .collect::<Vec<_>>();623 let len = fields.len();624 quote! {625 const ITEMS: [(&'static str, &'static ComplexValType); #len] = [626 #(#fields,)*627 ];628 impl Typed for #ident {629 const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);630631 fn from_untyped(value: Val, s: State) -> Result<Self> {632 let obj = value.as_obj().expect("shape is correct");633 Self::parse(&obj, s)634 }635636 fn into_untyped(value: Self, s: State) -> Result<Val> {637 let mut out = ObjValueBuilder::new();638 value.serialize(s, &mut out)?;639 Ok(Val::Obj(out.build()))640 }641642 }643 }644 };645646 let fields_parse = fields.iter().map(TypedField::expand_parse);647 let fields_serialize = fields648 .iter()649 .map(TypedField::expand_serialize)650 .collect::<Result<Vec<_>>>()?;651652 Ok(quote! {653 const _: () = {654 use ::jrsonnet_evaluator::{655 typed::{ComplexValType, Typed, TypedObj, CheckType},656 Val, State,657 error::{LocError, Error, Result},658 ObjValueBuilder, ObjValue,659 };660661 #typed662663 impl TypedObj for #ident {664 fn serialize(self, s: State, out: &mut ObjValueBuilder) -> Result<(), LocError> {665 #(#fields_serialize)*666667 Ok(())668 }669 fn parse(obj: &ObjValue, s: State) -> Result<Self, LocError> {670 Ok(Self {671 #(#fields_parse)*672 })673 }674 }675 };676 })677}crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -137,43 +137,36 @@
builder
.member(name.into())
.hide()
- .value(s.clone(), Val::Func(FuncVal::StaticBuiltin(builtin)))
+ .value(Val::Func(FuncVal::StaticBuiltin(builtin)))
.expect("no conflict");
}
builder
.member("extVar".into())
.hide()
- .value(
- s.clone(),
- Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {
- settings: settings.clone()
- })))),
- )
+ .value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {
+ settings: settings.clone()
+ })))))
.expect("no conflict");
builder
.member("native".into())
.hide()
- .value(
- s.clone(),
- Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {
- settings: settings.clone()
- })))),
- )
+ .value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {
+ settings: settings.clone()
+ })))))
.expect("no conflict");
builder
.member("trace".into())
.hide()
- .value(
- s.clone(),
- Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace { settings })))),
- )
+ .value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace {
+ settings
+ })))))
.expect("no conflict");
builder
.member("id".into())
.hide()
- .value(s, Val::Func(FuncVal::Id))
+ .value(Val::Func(FuncVal::Id))
.expect("no conflict");
builder.build()