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.rsdiffbeforeafterboth1#![allow(clippy::box_default)]23pub mod interop;45pub mod import;6pub mod native;7pub mod val_extract;8pub mod val_make;9pub mod val_modify;10pub mod vars_tlas;1112use std::{13 alloc::Layout,14 any::Any,15 borrow::Cow,16 cell::RefCell,17 ffi::{CStr, CString, OsStr},18 os::raw::{c_char, c_double, c_int, c_uint},19 path::{Path, PathBuf},20 rc::Rc,21};2223use jrsonnet_evaluator::{24 apply_tla, bail,25 function::TlaArg,26 gc::WithCapacityExt as _,27 manifest::{JsonFormat, ManifestFormat, ToStringFormat},28 rustc_hash::FxHashMap,29 stack::set_stack_depth_limit,30 trace::{CompactFormat, PathResolver, TraceFormat},31 FileImportResolver, IStr, ImportResolver, Result, State, Val,32};33use jrsonnet_gcmodule::Acyclic;34use jrsonnet_parser::SourcePath;35use jrsonnet_stdlib::ContextInitializer;3637/// WASM stub38#[cfg(target_arch = "wasm32")]39#[no_mangle]40pub extern "C" fn _start() {}4142/// Return the version string of the Jsonnet interpreter.43/// Conforms to [semantic versioning](http://semver.org/).44/// If this does not match `LIB_JSONNET_VERSION`45/// then there is a mismatch between header and compiled library.46#[no_mangle]47pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {48 b"v0.20.0\0"49}5051unsafe fn parse_path(input: &CStr) -> Cow<'_, Path> {52 #[cfg(target_family = "unix")]53 {54 use std::os::unix::ffi::OsStrExt;55 let str = OsStr::from_bytes(input.to_bytes());56 Cow::Borrowed(Path::new(str))57 }58 #[cfg(not(target_family = "unix"))]59 {60 let string = input.to_str().expect("bad utf-8");61 Cow::Borrowed(string.as_ref())62 }63}6465unsafe fn unparse_path(input: &Path) -> Cow<'_, CStr> {66 #[cfg(target_family = "unix")]67 {68 use std::os::unix::ffi::OsStrExt;69 let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");70 Cow::Owned(str)71 }72 #[cfg(not(target_family = "unix"))]73 {74 let str = input.as_os_str().to_str().expect("bad utf-8");75 let cstr = CString::new(str).expect("input has NUL inside");76 Cow::Owned(cstr)77 }78}7980#[derive(Acyclic)]81struct VMImportResolver {82 inner: RefCell<Rc<dyn ImportResolver>>,83}84impl VMImportResolver {85 fn new(value: impl ImportResolver) -> Self {86 Self {87 inner: RefCell::new(Rc::new(value)),88 }89 }90}91impl ImportResolver for VMImportResolver {92 fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>> {93 self.inner.borrow().load_file_contents(resolved)94 }9596 fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {97 self.inner.borrow().resolve_from(from, path)98 }99100 fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {101 self.inner.borrow().resolve_from_default(path)102 }103104 fn resolve(&self, path: &Path) -> Result<SourcePath> {105 self.inner.borrow().resolve(path)106 }107}108109pub struct VM {110 state: State,111 manifest_format: Box<dyn ManifestFormat>,112 trace_format: Box<dyn TraceFormat>,113 tla_args: FxHashMap<IStr, TlaArg>,114}115impl VM {116 fn replace_import_resolver(&self, resolver: impl ImportResolver) {117 *(self.state.import_resolver() as &dyn Any)118 .downcast_ref::<VMImportResolver>()119 .expect("valid resolver ty")120 .inner121 .borrow_mut() = Rc::new(resolver);122 }123 fn add_jpath(&self, path: PathBuf) {124 let ir = self.state.import_resolver();125 let vmi = (ir as &dyn Any)126 .downcast_ref::<VMImportResolver>()127 .expect("valid resolver ty");128 let vmi = &mut *vmi.inner.borrow_mut();129 (vmi as &mut dyn Any)130 .downcast_mut::<FileImportResolver>()131 .expect("jpaths are not compatible with callback imports!")132 .add_jpath(path);133 }134}135136/// Creates a new Jsonnet virtual machine.137#[no_mangle]138#[allow(clippy::box_default)]139pub extern "C" fn jsonnet_make() -> *mut VM {140 let mut state = State::builder();141 state142 .import_resolver(VMImportResolver::new(FileImportResolver::default()))143 .context_initializer(ContextInitializer::new(PathResolver::new_cwd_fallback()));144 let state = state.build();145 Box::into_raw(Box::new(VM {146 state,147 manifest_format: Box::new(JsonFormat::default()),148 trace_format: Box::new(CompactFormat::default()),149 tla_args: FxHashMap::new(),150 }))151}152153/// Complement of [`jsonnet_vm_make`].154#[no_mangle]155#[allow(clippy::boxed_local)]156pub extern "C" fn jsonnet_destroy(vm: Box<VM>) {157 drop(vm);158}159160/// Set the maximum stack depth.161#[no_mangle]162pub extern "C" fn jsonnet_max_stack(_vm: &VM, v: c_uint) {163 set_stack_depth_limit(v as usize);164}165166/// Set the number of objects required before a garbage collection cycle is allowed.167///168/// No-op for now169#[no_mangle]170pub extern "C" fn jsonnet_gc_min_objects(_vm: &VM, _v: c_uint) {}171172/// Run the garbage collector after this amount of growth in the number of objects173///174/// No-op for now175#[no_mangle]176pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &VM, _v: c_double) {}177178/// Expect a string as output and don't JSON encode it.179#[no_mangle]180pub extern "C" fn jsonnet_string_output(vm: &mut VM, v: c_int) {181 vm.manifest_format = match v {182 0 => Box::new(JsonFormat::default()),183 1 => Box::new(ToStringFormat),184 _ => panic!("incorrect output format"),185 };186}187188/// Allocate, resize, or free a buffer. This will abort if the memory cannot be allocated. It will189/// only return NULL if sz was zero.190///191/// # Safety192///193/// `buf` should be either previosly allocated by this library, or NULL194///195/// This function is most definitely broken, but it works somehow, see TODO inside196#[no_mangle]197pub unsafe extern "C" fn jsonnet_realloc(_vm: &VM, buf: *mut u8, sz: usize) -> *mut u8 {198 if buf.is_null() {199 if sz == 0 {200 return std::ptr::null_mut();201 }202 return unsafe {203 std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap())204 };205 }206 // TODO: Somehow store size of allocation, because its real size is probally not 16 :D207 // OR (Alternative way of fixing this TODO)208 // TODO: Standard allocator uses malloc, and it doesn't uses allocation size,209 // TODO: so it should work in normal cases. Maybe force allocator for this library?210 let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();211 if sz == 0 {212 unsafe { std::alloc::dealloc(buf, old_layout) };213 return std::ptr::null_mut();214 }215 unsafe { std::alloc::realloc(buf, old_layout, sz) }216}217218/// Clean up a JSON subtree.219///220/// This is useful if you want to abort with an error mid-way through building a complex value.221#[no_mangle]222#[allow(clippy::boxed_local)]223pub extern "C" fn jsonnet_json_destroy(_vm: &VM, v: Box<Val>) {224 drop(v);225}226227/// Set the number of lines of stack trace to display (0 for all of them).228#[no_mangle]229pub extern "C" fn jsonnet_max_trace(vm: &mut VM, v: c_uint) {230 if let Some(format) = vm.trace_format.as_any_mut().downcast_mut::<CompactFormat>() {231 format.max_trace = v as usize;232 } else {233 panic!("max_trace is not supported by current tracing format")234 }235}236237/// Evaluate a file containing Jsonnet code, return a JSON string.238///239/// The returned string should be cleaned up with `jsonnet_realloc`.240///241/// # Safety242///243/// `filename` should be a NUL-terminated string244#[no_mangle]245pub unsafe extern "C" fn jsonnet_evaluate_file(246 vm: &VM,247 filename: *const c_char,248 error: &mut c_int,249) -> *const c_char {250 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };251 match vm252 .state253 .import(filename)254 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))255 .and_then(|val| val.manifest(&vm.manifest_format))256 {257 Ok(v) => {258 *error = 0;259 CString::new(&*v as &str).unwrap().into_raw()260 }261 Err(e) => {262 *error = 1;263 let mut out = String::new();264 vm.trace_format.write_trace(&mut out, &e).unwrap();265 CString::new(&out as &str).unwrap().into_raw()266 }267 }268}269270/// Evaluate a string containing Jsonnet code, return a JSON string.271///272/// The returned string should be cleaned up with `jsonnet_realloc`.273///274/// # Safety275///276/// `filename`, `snippet` should be a NUL-terminated strings277#[no_mangle]278pub unsafe extern "C" fn jsonnet_evaluate_snippet(279 vm: &VM,280 filename: *const c_char,281 snippet: *const c_char,282 error: &mut c_int,283) -> *const c_char {284 let filename = unsafe { CStr::from_ptr(filename) };285 let snippet = unsafe { CStr::from_ptr(snippet) };286 match vm287 .state288 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())289 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))290 .and_then(|val| val.manifest(&vm.manifest_format))291 {292 Ok(v) => {293 *error = 0;294 CString::new(&*v as &str).unwrap().into_raw()295 }296 Err(e) => {297 *error = 1;298 let mut out = String::new();299 vm.trace_format.write_trace(&mut out, &e).unwrap();300 CString::new(&out as &str).unwrap().into_raw()301 }302 }303}304305fn val_to_multi(val: Val, format: &dyn ManifestFormat) -> Result<Vec<(IStr, IStr)>> {306 let Val::Obj(val) = val else {307 bail!("expected object as multi output")308 };309 let mut out = Vec::new();310 for (k, v) in val.iter(311 #[cfg(feature = "exp-preserve-order")]312 false,313 ) {314 out.push((k, v?.manifest(format)?.into()));315 }316 Ok(out)317}318319fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {320 let mut out = Vec::new();321 for (i, (k, v)) in multi.iter().enumerate() {322 if i != 0 {323 out.push(0);324 }325 out.extend_from_slice(k.as_bytes());326 out.push(0);327 out.extend_from_slice(v.as_bytes());328 }329 out.push(0);330 out.push(0);331 let v = out.as_ptr();332 std::mem::forget(out);333 v.cast::<c_char>()334}335336/// # Safety337#[no_mangle]338pub unsafe extern "C" fn jsonnet_evaluate_file_multi(339 vm: &VM,340 filename: *const c_char,341 error: &mut c_int,342) -> *const c_char {343 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };344 match vm345 .state346 .import(filename)347 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))348 .and_then(|val| val_to_multi(val, &vm.manifest_format))349 {350 Ok(v) => {351 *error = 0;352 multi_to_raw(v)353 }354 Err(e) => {355 *error = 1;356 let mut out = String::new();357 vm.trace_format.write_trace(&mut out, &e).unwrap();358 CString::new(&out as &str).unwrap().into_raw()359 }360 }361}362363/// # Safety364#[no_mangle]365pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi(366 vm: &VM,367 filename: *const c_char,368 snippet: *const c_char,369 error: &mut c_int,370) -> *const c_char {371 let filename = unsafe { CStr::from_ptr(filename) };372 let snippet = unsafe { CStr::from_ptr(snippet) };373 match vm374 .state375 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())376 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))377 .and_then(|val| val_to_multi(val, &vm.manifest_format))378 {379 Ok(v) => {380 *error = 0;381 multi_to_raw(v)382 }383 Err(e) => {384 *error = 1;385 let mut out = String::new();386 vm.trace_format.write_trace(&mut out, &e).unwrap();387 CString::new(&out as &str).unwrap().into_raw()388 }389 }390}391392fn val_to_stream(val: Val, format: &dyn ManifestFormat) -> Result<Vec<IStr>> {393 let Val::Arr(val) = val else {394 bail!("expected array as stream output")395 };396 let mut out = Vec::new();397 for item in val.iter() {398 out.push(item?.manifest(format)?.into());399 }400 Ok(out)401}402403fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {404 let mut out = Vec::new();405 for (i, v) in multi.iter().enumerate() {406 if i != 0 {407 out.push(0);408 }409 out.extend_from_slice(v.as_bytes());410 }411 out.push(0);412 out.push(0);413 let v = out.as_ptr();414 std::mem::forget(out);415 v.cast::<c_char>()416}417418/// # Safety419#[no_mangle]420pub unsafe extern "C" fn jsonnet_evaluate_file_stream(421 vm: &VM,422 filename: *const c_char,423 error: &mut c_int,424) -> *const c_char {425 let filename = unsafe { parse_path(CStr::from_ptr(filename)) };426 match vm427 .state428 .import(filename)429 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))430 .and_then(|val| val_to_stream(val, &vm.manifest_format))431 {432 Ok(v) => {433 *error = 0;434 stream_to_raw(v)435 }436 Err(e) => {437 *error = 1;438 let mut out = String::new();439 vm.trace_format.write_trace(&mut out, &e).unwrap();440 CString::new(&out as &str).unwrap().into_raw()441 }442 }443}444445/// # Safety446#[no_mangle]447pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream(448 vm: &VM,449 filename: *const c_char,450 snippet: *const c_char,451 error: &mut c_int,452) -> *const c_char {453 let filename = unsafe { CStr::from_ptr(filename) };454 let snippet = unsafe { CStr::from_ptr(snippet) };455 match vm456 .state457 .evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())458 .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))459 .and_then(|val| val_to_stream(val, &vm.manifest_format))460 {461 Ok(v) => {462 *error = 0;463 stream_to_raw(v)464 }465 Err(e) => {466 *error = 1;467 let mut out = String::new();468 vm.trace_format.write_trace(&mut out, &e).unwrap();469 CString::new(&out as &str).unwrap().into_raw()470 }471 }472}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.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/async_import.rs
+++ b/crates/jrsonnet-evaluator/src/async_import.rs
@@ -1,7 +1,6 @@
-use std::{any::Any, cell::RefCell, future::Future, path::Path};
+use std::{any::Any, cell::RefCell, future::Future};
use jrsonnet_gcmodule::Acyclic;
-use jrsonnet_interner::IStr;
use jrsonnet_parser::{
ArgsDesc, AssertStmt, BindSpec, CompSpec, Destruct, Expr, FieldMember, FieldName, ForSpecData,
IfSpecData, LocExpr, Member, ObjBody, Param, ParamsDesc, ParserSettings, SliceDesc, Source,
@@ -9,10 +8,10 @@
};
use rustc_hash::FxHashMap;
-use crate::{bail, FileData, ImportResolver, State};
+use crate::{AsPathLike, FileData, ImportResolver, ResolvePathOwned, State};
pub struct Import {
- path: IStr,
+ path: ResolvePathOwned,
expression: bool,
}
@@ -137,7 +136,7 @@
Expr::Import(v) | Expr::ImportStr(v) | Expr::ImportBin(v) => {
if let Expr::Str(s) = &*v.expr() {
out.0.push(Import {
- path: s.clone(),
+ path: ResolvePathOwned::Str(s.to_string()),
expression: matches!(&*expr.expr(), Expr::Import(_)),
});
}
@@ -229,16 +228,14 @@
fn resolve_from(
&self,
from: &SourcePath,
- path: &str,
+ path: &dyn AsPathLike,
) -> impl Future<Output = Result<SourcePath, Self::Error>>;
fn resolve_from_default(
&self,
- path: &str,
+ path: &dyn AsPathLike,
) -> impl Future<Output = Result<SourcePath, Self::Error>> {
async { self.resolve_from(&SourcePath::default(), path).await }
}
- /// Resolves absolute path, doesn't supports jpath and other fancy things
- fn resolve(&self, path: &Path) -> impl Future<Output = Result<SourcePath, Self::Error>>;
/// Load resolved file
/// This should only be called with value returned
@@ -253,31 +250,25 @@
#[derive(Acyclic)]
struct ResolvedImportResolver {
- resolved: RefCell<FxHashMap<(SourcePath, IStr), (SourcePath, bool)>>,
+ resolved: RefCell<FxHashMap<(SourcePath, ResolvePathOwned), (SourcePath, bool)>>,
}
impl ImportResolver for ResolvedImportResolver {
fn load_file_contents(&self, _resolved: &SourcePath) -> crate::Result<Vec<u8>> {
unreachable!("all files should be loaded at this point");
}
- fn resolve_from(&self, from: &SourcePath, path: &str) -> crate::Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> crate::Result<SourcePath> {
Ok(self
.resolved
.borrow()
- .get(&(from.clone(), path.into()))
+ .get(&(from.clone(), path.as_path().to_owned()))
.expect("all imports should be resolved at this point")
.0
.clone())
}
- fn resolve_from_default(&self, path: &str) -> crate::Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> crate::Result<SourcePath> {
self.resolve_from(&SourcePath::default(), path)
- }
-
- fn resolve(&self, path: &Path) -> crate::Result<SourcePath> {
- bail!(crate::error::ErrorKind::AbsoluteImportNotSupported(
- path.to_owned()
- ))
}
}
@@ -288,7 +279,7 @@
}
#[allow(clippy::future_not_send)]
-pub async fn async_import<H>(s: State, handler: H, path: impl AsRef<Path>) -> Result<(), H::Error>
+pub async fn async_import<H>(s: State, handler: H, path: &dyn AsPathLike) -> Result<(), H::Error>
where
H: AsyncImportResolver,
{
@@ -299,7 +290,7 @@
let mut resolved_map = resolved.resolved.borrow_mut();
let mut queue = vec![Job::LoadFile {
- path: handler.resolve(path.as_ref()).await?,
+ path: handler.resolve_from_default(path).await?,
parse: true,
}];
while let Some(job) = queue.pop() {
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>) {