difftreelog
refactor(treewide) custom path support
in: master
15 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth4 any::Any,4 any::Any,5 cell::RefCell,5 cell::RefCell,6 collections::HashMap,6 collections::HashMap,7 env::current_dir,7 ffi::{c_void, CStr, CString},8 ffi::{c_void, CStr, CString},8 fs::File,9 io::Read,10 os::raw::{c_char, c_int},9 os::raw::{c_char, c_int},11 path::{Path, PathBuf},10 path::PathBuf,12 ptr::null_mut,11 ptr::null_mut,13};12};141315use jrsonnet_evaluator::{14use jrsonnet_evaluator::{16 error::{Error::*, Result},15 error::{Error::*, Result},17 throw, ImportResolver, State,16 throw, FileImportResolver, ImportResolver, State,18};17};19use jrsonnet_parser::SourcePath;18use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};201921pub type JsonnetImportCallback = unsafe extern "C" fn(20pub type JsonnetImportCallback = unsafe extern "C" fn(22 ctx: *mut c_void,21 ctx: *mut c_void,33 out: RefCell<HashMap<SourcePath, Vec<u8>>>,32 out: RefCell<HashMap<SourcePath, Vec<u8>>>,34}33}35impl ImportResolver for CallbackImportResolver {34impl ImportResolver for CallbackImportResolver {36 fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {35 fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {37 let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();36 let base = if let Some(p) = from.downcast_ref::<SourceFile>() {37 let mut o = p.path().to_owned();38 o.pop();39 o40 } else if let Some(d) = from.downcast_ref::<SourceDirectory>() {41 d.path().to_owned()42 } else if from.is_default() {43 current_dir().map_err(|e| ImportIo(e.to_string()))?44 } else {45 unreachable!("can't resolve this path");46 };47 let base = unsafe { crate::unparse_path(&base) };38 let rel = CString::new(path).unwrap().into_raw();48 let rel = CString::new(path).unwrap();39 let found_here: *mut c_char = null_mut();49 let found_here: *mut c_char = null_mut();40 let mut success: i32 = 0;50 let mut success: i32 = 0;41 let result_ptr = unsafe {51 let result_ptr = unsafe {42 (self.cb)(52 (self.cb)(43 self.ctx,53 self.ctx,44 base,54 base.as_ptr(),45 rel,55 rel.as_ptr(),46 &mut (found_here as *const _),56 &mut (found_here as *const _),47 &mut success,57 &mut success,48 )58 )49 };59 };50 // Release memory occipied by arguments passed51 unsafe {52 let _ = CString::from_raw(base);53 let _ = CString::from_raw(rel);54 }55 let result_raw = unsafe { CStr::from_ptr(result_ptr) };60 let result_raw = unsafe { CStr::from_ptr(result_ptr) };56 let result_str = result_raw.to_str().unwrap();61 let result_str = result_raw.to_str().unwrap();57 assert!(success == 0 || success == 1);62 assert!(success == 0 || success == 1);62 }67 }636864 let found_here_raw = unsafe { CStr::from_ptr(found_here) };69 let found_here_raw = unsafe { CStr::from_ptr(found_here) };65 let found_here_buf = SourcePath::Path(PathBuf::from(found_here_raw.to_str().unwrap()));70 let found_here_buf = SourcePath::new(SourceFile::new(PathBuf::from(71 found_here_raw.to_str().unwrap(),72 )));66 unsafe {73 unsafe {67 let _ = CString::from_raw(found_here);74 let _ = CString::from_raw(found_here);68 }75 }79 Ok(self.out.borrow().get(resolved).unwrap().clone())86 Ok(self.out.borrow().get(resolved).unwrap().clone())80 }87 }818882 unsafe fn as_any(&self) -> &dyn Any {89 fn as_any(&self) -> &dyn Any {83 self90 self84 }91 }85}92}869387/// # Safety94/// # Safety95///96/// Caller should pass correct callback function88#[no_mangle]97#[no_mangle]89pub unsafe extern "C" fn jsonnet_import_callback(98pub unsafe extern "C" fn jsonnet_import_callback(90 vm: &State,99 vm: &State,98 }))107 }))99}108}100101/// Standard FS import resolver102#[derive(Default)]103pub struct NativeImportResolver {104 library_paths: RefCell<Vec<PathBuf>>,105}106impl NativeImportResolver {107 fn add_jpath(&self, path: PathBuf) {108 self.library_paths.borrow_mut().push(path);109 }110}111impl ImportResolver for NativeImportResolver {112 fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {113 let mut new_path = from.to_owned();114 new_path.push(path);115 if new_path.exists() {116 Ok(SourcePath::Path(new_path))117 } else {118 for library_path in self.library_paths.borrow().iter() {119 let mut cloned = library_path.clone();120 cloned.push(path);121 if cloned.exists() {122 return Ok(SourcePath::Path(cloned));123 }124 }125 throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))126 }127 }128 fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {129 let path = match id {130 SourcePath::Path(path) => path,131 _ => unreachable!("NativeImportResolver::resolve_file may only return plain paths"),132 };133 let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;134 let mut out = Vec::new();135 file.read_to_end(&mut out)136 .map_err(|e| ImportIo(e.to_string()))?;137 Ok(out)138 }139 unsafe fn as_any(&self) -> &dyn Any {140 self141 }142}143109144/// # Safety110/// # Safety145///111///146/// This function is safe, if received v is a pointer to normal C string112/// Caller should pass correct path: it should contain correct utf-8, and be \0-terminated147#[no_mangle]113#[no_mangle]148pub unsafe extern "C" fn jsonnet_jpath_add(vm: &State, v: *const c_char) {114pub unsafe extern "C" fn jsonnet_jpath_add(vm: &State, v: *const c_char) {149 let cstr = CStr::from_ptr(v);115 let cstr = CStr::from_ptr(v);150 let path = PathBuf::from(cstr.to_str().unwrap());116 let path = PathBuf::from(cstr.to_str().unwrap());151 let any_resolver = vm.import_resolver();117 let any_resolver = vm.import_resolver();152 let resolver = any_resolver118 let resolver = any_resolver153 .as_any()119 .as_any()154 .downcast_ref::<NativeImportResolver>()120 .downcast_ref::<FileImportResolver>()155 .expect("jpaths are not compatible with callback imports!");121 .expect("jpaths are not compatible with callback imports!");156 resolver.add_jpath(path);122 resolver.add_jpath(path);157}123}cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -19,6 +19,8 @@
]
# Destructuring of locals
exp-destruct = ["jrsonnet-evaluator/exp-destruct"]
+# std.thisFile support
+legacy-this-file = ["jrsonnet-cli/legacy-this-file"]
[dependencies]
jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2" }
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -1,5 +1,4 @@
use std::{
- env::current_dir,
fs::{create_dir_all, File},
io::{Read, Write},
};
@@ -140,7 +139,7 @@
let input_str = std::str::from_utf8(&input)?;
s.evaluate_snippet("<stdin>".to_owned(), input_str)?
} else {
- s.import(¤t_dir().expect("cwd"), &input)?
+ s.import(&input)?
};
let val = s.with_tla(val)?;
crates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-cli/Cargo.toml
+++ b/crates/jrsonnet-cli/Cargo.toml
@@ -15,6 +15,7 @@
"jrsonnet-evaluator/exp-serde-preserve-order",
"jrsonnet-stdlib/exp-serde-preserve-order",
]
+legacy-this-file = ["jrsonnet-stdlib/legacy-this-file"]
[dependencies]
jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.4.2", features = [
crates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -51,7 +51,7 @@
library_paths.extend(env::split_paths(path.as_os_str()));
}
- s.set_import_resolver(Box::new(FileImportResolver { library_paths }));
+ s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));
s.set_max_stack(self.max_stack);
Ok(())
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 clap::Parser;
-use jrsonnet_evaluator::{error::Result, State};
+use jrsonnet_evaluator::{error::Result, trace::PathResolver, State};
use crate::ConfigureState;
@@ -110,7 +110,8 @@
if self.no_stdlib {
return Ok(());
}
- let ctx = jrsonnet_stdlib::ContextInitializer::new(s.clone());
+ let ctx =
+ jrsonnet_stdlib::ContextInitializer::new(s.clone(), PathResolver::new_cwd_fallback());
for ext in self.ext_str.iter() {
ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
}
crates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/trace.rs
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -42,11 +42,7 @@
}
impl ConfigureState for TraceOpts {
fn configure(&self, s: &State) -> Result<()> {
- let resolver = if let Ok(dir) = std::env::current_dir() {
- PathResolver::Relative(dir)
- } else {
- PathResolver::Absolute
- };
+ let resolver = PathResolver::new_cwd_fallback();
match self
.trace_format
.as_ref()
crates/jrsonnet-interner/src/inner.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/inner.rs
+++ b/crates/jrsonnet-interner/src/inner.rs
@@ -84,7 +84,9 @@
unsafe { Self::new_raw(str.as_bytes(), true) }
}
- pub const fn as_slice(&self) -> &[u8] {
+ // `slice::from_raw_parts` is not yet stabilized
+ #[allow(clippy::missing_const_for_fn)]
+ pub fn as_slice(&self) -> &[u8] {
let header = Self::header(self);
// SAFETY: data is not null, and it is correctly initialized
let size = unsafe { (*header).size };
@@ -99,7 +101,7 @@
/// # Safety
/// Data should be checked to be utf8 via [`check_utf8`] first
- pub const unsafe fn as_str_unchecked(&self) -> &str {
+ pub unsafe fn as_str_unchecked(&self) -> &str {
// SAFETY: data is checked
unsafe { str::from_utf8_unchecked(self.as_slice()) }
}
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -133,7 +133,7 @@
}
#[must_use]
- pub const fn as_slice(&self) -> &[u8] {
+ pub fn as_slice(&self) -> &[u8] {
self.0.as_slice()
}
}
crates/jrsonnet-stdlib/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/build.rs
+++ b/crates/jrsonnet-stdlib/build.rs
@@ -1,4 +1,4 @@
-use std::{borrow::Cow, env, fs::File, io::Write, path::Path};
+use std::{env, fs::File, io::Write, path::Path};
use jrsonnet_parser::{parse, ParserSettings, Source};
use structdump::CodegenResult;
@@ -8,7 +8,7 @@
include_str!("./src/std.jsonnet"),
&ParserSettings {
file_name: Source::new_virtual(
- Cow::Borrowed("<std>"),
+ "<std>".into(),
include_str!("./src/std.jsonnet").into(),
),
},
crates/jrsonnet-stdlib/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/expr.rs
+++ b/crates/jrsonnet-stdlib/src/expr.rs
@@ -1,7 +1,7 @@
use jrsonnet_parser::LocExpr;
mod structdump_import {
- pub(super) use std::{borrow::Cow, option::Option, rc::Rc, vec};
+ pub(super) use std::{option::Option, rc::Rc, vec};
pub(super) use jrsonnet_parser::*;
}
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -1,5 +1,4 @@
use std::{
- borrow::Cow,
cell::{Ref, RefCell, RefMut},
collections::HashMap,
rc::Rc,
@@ -10,6 +9,7 @@
function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},
gc::{GcHashMap, TraceBox},
tb, throw_runtime,
+ trace::PathResolver,
typed::{Any, Either, Either2, Either4, VecVal, M1},
val::{equals, ArrValue},
Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
@@ -184,13 +184,27 @@
fn print_trace(&self, s: State, loc: CallLocation, value: IStr);
}
-pub struct StdTracePrinter;
+pub struct StdTracePrinter {
+ resolver: PathResolver,
+}
+impl StdTracePrinter {
+ pub fn new(resolver: PathResolver) -> Self {
+ Self { resolver }
+ }
+}
impl TracePrinter for StdTracePrinter {
fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {
eprint!("TRACE:");
if let Some(loc) = loc.0 {
let locs = loc.0.map_source_locations(&[loc.1]);
- eprint!(" {}:{}", loc.0.short_display(), locs[0].line);
+ eprint!(
+ " {}:{}",
+ match loc.0.source_path().path() {
+ Some(p) => self.resolver.resolve(p),
+ None => loc.0.source_path().to_string(),
+ },
+ locs[0].line
+ );
}
eprintln!(" {}", value);
}
@@ -205,22 +219,13 @@
pub globals: GcHashMap<IStr, Thunk<Val>>,
/// Used for `std.trace`
pub trace_printer: Box<dyn TracePrinter>,
-}
-
-impl Default for Settings {
- fn default() -> Self {
- Self {
- ext_vars: Default::default(),
- ext_natives: Default::default(),
- globals: Default::default(),
- trace_printer: Box::new(StdTracePrinter),
- }
- }
+ /// Used for `std.thisFile`
+ pub path_resolver: PathResolver,
}
pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {
let source_name = format!("<extvar:{}>", name);
- Source::new_virtual(Cow::Owned(source_name), code.into())
+ Source::new_virtual(source_name.into(), code.into())
}
pub struct ContextInitializer {
@@ -233,8 +238,15 @@
settings: Rc<RefCell<Settings>>,
}
impl ContextInitializer {
- pub fn new(s: State) -> Self {
- let settings = Rc::new(RefCell::new(Settings::default()));
+ pub fn new(s: State, resolver: PathResolver) -> Self {
+ let settings = Settings {
+ ext_vars: Default::default(),
+ ext_natives: Default::default(),
+ globals: Default::default(),
+ trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),
+ path_resolver: resolver,
+ };
+ let settings = Rc::new(RefCell::new(settings));
Self {
#[cfg(not(feature = "legacy-this-file"))]
context: {
@@ -313,13 +325,10 @@
.hide()
.value(
s,
- Val::Str(
- source
- .path()
- .map(|p| p.display().to_string())
- .unwrap_or_else(String::new)
- .into(),
- ),
+ Val::Str(match source.source_path().path() {
+ Some(p) => self.settings().path_resolver.resolve(p).into(),
+ None => source.source_path().to_string().into(),
+ }),
)
.expect("this object builder is empty");
let stdlib_with_this_file = builder.build();
@@ -329,12 +338,12 @@
"std".into(),
Thunk::evaluated(Val::Obj(stdlib_with_this_file)),
);
- for (k, v) in &self.settings().globals {
- context.bind(k.clone(), v.clone())
+ for (k, v) in self.settings().globals.iter() {
+ context.bind(k.clone(), v.clone());
}
context.build()
}
- unsafe fn as_any(&self) -> &dyn std::any::Any {
+ fn as_any(&self) -> &dyn std::any::Any {
self
}
}
@@ -540,12 +549,13 @@
impl StateExt for State {
fn with_stdlib(&self) {
- let initializer = ContextInitializer::new(self.clone());
+ let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());
self.settings_mut().context_initializer = Box::new(initializer)
}
fn add_global(&self, name: IStr, value: Thunk<Val>) {
- // Safety:
- unsafe { self.settings().context_initializer.as_any() }
+ self.settings()
+ .context_initializer
+ .as_any()
.downcast_ref::<ContextInitializer>()
.expect("not standard context initializer")
.settings_mut()
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -2,7 +2,7 @@
local std = self,
local id = std.id,
- thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support. This will slow down stdlib caching a bit, though',
+ thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',
toString(a):: '' + a,
tests/tests/golden.rsdiffbeforeafterboth--- a/tests/tests/golden.rs
+++ b/tests/tests/golden.rs
@@ -21,7 +21,7 @@
common::with_test(&s);
s.set_import_resolver(Box::new(FileImportResolver::default()));
- let v = match s.import(root, &file.display().to_string()) {
+ let v = match s.import(file) {
Ok(v) => v,
Err(e) => return s.stringify_err(&e),
};
tests/tests/suite.rsdiffbeforeafterboth--- a/tests/tests/suite.rs
+++ b/tests/tests/suite.rs
@@ -21,7 +21,7 @@
common::with_test(&s);
s.set_import_resolver(Box::new(FileImportResolver::default()));
- match s.import(root, &file.display().to_string()) {
+ match s.import(file) {
Ok(Val::Bool(true)) => {}
Ok(Val::Bool(false)) => panic!("test {} returned false", file.display()),
Ok(_) => panic!("test {} returned wrong type as result", file.display()),