difftreelog
refactor saner imports from TLA/std.extVars
in: master
13 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -14,7 +14,7 @@
use jrsonnet_evaluator::{
bail,
error::{ErrorKind::*, Result},
- ImportResolver,
+ AsPathLike, ImportResolver, ResolvePath,
};
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
@@ -38,7 +38,7 @@
out: RefCell<HashMap<SourcePath, Vec<u8>>>,
}
impl ImportResolver for CallbackImportResolver {
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
let base = if let Some(p) = from.downcast_ref::<SourceFile>() {
let mut o = p.path().to_owned();
o.pop();
@@ -51,7 +51,11 @@
unreachable!("can't resolve this path");
};
let base = unsafe { crate::unparse_path(&base) };
- let rel = CString::new(path).unwrap();
+ let rel = path.as_path();
+ let rel = match rel {
+ ResolvePath::Str(s) => CString::new(s.as_bytes()).unwrap(),
+ ResolvePath::Path(p) => unsafe { crate::unparse_path(p) },
+ };
let found_here: *mut c_char = null_mut();
let mut buf = null_mut();
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -28,7 +28,7 @@
rustc_hash::FxHashMap,
stack::set_stack_depth_limit,
trace::{CompactFormat, PathResolver, TraceFormat},
- FileImportResolver, IStr, ImportResolver, Result, State, Val,
+ AsPathLike, FileImportResolver, IStr, ImportResolver, Result, State, Val,
};
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_parser::SourcePath;
@@ -62,18 +62,18 @@
}
}
-unsafe fn unparse_path(input: &Path) -> Cow<'_, CStr> {
+unsafe fn unparse_path(input: &Path) -> CString {
#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;
let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");
- Cow::Owned(str)
+ str
}
#[cfg(not(target_family = "unix"))]
{
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)
+ cstr
}
}
@@ -93,16 +93,12 @@
self.inner.borrow().load_file_contents(resolved)
}
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
self.inner.borrow().resolve_from(from, path)
}
- fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {
self.inner.borrow().resolve_from_default(path)
- }
-
- fn resolve(&self, path: &Path) -> Result<SourcePath> {
- self.inner.borrow().resolve(path)
}
}
bindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -3,7 +3,6 @@
use std::{ffi::CStr, os::raw::c_char};
use jrsonnet_evaluator::{function::TlaArg, IStr};
-use jrsonnet_parser::{ParserSettings, Source};
use crate::VM;
@@ -84,14 +83,7 @@
let code = unsafe { CStr::from_ptr(code) };
let name: IStr = name.to_str().expect("name is not utf-8").into();
- let code: IStr = code.to_str().expect("code is not utf-8").into();
- let code = jrsonnet_parser::parse(
- &code,
- &ParserSettings {
- source: Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.clone()),
- },
- )
- .expect("can't parse TLA code");
+ let code: String = code.to_str().expect("code is not utf-8").to_owned();
- vm.tla_args.insert(name, TlaArg::Code(code));
+ vm.tla_args.insert(name, TlaArg::InlineCode(code));
}
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -182,7 +182,7 @@
let input_str = std::str::from_utf8(&input)?;
s.evaluate_snippet("<stdin>".to_owned(), input_str)?
} else {
- s.import(&input)?
+ s.import(input.as_str())?
};
let tla = opts.tla.tla_opts()?;
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -1,7 +1,7 @@
-use std::{fs::read_to_string, str::FromStr};
+use std::str::FromStr;
use clap::Parser;
-use jrsonnet_evaluator::{trace::PathResolver, Result};
+use jrsonnet_evaluator::{function::TlaArg, trace::PathResolver, Result};
use jrsonnet_stdlib::ContextInitializer;
#[derive(Clone)]
@@ -54,25 +54,20 @@
#[derive(Clone)]
pub struct ExtFile {
pub name: String,
- pub value: String,
+ pub path: String,
}
impl FromStr for ExtFile {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
- let out: Vec<&str> = s.split('=').collect();
- if out.len() != 2 {
+ let Some((name, path)) = s.split_once('=') else {
return Err("bad ext-file syntax".to_owned());
- }
- let file = read_to_string(out[1]);
- match file {
- Ok(content) => Ok(Self {
- name: out[0].into(),
- value: content,
- }),
- Err(e) => Err(format!("{e}")),
- }
+ };
+ Ok(Self {
+ name: name.into(),
+ path: path.into(),
+ })
}
}
@@ -110,16 +105,27 @@
}
let ctx = ContextInitializer::new(PathResolver::new_cwd_fallback());
for ext in &self.ext_str {
- ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
+ ctx.settings_mut().ext_vars.insert(
+ ext.name.as_str().into(),
+ TlaArg::String(ext.value.as_str().into()),
+ );
}
for ext in &self.ext_str_file {
- ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
+ ctx.settings_mut().ext_vars.insert(
+ ext.name.as_str().into(),
+ TlaArg::ImportStr(ext.path.clone()),
+ );
}
for ext in &self.ext_code {
- ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
+ ctx.settings_mut().ext_vars.insert(
+ ext.name.as_str().into(),
+ TlaArg::InlineCode(ext.value.clone()),
+ );
}
for ext in &self.ext_code_file {
- ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
+ ctx.settings_mut()
+ .ext_vars
+ .insert(ext.name.as_str().into(), TlaArg::Import(ext.path.clone()));
}
Ok(Some(ctx))
}
crates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -1,12 +1,5 @@
use clap::Parser;
-use jrsonnet_evaluator::{
- error::{ErrorKind, Result},
- function::TlaArg,
- gc::WithCapacityExt as _,
- rustc_hash::FxHashMap,
- IStr,
-};
-use jrsonnet_parser::{ParserSettings, Source};
+use jrsonnet_evaluator::{IStr, error::Result, function::TlaArg, gc::WithCapacityExt as _, rustc_hash::FxHashMap};
use crate::{ExtFile, ExtStr};
@@ -35,37 +28,27 @@
impl TlaOpts {
pub fn tla_opts(&self) -> Result<FxHashMap<IStr, TlaArg>> {
let mut out = FxHashMap::new();
- for (name, value) in self
- .tla_str
- .iter()
- .map(|c| (&c.name, &c.value))
- .chain(self.tla_str_file.iter().map(|c| (&c.name, &c.value)))
- {
- out.insert(name.into(), TlaArg::String(value.into()));
+ for ext in &self.tla_str {
+ out.insert(
+ ext.name.as_str().into(),
+ TlaArg::String(ext.value.as_str().into()),
+ );
}
- for (name, code) in self
- .tla_code
- .iter()
- .map(|c| (&c.name, &c.value))
- .chain(self.tla_code_file.iter().map(|c| (&c.name, &c.value)))
- {
- let source = Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.into());
+ for ext in &self.tla_str_file {
out.insert(
- (name as &str).into(),
- TlaArg::Code(
- jrsonnet_parser::parse(
- code,
- &ParserSettings {
- source: source.clone(),
- },
- )
- .map_err(|e| ErrorKind::ImportSyntaxError {
- path: source,
- error: Box::new(e),
- })?,
- ),
+ ext.name.as_str().into(),
+ TlaArg::ImportStr(ext.name.as_str().into()),
+ );
+ }
+ for ext in &self.tla_code {
+ out.insert(
+ ext.name.as_str().into(),
+ TlaArg::InlineCode(ext.value.clone()),
);
}
+ for ext in &self.tla_code_file {
+ out.insert(ext.name.as_str().into(), TlaArg::Import(ext.path.clone()));
+ }
Ok(out)
}
}
crates/jrsonnet-evaluator/src/async_import.rsdiffbeforeafterboth1use std::{any::Any, cell::RefCell, future::Future, path::Path};23use jrsonnet_gcmodule::Acyclic;4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6 ArgsDesc, AssertStmt, BindSpec, CompSpec, Destruct, Expr, FieldMember, FieldName, ForSpecData,7 IfSpecData, LocExpr, Member, ObjBody, Param, ParamsDesc, ParserSettings, SliceDesc, Source,8 SourcePath,9};10use rustc_hash::FxHashMap;1112use crate::{bail, FileData, ImportResolver, State};1314pub struct Import {15 path: IStr,16 expression: bool,17}1819pub struct FoundImports(Vec<Import>);2021// Visits all nodes, trying to find import statements22#[allow(clippy::too_many_lines)]23pub fn find_imports(expr: &LocExpr, out: &mut FoundImports) {24 fn in_destruct(dest: &Destruct, #[allow(unused_variables)] out: &mut FoundImports) {25 match dest {26 #[cfg(feature = "exp-destruct")]27 Destruct::Array {28 start,29 rest: _,30 end,31 } => {32 for dest in start {33 in_destruct(dest, out);34 }35 for dest in end {36 in_destruct(dest, out);37 }38 }39 #[cfg(feature = "exp-destruct")]40 Destruct::Object { fields, rest: _ } => {41 for (_, dest, default) in fields {42 if let Some(dest) = dest {43 in_destruct(dest, out);44 }45 if let Some(expr) = default {46 find_imports(expr, out);47 }48 }49 }50 #[cfg(feature = "exp-destruct")]51 Destruct::Skip => {}52 Destruct::Full(_) => {}53 }54 }55 fn in_compspec(specs: &[CompSpec], out: &mut FoundImports) {56 for spec in specs {57 match spec {58 CompSpec::IfSpec(IfSpecData(expr)) => find_imports(expr, out),59 CompSpec::ForSpec(ForSpecData(destruct, expr)) => {60 in_destruct(destruct, out);61 find_imports(expr, out);62 }63 }64 }65 }66 fn in_params(params: &ParamsDesc, out: &mut FoundImports) {67 for Param(dest, default) in &*params.0 {68 in_destruct(dest, out);69 if let Some(expr) = default {70 find_imports(expr, out);71 }72 }73 }74 fn in_bind(specs: &[BindSpec], out: &mut FoundImports) {75 for spec in specs {76 match spec {77 BindSpec::Field {78 into: dest,79 value: expr,80 } => {81 in_destruct(dest, out);82 find_imports(expr, out);83 }84 BindSpec::Function {85 name: _,86 params,87 value: expr,88 } => {89 in_params(params, out);90 find_imports(expr, out);91 }92 }93 }94 }95 fn in_args(ArgsDesc { unnamed, named }: &ArgsDesc, out: &mut FoundImports) {96 for expr in unnamed {97 find_imports(expr, out);98 }99 for (_, expr) in named {100 find_imports(expr, out);101 }102 }103 fn in_obj(obj: &ObjBody, out: &mut FoundImports) {104 match obj {105 ObjBody::MemberList(v) => {106 for member in v {107 match member {108 Member::Field(FieldMember {109 name,110 params,111 value,112 ..113 }) => {114 match name {115 FieldName::Fixed(_) => {}116 FieldName::Dyn(expr) => find_imports(expr, out),117 }118 if let Some(params) = params {119 in_params(params, out);120 }121 find_imports(value, out);122 }123 Member::BindStmt(_) => todo!(),124 Member::AssertStmt(AssertStmt(expr, expr2)) => {125 find_imports(expr, out);126 if let Some(expr) = expr2 {127 find_imports(expr, out);128 }129 }130 }131 }132 }133 ObjBody::ObjComp(_) => todo!(),134 }135 }136 match &*expr.expr() {137 Expr::Import(v) | Expr::ImportStr(v) | Expr::ImportBin(v) => {138 if let Expr::Str(s) = &*v.expr() {139 out.0.push(Import {140 path: s.clone(),141 expression: matches!(&*expr.expr(), Expr::Import(_)),142 });143 }144 // Non-string import will fail in runtime145 }146147 Expr::Literal(_) | Expr::Str(_) | Expr::Num(_) | Expr::Var(_) => {}148149 Expr::Arr(arr) => {150 for expr in arr {151 find_imports(expr, out);152 }153 }154 Expr::ArrComp(expr, specs) => {155 find_imports(expr, out);156 in_compspec(specs, out);157 }158 Expr::Obj(obj) => in_obj(obj, out),159 Expr::ObjExtend(expr, obj) => {160 find_imports(expr, out);161 in_obj(obj, out);162 }163 Expr::BinaryOp(a, _, b) => {164 find_imports(a, out);165 find_imports(b, out);166 }167 Expr::AssertExpr(AssertStmt(expr, expr2), then) => {168 find_imports(expr, out);169 if let Some(expr) = expr2 {170 find_imports(expr, out);171 }172 find_imports(then, out);173 }174 Expr::LocalExpr(specs, expr) => {175 in_bind(specs, out);176 find_imports(expr, out);177 }178 Expr::Apply(expr, args, _) => {179 find_imports(expr, out);180 in_args(args, out);181 }182 Expr::Index { indexable, parts } => {183 find_imports(indexable, out);184 for part in parts {185 find_imports(&part.value, out);186 }187 }188 Expr::Function(params, expr) => {189 in_params(params, out);190 find_imports(expr, out);191 }192 Expr::IfElse {193 cond: IfSpecData(expr),194 cond_then,195 cond_else,196 } => {197 find_imports(expr, out);198 find_imports(cond_then, out);199 if let Some(expr) = cond_else {200 find_imports(expr, out);201 }202 }203 Expr::Slice(expr, SliceDesc { start, end, step }) => {204 find_imports(expr, out);205 if let Some(expr) = start {206 find_imports(expr, out);207 }208 if let Some(expr) = end {209 find_imports(expr, out);210 }211 if let Some(expr) = step {212 find_imports(expr, out);213 }214 }215 Expr::Parened(expr) | Expr::UnaryOp(_, expr) | Expr::ErrorStmt(expr) => {216 find_imports(expr, out);217 }218 }219}220221pub trait AsyncImportResolver {222 type Error;223 /// Resolves file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond224 /// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`225 /// where `${vendor}` is a library path.226 ///227 /// `from` should only be returned from [`ImportResolver::resolve`],228 /// or from other defined file, any other value may result in panic229 fn resolve_from(230 &self,231 from: &SourcePath,232 path: &str,233 ) -> impl Future<Output = Result<SourcePath, Self::Error>>;234 fn resolve_from_default(235 &self,236 path: &str,237 ) -> impl Future<Output = Result<SourcePath, Self::Error>> {238 async { self.resolve_from(&SourcePath::default(), path).await }239 }240 /// Resolves absolute path, doesn't supports jpath and other fancy things241 fn resolve(&self, path: &Path) -> impl Future<Output = Result<SourcePath, Self::Error>>;242243 /// Load resolved file244 /// This should only be called with value returned245 /// from [`ImportResolver::resolve_file`]/[`ImportResolver::resolve`],246 /// this cannot be resolved using associated type,247 /// as the evaluator uses object instead of generic for [`ImportResolver`]248 fn load_file_contents(249 &self,250 resolved: &SourcePath,251 ) -> impl Future<Output = Result<Vec<u8>, Self::Error>>;252}253254#[derive(Acyclic)]255struct ResolvedImportResolver {256 resolved: RefCell<FxHashMap<(SourcePath, IStr), (SourcePath, bool)>>,257}258impl ImportResolver for ResolvedImportResolver {259 fn load_file_contents(&self, _resolved: &SourcePath) -> crate::Result<Vec<u8>> {260 unreachable!("all files should be loaded at this point");261 }262263 fn resolve_from(&self, from: &SourcePath, path: &str) -> crate::Result<SourcePath> {264 Ok(self265 .resolved266 .borrow()267 .get(&(from.clone(), path.into()))268 .expect("all imports should be resolved at this point")269 .0270 .clone())271 }272273 fn resolve_from_default(&self, path: &str) -> crate::Result<SourcePath> {274 self.resolve_from(&SourcePath::default(), path)275 }276277 fn resolve(&self, path: &Path) -> crate::Result<SourcePath> {278 bail!(crate::error::ErrorKind::AbsoluteImportNotSupported(279 path.to_owned()280 ))281 }282}283284enum Job {285 LoadFile { path: SourcePath, parse: bool },286 ParseFile(SourcePath),287 ResolveImport { from: SourcePath, import: Import },288}289290#[allow(clippy::future_not_send)]291pub async fn async_import<H>(s: State, handler: H, path: impl AsRef<Path>) -> Result<(), H::Error>292where293 H: AsyncImportResolver,294{295 let resolved = (s.import_resolver() as &dyn Any)296 .downcast_ref::<ResolvedImportResolver>()297 .expect("for async imports, import_resolver should be set to ResolvedImportResolver");298299 let mut resolved_map = resolved.resolved.borrow_mut();300301 let mut queue = vec![Job::LoadFile {302 path: handler.resolve(path.as_ref()).await?,303 parse: true,304 }];305 while let Some(job) = queue.pop() {306 match job {307 Job::LoadFile { path, parse } => {308 if !s.0.file_cache.borrow().contains_key(&path) {309 let data = handler.load_file_contents(&path).await?;310 s.0.file_cache311 .borrow_mut()312 .insert(path.clone(), FileData::new_bytes(data.as_slice().into()));313 }314 if parse {315 queue.push(Job::ParseFile(path));316 }317 }318 Job::ParseFile(path) => {319 if let Some(file) = s.0.file_cache.borrow_mut().get_mut(&path) {320 if file.parsed.is_none() {321 let Some(code) = file.get_string() else {322 continue;323 };324 let source = Source::new(path.clone(), code.clone());325 // If failed - then skip import326 file.parsed =327 jrsonnet_parser::parse(&code, &ParserSettings { source }).ok();328 if let Some(parsed) = &file.parsed {329 let mut imports = FoundImports(vec![]);330 find_imports(parsed, &mut imports);331 for import in imports.0 {332 queue.push(Job::ResolveImport {333 from: path.clone(),334 import,335 });336 }337 }338 }339 }340 }341 Job::ResolveImport { from, import } => {342 if let Some((resolved, expression)) =343 resolved_map.get_mut(&(from.clone(), import.path.clone()))344 {345 if import.expression && !*expression {346 *expression = true;347 queue.push(Job::ParseFile(resolved.clone()));348 }349 continue;350 }351 let resolved = handler.resolve_from(&from, &import.path).await?;352 queue.push(Job::LoadFile {353 path: resolved,354 parse: import.expression,355 });356 }357 }358 }359 Ok(())360}1use std::{any::Any, cell::RefCell, future::Future};23use jrsonnet_gcmodule::Acyclic;4use jrsonnet_parser::{5 ArgsDesc, AssertStmt, BindSpec, CompSpec, Destruct, Expr, FieldMember, FieldName, ForSpecData,6 IfSpecData, LocExpr, Member, ObjBody, Param, ParamsDesc, ParserSettings, SliceDesc, Source,7 SourcePath,8};9use rustc_hash::FxHashMap;1011use crate::{AsPathLike, FileData, ImportResolver, ResolvePathOwned, State};1213pub struct Import {14 path: ResolvePathOwned,15 expression: bool,16}1718pub struct FoundImports(Vec<Import>);1920// Visits all nodes, trying to find import statements21#[allow(clippy::too_many_lines)]22pub fn find_imports(expr: &LocExpr, out: &mut FoundImports) {23 fn in_destruct(dest: &Destruct, #[allow(unused_variables)] out: &mut FoundImports) {24 match dest {25 #[cfg(feature = "exp-destruct")]26 Destruct::Array {27 start,28 rest: _,29 end,30 } => {31 for dest in start {32 in_destruct(dest, out);33 }34 for dest in end {35 in_destruct(dest, out);36 }37 }38 #[cfg(feature = "exp-destruct")]39 Destruct::Object { fields, rest: _ } => {40 for (_, dest, default) in fields {41 if let Some(dest) = dest {42 in_destruct(dest, out);43 }44 if let Some(expr) = default {45 find_imports(expr, out);46 }47 }48 }49 #[cfg(feature = "exp-destruct")]50 Destruct::Skip => {}51 Destruct::Full(_) => {}52 }53 }54 fn in_compspec(specs: &[CompSpec], out: &mut FoundImports) {55 for spec in specs {56 match spec {57 CompSpec::IfSpec(IfSpecData(expr)) => find_imports(expr, out),58 CompSpec::ForSpec(ForSpecData(destruct, expr)) => {59 in_destruct(destruct, out);60 find_imports(expr, out);61 }62 }63 }64 }65 fn in_params(params: &ParamsDesc, out: &mut FoundImports) {66 for Param(dest, default) in &*params.0 {67 in_destruct(dest, out);68 if let Some(expr) = default {69 find_imports(expr, out);70 }71 }72 }73 fn in_bind(specs: &[BindSpec], out: &mut FoundImports) {74 for spec in specs {75 match spec {76 BindSpec::Field {77 into: dest,78 value: expr,79 } => {80 in_destruct(dest, out);81 find_imports(expr, out);82 }83 BindSpec::Function {84 name: _,85 params,86 value: expr,87 } => {88 in_params(params, out);89 find_imports(expr, out);90 }91 }92 }93 }94 fn in_args(ArgsDesc { unnamed, named }: &ArgsDesc, out: &mut FoundImports) {95 for expr in unnamed {96 find_imports(expr, out);97 }98 for (_, expr) in named {99 find_imports(expr, out);100 }101 }102 fn in_obj(obj: &ObjBody, out: &mut FoundImports) {103 match obj {104 ObjBody::MemberList(v) => {105 for member in v {106 match member {107 Member::Field(FieldMember {108 name,109 params,110 value,111 ..112 }) => {113 match name {114 FieldName::Fixed(_) => {}115 FieldName::Dyn(expr) => find_imports(expr, out),116 }117 if let Some(params) = params {118 in_params(params, out);119 }120 find_imports(value, out);121 }122 Member::BindStmt(_) => todo!(),123 Member::AssertStmt(AssertStmt(expr, expr2)) => {124 find_imports(expr, out);125 if let Some(expr) = expr2 {126 find_imports(expr, out);127 }128 }129 }130 }131 }132 ObjBody::ObjComp(_) => todo!(),133 }134 }135 match &*expr.expr() {136 Expr::Import(v) | Expr::ImportStr(v) | Expr::ImportBin(v) => {137 if let Expr::Str(s) = &*v.expr() {138 out.0.push(Import {139 path: ResolvePathOwned::Str(s.to_string()),140 expression: matches!(&*expr.expr(), Expr::Import(_)),141 });142 }143 // Non-string import will fail in runtime144 }145146 Expr::Literal(_) | Expr::Str(_) | Expr::Num(_) | Expr::Var(_) => {}147148 Expr::Arr(arr) => {149 for expr in arr {150 find_imports(expr, out);151 }152 }153 Expr::ArrComp(expr, specs) => {154 find_imports(expr, out);155 in_compspec(specs, out);156 }157 Expr::Obj(obj) => in_obj(obj, out),158 Expr::ObjExtend(expr, obj) => {159 find_imports(expr, out);160 in_obj(obj, out);161 }162 Expr::BinaryOp(a, _, b) => {163 find_imports(a, out);164 find_imports(b, out);165 }166 Expr::AssertExpr(AssertStmt(expr, expr2), then) => {167 find_imports(expr, out);168 if let Some(expr) = expr2 {169 find_imports(expr, out);170 }171 find_imports(then, out);172 }173 Expr::LocalExpr(specs, expr) => {174 in_bind(specs, out);175 find_imports(expr, out);176 }177 Expr::Apply(expr, args, _) => {178 find_imports(expr, out);179 in_args(args, out);180 }181 Expr::Index { indexable, parts } => {182 find_imports(indexable, out);183 for part in parts {184 find_imports(&part.value, out);185 }186 }187 Expr::Function(params, expr) => {188 in_params(params, out);189 find_imports(expr, out);190 }191 Expr::IfElse {192 cond: IfSpecData(expr),193 cond_then,194 cond_else,195 } => {196 find_imports(expr, out);197 find_imports(cond_then, out);198 if let Some(expr) = cond_else {199 find_imports(expr, out);200 }201 }202 Expr::Slice(expr, SliceDesc { start, end, step }) => {203 find_imports(expr, out);204 if let Some(expr) = start {205 find_imports(expr, out);206 }207 if let Some(expr) = end {208 find_imports(expr, out);209 }210 if let Some(expr) = step {211 find_imports(expr, out);212 }213 }214 Expr::Parened(expr) | Expr::UnaryOp(_, expr) | Expr::ErrorStmt(expr) => {215 find_imports(expr, out);216 }217 }218}219220pub trait AsyncImportResolver {221 type Error;222 /// Resolves file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond223 /// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`224 /// where `${vendor}` is a library path.225 ///226 /// `from` should only be returned from [`ImportResolver::resolve`],227 /// or from other defined file, any other value may result in panic228 fn resolve_from(229 &self,230 from: &SourcePath,231 path: &dyn AsPathLike,232 ) -> impl Future<Output = Result<SourcePath, Self::Error>>;233 fn resolve_from_default(234 &self,235 path: &dyn AsPathLike,236 ) -> impl Future<Output = Result<SourcePath, Self::Error>> {237 async { self.resolve_from(&SourcePath::default(), path).await }238 }239240 /// Load resolved file241 /// This should only be called with value returned242 /// from [`ImportResolver::resolve_file`]/[`ImportResolver::resolve`],243 /// this cannot be resolved using associated type,244 /// as the evaluator uses object instead of generic for [`ImportResolver`]245 fn load_file_contents(246 &self,247 resolved: &SourcePath,248 ) -> impl Future<Output = Result<Vec<u8>, Self::Error>>;249}250251#[derive(Acyclic)]252struct ResolvedImportResolver {253 resolved: RefCell<FxHashMap<(SourcePath, ResolvePathOwned), (SourcePath, bool)>>,254}255impl ImportResolver for ResolvedImportResolver {256 fn load_file_contents(&self, _resolved: &SourcePath) -> crate::Result<Vec<u8>> {257 unreachable!("all files should be loaded at this point");258 }259260 fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> crate::Result<SourcePath> {261 Ok(self262 .resolved263 .borrow()264 .get(&(from.clone(), path.as_path().to_owned()))265 .expect("all imports should be resolved at this point")266 .0267 .clone())268 }269270 fn resolve_from_default(&self, path: &dyn AsPathLike) -> crate::Result<SourcePath> {271 self.resolve_from(&SourcePath::default(), path)272 }273}274275enum Job {276 LoadFile { path: SourcePath, parse: bool },277 ParseFile(SourcePath),278 ResolveImport { from: SourcePath, import: Import },279}280281#[allow(clippy::future_not_send)]282pub async fn async_import<H>(s: State, handler: H, path: &dyn AsPathLike) -> Result<(), H::Error>283where284 H: AsyncImportResolver,285{286 let resolved = (s.import_resolver() as &dyn Any)287 .downcast_ref::<ResolvedImportResolver>()288 .expect("for async imports, import_resolver should be set to ResolvedImportResolver");289290 let mut resolved_map = resolved.resolved.borrow_mut();291292 let mut queue = vec![Job::LoadFile {293 path: handler.resolve_from_default(path).await?,294 parse: true,295 }];296 while let Some(job) = queue.pop() {297 match job {298 Job::LoadFile { path, parse } => {299 if !s.0.file_cache.borrow().contains_key(&path) {300 let data = handler.load_file_contents(&path).await?;301 s.0.file_cache302 .borrow_mut()303 .insert(path.clone(), FileData::new_bytes(data.as_slice().into()));304 }305 if parse {306 queue.push(Job::ParseFile(path));307 }308 }309 Job::ParseFile(path) => {310 if let Some(file) = s.0.file_cache.borrow_mut().get_mut(&path) {311 if file.parsed.is_none() {312 let Some(code) = file.get_string() else {313 continue;314 };315 let source = Source::new(path.clone(), code.clone());316 // If failed - then skip import317 file.parsed =318 jrsonnet_parser::parse(&code, &ParserSettings { source }).ok();319 if let Some(parsed) = &file.parsed {320 let mut imports = FoundImports(vec![]);321 find_imports(parsed, &mut imports);322 for import in imports.0 {323 queue.push(Job::ResolveImport {324 from: path.clone(),325 import,326 });327 }328 }329 }330 }331 }332 Job::ResolveImport { from, import } => {333 if let Some((resolved, expression)) =334 resolved_map.get_mut(&(from.clone(), import.path.clone()))335 {336 if import.expression && !*expression {337 *expression = true;338 queue.push(Job::ParseFile(resolved.clone()));339 }340 continue;341 }342 let resolved = handler.resolve_from(&from, &import.path).await?;343 queue.push(Job::LoadFile {344 path: resolved,345 parse: import.expression,346 });347 }348 }349 }350 Ok(())351}crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,7 +2,6 @@
cmp::Ordering,
convert::Infallible,
fmt::{Debug, Display},
- path::PathBuf,
};
use jrsonnet_gcmodule::Trace;
@@ -16,7 +15,7 @@
stdlib::format::FormatError,
typed::TypeLocError,
val::ConvertNumValueError,
- ObjValue,
+ ObjValue, ResolvePathOwned,
};
pub(crate) fn format_found(list: &[IStr], what: &str) -> String {
@@ -180,9 +179,7 @@
StandaloneSuper,
#[error("can't resolve {1} from {0}")]
- ImportFileNotFound(SourcePath, String),
- #[error("can't resolve absolute {0}")]
- AbsoluteImportFileNotFound(PathBuf),
+ ImportFileNotFound(SourcePath, ResolvePathOwned),
#[error("resolved file not found: {:?}", .0)]
ResolvedFileNotFound(SourcePath),
#[error("can't import {0}: is a directory")]
@@ -192,9 +189,7 @@
#[error("import io error: {0}")]
ImportIo(String),
#[error("tried to import {1} from {0}, but imports are not supported")]
- ImportNotSupported(SourcePath, String),
- #[error("tried to import {0}, but absolute imports are not supported")]
- AbsoluteImportNotSupported(PathBuf),
+ ImportNotSupported(SourcePath, ResolvePathOwned),
#[error("can't import from virtual file")]
CantImportFromVirtualFile,
#[error(
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -682,7 +682,7 @@
};
let tmp = loc.clone().0;
let s = ctx.state();
- let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;
+ let resolved_path = s.resolve_from(tmp.source_path(), path)?;
match i {
Import(_) => in_frame(
CallLocation::new(&loc),
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -2,7 +2,7 @@
use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, LocExpr};
+use jrsonnet_parser::{ArgsDesc, LocExpr, SourceFifo, SourcePath};
use crate::{evaluate, typed::Typed, Context, Result, Thunk, Val};
@@ -41,22 +41,34 @@
#[derive(Clone, Trace)]
pub enum TlaArg {
String(IStr),
- Code(LocExpr),
Val(Val),
Lazy(Thunk<Val>),
+ Import(String),
+ ImportStr(String),
+ InlineCode(String),
}
impl ArgLike for TlaArg {
- fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
+ fn evaluate_arg(&self, ctx: Context, _tailstrict: bool) -> Result<Thunk<Val>> {
match self {
Self::String(s) => Ok(Thunk::evaluated(Val::string(s.clone()))),
- Self::Code(code) => Ok(if tailstrict {
- Thunk::evaluated(evaluate(ctx, code)?)
- } else {
- let code = code.clone();
- Thunk!(move || evaluate(ctx, &code))
- }),
Self::Val(val) => Ok(Thunk::evaluated(val.clone())),
Self::Lazy(lazy) => Ok(lazy.clone()),
+ Self::Import(p) => {
+ let resolved = ctx.state().resolve_from_default(&p.as_str())?;
+ Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
+ }
+ Self::ImportStr(p) => {
+ let resolved = ctx.state().resolve_from_default(&p.as_str())?;
+ Ok(Thunk!(move || ctx
+ .state()
+ .import_resolved_str(resolved)
+ .map(Val::string)))
+ }
+ Self::InlineCode(p) => {
+ let resolved =
+ SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
+ Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
+ }
}
}
}
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -1,7 +1,8 @@
use std::{
any::Any,
+ borrow::Cow,
env::current_dir,
- fs,
+ fmt, fs,
io::{ErrorKind, Read},
path::{Path, PathBuf},
};
@@ -9,12 +10,85 @@
use fs::File;
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_interner::IBytes;
-use jrsonnet_parser::{SourceDirectory, SourceFifo, SourceFile, SourcePath};
+use jrsonnet_parser::{IStr, SourceDirectory, SourceFifo, SourceFile, SourcePath};
use crate::{
bail,
error::{ErrorKind::*, Result},
};
+#[derive(Clone, Debug, Acyclic, Eq, Hash, PartialEq)]
+pub enum ResolvePathOwned {
+ Str(String),
+ Path(PathBuf),
+}
+impl fmt::Display for ResolvePathOwned {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ ResolvePathOwned::Str(s) => write!(f, "{s}"),
+ ResolvePathOwned::Path(p) => write!(f, "{}", p.display()),
+ }
+ }
+}
+#[derive(Clone, Copy)]
+pub enum ResolvePath<'s> {
+ Str(&'s str),
+ Path(&'s Path),
+}
+impl ResolvePath<'_> {
+ pub fn to_owned(self) -> ResolvePathOwned {
+ match self {
+ ResolvePath::Str(s) => ResolvePathOwned::Str(s.to_owned()),
+ ResolvePath::Path(p) => ResolvePathOwned::Path(p.to_owned()),
+ }
+ }
+}
+impl AsRef<Path> for ResolvePath<'_> {
+ fn as_ref(&self) -> &Path {
+ match self {
+ ResolvePath::Str(s) => s.as_ref(),
+ ResolvePath::Path(p) => p,
+ }
+ }
+}
+pub trait AsPathLike {
+ fn as_path(&self) -> ResolvePath<'_>;
+}
+impl<T> AsPathLike for &T
+where
+ T: AsPathLike + ?Sized,
+{
+ fn as_path(&self) -> ResolvePath<'_> {
+ (*self).as_path()
+ }
+}
+impl AsPathLike for str {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Str(self)
+ }
+}
+impl AsPathLike for IStr {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Str(self)
+ }
+}
+impl AsPathLike for Cow<'_, Path> {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Path(self.as_ref())
+ }
+}
+impl AsPathLike for Path {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Path(self)
+ }
+}
+impl AsPathLike for ResolvePathOwned {
+ fn as_path(&self) -> ResolvePath<'_> {
+ match self {
+ ResolvePathOwned::Str(s) => ResolvePath::Str(s),
+ ResolvePathOwned::Path(path_buf) => ResolvePath::Path(path_buf),
+ }
+ }
+}
/// Implements file resolution logic for `import` and `importStr`
pub trait ImportResolver: Acyclic + Any {
@@ -24,15 +98,11 @@
///
/// `from` should only be returned from [`ImportResolver::resolve`], or from other defined file, any other value
/// may result in panic
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
- bail!(ImportNotSupported(from.clone(), path.into()))
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
+ bail!(ImportNotSupported(from.clone(), path.as_path().to_owned()))
}
- fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {
self.resolve_from(&SourcePath::default(), path)
- }
- /// Resolves absolute path, doesn't supports jpath and other fancy things
- fn resolve(&self, path: &Path) -> Result<SourcePath> {
- bail!(AbsoluteImportNotSupported(path.to_owned()))
}
/// Load resolved file
@@ -105,7 +175,8 @@
}
impl ImportResolver for FileImportResolver {
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
+ let path = path.as_path();
let mut direct = if let Some(f) = from.downcast_ref::<SourceFile>() {
let mut o = f.path().to_owned();
o.pop();
@@ -130,12 +201,6 @@
}
}
bail!(ImportFileNotFound(from.clone(), path.to_owned()))
- }
- fn resolve(&self, path: &Path) -> Result<SourcePath> {
- let Some(source) = check_path(path)? else {
- bail!(AbsoluteImportFileNotFound(path.to_owned()))
- };
- Ok(source)
}
fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
@@ -155,7 +220,7 @@
Ok(out)
}
- fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {
self.resolve_from(&SourcePath::default(), path)
}
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -29,7 +29,6 @@
cell::{RefCell, RefMut},
collections::hash_map::Entry,
fmt::{self, Debug},
- path::Path,
rc::Rc,
};
@@ -349,12 +348,12 @@
}
/// Has same semantics as `import 'path'` called from `from` file
- pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {
- let resolved = self.resolve_from(from, path)?;
+ pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {
+ let resolved = self.resolve_from(from, &path)?;
self.import_resolved(resolved)
}
- pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {
- let resolved = self.resolve(path)?;
+ pub fn import(&self, path: impl AsPathLike) -> Result<Val> {
+ let resolved = self.resolve_from_default(&path)?;
self.import_resolved(resolved)
}
@@ -468,14 +467,12 @@
impl State {
// Only panics in case of [`ImportResolver`] contract violation
#[allow(clippy::missing_panics_doc)]
- pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
- self.import_resolver().resolve_from(from, path.as_ref())
+ pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
+ self.import_resolver().resolve_from(from, path)
}
-
- // Only panics in case of [`ImportResolver`] contract violation
#[allow(clippy::missing_panics_doc)]
- pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {
- self.import_resolver().resolve(path.as_ref())
+ pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {
+ self.import_resolver().resolve_from_default(path)
}
pub fn import_resolver(&self) -> &dyn ImportResolver {
&*self.0.import_resolver
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -12,7 +12,7 @@
pub use encoding::*;
pub use hash::*;
use jrsonnet_evaluator::{
- error::{ErrorKind::*, Result},
+ error::Result,
function::{CallLocation, FuncVal, TlaArg},
trace::PathResolver,
val::NumValue,
@@ -377,23 +377,11 @@
.ext_vars
.insert(name, TlaArg::String(value));
}
- pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {
- let code = code.into();
- let source = extvar_source(name, code.clone());
- let parsed = jrsonnet_parser::parse(
- &code,
- &jrsonnet_parser::ParserSettings {
- source: source.clone(),
- },
- )
- .map_err(|e| ImportSyntaxError {
- path: source,
- error: Box::new(e),
- })?;
+ pub fn add_ext_code(&self, name: &str, code: impl AsRef<str>) -> Result<()> {
// self.data_mut().volatile_files.insert(source_name, code);
self.settings_mut()
.ext_vars
- .insert(name.into(), TlaArg::Code(parsed));
+ .insert(name.into(), TlaArg::InlineCode(code.as_ref().to_owned()));
Ok(())
}
pub fn add_native(&self, name: impl Into<IStr>, cb: impl Into<FuncVal>) {